@travelx/algob-ext v0.0.9
Algo Builder extension & helpers
[TOC]
π Description
All stuff which help us to develop and test Algorand Smart Contract
π¬ Resources
AlgoBuilder
(official site)- projects:
π Docs
π Ext
Main directory to customize scripts
β― AlgoBuilderScript
Basic AlgoBuilder script wrapper with some built-in extension to use out-of-box.
# scripts/my-script.ts
export default AlgoBuilderScript.of((ctx) => {
// Do stuff with the context
});
Context IRunScriptContext
runtimeEnv: RuntimeEnvExt
: Extended environmentRuntimeEnv
(from algo-builder) with properties on NetworkConfig.runtimeEnv.network.config.assets
: Config custom asset ids for external networks like TestNetruntimeEnv.network.config.addresses
: Config addresses without private key to use
deployerExt: IDeployerExt
: The built-in extension methods from base deployer of AlgoBuilderaddresses: IScriptAddresses
: Service to read and resolve addresses from config file.ctx.addresses.getOrFail('requiredAddress')
Deployer Ext IDeployerExt
algoBalanceOf
: Return balance in algos.usdcBalanceOf
: Return balance of common stable asset USDC (must be deployed before. See examples/scripts/usdc.ts)usdcDispenser
: Dispense $USDC to account.master
is the senderalgoDispenser
: Dispense $ALGO to account.master
is the senderfunder
: Return a callable dispense which send the amount of usdc & algo to reach thealgoLt
&usdcLt
configure amount.send(algoLt - account.algos)
accountSummary
: Print a summary of account in stdoutnftsOf
: Return a list of non-fungible asset which account hold.resolveAssetIndex
: Resolve the asset index from their name or fallback to artifact folder of algobuilder.
βοΈ β― AlgoBuilderScriptFactory
This is the main extensible feature to customize, configure and extend custom scripts contexts with custom plugins features.
// # scripts/ext/my-custom-script-context.ts
const MyAlgoBuilderScript = AlgoBuilderScriptFactory()
.withPlugin((ctx) => {
return {
...ctx, // It's the base context IRunScriptContext
myCoolStuff: (account: Account) => `Hello ${account.address}`
}
})
// Also can add more plugins .withPlugin(otherPlugin) with context inherit
// # scripts/use.ts
export default MyAlgoBuilderScript.of((ctx) => {
// ctx.myCoolStuff is available. And it have AUTOCOMPLETE!! π
ctx.myCoolStuff(ctx.deployer.accountsByName.get('Alice'))
// Hello AKCX..YQJETM
})
π Accouts
β― accountsBalanceReference
Show changes on the balances of accounts
const ref = await accountsBalanceReference(ctx.deployerExt).of([alice])
// transfer 1000 $ALGO to alice
ref.printDiff() // Yes!! it have colors π€ͺ
βββββββββ¬βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ¬ββββββββββββββββββ¬ββββββββββββββββββββββββββ¬βββββββββ¬ββββββββββββ
β Name β address β $Β΅ALGO β $Β΅ALGO diff β $USD β $USD diff β
βββββββββΌβββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββΌββββββββββββββββββΌββββββββββββββββββββββββββΌβββββββββΌββββββββββββ€
β Alice β FYPJZLHO67DNODLNNTUG7KQBE2ZTBOQJIXSBGSJB5DYFPARDKOZAEDCRYI β 0 -> 1000000000 β β 1000000000 β’ $409.131 β 0 -> 0 β 0 β
βββββββββ΄βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ΄ββββββββββββββββββ΄ββββββββββββββββββββββββββ΄βββββββββ΄ββββββββββββ
π Applications
β― AppState
Show changes on the state (global & local) of application (smart contracts)
const state = new AppState(ctx, 'Factory')
await state.takeSnapshot()
// do something interesting with the contract
await state.printDiff()
The following will be printed on the console:
ββββββββββββββββ¬βββββββββββββββββββββββββββββββββββββββββββββββ¬βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Key β Old Value β New Value β
ββββββββββββββββΌβββββββββββββββββββββββββββββββββββββββββββββββΌβββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β FEE_ADDR β 5rjfIlCKqXUaKhpXWP3rfLua3Nvb3OFHUYWDqk+qfxw= β 5rjfIlCKqXUaKhpXWP3rfLua3Nvb3OFHUYWDqk+qfxw= β
ββββββββββββββββΌβββββββββββββββββββββββββββββββββββββββββββββββΌβββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β MANAGER_ADDR β 5rjfIlCKqXUaKhpXWP3rfLua3Nvb3OFHUYWDqk+qfxw= β 5rjfIlCKqXUaKhpXWP3rfLua3Nvb3OFHUYWDqk+qfxw= β
ββββββββββββββββΌβββββββββββββββββββββββββββββββββββββββββββββββΌβββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β POFIP_HASH β null β hnzzWDKj8vXxjuf2+ysPFugHLyHbF4lNMTbUPRjbpQOGfPNYMqPy9fGO5/b7Kw8W6AcvIdsXiU0xNtQ9GNulAw== β
ββββββββββββββββ΄βββββββββββββββββββββββββββββββββββββββββββββββ΄βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
tip: you can call takeSnapshot()
again on the same object to update snapshot.
π Constants
β― FLAT_PAY
Common object to pass on ctx.deployer.executeTx()
β― flatPayWithMore(amountOfTransactions)
A flat pay to use on application call which do more than N
inner transaction
π Encoder
β― strToUInt8Array
Convert from string
to Uint8Array
β― uInt8ArrayToStr
Convert from Uint8Array
to string
β― b64ToString
Convert from encoded b64 string to string. Example b64ToString('SEVMTE8=')
-> HELLO
(playground)
π Model
Interfaces to use and type responses of AlgoClient.
π Terminal
β― forPressEnter
Just a sync method which wait to press enter on the stdin. It block the execution script.
TODO
- Extend asset list for customization (see
AssetName
) - Complete doc with more example and refs
- Split imports
import { AlgoBuilderScriptFactory } from '@travelx/algob-ext/ext/factory'
import { Encoder } from '@travelx/algob-ext/encoder'
. ThenEncoder.strToUInt8Array(..)