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

Create reusable scripts to query balance and transfer tokens #36

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .vscode/auto.code-workspace
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
"name": "auto-id",
"path": "../packages/auto-id",
},
{
"name": "node-examples",
"path": "../examples/node",
},
],
"settings": {
"editor.codeActionsOnSave": {
Expand Down
2 changes: 2 additions & 0 deletions examples/node/.env.local
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ALICE_SEED="//Alice"
BOB_SEED="//Bob"
33 changes: 33 additions & 0 deletions examples/node/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Autonomys SDK - Node Example

Simple list of scripts to query data and execute extrinsics:

## Install

```bash
yarn
```

## Run queries

```bash
yarn balance
```

## Execute extrinsics

```bash
yarn transfer
```

## Utility

```bash
yarn address
```

## Run All

```bash
yarn all
```
26 changes: 26 additions & 0 deletions examples/node/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"name": "node",
"version": "0.1.0",
"private": true,
"license": "MIT",
"packageManager": "[email protected]",
"repository": {
"type": "git",
"url": "https://github.com/subspace/auto-sdk"
},
"author": {
"name": "Autonomys",
"url": "https://www.autonomys.net"
},
"scripts": {
"all": "yarn address && yarn balance && yarn transfer",
"address": "npx ts-node ./src/address.ts",
"balance": "npx ts-node ./src/balance.ts",
"transfer": "npx ts-node ./src/transfer.ts"
},
"dependencies": {
"@autonomys/auto-consensus": "workspace:*",
"@autonomys/auto-utils": "workspace:*",
"dotenv": "^16.4.5"
}
}
26 changes: 26 additions & 0 deletions examples/node/src/address.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { address } from '@autonomys/auto-consensus'
import { setup } from './utils/setup'

const main = async () => {
const { alice, bob } = await setup()

// Alice's Addresses
console.log('\x1b[33m%s\x1b[0m', 'Alice Raw Address:', alice[0].address)
const aliceAddress = address(alice[0].address)
console.log('\x1b[32m%s\x1b[0m', 'Alice Clean Address:', aliceAddress, '\n')

// Bob's Addresses
console.log('\x1b[33m%s\x1b[0m', 'Bob Raw Address:', bob[0].address)
const bobAddress = address(bob[0].address)
console.log('\x1b[32m%s\x1b[0m', 'Bob Clean Address:', bobAddress, '\n')
}

main()
.then(() => {
console.log('\x1b[34m%s\x1b[0m', 'Script executed successfully')
process.exit(0)
})
.catch((e) => {
console.error('\x1b[31m%s\x1b[0m', 'Error with script:', e)
process.exit(1)
})
43 changes: 43 additions & 0 deletions examples/node/src/balance.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { address, balance } from '@autonomys/auto-consensus'
import { setup } from './utils/setup'

const main = async () => {
const { api, alice, bob } = await setup()

// Alice's Addresses and Balance
const aliceAddress = address(alice[0].address)
console.log('\x1b[32m%s\x1b[0m', 'Alice Clean Address:', aliceAddress)
const aliceBalance = await balance(api, aliceAddress)
console.log(
'\x1b[36m%s\x1b[0m',
'Alice Free Balance:',
aliceBalance.free.toString(),
'\x1b[36m',
'ATC',
'\x1b[0m\n',
)

// Bob's Addresses and Balance
const bobAddress = address(bob[0].address)
console.log('\x1b[32m%s\x1b[0m', 'Bob Clean Address:', bobAddress)
const bobBalance = await balance(api, bobAddress)
console.log(
'\x1b[36m%s\x1b[0m',
'Bob Free Balance:',
bobBalance.free.toString(),
'\x1b[36m',
'ATC',
'\x1b[0m\n',
'\n',
)
}

main()
.then(() => {
console.log('\x1b[34m%s\x1b[0m', 'Script executed successfully')
process.exit(0)
})
.catch((e) => {
console.error('\x1b[31m%s\x1b[0m', 'Error with script:', e)
process.exit(1)
})
93 changes: 93 additions & 0 deletions examples/node/src/transfer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import { address, balance, transfer } from '@autonomys/auto-consensus'
import { setup } from './utils/setup'

const main = async () => {
const { api, alice, bob } = await setup()

// Alice's Addresses
const aliceAddress = address(alice[0].address)
console.log('\x1b[32m%s\x1b[0m', 'Alice Clean Address:', aliceAddress)

// Bob's Addresses
const bobAddress = address(bob[0].address)
console.log('\x1b[32m%s\x1b[0m', 'Bob Clean Address:', bobAddress, '\n')

// Initial Balances
const initialAliceBalance = await balance(api, aliceAddress)
console.log(
'\x1b[36m%s\x1b[0m',
'Alice Initial Balance:',
initialAliceBalance.free.toString(),
'\x1b[36m',
'ATC',
'\x1b[0m',
)
const initialBobBalance = await balance(api, bobAddress)
console.log(
'\x1b[36m%s\x1b[0m',
'Bob Initial Balance:',
initialBobBalance.free.toString(),
'\x1b[36m',
'ATC',
'\x1b[0m\n',
)

// Transfer 2x10^18 ATC tokens from Alice to Bob
const transferAmount = BigInt(2 * 10 ** 18)
const tx = await transfer(api, bob[0].address, transferAmount)

console.log('\x1b[32m%s\x1b[0m', 'Transaction Prepared! (with hash:', tx.hash.toHex(), ')')
console.log('\x1b[33m%s\x1b[0m', 'Now broadcasting transaction!\n')

let txHashHex: string | undefined = undefined
let blockHash: string | undefined = undefined
await new Promise<void>((resolve, reject) => {
tx.signAndSend(alice[0], ({ events, status, txHash }) => {
if (status.isInBlock) {
txHashHex = txHash.toHex()
blockHash = status.asInBlock.toHex()
console.log('\x1b[32m%s\x1b[0m', 'Successful tx', txHashHex)
console.log('\x1b[32m%s\x1b[0m', 'In block', blockHash)
resolve()
} else if (
status.isRetracted ||
status.isFinalityTimeout ||
status.isDropped ||
status.isInvalid
) {
console.error('Transaction failed')
reject(new Error('Transaction failed'))
}
})
})

// Final Balances
const finalAliceBalance = await balance(api, aliceAddress)
console.log(
'\n\x1b[36m%s\x1b[0m',
'Alice Final Balance:',
finalAliceBalance.free.toString(),
'\x1b[36m',
'ATC',
'\x1b[0m',
)
const finalBobBalance = await balance(api, bobAddress)
console.log(
'\x1b[36m%s\x1b[0m',
'Bob Final Balance:',
finalBobBalance.free.toString(),
'\x1b[36m',
'ATC',
'\x1b[0m\n',
)
}

main()
.then(() => {
console.log('\x1b[34m%s\x1b[0m', 'Script executed successfully')
process.exit(0)
})
.catch((e) => {
console.error('\x1b[31m%s\x1b[0m', 'Error with script:', e)
process.exit(1)
})
24 changes: 24 additions & 0 deletions examples/node/src/utils/setup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { ActivateWalletInput, activateWallet, networks } from '@autonomys/auto-utils'
import 'dotenv/config'

export const setup = async () => {
if (!process.env.ALICE_SEED) throw new Error('Missing ALICE_SEED in .env')
if (!process.env.BOB_SEED) throw new Error('Missing BOB_SEED in .env')

const config =
process.env.LOCALHOST !== 'true'
? { networkId: networks[0].id }
: { networkId: 'autonomys-localhost' }

const { api, accounts: alice } = await activateWallet({
...config,
uri: process.env.ALICE_SEED,
} as ActivateWalletInput)

const { accounts: bob } = await activateWallet({
...config,
uri: process.env.BOB_SEED,
} as ActivateWalletInput)

return { api, alice, bob }
}
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"private": true,
"license": "MIT",
"workspaces": [
"packages/*"
"packages/*",
"examples/*"
],
"scripts": {
"build": "yarn workspaces foreach --all run build",
Expand Down
19 changes: 18 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ __metadata:
languageName: node
linkType: hard

"@autonomys/auto-consensus@workspace:packages/auto-consensus":
"@autonomys/auto-consensus@workspace:*, @autonomys/auto-consensus@workspace:packages/auto-consensus":
version: 0.0.0-use.local
resolution: "@autonomys/auto-consensus@workspace:packages/auto-consensus"
dependencies:
Expand Down Expand Up @@ -2332,6 +2332,13 @@ __metadata:
languageName: node
linkType: hard

"dotenv@npm:^16.4.5":
version: 16.4.5
resolution: "dotenv@npm:16.4.5"
checksum: 10c0/48d92870076832af0418b13acd6e5a5a3e83bb00df690d9812e94b24aff62b88ade955ac99a05501305b8dc8f1b0ee7638b18493deb6fe93d680e5220936292f
languageName: node
linkType: hard

"eastasianwidth@npm:^0.2.0":
version: 0.2.0
resolution: "eastasianwidth@npm:0.2.0"
Expand Down Expand Up @@ -4078,6 +4085,16 @@ __metadata:
languageName: node
linkType: hard

"node@workspace:examples/node":
version: 0.0.0-use.local
resolution: "node@workspace:examples/node"
dependencies:
"@autonomys/auto-consensus": "workspace:*"
"@autonomys/auto-utils": "workspace:*"
dotenv: "npm:^16.4.5"
languageName: unknown
linkType: soft

"nopt@npm:^7.0.0":
version: 7.2.1
resolution: "nopt@npm:7.2.1"
Expand Down
Loading