-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #20 from subspace/4-connect-a-wallet-to-the-api-an…
…d-add-a-transfer-function Connect a wallet to the api and add balance and transfer function
- Loading branch information
Showing
29 changed files
with
1,365 additions
and
33 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,26 +9,26 @@ jobs: | |
steps: | ||
- name: Checkout 🛎️ | ||
uses: actions/checkout@v3 | ||
|
||
- name: Set up Node.js | ||
uses: actions/setup-node@v2 | ||
with: | ||
node-version: '18' | ||
|
||
- name: Enable Corepack | ||
run: corepack enable | ||
|
||
- name: Set Yarn version to Berry | ||
run: corepack prepare [email protected] --activate | ||
|
||
- name: Install dependencies | ||
run: yarn install | ||
|
||
- name: Build auto-utils package 🔧 | ||
run: yarn workspace @autonomys/auto-utils build | ||
|
||
- name: Build all packages 🔧 | ||
run: yarn build | ||
|
||
- name: Run tests 🧪 | ||
run: yarn test |
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
Binary file not shown.
Large diffs are not rendered by default.
Oops, something went wrong.
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 +1,3 @@ | ||
nodeLinker: node-modules | ||
|
||
yarnPath: .yarn/releases/yarn-4.2.2.cjs |
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 |
---|---|---|
|
@@ -8,12 +8,28 @@ | |
], | ||
"scripts": { | ||
"build": "yarn workspaces foreach --all run build", | ||
"clean": "yarn workspaces foreach --all run clean", | ||
"format": "yarn workspaces foreach --all run format", | ||
"test": "yarn workspaces foreach --all run test" | ||
}, | ||
"packageManager": "[email protected]", | ||
"engines": { | ||
"node": ">=18.0.0" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/subspace/auto-sdk" | ||
}, | ||
"author": { | ||
"name": "Autonomys", | ||
"url": "https://www.autonomys.net" | ||
}, | ||
"devDependencies": { | ||
"@types/jest": "^29.5.12", | ||
"eslint": "^8.57.0", | ||
"jest": "^29.7.0", | ||
"prettier": "^3.2.5", | ||
"ts-jest": "^29.1.4", | ||
"typescript": "^5.4.5" | ||
}, | ||
"packageManager": "[email protected]" | ||
} | ||
} |
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,21 +1,74 @@ | ||
import { activate, disconnect } from '@autonomys/auto-utils' | ||
import { totalIssuance } from '../src/balances' | ||
import type { NetworkInput } from '@autonomys/auto-utils' | ||
import { | ||
ActivateWalletInput, | ||
activate, | ||
activateWallet, | ||
disconnect, | ||
networks, | ||
} from '@autonomys/auto-utils' | ||
import { address } from '../src/address' | ||
import { balance, totalIssuance } 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 = '5GmS1wtCfR4tK5SSgnZbVT4kYw5W8NmxmijcsxCQE6oLW6A8' | ||
const ALICE_URI = '//Alice' | ||
const ALICE_ADDRESS = '5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY' | ||
const BOB_URI = '//Bob' | ||
const BOB_ADDRESS = '5FHneW46xGXgs5mUiveU4sbTyGBzmstUspZC92UhjJM694ty' | ||
|
||
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, | ||
} as ActivateWalletInput) | ||
expect(accounts.length).toBeGreaterThan(0) | ||
expect(accounts[0].address).toEqual(TEST_ADDRESS) | ||
|
||
const _balance = await balance(api, address(accounts[0].address)) | ||
expect(_balance.free).toEqual(BigInt(0)) | ||
}) | ||
|
||
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)) | ||
expect(_balance.free).toBeGreaterThan(BigInt(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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import type { NetworkInput } from '@autonomys/auto-utils' | ||
import { | ||
ActivateWalletInput, | ||
activate, | ||
activateWallet, | ||
disconnect, | ||
networks, | ||
} from '@autonomys/auto-utils' | ||
import { address } from '../src/address' | ||
import { balance } from '../src/balances' | ||
import { transfer } from '../src/transfer' | ||
|
||
describe('Verify transfer 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 = '5GmS1wtCfR4tK5SSgnZbVT4kYw5W8NmxmijcsxCQE6oLW6A8' | ||
const ALICE_URI = '//Alice' | ||
const ALICE_ADDRESS = '5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY' | ||
const BOB_URI = '//Bob' | ||
const BOB_ADDRESS = '5FHneW46xGXgs5mUiveU4sbTyGBzmstUspZC92UhjJM694ty' | ||
|
||
beforeAll(async () => { | ||
await activate(TEST_NETWORK) | ||
}) | ||
|
||
afterAll(async () => { | ||
await disconnect() | ||
}) | ||
|
||
if (isLocalhost) { | ||
describe('Test transfer()', () => { | ||
test('Check transfer 1 ATC between Alice and Bob and check the balance before and after', 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 sender = accounts[0] | ||
let txHash: string | undefined | ||
|
||
const _balanceSenderStart = await balance(api, address(sender.address)) | ||
const _balanceReceiverStart = await balance(api, address(BOB_ADDRESS)) | ||
expect(_balanceSenderStart.free).toBeGreaterThan(BigInt(0)) | ||
|
||
const tx = await transfer(api, BOB_ADDRESS, 1) | ||
|
||
await new Promise<void>((resolve, reject) => { | ||
tx.signAndSend(sender, ({ status }) => { | ||
if (status.isInBlock) { | ||
txHash = status.asInBlock.toHex() | ||
console.log('Successful transfer of 1 with hash ' + txHash) | ||
resolve() | ||
} else if ( | ||
status.isRetracted || | ||
status.isFinalityTimeout || | ||
status.isDropped || | ||
status.isInvalid | ||
) { | ||
reject(new Error('Transaction failed')) | ||
} else { | ||
console.log('Status of transfer: ' + status.type) | ||
} | ||
}) | ||
}) | ||
|
||
expect(txHash).toBeDefined() | ||
|
||
const _balanceSenderEnd = await balance(api, address(sender.address)) | ||
const _balanceReceiverEnd = await balance(api, address(BOB_ADDRESS)) | ||
expect(_balanceSenderEnd.free).toBeLessThan(_balanceSenderStart.free) | ||
expect(_balanceReceiverEnd.free).toBeGreaterThan(_balanceReceiverStart.free) | ||
}) | ||
}) | ||
} else { | ||
test('Transfer test only run on localhost', async () => { | ||
expect(true).toBeTruthy() | ||
}) | ||
} | ||
}) |
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,3 @@ | ||
import { encodeAddress } from '@polkadot/keyring' | ||
|
||
export const address = (address: string | Uint8Array): string => encodeAddress(address, 2254) |
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,2 +1,4 @@ | ||
export * from './address' | ||
export * from './balances' | ||
export * from './info' | ||
export * from './transfer' |
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
Oops, something went wrong.