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

add utils ts code examples #338

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
79 changes: 79 additions & 0 deletions examples/accounts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import { Account } from 'algosdk'
// import { AlgorandClient } from '@algorandfoundation/algokit-utils'
import { AlgorandClient } from '../src'

/* eslint-disable no-console */
async function main() {
const algorand = AlgorandClient.defaultLocalNet()

// example: RANDOM_ACCOUNT_CREATE
const alice = algorand.account.random()
console.log(`Alice account: ${alice.addr}\n`)
const bob = algorand.account.random()
console.log(`Bob account: ${bob.addr}\n`)
// example: RANDOM_ACCOUNT_CREATE

// example: ACCOUNT_RECOVER_MNEMONIC
// restore 25-word mnemonic from a string
// Note the mnemonic should _never_ appear in your source code
const mnemonic =
'creek phrase island true then hope employ veteran rapid hurdle above liberty tissue connect alcohol timber idle ten frog bulb embody crunch taxi abstract month'
const recoveredAccount = await algorand.account.fromMnemonic(mnemonic)
console.log(`Recovered mnemonic account: ${recoveredAccount.addr}\n`)
// example: ACCOUNT_RECOVER_MNEMONIC

// example: DISPENSER_ACCOUNT_CREATE
const dispenser = await algorand.account.localNetDispenser()
console.log(`Dispenser account: ${dispenser.addr}\n`)

// example: MULTISIG_CREATE
const signerAccounts: Account[] = []
signerAccounts.push(algorand.account.random().account)
signerAccounts.push(algorand.account.random().account)
signerAccounts.push(algorand.account.random().account)

// multiSigParams is used when creating the address and when signing transactions
const multiSigParams = {
version: 1,
threshold: 2,
addrs: signerAccounts.map((a) => a.addr),
}
algorand.account.getAccount
const multisigAccount = await algorand.account.multisig(multiSigParams, signerAccounts)
console.log(`Multisig account: ${multisigAccount.addr}\n`)
// example: MULTISIG_CREATE

// example: MULTISIG_SIGN
// fund the multisig account
await algorand.send.payment({
sender: dispenser.addr,
receiver: multisigAccount.addr,
amount: (1).algos(),
})
// TODO: send payment txn with multisigAccount
// example: MULTISIG_SIGN

// example: ACCOUNT_REKEY
// fund Alice's account from the dispenser
await algorand.send.payment({
sender: dispenser.addr,
receiver: alice.addr,
amount: (1).algos(),
})

// rekey the original account to the new signer via a payment transaction
// Note any transaction type can be used to rekey an account
await algorand.account.rekeyAccount(alice, bob)

let rekeyedAliceInfo = await algorand.account.getInformation(alice.addr)
console.log(`\n Alice signer rekeyed to: ${rekeyedAliceInfo['authAddr']}\n`)
// example: ACCOUNT_REKEY

// rekey back to the original account
await algorand.account.rekeyAccount(alice, alice)

rekeyedAliceInfo = await algorand.account.getInformation(alice.addr)
console.log(`\n authAddr for Alice: ${rekeyedAliceInfo['authAddr']}`)
}

main()
182 changes: 182 additions & 0 deletions examples/asa.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
// import { AlgorandClient } from '@algorandfoundation/algokit-utils'
import { AlgorandClient } from '../src'
import { runWhenIndexerCaughtUp } from '../src/testing/indexer'

/* eslint-disable no-console */
async function main() {
const algorand = AlgorandClient.defaultLocalNet()

// Create a random account and fund it
const creator = await algorand.account.random()
const dispenser = await algorand.account.localNetDispenser()

await algorand.send.payment({
sender: dispenser.addr,
/** That account that will receive the ALGO */
receiver: creator.addr,
/** Amount to send */
amount: (1).algos(),
})

// example: ASSET_CREATE
const createResult = await algorand.send.assetCreate({
sender: creator.addr,
/** The total amount of the smallest divisible unit to create */
total: 1000n,
/** The amount of decimal places the asset should have */
decimals: 0,
/** Whether the asset is frozen by default in the creator address */
defaultFrozen: false,
/** The address that can change the manager, reserve, clawback, and freeze addresses. There will permanently be no manager if undefined or an empty string */
manager: creator.addr,
/** The address that holds the uncirculated supply */
reserve: creator.addr,
/** The address that can freeze the asset in any account. Freezing will be permanently disabled if undefined or an empty string. */
freeze: creator.addr,
/** The address that can clawback the asset from any account. Clawback will be permanently disabled if undefined or an empty string. */
clawback: creator.addr,
/** The short ticker name for the asset */
unitName: 'ora',
/** The full name of the asset */
assetName: 'Orange',
/** The metadata URL for the asset */
url: 'http://path/to/my/asset/details',
})

const assetId = createResult.assetId
console.log(`Asset ID created: ${assetId}\n`)

const asset_create_confirmed_round = createResult.confirmation.confirmedRound!
console.log(`Asset creation confirmed round: ${asset_create_confirmed_round}\n`)
// example: ASSET_CREATE

// example: ASSET_INFO
const assetInfo = await algorand.asset.getById(assetId)
console.log(`Asset Name: ${assetInfo.assetName}\n`)
console.log('Asset Params: ', assetInfo, '\n')
// example: ASSET_INFO

// example: INDEXER_LOOKUP_ASSET
// ensure indexer is caught up
const indexerAssetInfo = await runWhenIndexerCaughtUp(() => algorand.client.indexer.lookupAssetByID(Number(assetId)).do())
console.log('Indexer Asset Info: ', indexerAssetInfo, '\n')
// example: INDEXER_LOOKUP_ASSET

// example: ASSET_CONFIG
const manager = await algorand.account.random()

await algorand.send.payment({
sender: dispenser.addr,
receiver: manager.addr,
amount: (1).algos(),
})

const configResult = await algorand.send.assetConfig({
sender: creator.addr,
/** ID of the asset */
assetId: BigInt(assetId),
/** The address that can change the manager, reserve, clawback, and freeze addresses. There will permanently be no manager if undefined or an empty string */
manager: manager.addr,
/** The address that holds the uncirculated supply */
reserve: manager.addr,
/** The address that can freeze the asset in any account. Freezing will be permanently disabled if undefined or an empty string. */
freeze: manager.addr,
/** The address that can clawback the asset from any account. Clawback will be permanently disabled if undefined or an empty string. */
clawback: manager.addr,
})
console.log(`\nAsset Config Txn ID: ${configResult.txIds}\n`)
// example: ASSET_CONFIG

// example: ASSET_OPTIN
const receiver = await algorand.account.random()

await algorand.send.payment({
sender: dispenser.addr,
receiver: receiver.addr,
amount: (1).algos(),
})

await algorand.send.assetOptIn({
sender: receiver.addr,
/** ID of the asset */
assetId,
})

let receiverAssetInfo = await algorand.asset.getAccountInformation(receiver.addr, assetId)
console.log(`\nAsset holding before asset_xfer: ${receiverAssetInfo.balance}\n`)
// example: ASSET_OPTIN

// example: ASSET_XFER
await algorand.send.assetTransfer({
sender: creator.addr,
/** The account to send the asset to */
receiver: receiver.addr,
/** ID of the asset */
assetId,
/** Amount of the asset to transfer (smallest divisible unit) */
amount: 1n,
})

receiverAssetInfo = await algorand.asset.getAccountInformation(receiver.addr, assetId)
console.log(`\nAsset holding after asset_xfer: ${receiverAssetInfo.balance}\n`)
// example: ASSET_XFER

// example: ASSET_FREEZE
await algorand.send.assetFreeze({
sender: manager.addr,
/** The ID of the asset */
assetId,
/** The account to freeze or unfreeze */
account: receiver.addr,
/** Whether the assets in the account should be frozen */
frozen: true,
})

receiverAssetInfo = await algorand.asset.getAccountInformation(receiver.addr, assetId)
console.log(`\nAsset frozen in ${receiver.addr}?: ${receiverAssetInfo.frozen}\n`)
// example: ASSET_FREEZE

// example: ASSET_CLAWBACK
await algorand.send.assetTransfer({
sender: manager.addr,
/** ID of the asset */
assetId,
/** Amount of the asset to transfer (smallest divisible unit) */
amount: 1n,
/** The account to send the asset to */
receiver: creator.addr,
/** The account to take the asset from */
clawbackTarget: receiver.addr,
})

receiverAssetInfo = await algorand.asset.getAccountInformation(receiver.addr, assetId)
console.log(`\nAsset holding after clawback: ${receiverAssetInfo.balance}\n`)
// example: ASSET_CLAWBACK

// example: ASSET_OPT_OUT
// opt-out is an zero amount transfer with the `closeRemainderTo` field set to
// any account that can receive the asset.
// note that closing to the asset creator will always succeed
await algorand.send.assetTransfer({
sender: receiver.addr,
/** ID of the asset */
assetId,
/** Amount of the asset to transfer (smallest divisible unit) */
amount: 0n,
/** The account to send the asset to */
receiver: creator.addr,
/** The account to close the asset to */
closeAssetTo: creator.addr,
})
// example: ASSET_OPT_OUT

// example: ASSET_DELETE
await algorand.send.assetDestroy({
sender: manager.addr,
/** ID of the asset */
assetId,
})
// example: ASSET_DELETE
}

main()
Loading
Loading