Skip to content

Commit b4bdb28

Browse files
committed
change transfer() to transfer_keep_alive()
1 parent 116b8a3 commit b4bdb28

File tree

3 files changed

+35
-64
lines changed

3 files changed

+35
-64
lines changed

clients/tfchain-client-go/transfer.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ func (s *Substrate) Transfer(identity Identity, amount uint64, destination Accou
1919
}
2020
bal := big.NewInt(int64(amount))
2121

22-
c, err := types.NewCall(meta, "Balances.transfer", dest, types.NewUCompact(bal))
22+
c, err := types.NewCall(meta, "Balances.transfer_keep_alive", dest, types.NewUCompact(bal))
2323
if err != nil {
2424
panic(err)
2525
}

substrate-node/pallets/pallet-smart-contract/src/mock.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -145,12 +145,12 @@ impl frame_system::Config for TestRuntime {
145145
type MaxConsumers = ConstU32<16>;
146146
}
147147

148-
pub const EXISTENTIAL_DEPOSIT: u64 = 500;
148+
pub const EXISTENTIAL_DEPOSIT: u64 = 2;
149149

150150
parameter_types! {
151151
pub const MaxLocks: u32 = 50;
152152
pub const MaxReserves: u32 = 50;
153-
pub const ExistentialDepositBalance: u64 = EXISTENTIAL_DEPOSIT;
153+
pub const ExistentialDeposit: u64 = EXISTENTIAL_DEPOSIT;
154154
}
155155

156156
impl pallet_balances::Config for TestRuntime {
@@ -162,7 +162,7 @@ impl pallet_balances::Config for TestRuntime {
162162
/// The ubiquitous event type.
163163
type RuntimeEvent = RuntimeEvent;
164164
type DustRemoval = ();
165-
type ExistentialDeposit = ExistentialDepositBalance;
165+
type ExistentialDeposit = ExistentialDeposit;
166166
type AccountStore = System;
167167
type WeightInfo = pallet_balances::weights::SubstrateWeight<TestRuntime>;
168168
type FreezeIdentifier = ();

substrate-node/pallets/pallet-smart-contract/src/tests.rs

Lines changed: 31 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::{
55
use frame_support::{
66
assert_noop, assert_ok,
77
dispatch::Pays,
8-
traits::{Currency, ExistenceRequirement, LockableCurrency, WithdrawReasons},
8+
traits::{LockableCurrency, WithdrawReasons},
99
BoundedVec,
1010
};
1111
use frame_system::{EventRecord, Phase, RawOrigin};
@@ -1505,11 +1505,10 @@ fn test_node_contract_billing_cycles_cancel_contract_during_cycle_without_balanc
15051505
let total_amount_billed = initial_twin_balance - usable_balance;
15061506

15071507
let extrinsic_fee = 10000;
1508-
Balances::transfer(
1509-
&bob(),
1510-
&alice(),
1508+
Balances::transfer_keep_alive(
1509+
RuntimeOrigin::signed(bob()),
1510+
alice(),
15111511
initial_twin_balance - total_amount_billed - extrinsic_fee,
1512-
ExistenceRequirement::AllowDeath,
15131512
)
15141513
.unwrap();
15151514

@@ -1642,13 +1641,7 @@ fn test_restore_node_contract_in_grace_works() {
16421641
run_to_block(31, Some(&mut pool_state));
16431642
run_to_block(41, Some(&mut pool_state));
16441643
// Transfer some balance to the owner of the contract to trigger the grace period to stop
1645-
Balances::transfer(
1646-
&bob(),
1647-
&charlie(),
1648-
100000000,
1649-
ExistenceRequirement::AllowDeath,
1650-
)
1651-
.unwrap();
1644+
Balances::transfer_keep_alive(RuntimeOrigin::signed(bob()), charlie(), 100000000).unwrap();
16521645
run_to_block(52, Some(&mut pool_state));
16531646
run_to_block(62, Some(&mut pool_state));
16541647

@@ -1861,7 +1854,9 @@ fn test_rent_contract_billing_cancel_should_bill_reserved_balance() {
18611854
.should_call_bill_contract(contract_id, Ok(Pays::Yes.into()), 11);
18621855
run_to_block(11, Some(&mut pool_state));
18631856

1864-
let (amount_due_as_u128, discount_received) = calculate_tft_cost(contract_id, 2, 10);
1857+
let bob_twin_id = 2;
1858+
let (amount_due_as_u128, discount_received) =
1859+
calculate_tft_cost(contract_id, bob_twin_id, 10);
18651860
assert_ne!(amount_due_as_u128, 0);
18661861
check_report_cost(
18671862
contract_id,
@@ -1870,39 +1865,33 @@ fn test_rent_contract_billing_cancel_should_bill_reserved_balance() {
18701865
discount_received.clone(),
18711866
);
18721867

1873-
let twin = TfgridModule::twins(2).unwrap();
1874-
let usable_balance = Balances::usable_balance(&twin.account_id);
1875-
let free_balance = Balances::free_balance(&twin.account_id);
1876-
assert_ne!(usable_balance, free_balance);
1868+
let bob_twin = TfgridModule::twins(bob_twin_id).unwrap();
1869+
let usable_balance = Balances::usable_balance(&bob_twin.account_id);
1870+
let free_balance = Balances::free_balance(&bob_twin.account_id);
1871+
assert_ne!(usable_balance, free_balance); // there is some locked balance
1872+
let locked_balance = free_balance - usable_balance;
18771873

18781874
run_to_block(13, Some(&mut pool_state));
18791875
// cancel contract
18801876
// it will bill before removing the contract and it should bill all
18811877
// reserved balance
1882-
let (amount_due_as_u128, discount_received) = calculate_tft_cost(contract_id, 2, 2);
1878+
let (amount_due_as_u128, discount_received) =
1879+
calculate_tft_cost(contract_id, bob_twin_id, 2);
1880+
18831881
assert_ok!(SmartContractModule::cancel_contract(
18841882
RuntimeOrigin::signed(bob()),
18851883
contract_id
18861884
));
18871885

1888-
let twin = TfgridModule::twins(2).unwrap();
1889-
let usable_balance = Balances::usable_balance(&twin.account_id);
1890-
assert_ne!(usable_balance, 0);
1891-
Balances::transfer(
1892-
&bob(),
1893-
&alice(),
1894-
usable_balance,
1895-
ExistenceRequirement::AllowDeath,
1896-
)
1897-
.unwrap();
1898-
18991886
// Last amount due is the same as the first one
19001887
assert_ne!(amount_due_as_u128, 0);
19011888
check_report_cost(contract_id, amount_due_as_u128, 13, discount_received);
19021889

1903-
let usable_balance = Balances::usable_balance(&twin.account_id);
1904-
let free_balance = Balances::free_balance(&twin.account_id);
1905-
assert_eq!(usable_balance, free_balance);
1890+
let usable_balance = Balances::usable_balance(&bob_twin.account_id);
1891+
let past_free_balance = free_balance;
1892+
let free_balance = Balances::free_balance(&bob_twin.account_id);
1893+
assert_eq!(usable_balance, free_balance); // there is no more locked balance
1894+
assert_eq!(past_free_balance - free_balance, locked_balance); // locked balance was transfered to alice
19061895
});
19071896
}
19081897

@@ -2271,13 +2260,7 @@ fn test_restore_rent_contract_in_grace_works() {
22712260
run_to_block(31, Some(&mut pool_state));
22722261

22732262
// Transfer some balance to the owner of the contract to trigger the grace period to stop
2274-
Balances::transfer(
2275-
&bob(),
2276-
&charlie(),
2277-
100000000,
2278-
ExistenceRequirement::AllowDeath,
2279-
)
2280-
.unwrap();
2263+
Balances::transfer_keep_alive(RuntimeOrigin::signed(bob()), charlie(), 100000000).unwrap();
22812264

22822265
pool_state
22832266
.write()
@@ -2380,13 +2363,7 @@ fn test_restore_rent_contract_and_node_contracts_in_grace_works() {
23802363
run_to_block(32, Some(&mut pool_state));
23812364

23822365
// Transfer some balance to the owner of the contract to trigger the grace period to stop
2383-
Balances::transfer(
2384-
&bob(),
2385-
&charlie(),
2386-
100000000,
2387-
ExistenceRequirement::AllowDeath,
2388-
)
2389-
.unwrap();
2366+
Balances::transfer_keep_alive(RuntimeOrigin::signed(bob()), charlie(), 100000000).unwrap();
23902367

23912368
pool_state
23922369
.write()
@@ -3342,17 +3319,11 @@ fn test_service_contract_bill_out_of_funds_fails() {
33423319
approve_service_consumer_contract(service_contract_id);
33433320

33443321
// Drain consumer account
3345-
let consumer_twin = TfgridModule::twins(2).unwrap();
3346-
let consumer_balance = Balances::free_balance(&consumer_twin.account_id);
3347-
Balances::transfer(
3348-
&bob(),
3349-
&alice(),
3350-
consumer_balance,
3351-
ExistenceRequirement::AllowDeath,
3352-
)
3353-
.unwrap();
3354-
let consumer_balance = Balances::free_balance(&consumer_twin.account_id);
3355-
assert_eq!(consumer_balance, 0);
3322+
let consumer_twin_id = 2;
3323+
let consumer_twin = TfgridModule::twins(consumer_twin_id).unwrap();
3324+
Balances::transfer_all(RuntimeOrigin::signed(bob()), alice(), true).unwrap();
3325+
let consumer_free_balance = Balances::free_balance(&consumer_twin.account_id);
3326+
assert_eq!(consumer_free_balance, EXISTENTIAL_DEPOSIT);
33563327

33573328
// Bill 1h after contract approval
33583329
run_to_block(601, Some(&mut pool_state));
@@ -3412,7 +3383,7 @@ fn test_lock() {
34123383

34133384
let id: u64 = 1;
34143385
// Try to lock less than EXISTENTIAL_DEPOSIT should fail
3415-
Balances::set_lock(id.to_be_bytes(), &bob(), 100, WithdrawReasons::all());
3386+
Balances::set_lock(id.to_be_bytes(), &bob(), 1, WithdrawReasons::all());
34163387

34173388
// usable balance should now return free balance - EXISTENTIAL_DEPOSIT cause there was some activity
34183389
let usable_balance = Balances::usable_balance(&bob());
@@ -3421,7 +3392,7 @@ fn test_lock() {
34213392

34223393
// ----- INITIAL ------ //
34233394
// Try to lock more than EXISTENTIAL_DEPOSIT should succeed
3424-
let to_lock = 100 + EXISTENTIAL_DEPOSIT;
3395+
let to_lock = 1 + EXISTENTIAL_DEPOSIT;
34253396

34263397
Balances::set_lock(id.to_be_bytes(), &bob(), to_lock, WithdrawReasons::all());
34273398

@@ -3432,7 +3403,7 @@ fn test_lock() {
34323403

34333404
// ----- UPDATE ------ //
34343405
// updating a lock should succeed
3435-
let to_lock = 500 + EXISTENTIAL_DEPOSIT;
3406+
let to_lock = 10 + EXISTENTIAL_DEPOSIT;
34363407

34373408
Balances::set_lock(id.to_be_bytes(), &bob(), to_lock, WithdrawReasons::all());
34383409

0 commit comments

Comments
 (0)