@travelx/algob-ext
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.masteris the senderalgoDispenser: Dispense $ALGO to account.masteris the senderfunder: Return a callable dispense which send the amount of usdc & algo to reach thealgoLt&usdcLtconfigure 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(..)
