Skip to content

Commit

Permalink
chore: replace all uses of tx.balances.transfer() (#903)
Browse files Browse the repository at this point in the history
* chore: replace all uses of tx.balances.transfer()
* test: update mocks
  • Loading branch information
rflechtner authored Sep 5, 2024
1 parent b657abb commit 4781dc8
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 28 deletions.
18 changes: 9 additions & 9 deletions packages/chain-helpers/src/blockchain/Blockchain.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,23 +34,23 @@ describe('Blockchain', () => {

it('allows waiting for finalization', async () => {
api.__setDefaultResult({ isFinalized: true })
const tx = api.tx.balances.transfer('abcdef', 50)
const tx = api.tx.balances.transferAllowDeath('abcdef', 50)
expect(
await signAndSubmitTx(tx, pair, { resolveOn: IS_FINALIZED })
).toHaveProperty('isFinalized', true)
})

it('allows waiting for in block', async () => {
api.__setDefaultResult({ isInBlock: true })
const tx = api.tx.balances.transfer('abcdef', 50)
const tx = api.tx.balances.transferAllowDeath('abcdef', 50)
expect(
await signAndSubmitTx(tx, pair, { resolveOn: IS_IN_BLOCK })
).toHaveProperty('isInBlock', true)
})

it('allows waiting for ready', async () => {
api.__setDefaultResult({ isReady: true })
const tx = api.tx.balances.transfer('abcdef', 50)
const tx = api.tx.balances.transferAllowDeath('abcdef', 50)
expect(
await signAndSubmitTx(tx, pair, { resolveOn: IS_READY })
).toHaveProperty('status.isReady', true)
Expand All @@ -59,20 +59,20 @@ describe('Blockchain', () => {
it('uses default resolution config', async () => {
api.__setDefaultResult({ isReady: true })
ConfigService.set({ submitTxResolveOn: IS_READY })
let tx = api.tx.balances.transfer('abcdef', 50)
let tx = api.tx.balances.transferAllowDeath('abcdef', 50)
expect(await signAndSubmitTx(tx, pair)).toHaveProperty(
'status.isReady',
true
)

api.__setDefaultResult({ isInBlock: true })
ConfigService.set({ submitTxResolveOn: IS_IN_BLOCK })
tx = api.tx.balances.transfer('abcdef', 50)
tx = api.tx.balances.transferAllowDeath('abcdef', 50)
expect(await signAndSubmitTx(tx, pair)).toHaveProperty('isInBlock', true)

api.__setDefaultResult({ isFinalized: true })
ConfigService.set({ submitTxResolveOn: IS_FINALIZED })
tx = api.tx.balances.transfer('abcdef', 50)
tx = api.tx.balances.transferAllowDeath('abcdef', 50)
expect(await signAndSubmitTx(tx, pair)).toHaveProperty(
'isFinalized',
true
Expand All @@ -81,7 +81,7 @@ describe('Blockchain', () => {

it('rejects on error condition', async () => {
api.__setDefaultResult({ isInvalid: true })
const tx = api.tx.balances.transfer('abcdef', 50)
const tx = api.tx.balances.transferAllowDeath('abcdef', 50)
await expect(
signAndSubmitTx(tx, pair, { resolveOn: IS_FINALIZED })
).rejects.toHaveProperty('isError', true)
Expand All @@ -90,7 +90,7 @@ describe('Blockchain', () => {
it('throws if subscriptions not supported', async () => {
// @ts-ignore
api.hasSubscriptions = false
const tx = api.tx.balances.transfer('abcdef', 50)
const tx = api.tx.balances.transferAllowDeath('abcdef', 50)
await expect(
signAndSubmitTx(tx, pair, { resolveOn: IS_FINALIZED })
).rejects.toThrow(SDKErrors.SubscriptionsNotSupportedError)
Expand All @@ -102,7 +102,7 @@ describe('Blockchain', () => {
// mock disconnect 500 ms after submission
if (ev === 'disconnected') setTimeout(callback, 500)
})
const tx = api.tx.balances.transfer('abcdef', 50)
const tx = api.tx.balances.transferAllowDeath('abcdef', 50)
await expect(
signAndSubmitTx(tx, pair, { resolveOn: IS_FINALIZED })
).rejects.toHaveProperty('internalError', expect.any(Error))
Expand Down
22 changes: 14 additions & 8 deletions tests/integration/Balance.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,10 @@ describe('when there is a dev chain with a faucet', () => {
const spy = jest.fn<any>()
api.query.system.account(address, spy)
const balanceBefore = (await api.query.system.account(faucet.address)).data
const transferTx = api.tx.balances.transfer(address, EXISTENTIAL_DEPOSIT)
const transferTx = api.tx.balances.transferAllowDeath(
address,
EXISTENTIAL_DEPOSIT
)
await submitTx(transferTx, faucet)
const balanceAfter = (await api.query.system.account(faucet.address)).data
const balanceIdent = (await api.query.system.account(address)).data
Expand All @@ -95,7 +98,7 @@ describe('When there are haves and have-nots', () => {
})

it('can transfer tokens from the rich to the poor', async () => {
const transferTx = api.tx.balances.transfer(
const transferTx = api.tx.balances.transferAllowDeath(
stormyD.address,
EXISTENTIAL_DEPOSIT
)
Expand All @@ -107,7 +110,7 @@ describe('When there are haves and have-nots', () => {
it('should not accept transactions from KeyringPair with zero balance', async () => {
const originalBalance = (await api.query.system.account(stormyD.address))
.data
const transferTx = api.tx.balances.transfer(
const transferTx = api.tx.balances.transferAllowDeath(
stormyD.address,
EXISTENTIAL_DEPOSIT
)
Expand All @@ -125,7 +128,7 @@ describe('When there are haves and have-nots', () => {
it.skip('should not accept transactions when sender cannot pay gas, but will keep gas fee', async () => {
const RichieBalance = (await api.query.system.account(richieRich.address))
.data
const transferTx = api.tx.balances.transfer(
const transferTx = api.tx.balances.transferAllowDeath(
bobbyBroke.address,
RichieBalance.free
)
Expand All @@ -142,12 +145,12 @@ describe('When there are haves and have-nots', () => {
const spy = jest.fn<any>()
api.query.system.account(faucet.address, spy)

const transferTx1 = api.tx.balances.transfer(
const transferTx1 = api.tx.balances.transferAllowDeath(
richieRich.address,
EXISTENTIAL_DEPOSIT
)
await submitTx(transferTx1, faucet)
const transferTx2 = api.tx.balances.transfer(
const transferTx2 = api.tx.balances.transferAllowDeath(
stormyD.address,
EXISTENTIAL_DEPOSIT
)
Expand All @@ -161,8 +164,11 @@ describe('When there are haves and have-nots', () => {
api.query.system.account(faucet.address, listener)

const batch = api.tx.utility.batchAll([
api.tx.balances.transfer(richieRich.address, EXISTENTIAL_DEPOSIT),
api.tx.balances.transfer(stormyD.address, EXISTENTIAL_DEPOSIT),
api.tx.balances.transferAllowDeath(
richieRich.address,
EXISTENTIAL_DEPOSIT
),
api.tx.balances.transferAllowDeath(stormyD.address, EXISTENTIAL_DEPOSIT),
])
await submitTx(batch, faucet)

Expand Down
10 changes: 5 additions & 5 deletions tests/integration/Blockchain.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,19 @@ describe('Chain returns specific errors, that we check for', () => {
testIdentity = (await makeSigningKeyTool()).keypair
charlie = devCharlie

const transferTx = api.tx.balances.transfer(
const transferTx = api.tx.balances.transferAllowDeath(
testIdentity.address,
BalanceUtils.toFemtoKilt(10000)
)
await submitTx(transferTx, faucet)
}, 40000)

it(`throws TxOutdated error if the nonce was already used for Tx in block`, async () => {
const tx = api.tx.balances.transfer(
const tx = api.tx.balances.transferAllowDeath(
charlie.address,
new BN('1000000000000001')
)
const errorTx = api.tx.balances.transfer(
const errorTx = api.tx.balances.transferAllowDeath(
charlie.address,
new BN('1000000000000000')
)
Expand Down Expand Up @@ -93,11 +93,11 @@ describe('Chain returns specific errors, that we check for', () => {
}, 40000)

it(`throws 'ERROR_TRANSACTION_USURPED' error if separate Tx was imported with identical nonce but higher priority while Tx is in pool`, async () => {
const tx = api.tx.balances.transfer(
const tx = api.tx.balances.transferAllowDeath(
charlie.address,
new BN('1000000000000000')
)
const errorTx = api.tx.balances.transfer(
const errorTx = api.tx.balances.transferAllowDeath(
charlie.address,
new BN('1000000000000000')
)
Expand Down
5 changes: 4 additions & 1 deletion tests/integration/Ctypes.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,10 @@ describe('When there is an CtypeCreator and a verifier', () => {
).data.free.toBigInt() < minBalance
) {
console.log('sending funds to assertion method key account...')
const fundsTx = api.tx.balances.transfer(assertionMethodKey, minBalance)
const fundsTx = api.tx.balances.transferAllowDeath(
assertionMethodKey,
minBalance
)
await submitTx(fundsTx, paymentAccount)
console.log('sending funds completed')
}
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/ErrorHandler.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ beforeAll(async () => {
}, 60_000)

it('records an extrinsic error when transferring less than the existential amount to new identity', async () => {
const transferTx = api.tx.balances.transfer(addressFromRandom(), 1)
const transferTx = api.tx.balances.transferAllowDeath(addressFromRandom(), 1)
const promise = submitTx(transferTx, paymentAccount)
if (api.runtimeVersion.specVersion.toBigInt() >= 11_200n) {
await expect(promise).rejects.toMatchInlineSnapshot(`
Expand Down
5 changes: 4 additions & 1 deletion tests/integration/PublicCredentials.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,10 @@ describe('When there is an attester and ctype NFT name', () => {
).data.free.toBigInt() < minBalance
) {
console.log('sending funds to assertion method key account...')
const fundsTx = api.tx.balances.transfer(assertionMethodKey, minBalance)
const fundsTx = api.tx.balances.transferAllowDeath(
assertionMethodKey,
minBalance
)
await submitTx(fundsTx, tokenHolder)
console.log('sending funds completed')
}
Expand Down
6 changes: 4 additions & 2 deletions tests/integration/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,9 @@ export async function endowAccounts(
): Promise<void> {
const api = ConfigService.get('api')
const transactions = await Promise.all(
addresses.map((address) => api.tx.balances.transfer(address, ENDOWMENT))
addresses.map((address) =>
api.tx.balances.transferKeepAlive(address, ENDOWMENT)
)
)
const batch = api.tx.utility.batchAll(transactions)
await Blockchain.signAndSubmitTx(batch, faucet, { resolveOn })
Expand All @@ -182,7 +184,7 @@ export async function fundAccount(
amount: BN
): Promise<void> {
const api = ConfigService.get('api')
const transferTx = api.tx.balances.transfer(address, amount)
const transferTx = api.tx.balances.transferKeepAlive(address, amount)
await submitTx(transferTx, devFaucet)
}

Expand Down
3 changes: 2 additions & 1 deletion tests/testUtils/mocks/mockedApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,8 @@ export function getMockedApi(): MockApiPromise {
reclaimDeposit: jest.fn((claimHash: string) => getMockSubmittableTx()),
},
balances: {
transfer: jest.fn(() => getMockSubmittableTx()),
transferAllowDeath: jest.fn(() => getMockSubmittableTx()),
transferKeepAlive: jest.fn(() => getMockSubmittableTx()),
},
ctype: {
add: jest.fn((hash, signature) => getMockSubmittableTx()),
Expand Down

0 comments on commit 4781dc8

Please sign in to comment.