Skip to content

Commit

Permalink
Merge pull request #53 from subspace/34-create-a-cli-to-target-all-fu…
Browse files Browse the repository at this point in the history
…nctionality

Create a cli to target all functionality
  • Loading branch information
marc-aurele-besner authored Jul 7, 2024
2 parents 41524f0 + 0eccf40 commit 00c21c9
Show file tree
Hide file tree
Showing 15 changed files with 148 additions and 36 deletions.
Binary file modified .yarn/install-state.gz
Binary file not shown.
6 changes: 5 additions & 1 deletion examples/node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"url": "https://www.autonomys.net"
},
"scripts": {
"cli": "npx ts-node ./src/cli.ts",
"all": "yarn address && yarn balance && yarn transfer && yarn operators && yarn register-operator && yarn nominate-operator && yarn withdraw-stake && yarn deregister-operator",
"address": "npx ts-node ./src/address.ts",
"balance": "npx ts-node ./src/balance.ts",
Expand All @@ -27,7 +28,10 @@
},
"dependencies": {
"@autonomys/auto-consensus": "workspace:*",
"@autonomys/auto-utils": "workspace:*",
"@autonomys/auto-utils": "workspace:*"
},
"devDependencies": {
"commander": "^12.1.0",
"dotenv": "^16.4.5"
}
}
4 changes: 2 additions & 2 deletions examples/node/src/address.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { address } from '@autonomys/auto-consensus'
import { setup } from './utils/setup'

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

// Alice's Addresses
Expand All @@ -15,7 +15,7 @@ const main = async () => {
console.log('\x1b[32m%s\x1b[0m', 'Bob Clean Address:', bobAddress, '\n')
}

main()
addressFunction()
.then(() => {
console.log('\x1b[34m%s\x1b[0m', 'Script executed successfully')
process.exit(0)
Expand Down
4 changes: 2 additions & 2 deletions examples/node/src/balance.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { address, balance } from '@autonomys/auto-consensus'
import { setup } from './utils/setup'

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

// Alice's Addresses and Balance
Expand Down Expand Up @@ -32,7 +32,7 @@ const main = async () => {
)
}

main()
balanceFunction()
.then(() => {
console.log('\x1b[34m%s\x1b[0m', 'Script executed successfully')
process.exit(0)
Expand Down
110 changes: 110 additions & 0 deletions examples/node/src/cli.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
import { Command } from 'commander'

const program = new Command()
console.log(
'\x1b[34m',
`
t) d) k)
t)tTTT d) k)
a)AAAA u) UU t) o)OOO s)SSSS d)DDDD k) KK
a)AAA u) UU t) o) OO ####### s)SSSS d) DD k)KK
a) A u) UU t) o) OO s) d) DD k) KK
a)AAAA u)UUU t)T o)OOO s)SSSS d)DDDD k) KK
`,
'\x1b[33m',
`
l)L
l)
e)EEEEE x) XX a)AAAA m)MM MMM p)PPPP l) e)EEEEE
e)EEEE x)X a)AAA m) MM MM p) PP l) e)EEEE
e) x)X a) A m) MM MM p) PP l) e)
e)EEEE x) XX a)AAAA m) MM p)PPPP l)LL e)EEEE
p)
p)
`,
'\x1b[0m',
)

program.name('auto-sdk-example-cli').description('CLI for Autonomys functions').version('0.1.0')

program
.command('address')
.description('Run the address function')
.action(async () => {
const { addressFunction } = await import('./address')
addressFunction()
})

program
.command('balance')
.description('Run the balance function')
.action(async () => {
const { balanceFunction } = await import('./balance')
balanceFunction()
})

program
.command('transfer')
.description('Run the transfer function')
.action(async () => {
const { transferFunction } = await import('./transfer')
transferFunction()
})

program
.command('operators')
.description('Run the operators function')
.action(async () => {
const { operatorsFunction } = await import('./operators')
operatorsFunction()
})

program
.command('register-operator')
.description('Run the register operator function')
.action(async () => {
const { registerOperatorFunction } = await import('./register-operator')
registerOperatorFunction()
})

program
.command('nominate-operator')
.description('Run the nominate operator function')
.action(async () => {
const { nominateOperatorFunction } = await import('./nominate-operator')
nominateOperatorFunction()
})

program
.command('withdraw-stake')
.description('Run the withdraw stake function')
.action(async () => {
const { withdrawStakeFunction } = await import('./withdraw-stake')
withdrawStakeFunction()
})

program
.command('deregister-operator')
.description('Run the deregister operator function')
.action(async () => {
const { deregisterOperatorFunction } = await import('./deregister-operator')
deregisterOperatorFunction()
})

program
.command('unlock-funds')
.description('Run the unlock funds function')
.action(async () => {
const { unlockFundsFunction } = await import('./unlock-funds')
unlockFundsFunction()
})

program
.command('unlock-nominator')
.description('Run the unlock nominator function')
.action(async () => {
const { unlockNominatorFunction } = await import('./unlock-nominator')
unlockNominatorFunction()
})

program.parse(process.argv)
5 changes: 3 additions & 2 deletions examples/node/src/deregister-operator.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { deregisterOperator } from '@autonomys/auto-consensus'
import { setup, signAndSend } from './utils'

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

const operatorId = '1'
Expand All @@ -15,8 +15,9 @@ const main = async () => {
console.log('\x1b[33m%s\x1b[0m', 'Now broadcasting transaction!\n')

await signAndSend(alice[0], tx)
}

main()
deregisterOperatorFunction()
.then(() => {
console.log('\x1b[34m%s\x1b[0m', 'Script executed successfully')
process.exit(0)
Expand Down
4 changes: 2 additions & 2 deletions examples/node/src/nominate-operator.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { address, balance, nominateOperator } from '@autonomys/auto-consensus'
import { setup, signAndSend } from './utils'

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

// Alice's Addresses
Expand Down Expand Up @@ -43,7 +43,7 @@ const main = async () => {
)
}

main()
nominateOperatorFunction()
.then(() => {
console.log('\x1b[34m%s\x1b[0m', 'Script executed successfully')
process.exit(0)
Expand Down
4 changes: 2 additions & 2 deletions examples/node/src/operators.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { operators } from '@autonomys/auto-consensus'
import { setup } from './utils/setup'

const main = async () => {
export const operatorsFunction = 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()
operatorsFunction()
.then(() => {
console.log('\x1b[34m%s\x1b[0m', 'Script executed successfully')
process.exit(0)
Expand Down
4 changes: 2 additions & 2 deletions examples/node/src/register-operator.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { address, balance, registerOperator } from '@autonomys/auto-consensus'
import { setup, signAndSend } from './utils'

const main = async () => {
export const registerOperatorFunction = async () => {
const { api, alice, randomUser } = await setup()

// Alice's Addresses
Expand Down Expand Up @@ -46,7 +46,7 @@ const main = async () => {
)
}

main()
registerOperatorFunction()
.then(() => {
console.log('\x1b[34m%s\x1b[0m', 'Script executed successfully')
process.exit(0)
Expand Down
4 changes: 2 additions & 2 deletions examples/node/src/transfer.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { address, balance, transfer } from '@autonomys/auto-consensus'
import { setup, signAndSend } from './utils'

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

// Alice's Addresses
Expand Down Expand Up @@ -62,7 +62,7 @@ const main = async () => {
)
}

main()
transferFunction()
.then(() => {
console.log('\x1b[34m%s\x1b[0m', 'Script executed successfully')
process.exit(0)
Expand Down
4 changes: 2 additions & 2 deletions examples/node/src/unlock-funds.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { unlockFunds } from '@autonomys/auto-consensus'
import { setup, signAndSend } from './utils'

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

const operatorId = '1'
Expand All @@ -17,7 +17,7 @@ const main = async () => {
await signAndSend(alice[0], tx)
}

main()
unlockFundsFunction()
.then(() => {
console.log('\x1b[34m%s\x1b[0m', 'Script executed successfully')
process.exit(0)
Expand Down
4 changes: 2 additions & 2 deletions examples/node/src/unlock-nominator.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { unlockNominator } from '@autonomys/auto-consensus'
import { setup, signAndSend } from './utils'

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

const operatorId = '1'
Expand All @@ -17,7 +17,7 @@ const main = async () => {
await signAndSend(alice[0], tx)
}

main()
unlockNominatorFunction()
.then(() => {
console.log('\x1b[34m%s\x1b[0m', 'Script executed successfully')
process.exit(0)
Expand Down
19 changes: 4 additions & 15 deletions examples/node/src/utils/setup.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,22 @@
import { ActivateWalletInput, activateWallet, networks } from '@autonomys/auto-utils'
import { ActivateWalletInput, activateWallet, mockWallets, networks } from '@autonomys/auto-utils'
import { mnemonicGenerate } from '@polkadot/util-crypto'
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' }

console.log('\x1b[32m%s\x1b[0m', 'Network:', config.networkId, '\n')

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)
const wallets = await mockWallets(config)

const randomMnemonic = mnemonicGenerate()
const { accounts: randomUser } = await activateWallet({
const { api, accounts: randomUser } = await activateWallet({
...config,
uri: randomMnemonic,
} as ActivateWalletInput)

return { api, alice, bob, randomUser }
return { api, alice: wallets[0].accounts, bob: wallets[1].accounts, randomUser }
}
4 changes: 2 additions & 2 deletions examples/node/src/withdraw-stake.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { operator, withdrawStake } from '@autonomys/auto-consensus'
import { setup, signAndSend } from './utils'

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

const operatorId = '1'
Expand All @@ -19,7 +19,7 @@ const main = async () => {
await signAndSend(alice[0], tx)
}

main()
withdrawStakeFunction()
.then(() => {
console.log('\x1b[34m%s\x1b[0m', 'Script executed successfully')
process.exit(0)
Expand Down
8 changes: 8 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3741,6 +3741,13 @@ __metadata:
languageName: node
linkType: hard

"commander@npm:^12.1.0":
version: 12.1.0
resolution: "commander@npm:12.1.0"
checksum: 10c0/6e1996680c083b3b897bfc1cfe1c58dfbcd9842fd43e1aaf8a795fbc237f65efcc860a3ef457b318e73f29a4f4a28f6403c3d653d021d960e4632dd45bde54a9
languageName: node
linkType: hard

"commander@npm:^4.0.0":
version: 4.1.1
resolution: "commander@npm:4.1.1"
Expand Down Expand Up @@ -7916,6 +7923,7 @@ __metadata:
dependencies:
"@autonomys/auto-consensus": "workspace:*"
"@autonomys/auto-utils": "workspace:*"
commander: "npm:^12.1.0"
dotenv: "npm:^16.4.5"
languageName: unknown
linkType: soft
Expand Down

0 comments on commit 00c21c9

Please sign in to comment.