Skip to content

Commit

Permalink
Merge pull request #61 from subspace/feat/improve-auto-utils
Browse files Browse the repository at this point in the history
Improve auto utils
  • Loading branch information
marc-aurele-besner authored Jul 8, 2024
2 parents 00c21c9 + 2f4a493 commit a1810b4
Show file tree
Hide file tree
Showing 34 changed files with 185 additions and 99 deletions.
Binary file removed .yarn/install-state.gz
Binary file not shown.
4 changes: 2 additions & 2 deletions packages/auto-consensus/__test__/balances.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { address, balance, totalIssuance } from '@autonomys/auto-consensus'
import { ActivateWalletInput, activateWallet, getMockWallet } from '@autonomys/auto-utils'
import { balance, totalIssuance } from '@autonomys/auto-consensus'
import { ActivateWalletInput, activateWallet, address, getMockWallet } from '@autonomys/auto-utils'
import { setup } from './helpers'

describe('Verify balances functions', () => {
Expand Down
1 change: 0 additions & 1 deletion packages/auto-consensus/__test__/helpers/staking.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import type { ApiPromise } from '@polkadot/api'
import { u8aToHex } from '@polkadot/util'
import { operator, operators, RegisterOperatorInput } from '../../src/staking'

Expand Down
3 changes: 1 addition & 2 deletions packages/auto-consensus/__test__/helpers/tx.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Events, signAndSendTx as signAndSend } from '@autonomys/auto-consensus'
import type { AddressOrPair, SubmittableExtrinsic } from '@polkadot/api/types'
import type { ISubmittableResult } from '@polkadot/types/types'
import type { AddressOrPair, ISubmittableResult, SubmittableExtrinsic } from '@autonomys/auto-utils'

export const signAndSendTx = async (
sender: AddressOrPair,
Expand Down
24 changes: 14 additions & 10 deletions packages/auto-consensus/__test__/staking.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import {
address,
balance,
deregisterOperator,
events,
Expand All @@ -9,8 +8,13 @@ import {
sudo,
withdrawStake,
} from '@autonomys/auto-consensus'
import { ActivateWalletInput, activateWallet, getMockWallet } from '@autonomys/auto-utils'
import { mnemonicGenerate } from '@polkadot/util-crypto'
import {
ActivateWalletInput,
activateWallet,
address,
generateWallet,
getMockWallet,
} from '@autonomys/auto-utils'
import {
setup,
signAndSendTx,
Expand All @@ -26,10 +30,10 @@ describe('Verify staking functions', () => {
if (isLocalhost) {
describe('Test registerOperator()', () => {
test('Check Alice can register random wallet as an operator', async () => {
const mnemonicOperator = mnemonicGenerate()
const { mnemonic } = generateWallet()
const { accounts: operatorAccounts } = await activateWallet({
...TEST_NETWORK,
uri: mnemonicOperator,
mnemonic,
} as ActivateWalletInput)

const sender = alice.accounts[0]
Expand Down Expand Up @@ -87,10 +91,10 @@ describe('Verify staking functions', () => {
}, 10000)

test('Check Operator can addFunds after registration', async () => {
const mnemonicOperator = mnemonicGenerate()
const { mnemonic } = generateWallet()
const { accounts: operatorAccounts } = await activateWallet({
...TEST_NETWORK,
uri: mnemonicOperator,
mnemonic,
} as ActivateWalletInput)

const sender = alice.accounts[0]
Expand Down Expand Up @@ -139,10 +143,10 @@ describe('Verify staking functions', () => {

describe('Test deregisterOperator()', () => {
test('Check Operator can deregisterOperator after registration', async () => {
const mnemonicOperator = mnemonicGenerate()
const { mnemonic } = generateWallet()
const { accounts: operatorAccounts } = await activateWallet({
...TEST_NETWORK,
uri: mnemonicOperator,
mnemonic,
} as ActivateWalletInput)

const sender = alice.accounts[0]
Expand Down Expand Up @@ -181,7 +185,7 @@ describe('Verify staking functions', () => {
api: alice.api,
operatorId: findOperator.operatorId,
}),
[events.operatorDeRegistered],
[events.operatorDeregistered],
)
}
}, 60000)
Expand Down
4 changes: 2 additions & 2 deletions packages/auto-consensus/__test__/transfer.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { address, balance, events, transfer } from '@autonomys/auto-consensus'
import { getMockWallet } from '@autonomys/auto-utils'
import { balance, events, transfer } from '@autonomys/auto-consensus'
import { address, getMockWallet } from '@autonomys/auto-utils'
import { setup, signAndSendTx } from './helpers'

describe('Verify transfer functions', () => {
Expand Down
5 changes: 0 additions & 5 deletions packages/auto-consensus/src/address.ts

This file was deleted.

3 changes: 1 addition & 2 deletions packages/auto-consensus/src/balances.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
// file: src/balances.ts

import type { ApiPromise, BN } from '@autonomys/auto-utils'
import { activate } from '@autonomys/auto-utils'
import type { ApiPromise } from '@polkadot/api'
import type { BN } from '@polkadot/util'

type RawBalanceData = {
free: BN
Expand Down
2 changes: 1 addition & 1 deletion packages/auto-consensus/src/batch.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import type { ApiPromise } from '@polkadot/api'
import type { ApiPromise } from '@autonomys/auto-utils'

export const batch = async (api: ApiPromise, txs: any[]) => await api.tx.utility.batch(txs)
1 change: 0 additions & 1 deletion packages/auto-consensus/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// file: src/index.ts

export * from './address'
export * from './balances'
export * from './batch'
export * from './info'
Expand Down
2 changes: 1 addition & 1 deletion packages/auto-consensus/src/remark.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { ApiPromise } from '@polkadot/api'
import type { ApiPromise } from '@autonomys/auto-utils'

export const remark = async (api: ApiPromise, remark: string, withEvent?: boolean) =>
!withEvent ? await api.tx.system.remark(remark) : await api.tx.system.remarkWithEvent(remark)
11 changes: 4 additions & 7 deletions packages/auto-consensus/src/staking.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
// file: src/staking.ts

import type { ApiPromise } from '@polkadot/api'
import type { KeyringPair } from '@polkadot/keyring/types'
import type { ApiPromise, KeyringPair } from '@autonomys/auto-utils'
import { createAccountIdType, signingKey } from '@autonomys/auto-utils'
import type { StorageKey } from '@polkadot/types'
import { createType } from '@polkadot/types'
import type { AnyTuple, Codec } from '@polkadot/types-codec/types'
import { u8aToHex } from '@polkadot/util'

type RawOperatorId = string[]
type RawOperatorDetails = {
Expand Down Expand Up @@ -126,15 +124,14 @@ export const registerOperator = async (input: RegisterOperatorInput) => {
nominationTax,
} = input

const message = createType(api.registry, 'AccountId', senderAddress).toU8a()
const signingKey = u8aToHex(Operator.publicKey)
const message = createAccountIdType(api, senderAddress)
const signature = Operator.sign(message)

return await api.tx.domains.registerOperator(
parseString(domainId),
parseString(amountToStake),
{
signingKey,
signingKey: signingKey(Operator.publicKey),
minimumNominatorStake: parseString(minimumNominatorStake),
nominationTax: parseString(nominationTax),
},
Expand Down
2 changes: 1 addition & 1 deletion packages/auto-consensus/src/transfer.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// file: src/transfer.ts

import type { ApiPromise } from '@polkadot/api'
import type { ApiPromise } from '@autonomys/auto-utils'

export type Amount = BigInt | number | string

Expand Down
2 changes: 1 addition & 1 deletion packages/auto-consensus/src/utils/detectTxSuccess.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// file: src/utils/detectTxSuccess.ts

import type { EventRecord } from '@polkadot/types/interfaces'
import type { EventRecord } from '@autonomys/auto-utils'
import { expectSuccessfulTxEvent } from './events'

export const detectTxSuccess = (events: EventRecord[]): boolean => {
Expand Down
3 changes: 1 addition & 2 deletions packages/auto-consensus/src/utils/signAndSendTx.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// file: src/utils/signAndSendTx.ts

import type { AddressOrPair, SubmittableExtrinsic } from '@polkadot/api/types'
import type { ISubmittableResult } from '@polkadot/types/types'
import type { AddressOrPair, ISubmittableResult, SubmittableExtrinsic } from '@autonomys/auto-utils'
import type { Events } from '../types/events'
import type { EventsValidated, TransactionSignedAndSend } from '../types/transaction'
import { detectTxSuccess } from './detectTxSuccess'
Expand Down
9 changes: 6 additions & 3 deletions packages/auto-consensus/src/utils/sudo.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
// file: src/utils/sudo.ts

import { ApiPromise } from '@polkadot/api'
import type { AddressOrPair, SubmittableExtrinsic } from '@polkadot/api/types'
import type { ISubmittableResult } from '@polkadot/types/types'
import type {
AddressOrPair,
ApiPromise,
ISubmittableResult,
SubmittableExtrinsic,
} from '@autonomys/auto-utils'
import type { Events } from '../types/events'
import { expectSuccessfulTxEvent } from './events'
import { signAndSendTx } from './signAndSendTx'
Expand Down
2 changes: 1 addition & 1 deletion packages/auto-consensus/src/utils/validateEvents.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// file: src/utils/validateEvents.ts

import type { EventRecord } from '@polkadot/types/interfaces'
import type { EventRecord } from '@autonomys/auto-utils'
import type { Events } from '../types/events'
import type { EventsValidated } from '../types/transaction'
import { expectSuccessfulTxEvent } from './events'
Expand Down
18 changes: 11 additions & 7 deletions packages/auto-utils/__test__/wallet.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
NetworkInput,
WalletActivated,
activateWallet,
address,
mockWallets,
networks,
setupWallet,
Expand Down Expand Up @@ -30,22 +31,25 @@ describe('Verify wallet functions', () => {
wallets = await mockWallets()
aliceWallet = wallets[0].accounts[0]
bobWallet = wallets[1].accounts[0]
})
}, 20000)

describe('Test setupWallet()', () => {
test('Check setupWallet return a pair with matching address and public key when provided with a mnemonic', async () => {
const pair = await setupWallet({ mnemonic: TEST_MNEMONIC })
expect(pair.address).toEqual(TEST_ADDRESS)
const wallet = setupWallet({ mnemonic: TEST_MNEMONIC })
expect(wallet.commonAddress).toEqual(TEST_ADDRESS)
expect(wallet.address).toEqual(address(TEST_ADDRESS))
})

test('Check setupWallet return a pair with matching private key when provided with Alice seed', async () => {
const pair = await setupWallet({ uri: ALICE_URI })
expect(pair.address).toEqual(aliceWallet.address)
const wallet = setupWallet({ uri: ALICE_URI })
expect(wallet.commonAddress).toEqual(aliceWallet.address)
expect(wallet.address).toEqual(address(aliceWallet.address))
})

test('Check setupWallet return a pair with matching private key when provided with Bob seed', async () => {
const pair = await setupWallet({ uri: BOB_URI })
expect(pair.address).toEqual(bobWallet.address)
const wallet = setupWallet({ uri: BOB_URI })
expect(wallet.commonAddress).toEqual(bobWallet.address)
expect(wallet.address).toEqual(address(bobWallet.address))
})
})

Expand Down
9 changes: 9 additions & 0 deletions packages/auto-utils/src/address.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// file: src/address.ts

import { decodeAddress, encodeAddress } from '@polkadot/keyring'
import { DEFAULT_SS58_FORMAT } from './constants/wallet'

export const address = (address: string | Uint8Array): string =>
encodeAddress(address, DEFAULT_SS58_FORMAT)

export const decode = (address: string): Uint8Array => decodeAddress(address)
42 changes: 12 additions & 30 deletions packages/auto-utils/src/api.ts
Original file line number Diff line number Diff line change
@@ -1,50 +1,32 @@
import { ApiPromise as PolkadotApiPromise, WsProvider } from '@polkadot/api'
import { ApiPromise, WsProvider } from '@polkadot/api'
import { getNetworkDomainRpcUrls, getNetworkRpcUrls } from './network'
import type { ApiPromise } from './types/api'
import type { DomainInput, NetworkInput } from './types/network'

let provider: WsProvider | null = null
let apiInstance: ApiPromise | null = null

let domainProvider: WsProvider | null = null
let apiDomainInstance: ApiPromise | null = null

export const activate = async (input?: NetworkInput): Promise<ApiPromise> => {
// Get the first rpc urls for the network
const rpcUrl = getNetworkRpcUrls(input)
// Create the provider
provider = new WsProvider(rpcUrl[0])
const provider = new WsProvider(rpcUrl[0])
// Create the API instance
apiInstance = await PolkadotApiPromise.create({ provider })
const api = await ApiPromise.create({ provider })
await api.isReady

return apiInstance
return api
}

export const activateDomain = async (input: DomainInput): Promise<ApiPromise> => {
// Get the first rpc urls for the network
const rpcUrl = getNetworkDomainRpcUrls(input)
// Create the provider
domainProvider = new WsProvider(rpcUrl[0])
const provider = new WsProvider(rpcUrl[0])
// Create the API instance
apiDomainInstance = await PolkadotApiPromise.create({ provider: domainProvider })

return apiDomainInstance
}
const api = await ApiPromise.create({ provider })
await api.isReady

export const disconnect = async (): Promise<void> => {
// Disconnect the API instance and the provider if they exist
if (apiInstance) {
await apiInstance.disconnect()
apiInstance = null
provider = null
}
return api
}

export const disconnectDomain = async (): Promise<void> => {
// Disconnect the API instance and the provider if they exist
if (apiDomainInstance) {
await apiDomainInstance.disconnect()
apiDomainInstance = null
domainProvider = null
}
export const disconnect = async (api: ApiPromise): Promise<void> => {
// Disconnect the API instance and the provider
await api.disconnect()
}
2 changes: 2 additions & 0 deletions packages/auto-utils/src/constants/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,5 @@ export const mockURIs = [
'//Ivy',
'//Jacob',
]

export const DEFAULT_SS58_FORMAT = 2254
2 changes: 2 additions & 0 deletions packages/auto-utils/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// file: src/index.ts

export * from './address'
export * from './api'
export * from './crypto'
export * from './network'
Expand All @@ -10,3 +11,4 @@ export * from './wallet'

export * from './constants'
export * from './types'
export * from './utils'
3 changes: 0 additions & 3 deletions packages/auto-utils/src/types/api.ts

This file was deleted.

3 changes: 3 additions & 0 deletions packages/auto-utils/src/types/event.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import type { EventRecord } from '@polkadot/types/interfaces'

export type { EventRecord }
7 changes: 7 additions & 0 deletions packages/auto-utils/src/types/extrinsic.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// file: src/types/extrinsic.ts

import type { SubmittableExtrinsic, SubmittableModuleExtrinsics } from '@polkadot/api/types'
import type { Hash } from '@polkadot/types/interfaces'
import type { ISubmittableResult } from '@polkadot/types/types'

export type { Hash, ISubmittableResult, SubmittableExtrinsic, SubmittableModuleExtrinsics }
7 changes: 6 additions & 1 deletion packages/auto-utils/src/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
export * from './api'
import type { BN } from '@polkadot/util'

export * from './event'
export * from './extrinsic'
export * from './network'
export * from './wallet'

export type { BN }
Loading

0 comments on commit a1810b4

Please sign in to comment.