SmartWeave SDK
June 29, 2021 ยท View on GitHub
The SmartWeave SDK can be installed from Arweave:
npm install smartweave
You can import the full API or individual methods.
import * as SmartWeaveSdk from 'smartweave'
import { readContract, interactWrite, createContract } from 'smartweave'
SDK Methods
createContract
async function createContract(arweave: Arweave, wallet: JWKInterface, contractSrc: string, initState: string, minFee?: number): Promise<string>
Create a new contract from a contract source file and an initial state. Returns the contract id.
arweavean Arweave client instancewalleta wallet private or public keycontractSrcthe contract source code as string.initStatethe contract initial state, as a JSON string.
createContractFromTx
async function createContractFromTx(arweave: Arweave, wallet: JWKInterface, srcTxId: string, state: string, minFee?: number): Promise<string>
Create a new contract from an existing contract source tx, with an initial state. Returns the contract id.
arweavean Arweave client instancewalleta wallet private or public keysrcTxIdthe contract source Tx id.statethe initial state, as a JSON string.
interactWrite
async function interactWrite(arweave: Arweave, wallet: JWKInterface, contractId: string, input: any, tags: { name: string; value: string }[] = [], target: string = '', winstonQty: string = ''): Promise<string>
Writes an interaction on the blockchain. This creates an interaction tx and posts it, it does not need to know the current state of the contract.
arweavean Arweave client instancewalleta wallet private keycontractIdthe Transaction Id of the contractinputthe interaction input, will be serialized as Json.tagsan array of tags with name/value as objectstargetif needed to send AR to an address, this is the targetwinstonQtyamount of winston to send to the target, if needed
interactWriteDryRun
async function interactWriteDryRun(arweave: Arweave, wallet: JWKInterface, contractId: string, input: any, tags: { name: string; value: string }[] = [], target: string = '', winstonQty: string = ''): Promise<ContractInteractionResult>
This will load a contract to its latest state, and do a dry run of an interaction, without writing anything to the chain, simulating a write.
arweavean Arweave client instancewalleta wallet private or public keycontractIdthe Transaction Id of the contractinputthe interaction inputtagsan array of tags with name/value as objectstargetif needed to send AR to an address, this is the targetwinstonQtyamount of winston to send to the target, if needed
interactRead
async function interactRead(arweave: Arweave, wallet: JWKInterface, contractId: string, input: any, tags: { name: string; value: string }[] = [], target: string = '', winstonQty: string = ''): Promise<any>
Load a contract to its latest state, and execute a read interaction that does not change any state.
arweavean Arweave client instancewalleta wallet private or public key (optional)contractIdthe Transaction Id of the contractinputthe interaction inputtagsan array of tags with name/value as objectstargetif needed to send AR to an address, this is the targetwinstonQtyamount of winston to send to the target, if needed
readContract
async function readContract(arweave: Arweave, contractId: string, height?: number, returnValidity?: boolean): Promise<any>
Queries all interaction transactions and replays a contract to its latest state. If height is provided, will replay only to that block height.
arweavean Arweave client instancecontractIdthe Transaction Id of the contractheightif specified the contract will be replayed only to this block heightreturnValidityif true, the function will return valid and invalid transaction IDs along with the state
selectWeightedPstHolder
function selectWeightedPstHolder(balances: Record<string, number>): string
A utility function for PST tokens. Given an map of address->balance, select one random address weighted by the amount of tokens they hold.
balancesA balances object