4.4 KiB
SmartWeave SDK v2 - Contract methods
Contract Methods
connect
async function connect(wallet: ArWallet): Contract<State>
Allows to connect wallet to a contract. Connecting a wallet MAY be done before "viewState" (depending on contract implementation, ie. whether called contract's function required "caller" info) Connecting a wallet MUST be done before "writeInteraction".
walleta JWK object with private key or 'use_wallet' string.
Example
const contract = smartweave
.contract("YOUR_CONTRACT_TX_ID")
.connect(jwk);
setEvaluationOptions
function setEvaluationOptions(options: Partial<EvaluationOptions>): Contract<State>
Allows to set (EvaluationOptions)
optionsthe interaction inputoptions.ignoreExceptionsenables exceptions ignoringoptions.waitForConfirmationenables waiting for transaction confirmation
Example
const contract = smartweave
.contract("YOUR_CONTRACT_TX_ID")
.setEvaluationOptions({
waitForConfirmation: true,
ignoreExceptions: false,
});
readState
async function readState(blockHeight?: number, currentTx?: { contractTxId: string; interactionTxId: string }[]): Promise<EvalStateResult<State>>
Returns state of the contract at required blockHeight. Similar to the readContract from the version 1.
blockHeightBlock height for statecurrentTxIf specified, will be used as a current transaction
Example
const { state, validity } = await contract.readState();
viewState
async function viewState<Input, View>(input: Input, blockHeight?: number, tags?: Tags, transfer?: ArTransfer): Promise<InteractionResult<State, View>>
Returns the "view" of the state, computed by the SWC - ie. object that is a derivative of a current state and some specific smart contract business logic. Similar to the interactRead from the current SDK version.
inputthe interaction inputblockHeightif specified the contract will be replayed only to this block heighttagsan array of tags with name/value as objectstransfertarget and winstonQty for transfer
Example
const { result } = await contract.viewState<any, any>({
function: "NAME_OF_YOUR_FUNCTION",
data: { ... }
});
viewStateForTx
async function viewStateForTx<Input, View>(input: Input, transaction: InteractionTx): Promise<InteractionResult<State, View>>
A version of the viewState method to be used from within the contract's source code. The transaction passed as an argument is the currently processed interaction transaction. The "caller" will be se to the owner of the interaction transaction, that requires to call this method.
💡 Note! calling "interactRead" from withing contract's source code was not previously possible - this is a new feature.
inputthe interaction inputtransactioninteraction transaction
Example
const { result } = await contract.viewStateForTx<any, any>({
function: "NAME_OF_YOUR_FUNCTION",
data: { ... }
}, transaction);
writeInteraction
async function writeInteraction<Input>(input: Input, tags?: Tags, transfer?: ArTransfer): Promise<string>
Writes a new "interaction" transaction - ie. such transaction that stores input for the contract.
inputthe interaction inputtagsan array of tags with name/value as objectstransfertarget and winstonQty for transfer
Example
const result = await contract.writeInteraction({
function: "NAME_OF_YOUR_FUNCTION",
data: { ... }
});
Need help? 🙋♂️
Please feel free to contact us on Discord if you face any problems.