Skip to content

Commit 6aef6f0

Browse files
Merge pull request #36 from subspace/32-create-reusable-scripts-to-query-balance-and-transfer-tokens
Create reusable scripts to query balance and transfer tokens
2 parents 2a66934 + 8bf56fb commit 6aef6f0

File tree

10 files changed

+271
-2
lines changed

10 files changed

+271
-2
lines changed

.vscode/auto.code-workspace

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@
1616
"name": "auto-id",
1717
"path": "../packages/auto-id",
1818
},
19+
{
20+
"name": "node-examples",
21+
"path": "../examples/node",
22+
},
1923
],
2024
"settings": {
2125
"editor.codeActionsOnSave": {

examples/node/.env.local

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
ALICE_SEED="//Alice"
2+
BOB_SEED="//Bob"

examples/node/README.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Autonomys SDK - Node Example
2+
3+
Simple list of scripts to query data and execute extrinsics:
4+
5+
## Install
6+
7+
```bash
8+
yarn
9+
```
10+
11+
## Run queries
12+
13+
```bash
14+
yarn balance
15+
```
16+
17+
## Execute extrinsics
18+
19+
```bash
20+
yarn transfer
21+
```
22+
23+
## Utility
24+
25+
```bash
26+
yarn address
27+
```
28+
29+
## Run All
30+
31+
```bash
32+
yarn all
33+
```

examples/node/package.json

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"name": "node",
3+
"version": "0.1.0",
4+
"private": true,
5+
"license": "MIT",
6+
"packageManager": "yarn@4.2.2",
7+
"repository": {
8+
"type": "git",
9+
"url": "https://github.com/subspace/auto-sdk"
10+
},
11+
"author": {
12+
"name": "Autonomys",
13+
"url": "https://www.autonomys.net"
14+
},
15+
"scripts": {
16+
"all": "yarn address && yarn balance && yarn transfer",
17+
"address": "npx ts-node ./src/address.ts",
18+
"balance": "npx ts-node ./src/balance.ts",
19+
"transfer": "npx ts-node ./src/transfer.ts"
20+
},
21+
"dependencies": {
22+
"@autonomys/auto-consensus": "workspace:*",
23+
"@autonomys/auto-utils": "workspace:*",
24+
"dotenv": "^16.4.5"
25+
}
26+
}

examples/node/src/address.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { address } from '@autonomys/auto-consensus'
2+
import { setup } from './utils/setup'
3+
4+
const main = async () => {
5+
const { alice, bob } = await setup()
6+
7+
// Alice's Addresses
8+
console.log('\x1b[33m%s\x1b[0m', 'Alice Raw Address:', alice[0].address)
9+
const aliceAddress = address(alice[0].address)
10+
console.log('\x1b[32m%s\x1b[0m', 'Alice Clean Address:', aliceAddress, '\n')
11+
12+
// Bob's Addresses
13+
console.log('\x1b[33m%s\x1b[0m', 'Bob Raw Address:', bob[0].address)
14+
const bobAddress = address(bob[0].address)
15+
console.log('\x1b[32m%s\x1b[0m', 'Bob Clean Address:', bobAddress, '\n')
16+
}
17+
18+
main()
19+
.then(() => {
20+
console.log('\x1b[34m%s\x1b[0m', 'Script executed successfully')
21+
process.exit(0)
22+
})
23+
.catch((e) => {
24+
console.error('\x1b[31m%s\x1b[0m', 'Error with script:', e)
25+
process.exit(1)
26+
})

examples/node/src/balance.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import { address, balance } from '@autonomys/auto-consensus'
2+
import { setup } from './utils/setup'
3+
4+
const main = async () => {
5+
const { api, alice, bob } = await setup()
6+
7+
// Alice's Addresses and Balance
8+
const aliceAddress = address(alice[0].address)
9+
console.log('\x1b[32m%s\x1b[0m', 'Alice Clean Address:', aliceAddress)
10+
const aliceBalance = await balance(api, aliceAddress)
11+
console.log(
12+
'\x1b[36m%s\x1b[0m',
13+
'Alice Free Balance:',
14+
aliceBalance.free.toString(),
15+
'\x1b[36m',
16+
'ATC',
17+
'\x1b[0m\n',
18+
)
19+
20+
// Bob's Addresses and Balance
21+
const bobAddress = address(bob[0].address)
22+
console.log('\x1b[32m%s\x1b[0m', 'Bob Clean Address:', bobAddress)
23+
const bobBalance = await balance(api, bobAddress)
24+
console.log(
25+
'\x1b[36m%s\x1b[0m',
26+
'Bob Free Balance:',
27+
bobBalance.free.toString(),
28+
'\x1b[36m',
29+
'ATC',
30+
'\x1b[0m\n',
31+
'\n',
32+
)
33+
}
34+
35+
main()
36+
.then(() => {
37+
console.log('\x1b[34m%s\x1b[0m', 'Script executed successfully')
38+
process.exit(0)
39+
})
40+
.catch((e) => {
41+
console.error('\x1b[31m%s\x1b[0m', 'Error with script:', e)
42+
process.exit(1)
43+
})

examples/node/src/transfer.ts

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
import { address, balance, transfer } from '@autonomys/auto-consensus'
2+
import { setup } from './utils/setup'
3+
4+
const main = async () => {
5+
const { api, alice, bob } = await setup()
6+
7+
// Alice's Addresses
8+
const aliceAddress = address(alice[0].address)
9+
console.log('\x1b[32m%s\x1b[0m', 'Alice Clean Address:', aliceAddress)
10+
11+
// Bob's Addresses
12+
const bobAddress = address(bob[0].address)
13+
console.log('\x1b[32m%s\x1b[0m', 'Bob Clean Address:', bobAddress, '\n')
14+
15+
// Initial Balances
16+
const initialAliceBalance = await balance(api, aliceAddress)
17+
console.log(
18+
'\x1b[36m%s\x1b[0m',
19+
'Alice Initial Balance:',
20+
initialAliceBalance.free.toString(),
21+
'\x1b[36m',
22+
'ATC',
23+
'\x1b[0m',
24+
)
25+
const initialBobBalance = await balance(api, bobAddress)
26+
console.log(
27+
'\x1b[36m%s\x1b[0m',
28+
'Bob Initial Balance:',
29+
initialBobBalance.free.toString(),
30+
'\x1b[36m',
31+
'ATC',
32+
'\x1b[0m\n',
33+
)
34+
35+
// Transfer 2x10^18 ATC tokens from Alice to Bob
36+
const transferAmount = BigInt(2 * 10 ** 18)
37+
const tx = await transfer(api, bob[0].address, transferAmount)
38+
39+
console.log('\x1b[32m%s\x1b[0m', 'Transaction Prepared! (with hash:', tx.hash.toHex(), ')')
40+
console.log('\x1b[33m%s\x1b[0m', 'Now broadcasting transaction!\n')
41+
42+
let txHashHex: string | undefined = undefined
43+
let blockHash: string | undefined = undefined
44+
await new Promise<void>((resolve, reject) => {
45+
tx.signAndSend(alice[0], ({ events, status, txHash }) => {
46+
if (status.isInBlock) {
47+
txHashHex = txHash.toHex()
48+
blockHash = status.asInBlock.toHex()
49+
console.log('\x1b[32m%s\x1b[0m', 'Successful tx', txHashHex)
50+
console.log('\x1b[32m%s\x1b[0m', 'In block', blockHash)
51+
resolve()
52+
} else if (
53+
status.isRetracted ||
54+
status.isFinalityTimeout ||
55+
status.isDropped ||
56+
status.isInvalid
57+
) {
58+
console.error('Transaction failed')
59+
reject(new Error('Transaction failed'))
60+
}
61+
})
62+
})
63+
64+
// Final Balances
65+
const finalAliceBalance = await balance(api, aliceAddress)
66+
console.log(
67+
'\n\x1b[36m%s\x1b[0m',
68+
'Alice Final Balance:',
69+
finalAliceBalance.free.toString(),
70+
'\x1b[36m',
71+
'ATC',
72+
'\x1b[0m',
73+
)
74+
const finalBobBalance = await balance(api, bobAddress)
75+
console.log(
76+
'\x1b[36m%s\x1b[0m',
77+
'Bob Final Balance:',
78+
finalBobBalance.free.toString(),
79+
'\x1b[36m',
80+
'ATC',
81+
'\x1b[0m\n',
82+
)
83+
}
84+
85+
main()
86+
.then(() => {
87+
console.log('\x1b[34m%s\x1b[0m', 'Script executed successfully')
88+
process.exit(0)
89+
})
90+
.catch((e) => {
91+
console.error('\x1b[31m%s\x1b[0m', 'Error with script:', e)
92+
process.exit(1)
93+
})

examples/node/src/utils/setup.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { ActivateWalletInput, activateWallet, networks } from '@autonomys/auto-utils'
2+
import 'dotenv/config'
3+
4+
export const setup = async () => {
5+
if (!process.env.ALICE_SEED) throw new Error('Missing ALICE_SEED in .env')
6+
if (!process.env.BOB_SEED) throw new Error('Missing BOB_SEED in .env')
7+
8+
const config =
9+
process.env.LOCALHOST !== 'true'
10+
? { networkId: networks[0].id }
11+
: { networkId: 'autonomys-localhost' }
12+
13+
const { api, accounts: alice } = await activateWallet({
14+
...config,
15+
uri: process.env.ALICE_SEED,
16+
} as ActivateWalletInput)
17+
18+
const { accounts: bob } = await activateWallet({
19+
...config,
20+
uri: process.env.BOB_SEED,
21+
} as ActivateWalletInput)
22+
23+
return { api, alice, bob }
24+
}

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
"private": true,
55
"license": "MIT",
66
"workspaces": [
7-
"packages/*"
7+
"packages/*",
8+
"examples/*"
89
],
910
"scripts": {
1011
"build": "yarn workspaces foreach --all run build",

yarn.lock

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ __metadata:
1515
languageName: node
1616
linkType: hard
1717

18-
"@autonomys/auto-consensus@workspace:packages/auto-consensus":
18+
"@autonomys/auto-consensus@workspace:*, @autonomys/auto-consensus@workspace:packages/auto-consensus":
1919
version: 0.0.0-use.local
2020
resolution: "@autonomys/auto-consensus@workspace:packages/auto-consensus"
2121
dependencies:
@@ -2333,6 +2333,13 @@ __metadata:
23332333
languageName: node
23342334
linkType: hard
23352335

2336+
"dotenv@npm:^16.4.5":
2337+
version: 16.4.5
2338+
resolution: "dotenv@npm:16.4.5"
2339+
checksum: 10c0/48d92870076832af0418b13acd6e5a5a3e83bb00df690d9812e94b24aff62b88ade955ac99a05501305b8dc8f1b0ee7638b18493deb6fe93d680e5220936292f
2340+
languageName: node
2341+
linkType: hard
2342+
23362343
"eastasianwidth@npm:^0.2.0":
23372344
version: 0.2.0
23382345
resolution: "eastasianwidth@npm:0.2.0"
@@ -4079,6 +4086,16 @@ __metadata:
40794086
languageName: node
40804087
linkType: hard
40814088

4089+
"node@workspace:examples/node":
4090+
version: 0.0.0-use.local
4091+
resolution: "node@workspace:examples/node"
4092+
dependencies:
4093+
"@autonomys/auto-consensus": "workspace:*"
4094+
"@autonomys/auto-utils": "workspace:*"
4095+
dotenv: "npm:^16.4.5"
4096+
languageName: unknown
4097+
linkType: soft
4098+
40824099
"nopt@npm:^7.0.0":
40834100
version: 7.2.1
40844101
resolution: "nopt@npm:7.2.1"

0 commit comments

Comments
 (0)