Skip to content

Commit

Permalink
docs: Tweaks to examples
Browse files Browse the repository at this point in the history
  • Loading branch information
robdmoore committed Oct 30, 2024
1 parent faf0a50 commit 9b5c8c1
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 79 deletions.
34 changes: 12 additions & 22 deletions examples/accounts.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import * as algokit from '../src/index'
import { SigningAccount } from '../src/types/account'
import { Account } from 'algosdk'
// import { AlgorandClient } from '@algorandfoundation/algokit-utils'
import { AlgorandClient } from '../src'

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

// example: RANDOM_ACCOUNT_CREATE
const alice = algorand.account.random()
Expand All @@ -26,10 +27,10 @@ async function main() {
console.log(`Dispenser account: ${dispenser.addr}\n`)

// example: MULTISIG_CREATE
const signerAccounts: SigningAccount[] = []
signerAccounts.push(algorand.account.random() as unknown as SigningAccount)
signerAccounts.push(algorand.account.random() as unknown as SigningAccount)
signerAccounts.push(algorand.account.random() as unknown as SigningAccount)
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 = {
Expand All @@ -47,7 +48,7 @@ async function main() {
await algorand.send.payment({
sender: dispenser.addr,
receiver: multisigAccount.addr,
amount: algokit.algos(1),
amount: (1).algos(),
})
// TODO: send payment txn with multisigAccount
// example: MULTISIG_SIGN
Expand All @@ -57,30 +58,19 @@ async function main() {
await algorand.send.payment({
sender: dispenser.addr,
receiver: alice.addr,
amount: algokit.algos(1),
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.send.payment({
sender: alice.addr,
receiver: alice.addr,
amount: algokit.algos(0),
rekeyTo: bob.addr,
})
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.send.payment({
sender: alice.addr,
receiver: alice.addr,
amount: algokit.algos(0),
signer: bob,
rekeyTo: alice.addr,
})
await algorand.account.rekeyAccount(alice, alice)

rekeyedAliceInfo = await algorand.account.getInformation(alice.addr)
console.log(`\n authAddr for Alice: ${rekeyedAliceInfo['authAddr']}`)
Expand Down
53 changes: 26 additions & 27 deletions examples/asa.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import * as algokit from '../src/index'
import { indexerWaitForRound } from './utils'
// 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 = algokit.AlgorandClient.defaultLocalNet()
const algorand = AlgorandClient.defaultLocalNet()

// Create a random account and fund it
const creator = await algorand.account.random()
Expand All @@ -14,14 +15,14 @@ async function main() {
/** That account that will receive the ALGO */
receiver: creator.addr,
/** Amount to send */
amount: algokit.algos(1),
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: BigInt(1000),
total: 1000n,
/** The amount of decimal places the asset should have */
decimals: 0,
/** Whether the asset is frozen by default in the creator address */
Expand All @@ -42,24 +43,22 @@ async function main() {
url: 'http://path/to/my/asset/details',
})

const assetId = createResult.confirmation.assetIndex!
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.client.algod.getAssetByID(Number(assetId)).do()
console.log(`Asset Name: ${assetInfo.params.name}\n`)
console.log('Asset Params: ', assetInfo.params, '\n')
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
await indexerWaitForRound(algorand.client.indexer, asset_create_confirmed_round, 30)

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

Expand All @@ -69,7 +68,7 @@ async function main() {
await algorand.send.payment({
sender: dispenser.addr,
receiver: manager.addr,
amount: algokit.algos(1),
amount: (1).algos(),
})

const configResult = await algorand.send.assetConfig({
Expand All @@ -94,16 +93,16 @@ async function main() {
await algorand.send.payment({
sender: dispenser.addr,
receiver: receiver.addr,
amount: algokit.algos(1),
amount: (1).algos(),
})

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

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

Expand All @@ -113,44 +112,44 @@ async function main() {
/** The account to send the asset to */
receiver: receiver.addr,
/** ID of the asset */
assetId: BigInt(assetId),
assetId,
/** Amount of the asset to transfer (smallest divisible unit) */
amount: BigInt(1),
amount: 1n,
})

receiverAssetInfo = await algorand.account.getAssetInformation(receiver.addr, Number(assetId))
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: BigInt(assetId),
assetId,
/** The account to freeze or unfreeze */
account: receiver.addr,
/** Whether the assets in the account should be frozen */
frozen: true,
})

receiverAssetInfo = await algorand.account.getAssetInformation(receiver.addr, Number(assetId))
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: BigInt(assetId),
assetId,
/** Amount of the asset to transfer (smallest divisible unit) */
amount: BigInt(1),
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.account.getAssetInformation(receiver.addr, Number(assetId))
receiverAssetInfo = await algorand.asset.getAccountInformation(receiver.addr, assetId)
console.log(`\nAsset holding after clawback: ${receiverAssetInfo.balance}\n`)
// example: ASSET_CLAWBACK

Expand All @@ -161,9 +160,9 @@ async function main() {
await algorand.send.assetTransfer({
sender: receiver.addr,
/** ID of the asset */
assetId: BigInt(assetId),
assetId,
/** Amount of the asset to transfer (smallest divisible unit) */
amount: BigInt(0),
amount: 0n,
/** The account to send the asset to */
receiver: creator.addr,
/** The account to close the asset to */
Expand All @@ -175,7 +174,7 @@ async function main() {
await algorand.send.assetDestroy({
sender: manager.addr,
/** ID of the asset */
assetId: BigInt(assetId),
assetId,
})
// example: ASSET_DELETE
}
Expand Down
30 changes: 0 additions & 30 deletions examples/utils.ts

This file was deleted.

0 comments on commit 9b5c8c1

Please sign in to comment.