Skip to content

Commit

Permalink
improve balance and add address + tests
Browse files Browse the repository at this point in the history
  • Loading branch information
marc-aurele-besner committed Jun 5, 2024
1 parent b52c5d2 commit dd93119
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 11 deletions.
78 changes: 68 additions & 10 deletions packages/auto-consensus/__test__/balances.test.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,79 @@
import { activate, disconnect } from '@autonomys/auto-utils'
import { totalIssuance } from '../src/balances'
import type { NetworkInput } from '@autonomys/auto-utils'
import { activate, activateWallet, disconnect, networks } from '@autonomys/auto-utils'
import { address } from '../src/address'
import { totalIssuance, transfer } from '../src/balances'

describe('Verify balances functions', () => {
const isLocalhost = process.env.LOCALHOST === 'true'

// Define the test network and its details
const TEST_NETWORK: NetworkInput = !isLocalhost
? { networkId: networks[0].id }
: { networkId: 'autonomys-localhost' }
const TEST_INVALID_NETWORK = { networkId: 'invalid-network' }

const TEST_MNEMONIC = 'test test test test test test test test test test test junk'
const TEST_ADDRESS = '5Fj5aLd4crCYn7zM5hLZL8m6e9aNzWssiTgA3TrprLjxy6Mc'
const ALICE_URI = '//BOB'
const ALICE_ADDRESS = '5DAw2FpYk2y3JHrsia14KEx7tpezNymdFKkunicZ5ygPGXYF'
const BOB_URI = '//BOB'
const BOB_ADDRESS = '5DAw2FpYk2y3JHrsia14KEx7tpezNymdFKkunicZ5ygPGXYF'

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

afterAll(async () => {
await disconnect()
})

test('Check totalIssuance return a number greater than zero', async () => {
// totalIssuance is an async function that returns a hex number as a string
const rawIssuance = await totalIssuance()
// Convert the hex number to a BigInt
const issuance = BigInt(rawIssuance.toString())
// Check if the issuance is greater than zero
expect(issuance).toBeGreaterThan(BigInt(0))
describe('Test totalIssuance()', () => {
test('Check totalIssuance return a number greater than zero', async () => {
// totalIssuance is an async function that returns a hex number as a string
const rawIssuance = await totalIssuance()
// Convert the hex number to a BigInt
const issuance = BigInt(rawIssuance.toString())
// Check if the issuance is greater than zero
expect(issuance).toBeGreaterThan(BigInt(0))
})
})

describe('Test balance()', () => {
test('Check balance of Test wallet is 0', async () => {
const { api, accounts } = await activateWallet({
...TEST_NETWORK,
mnemonic: TEST_MNEMONIC,
})

const balance = await api.query.balances.account(address(accounts[0].address))
expect(balance.free.toBigInt()).toEqual(BigInt(0))
})
})

describe('Test transfer()', () => {
if (isLocalhost) {
test('Check transfer 1 ATC between Alice and Bob', async () => {
const { api, accounts } = await activateWallet({
...TEST_NETWORK,
uri: ALICE_URI,
})
const sender = accounts[0]

const hash = await transfer(api, sender, BOB_ADDRESS, 1)
})
}

test('Check transfer 1 ATC between Test wallet and Alice should fail (no balance)', async () => {
const { api, accounts } = await activateWallet({
...TEST_NETWORK,
mnemonic: TEST_MNEMONIC,
})
const sender = accounts[0]

expect(() => transfer(api, sender, ALICE_ADDRESS, 1)).toThrow(
'Unreachable code should not be executed (evaluating',
// To-Do: Confirm this is the expected error message
)
})
})
})
5 changes: 5 additions & 0 deletions packages/auto-consensus/src/address.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { encodeAddress } from '@polkadot/keyring'

export const address = (address: string | Uint8Array): string => {
return encodeAddress(address, 2254)
}
14 changes: 14 additions & 0 deletions packages/auto-consensus/src/balances.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { activate } from '@autonomys/auto-utils'
import { ApiPromise } from '@polkadot/api'
import type { KeyringPair } from '@polkadot/keyring/types'

export const totalIssuance = async (networkId?: string) => {
// Get the api instance for the network
Expand All @@ -9,3 +11,15 @@ export const totalIssuance = async (networkId?: string) => {

return totalIssuance
}

export const transfer = async (
api: ApiPromise,
sender: KeyringPair,
receiver: string,
amount: BigInt | number | string,
) => {
// Transfer the tokens
const transfer = await api.tx.balances.transferKeepAlive(receiver, amount).signAndSend(sender)

return transfer
}
1 change: 1 addition & 0 deletions packages/auto-consensus/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './address'
export * from './balances'
export * from './info'
2 changes: 1 addition & 1 deletion packages/auto-utils/__test__/wallet.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ describe('Verify wallet functions', () => {

const TEST_INVALID_NETWORK = { networkId: 'invalid-network' }

const TEST_MNEMONIC = 'test test test test test test test test test test test junk' //Alice
const TEST_MNEMONIC = 'test test test test test test test test test test test junk'
const TEST_ADDRESS = '5Fj5aLd4crCYn7zM5hLZL8m6e9aNzWssiTgA3TrprLjxy6Mc'
const ALICE_URI = '//BOB'
const ALICE_ADDRESS = '5DAw2FpYk2y3JHrsia14KEx7tpezNymdFKkunicZ5ygPGXYF'
Expand Down

0 comments on commit dd93119

Please sign in to comment.