Skip to content

Commit

Permalink
basic code examples
Browse files Browse the repository at this point in the history
  • Loading branch information
mnaamani committed Feb 5, 2024
1 parent 03a59f7 commit 6f2a6d3
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
28 changes: 28 additions & 0 deletions utils/api-scripts/src/create-account.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// @ts-check
import { cryptoWaitReady, mnemonicGenerate } from '@polkadot/util-crypto'
import { Keyring } from '@polkadot/keyring'

async function main() {
await cryptoWaitReady()

// https://polkadot.js.org/docs/keyring/start/create
const keyring = new Keyring({
type: 'sr25519',
ss58Format: 126,
})

// Generate a new mnemonic
const mnemonic = mnemonicGenerate()

const keyringPair = keyring.addFromMnemonic(
mnemonic,
{
name: 'User 1',
},
'sr25519'
)

console.log(keyringPair.address)
}

main().catch(console.error)
38 changes: 38 additions & 0 deletions utils/api-scripts/src/create-signed-transfer-tx.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { ApiPromise, WsProvider } from '@polkadot/api'
import { cryptoWaitReady } from '@polkadot/util-crypto'
import { Keyring } from '@polkadot/keyring'
import { BN } from '@polkadot/util'

async function main() {
await cryptoWaitReady()

// Initialise the provider to connect to the local node
const provider = new WsProvider('wss://rpc.joystream.org')

// Create the API and wait until ready
const api = await ApiPromise.create({ provider })

const keyring = new Keyring({ type: 'sr25519', ss58Format: 126 })

const keyringPair = keyring.addFromMnemonic('your mnemonic phrase', {}, 'sr25519')

// Create a transfer transaction
const OneJoy = new BN(10000000000)
const tx = api.tx.balances.transfer('j4W34M9gBApzi8Sj9nXSSHJmv3UHnGKecyTFjLGnmxtJDi98L', OneJoy)

// Get the next account nonce
const nonce = await api.rpc.system.accountNextIndex(keyringPair.address)

// We do not need the provider anymore, close it.
await api.disconnect()

// Sign the transaction
const signedTx = tx.sign(keyringPair, { nonce })

// Paste the hex here to decode it.
// https://polkadot.js.org/apps/?rpc=wss%3A%2F%2Frpc.joystream.org#/extrinsics/decode
// hex is the signed transaction which can be submitted to the network
console.log(signedTx.toHex())
}

main().catch(console.error)

0 comments on commit 6f2a6d3

Please sign in to comment.