-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* test tx fee handler * fix test
- Loading branch information
Showing
3 changed files
with
93 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,65 @@ | ||
import { ApiPromise, Keyring, WsProvider } from '@polkadot/api'; | ||
import { submitExtrinsic } from '../common'; | ||
import assert from 'node:assert'; | ||
import { getAddressFromModuleId, getFreeBalance } from '../common'; | ||
|
||
async function run(nodeName: string, networkInfo: any, _jsArgs: any) { | ||
const { wsUri } = networkInfo.nodesByName[nodeName]; | ||
const api = await ApiPromise.create({ provider: new WsProvider(wsUri) }); | ||
const { wsUri: regionXUri } = networkInfo.nodesByName[nodeName]; | ||
const regionXApi = await ApiPromise.create({ provider: new WsProvider(regionXUri) }); | ||
|
||
// account to submit tx | ||
const keyring = new Keyring({ type: 'sr25519' }); | ||
const alice = keyring.addFromUri('//Alice'); | ||
const bob = keyring.addFromUri('//Bob'); | ||
|
||
const call = api.tx.balances.transferKeepAlive(bob.address, 10n ** 6n); | ||
const sudo = api.tx.sudo.sudo(call); | ||
await submitExtrinsic(alice, sudo, {}); | ||
const treasuryId = regionXApi.consts.treasury.palletId.toHuman() as string; | ||
const treasuryAccount = getAddressFromModuleId(treasuryId); | ||
|
||
const podId = 'PotStake'; // FIXME: remove this hard-coded constant and fetch the on-chain value. | ||
const potAccount = getAddressFromModuleId(podId); | ||
|
||
const treasuryBalanceOld = await getFreeBalance(regionXApi, treasuryAccount); | ||
const potBalanceOld = await getFreeBalance(regionXApi, potAccount); | ||
|
||
const call = regionXApi.tx.system.remark('0x44'); | ||
|
||
let fee = BigInt(0), | ||
tips = BigInt(0); | ||
|
||
const promise: Promise<void> = new Promise((resolve, reject) => { | ||
const unsub = call.signAndSend(alice, { tip: 1_000_000_000 }, ({ status, isError, events }) => { | ||
console.log(`Current status is ${status}`); | ||
if (status.isInBlock) { | ||
console.log(`Transaction included at blockHash ${status.asInBlock}`); | ||
} else if (status.isFinalized) { | ||
console.log(`Transaction finalized at blockHash ${status.asFinalized}`); | ||
for (const event of events) { | ||
const { | ||
event: { data, method, section }, | ||
} = event; | ||
if (section === 'transactionPayment' && method === 'TransactionFeePaid') { | ||
const args = data.toJSON() as [string, number, number]; | ||
tips = BigInt(args[2]); | ||
fee = BigInt(args[1]) - tips; | ||
} | ||
} | ||
|
||
unsub.then(); | ||
return resolve(); | ||
} else if (isError) { | ||
console.log('Transaction error'); | ||
unsub.then(); | ||
return reject(); | ||
} | ||
}); | ||
}); | ||
await promise; | ||
const treasuryBalanceNew = await getFreeBalance(regionXApi, treasuryAccount); | ||
const potBalanceNew = await getFreeBalance(regionXApi, potAccount); | ||
|
||
const fee2Treasury = (fee * 60n) / 100n; | ||
const fee2Collators = fee - fee2Treasury + tips; | ||
|
||
assert.equal(treasuryBalanceNew - treasuryBalanceOld, fee2Treasury); | ||
assert.equal(potBalanceNew - potBalanceOld, fee2Collators); | ||
} | ||
|
||
export { run }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters