Skip to content

Commit

Permalink
add/improve staking tests
Browse files Browse the repository at this point in the history
  • Loading branch information
marc-aurele-besner committed Jun 12, 2024
1 parent f85ca8d commit ffe28b1
Showing 1 changed file with 78 additions and 4 deletions.
82 changes: 78 additions & 4 deletions packages/auto-consensus/__test__/staking.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,16 @@ import { ActivateWalletInput, activateWallet } from '@autonomys/auto-utils'
import { mnemonicGenerate } from '@polkadot/util-crypto'
import { address } from '../src/address'
import { balance } from '../src/balances'
import { deregisterOperator, nominateOperator, registerOperator } from '../src/staking'
import {
deregisterOperator,
nominateOperator,
registerOperator,
unlockFunds,
withdrawStake,
} from '../src/staking'
import { transfer } from '../src/transfer'
import {
events,
setup,
signAndSendTx,
sudo,
Expand Down Expand Up @@ -47,7 +54,7 @@ describe('Verify staking functions', () => {
minimumNominatorStake,
nominationTax,
}
await signAndSendTx(sender, await registerOperator(txInput))
await signAndSendTx(sender, await registerOperator(txInput), [events.operatorRegistered])
const findOperator = await verifyOperatorRegistration(txInput)

const _balanceSenderEnd = await balance(api, address(sender.address))
Expand Down Expand Up @@ -123,7 +130,7 @@ describe('Verify staking functions', () => {
minimumNominatorStake,
nominationTax,
}
await signAndSendTx(sender, await registerOperator(txInput))
await signAndSendTx(sender, await registerOperator(txInput), [events.operatorRegistered])
await verifyOperatorRegistration(txInput)

const _balanceSenderEnd = await balance(api, address(sender.address))
Expand All @@ -136,6 +143,72 @@ describe('Verify staking functions', () => {
}, 30000)
})

describe('Test withdrawStake()', () => {
test('Check Operator can withdrawStake after registration', async () => {
const mnemonicOperator = mnemonicGenerate()
const { api, accounts } = await activateWallet({
...TEST_NETWORK,
uri: ALICE_URI,
} as ActivateWalletInput)
const { accounts: operatorAccounts } = await activateWallet({
...TEST_NETWORK,
uri: mnemonicOperator,
} as ActivateWalletInput)
expect(accounts.length).toBeGreaterThan(0)
expect(accounts[0].address).toEqual(ALICE_ADDRESS)

const sender = accounts[0]

const _balanceSenderStart = await balance(api, address(sender.address))
expect(_balanceSenderStart.free).toBeGreaterThan(BigInt(0))

// Transfer some funds to the operator
const amountToTransfer = '10000000000000000000'
await signAndSendTx(
sender,
await transfer(api, operatorAccounts[0].address, amountToTransfer),
)

const domainId = '0'
const amountToStake = '100000000000000000000'
const minimumNominatorStake = '1000000000000000000'
const nominationTax = '5'
const txInput = {
api,
senderAddress: ALICE_ADDRESS,
Operator: operatorAccounts[0],
domainId,
amountToStake,
minimumNominatorStake,
nominationTax,
}
await signAndSendTx(sender, await registerOperator(txInput), [events.operatorRegistered])
await verifyOperatorRegistration(txInput)

const _balanceSenderEnd = await balance(api, address(sender.address))
expect(_balanceSenderEnd.free).toBeLessThan(
_balanceSenderStart.free - BigInt(amountToStake),
)

await sudo(api, sender, await api.tx.domains.forceStakingEpochTransition(domainId))
const findOperator = await verifyOperatorRegistrationFinal(txInput)

await sudo(api, sender, await api.tx.domains.forceStakingEpochTransition(domainId))

if (findOperator) {
await signAndSendTx(
operatorAccounts[0],
await withdrawStake({
api,
operatorId: findOperator.operatorId,
shares: findOperator.operatorDetails.currentTotalShares,
}),
[events.withdrawStake],
)
}
}, 60000)
})

describe('Test deregisterOperator()', () => {
test('Check Operator can deregisterOperator after registration', async () => {
const mnemonicOperator = mnemonicGenerate()
Expand Down Expand Up @@ -175,7 +248,7 @@ describe('Verify staking functions', () => {
minimumNominatorStake,
nominationTax,
}
await signAndSendTx(sender, await registerOperator(txInput))
await signAndSendTx(sender, await registerOperator(txInput), [events.operatorRegistered])
await verifyOperatorRegistration(txInput)

const _balanceSenderEnd = await balance(api, address(sender.address))
Expand All @@ -193,6 +266,7 @@ describe('Verify staking functions', () => {
api,
operatorId: findOperator.operatorId,
}),
[events.operatorDeRegistered],
)
}
}, 30000)
Expand Down

0 comments on commit ffe28b1

Please sign in to comment.