-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* fix: fund-redeem test * test: add multi governance members test to concurrent * chore: fmt
- Loading branch information
Showing
9 changed files
with
124 additions
and
115 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
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 |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { HexString } from '@polkadot/util/types'; | ||
import { newAddress, observeBalanceIncrease } from '../shared/utils'; | ||
import { getBalance } from '../shared/get_balance'; | ||
import { fundFlip } from '../shared/fund_flip'; | ||
import { redeemFlip } from '../shared/redeem_flip'; | ||
import { newStatechainAddress } from '../shared/new_statechain_address'; | ||
|
||
// Uses the seed to generate a new SC address and ETH address. | ||
// It then funds the SC address with FLIP, and redeems the FLIP to the ETH address | ||
// checking that the balance has increased. | ||
export async function testFundRedeem(seed: string) { | ||
const redeemSCAddress = await newStatechainAddress(seed); | ||
const redeemEthAddress = await newAddress('ETH', seed); | ||
console.log(`FLIP Redeem address: ${redeemSCAddress}`); | ||
console.log(`ETH Redeem address: ${redeemEthAddress}`); | ||
const initBalance = await getBalance('FLIP', redeemEthAddress); | ||
console.log(`Initial ERC20-FLIP balance: ${initBalance.toString()}`); | ||
const amount = 1000; | ||
// We fund to a specific SC address. | ||
await fundFlip(redeemSCAddress, amount.toString()); | ||
|
||
// The ERC20 FLIP is sent back to an ETH address, and the registered claim can only be executed by that address. | ||
await redeemFlip(seed, redeemEthAddress as HexString, (amount / 2).toString()); | ||
console.log('Observed RedemptionSettled event'); | ||
const newBalance = await observeBalanceIncrease('FLIP', redeemEthAddress, initBalance); | ||
console.log(`Redemption success! New balance: ${newBalance.toString()}`); | ||
console.log('=== Fund/Redeem Test Success ==='); | ||
process.exit(0); | ||
} |
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 |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import Keyring from '@polkadot/keyring'; | ||
import { cryptoWaitReady } from '@polkadot/util-crypto'; | ||
import { assert } from '@polkadot/util'; | ||
import { getChainflipApi, observeEvent } from '../shared/utils'; | ||
import { snowWhite, submitGovernanceExtrinsic } from '../shared/cf_governance'; | ||
|
||
async function getGovernanceMembers(): Promise<string[]> { | ||
const chainflip = await getChainflipApi(); | ||
|
||
const res = (await chainflip.query.governance.members()).toJSON(); | ||
return res as string[]; | ||
} | ||
|
||
async function setGovernanceMembers(members: string[]) { | ||
const chainflip = await getChainflipApi(); | ||
|
||
await submitGovernanceExtrinsic(chainflip.tx.governance.newMembershipSet(members)); | ||
} | ||
|
||
await cryptoWaitReady(); | ||
const keyring = new Keyring({ type: 'sr25519' }); | ||
keyring.setSS58Format(2112); | ||
|
||
const alice = keyring.createFromUri('//Alice'); | ||
|
||
async function addAliceToGovernance() { | ||
const initMembers = await getGovernanceMembers(); | ||
assert(initMembers.length === 1, 'Governance should only have 1 member'); | ||
|
||
const newMembers = [...initMembers, alice.address]; | ||
|
||
await setGovernanceMembers(newMembers); | ||
|
||
const chainflip = await getChainflipApi(); | ||
await observeEvent('governance:Executed', chainflip); | ||
|
||
assert((await getGovernanceMembers()).length === 2, 'Governance should now have 2 members'); | ||
|
||
console.log('Added Alice to governance!'); | ||
} | ||
|
||
async function submitWithMultipleGovernanceMembers() { | ||
const chainflip = await getChainflipApi(); | ||
|
||
// Killing 2 birds with 1 stone: testing governance execution with multiple | ||
// members *and* restoring governance to its original state | ||
await submitGovernanceExtrinsic(chainflip.tx.governance.newMembershipSet([snowWhite.address])); | ||
|
||
const proposalId = Number((await observeEvent('governance:Proposed', chainflip)).data); | ||
|
||
// Note that with two members, we need to approve with the other account: | ||
await chainflip.tx.governance.approve(proposalId).signAndSend(alice, { nonce: -1 }); | ||
|
||
const executedProposalId = Number((await observeEvent('governance:Executed', chainflip)).data); | ||
assert(proposalId === executedProposalId, 'Proposal Ids should match'); | ||
|
||
assert( | ||
(await getGovernanceMembers()).length === 1, | ||
'Governance should have been restored to 1 member', | ||
); | ||
|
||
console.log('Removed Alice from governance!'); | ||
} | ||
|
||
export async function testMultipleMembersGovernance() { | ||
console.log('=== Testing multiple members governance ==='); | ||
await addAliceToGovernance(); | ||
await submitWithMultipleGovernanceMembers(); | ||
|
||
console.log('=== Multiple members governance test complete ==='); | ||
process.exit(0); | ||
} |
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
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,29 +1,8 @@ | ||
#!/usr/bin/env -S pnpm tsx | ||
import { HexString } from '@polkadot/util/types'; | ||
import { newAddress, observeBalanceIncrease, runWithTimeout } from '../shared/utils'; | ||
import { getBalance } from '../shared/get_balance'; | ||
import { fundFlip } from '../shared/fund_flip'; | ||
import { redeemFlip } from '../shared/redeem_flip'; | ||
import { newStatechainAddress } from '../shared/new_statechain_address'; | ||
import { testFundRedeem } from '../shared/fund_redeem'; | ||
import { runWithTimeout } from '../shared/utils'; | ||
|
||
export async function fundRedeemTest() { | ||
const seed = 'redeem'; | ||
const redeemFlipAddress = await newStatechainAddress(seed); | ||
const redeemEthAddress = await newAddress('ETH', seed); | ||
console.log(`FLIP Redeem address: ${redeemFlipAddress}`); | ||
console.log(`ETH Redeem address: ${redeemEthAddress}`); | ||
const initBalance = await getBalance('FLIP', redeemEthAddress); | ||
console.log(`Initial ERC20-FLIP balance: ${initBalance.toString()}`); | ||
const amount = 1000; | ||
await fundFlip(redeemFlipAddress, amount.toString()); | ||
await redeemFlip(seed, redeemEthAddress as HexString, (amount / 2).toString()); | ||
console.log('Observed RedemptionSettled event'); | ||
const newBalance = await observeBalanceIncrease('FLIP', redeemEthAddress, initBalance); | ||
console.log(`Redemption success! New balance: ${newBalance.toString()}`); | ||
process.exit(0); | ||
} | ||
|
||
runWithTimeout(fundRedeemTest(), 600000).catch((error) => { | ||
runWithTimeout(testFundRedeem('redeem'), 600000).catch((error) => { | ||
console.error(error); | ||
process.exit(-1); | ||
}); |
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,78 +1,9 @@ | ||
#!/usr/bin/env -S pnpm tsx | ||
import Keyring from '@polkadot/keyring'; | ||
import { cryptoWaitReady } from '@polkadot/util-crypto'; | ||
import { assert } from '@polkadot/util'; | ||
import { getChainflipApi, observeEvent, runWithTimeout } from '../shared/utils'; | ||
import { snowWhite, submitGovernanceExtrinsic } from '../shared/cf_governance'; | ||
|
||
async function getGovernanceMembers(): Promise<string[]> { | ||
const chainflip = await getChainflipApi(); | ||
import { runWithTimeout } from '../shared/utils'; | ||
import { testMultipleMembersGovernance } from '../shared/multiple_members_governance'; | ||
|
||
const res = (await chainflip.query.governance.members()).toJSON(); | ||
return res as string[]; | ||
} | ||
|
||
async function setGovernanceMembers(members: string[]) { | ||
const chainflip = await getChainflipApi(); | ||
|
||
await submitGovernanceExtrinsic(chainflip.tx.governance.newMembershipSet(members)); | ||
} | ||
|
||
await cryptoWaitReady(); | ||
const keyring = new Keyring({ type: 'sr25519' }); | ||
keyring.setSS58Format(2112); | ||
|
||
const alice = keyring.createFromUri('//Alice'); | ||
|
||
async function addAliceToGovernance() { | ||
const initMembers = await getGovernanceMembers(); | ||
assert(initMembers.length === 1, 'Governance should only have 1 member'); | ||
|
||
const newMembers = [...initMembers, alice.address]; | ||
|
||
await setGovernanceMembers(newMembers); | ||
|
||
const chainflip = await getChainflipApi(); | ||
await observeEvent('governance:Executed', chainflip); | ||
|
||
assert((await getGovernanceMembers()).length === 2, 'Governance should now have 2 members'); | ||
|
||
console.log('Added Alice to governance!'); | ||
} | ||
|
||
async function submitWithMultipleGovernanceMembers() { | ||
const chainflip = await getChainflipApi(); | ||
|
||
// Killing 2 birds with 1 stone: testing governance execution with multiple | ||
// members *and* restoring governance to its original state | ||
await submitGovernanceExtrinsic(chainflip.tx.governance.newMembershipSet([snowWhite.address])); | ||
|
||
const proposalId = Number((await observeEvent('governance:Proposed', chainflip)).data); | ||
|
||
// Note that with two members, we need to approve with the other account: | ||
await chainflip.tx.governance.approve(proposalId).signAndSend(alice, { nonce: -1 }); | ||
|
||
const executedProposalId = Number((await observeEvent('governance:Executed', chainflip)).data); | ||
assert(proposalId === executedProposalId, 'Proposal Ids should match'); | ||
|
||
assert( | ||
(await getGovernanceMembers()).length === 1, | ||
'Governance should have been restored to 1 member', | ||
); | ||
|
||
console.log('Removed Alice from governance!'); | ||
} | ||
|
||
async function main() { | ||
console.log('=== Testing multiple members governance ==='); | ||
await addAliceToGovernance(); | ||
await submitWithMultipleGovernanceMembers(); | ||
|
||
console.log('=== Multiple members governance test complete ==='); | ||
process.exit(0); | ||
} | ||
|
||
runWithTimeout(main(), 120000).catch((error) => { | ||
runWithTimeout(testMultipleMembersGovernance(), 120000).catch((error) => { | ||
console.error(error); | ||
process.exit(-1); | ||
}); |
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