-
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 #36 from subspace/32-create-reusable-scripts-to-qu…
…ery-balance-and-transfer-tokens Create reusable scripts to query balance and transfer tokens
- Loading branch information
Showing
10 changed files
with
271 additions
and
2 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
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,2 @@ | ||
ALICE_SEED="//Alice" | ||
BOB_SEED="//Bob" |
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,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 | ||
``` |
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,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" | ||
} | ||
} |
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,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) | ||
}) |
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,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) | ||
}) |
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,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) | ||
}) |
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,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 } | ||
} |
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