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'
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.
arweave
an Arweave client instancewallet
a wallet private or public keycontractSrc
the contract source code as string.initState
the contract initial state, as a JSON string.
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.
arweave
an Arweave client instancewallet
a wallet private or public keysrcTxId
the contract source Tx id.state
the initial state, as a JSON string.
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.
arweave
an Arweave client instancewallet
a wallet private keycontractId
the Transaction Id of the contractinput
the interaction input, will be serialized as Json.tags
an array of tags with name/value as objectstarget
if needed to send AR to an address, this is the targetwinstonQty
amount of winston to send to the target, if needed
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.
arweave
an Arweave client instancewallet
a wallet private or public keycontractId
the Transaction Id of the contractinput
the interaction inputtags
an array of tags with name/value as objectstarget
if needed to send AR to an address, this is the targetwinstonQty
amount of winston to send to the target, if needed
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.
arweave
an Arweave client instancewallet
a wallet private or public key (optional)contractId
the Transaction Id of the contractinput
the interaction inputtags
an array of tags with name/value as objectstarget
if needed to send AR to an address, this is the targetwinstonQty
amount of winston to send to the target, if needed
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.
arweave
an Arweave client instancecontractId
the Transaction Id of the contractheight
if specified the contract will be replayed only to this block heightreturnValidity
if true, the function will return valid and invalid transaction IDs along with the state
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.
balances
A balances object