Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Export more utility functions and structures #44

Merged
19 changes: 6 additions & 13 deletions packages/auto-consensus/__test__/balances.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { ActivateWalletInput, activateWallet } from '@autonomys/auto-utils'
import { address } from '../src/address'
import { balance, totalIssuance } from '../src/balances'
import { address, balance, totalIssuance } from '@autonomys/auto-consensus'
import { ActivateWalletInput, activateWallet, getMockWallet } from '@autonomys/auto-utils'
import { setup } from './helpers'

describe('Verify balances functions', () => {
const { isLocalhost, TEST_NETWORK, TEST_MNEMONIC, TEST_ADDRESS, ALICE_URI, ALICE_ADDRESS } =
setup()
const { isLocalhost, TEST_NETWORK, TEST_MNEMONIC, TEST_ADDRESS, wallets } = setup()

const alice = getMockWallet('Alice', wallets)

describe('Test totalIssuance()', () => {
test('Check totalIssuance return a number greater than zero', async () => {
Expand Down Expand Up @@ -33,14 +33,7 @@ describe('Verify balances functions', () => {

if (isLocalhost) {
test('Check balance of Alice wallet is greater than 0', async () => {
const { api, accounts } = await activateWallet({
...TEST_NETWORK,
uri: ALICE_URI,
} as ActivateWalletInput)
expect(accounts.length).toBeGreaterThan(0)
expect(accounts[0].address).toEqual(ALICE_ADDRESS)

const _balance = await balance(api, address(accounts[0].address))
const _balance = await balance(alice.api, address(alice.accounts[0].address))
expect(_balance.free).toBeGreaterThan(BigInt(0))
})
}
Expand Down
2 changes: 0 additions & 2 deletions packages/auto-consensus/__test__/helpers/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
export * from './events'
export * from './setup'
export * from './staking'
export * from './sudo'
export * from './tx'
14 changes: 5 additions & 9 deletions packages/auto-consensus/__test__/helpers/setup.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { NetworkInput } from '@autonomys/auto-utils'
import { activate, disconnect, networks } from '@autonomys/auto-utils'
import { activate, disconnect, mockWallets, networks, WalletActivated } from '@autonomys/auto-utils'

export const setup = () => {
const isLocalhost = process.env.LOCALHOST === 'true'
Expand All @@ -12,13 +12,12 @@ export const setup = () => {

const TEST_MNEMONIC = 'test test test test test test test test test test test junk'
const TEST_ADDRESS = '5GmS1wtCfR4tK5SSgnZbVT4kYw5W8NmxmijcsxCQE6oLW6A8'
const ALICE_URI = '//Alice'
const ALICE_ADDRESS = '5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY'
const BOB_URI = '//Bob'
const BOB_ADDRESS = '5FHneW46xGXgs5mUiveU4sbTyGBzmstUspZC92UhjJM694ty'

let wallets: WalletActivated[] = []

beforeAll(async () => {
await activate(TEST_NETWORK)
wallets = await mockWallets()
})

afterAll(async () => {
Expand All @@ -31,9 +30,6 @@ export const setup = () => {
TEST_INVALID_NETWORK,
TEST_MNEMONIC,
TEST_ADDRESS,
ALICE_URI,
ALICE_ADDRESS,
BOB_URI,
BOB_ADDRESS,
wallets,
}
}
62 changes: 6 additions & 56 deletions packages/auto-consensus/__test__/helpers/tx.ts
Original file line number Diff line number Diff line change
@@ -1,68 +1,18 @@
import { Events, signAndSendTx as signAndSend } from '@autonomys/auto-consensus'
import type { AddressOrPair, SubmittableExtrinsic } from '@polkadot/api/types'
import type { EventRecord } from '@polkadot/types/interfaces'
import type { ISubmittableResult } from '@polkadot/types/types'
import type { Events } from './events'

const validateEvents = (
events: EventRecord[],
eventsExpected: Events,
tx: string,
block: string,
log: boolean = true,
) => {
const _eventsExpected =
typeof eventsExpected === 'string'
? [eventsExpected]
: eventsExpected.map((e: string | string[]) => (typeof e === 'string' ? [e] : e)).flat()

events.forEach(({ event: { data, method, section } }) => {
// if (log) console.log(`${section}.${method}`, data.toString()) // Uncomment this line to log every events with their data
const index = _eventsExpected.indexOf(`${section}.${method}`)
if (index > -1) _eventsExpected.splice(index, 1)
else if (log)
console.log('Event not expected', `${section}.${method}`, 'tx', tx, 'block', block)
})
if (_eventsExpected.length > 0)
console.log('Events not found', _eventsExpected, 'tx', tx, 'block', block)

expect(_eventsExpected).toHaveLength(0)

return _eventsExpected
}

export const signAndSendTx = async (
sender: AddressOrPair,
tx: SubmittableExtrinsic<'promise', ISubmittableResult>,
eventsExpected: Events = [],
log: boolean = true,
) => {
let txHashHex: string | undefined = undefined
let blockHash: string | undefined = undefined
await new Promise<void>((resolve, reject) => {
tx.signAndSend(sender, ({ events, status, txHash }) => {
if (status.isInBlock) {
txHashHex = txHash.toHex()
blockHash = status.asInBlock.toHex()
if (log) console.log('Successful tx', txHashHex, 'in block', blockHash)
const result = await signAndSend(sender, tx, eventsExpected, log)

if (eventsExpected.length > 0) {
eventsExpected = validateEvents(events, eventsExpected, txHashHex, blockHash, log)
if (eventsExpected.length === 0) resolve()
else reject(new Error('Events not found'))
} else resolve()
} else if (
status.isRetracted ||
status.isFinalityTimeout ||
status.isDropped ||
status.isInvalid
) {
if (log) console.error('Transaction failed')
reject(new Error('Transaction failed'))
}
})
})
expect(txHashHex).toBeDefined()
expect(blockHash).toBeDefined()
expect(result.txHash).toBeDefined()
expect(result.blockHash).toBeDefined()
expect(result.eventsExpectedMissing).toHaveLength(0)

return { txHash: txHashHex, blockHash }
return result
}
2 changes: 1 addition & 1 deletion packages/auto-consensus/__test__/info.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { networkTimestamp } from '../src/info'
import { networkTimestamp } from '@autonomys/auto-consensus'
import { setup } from './helpers'

describe('Verify info functions', () => {
Expand Down
Loading
Loading