-
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 #39 from subspace/33-create-reusable-scripts-to-co…
…ver-all-staking-functions Create reusable example scripts to cover all staking functions
- Loading branch information
Showing
16 changed files
with
341 additions
and
25 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 |
---|---|---|
@@ -1,2 +1,4 @@ | ||
ALICE_SEED="//Alice" | ||
BOB_SEED="//Bob" | ||
|
||
LOCALHOST="true" |
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
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,27 @@ | ||
import { deregisterOperator } from '@autonomys/auto-consensus' | ||
import { setup, signAndSend } from './utils' | ||
|
||
const main = async () => { | ||
const { api, alice } = await setup() | ||
|
||
const operatorId = '1' | ||
|
||
const tx = await deregisterOperator({ | ||
api, | ||
operatorId, | ||
}) | ||
|
||
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') | ||
|
||
await signAndSend(alice[0], tx) | ||
|
||
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,54 @@ | ||
import { address, balance, nominateOperator } from '@autonomys/auto-consensus' | ||
import { setup, signAndSend } from './utils' | ||
|
||
const main = async () => { | ||
const { api, alice } = await setup() | ||
|
||
// Alice's Addresses | ||
const aliceAddress = address(alice[0].address) | ||
|
||
// 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', | ||
) | ||
|
||
// Transfer 2x10^18 ATC tokens from Alice to Bob | ||
const amountToStake = BigInt(10 * 10 ** 18) | ||
const tx = await nominateOperator({ | ||
api, | ||
operatorId: '0', | ||
amountToStake, | ||
}) | ||
|
||
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') | ||
|
||
await signAndSend(alice[0], tx) | ||
|
||
// 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', | ||
) | ||
} | ||
|
||
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,19 @@ | ||
import { operator } from '@autonomys/auto-consensus' | ||
import { setup } from './utils/setup' | ||
|
||
const main = async () => { | ||
const { api } = await setup() | ||
|
||
const operatorOne = await operator(api, 1) | ||
console.log('\x1b[36m%s\x1b[0m', 'operatorOne:', operatorOne, '\x1b[0m') | ||
} | ||
|
||
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,20 @@ | ||
import { operators } from '@autonomys/auto-consensus' | ||
import { setup } from './utils/setup' | ||
|
||
const main = async () => { | ||
const { api } = await setup() | ||
|
||
// Query all operators | ||
const allOperators = await operators(api) | ||
console.log('\x1b[36m%s\x1b[0m', 'allOperators:', allOperators, '\x1b[0m') | ||
} | ||
|
||
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,57 @@ | ||
import { address, balance, registerOperator } from '@autonomys/auto-consensus' | ||
import { setup, signAndSend } from './utils' | ||
|
||
const main = async () => { | ||
const { api, alice, randomUser } = await setup() | ||
|
||
// Alice's Addresses | ||
const aliceAddress = address(alice[0].address) | ||
|
||
// 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', | ||
) | ||
// Transfer 2x10^18 ATC tokens from Alice to Bob | ||
const amountToStake = BigInt(100 * 10 ** 18) | ||
const tx = await registerOperator({ | ||
api, | ||
senderAddress: alice[0].address, | ||
Operator: randomUser[0], | ||
domainId: '0', | ||
amountToStake, | ||
minimumNominatorStake: BigInt(10 * 10 ** 18), | ||
nominationTax: '5', | ||
}) | ||
|
||
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') | ||
|
||
await signAndSend(alice[0], tx) | ||
|
||
// 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', | ||
) | ||
} | ||
|
||
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
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,28 @@ | ||
import { unlockFunds } from '@autonomys/auto-consensus' | ||
import { setup, signAndSend } from './utils' | ||
|
||
const main = async () => { | ||
const { api, alice } = await setup() | ||
|
||
const operatorId = '1' | ||
|
||
const tx = await unlockFunds({ | ||
api, | ||
operatorId, | ||
}) | ||
|
||
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') | ||
|
||
await signAndSend(alice[0], tx) | ||
} | ||
|
||
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,28 @@ | ||
import { unlockNominator } from '@autonomys/auto-consensus' | ||
import { setup, signAndSend } from './utils' | ||
|
||
const main = async () => { | ||
const { api, alice } = await setup() | ||
|
||
const operatorId = '1' | ||
|
||
const tx = await unlockNominator({ | ||
api, | ||
operatorId, | ||
}) | ||
|
||
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') | ||
|
||
await signAndSend(alice[0], tx) | ||
} | ||
|
||
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,2 @@ | ||
export * from './setup' | ||
export * from './signAndSend' |
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,45 @@ | ||
import type { AddressOrPair, SubmittableExtrinsic } from '@polkadot/api/types' | ||
import type { ISubmittableResult } from '@polkadot/types/types' | ||
import 'dotenv/config' | ||
|
||
export const signAndSend = async ( | ||
sender: AddressOrPair, | ||
tx: SubmittableExtrinsic<'promise', ISubmittableResult>, | ||
) => { | ||
let txHashHex: string | undefined = undefined | ||
let blockHash: string | undefined = undefined | ||
let success = false | ||
|
||
await new Promise<void>((resolve, reject) => { | ||
tx.signAndSend(sender, ({ 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, '\n') | ||
|
||
events.forEach(({ event: { data, method, section } }) => { | ||
if (section === 'system' && method === 'ExtrinsicSuccess') success = true | ||
console.log( | ||
'Event Emitted:', | ||
'\x1b[33m', | ||
`${section}.${method}`, | ||
'\x1b[0m', | ||
data.toString(), | ||
) | ||
}) | ||
resolve() | ||
} else if ( | ||
status.isRetracted || | ||
status.isFinalityTimeout || | ||
status.isDropped || | ||
status.isInvalid | ||
) { | ||
console.error('Transaction failed') | ||
reject(new Error('Transaction failed')) | ||
} | ||
}) | ||
}) | ||
|
||
return { txHashHex, blockHash, success } | ||
} |
Oops, something went wrong.