description |
---|
Read from a smart contract on Arweave |
The readContractState
function enables reading the contract state from a deployed contract based on the input parameters.
{% hint style="info" %} To retrieve the result from a contract's read function, it is recommended to utilize the viewContractState function. {% endhint %}
The function is called as follows:
import { readContractState } from 'arweavekit/contract'
const readResult = await readContractState({params});
The following params are available for this function and they must be passed in as an object:
environment: 'local' | 'testnet' | 'mainnet'
: The environment in which the smart contract was created in. Thetestnet
is a pseudo testing environment created on top of themainnet
with the help of custom tags.
{% hint style="info" %}
If the contract was created in a local
environment, please make sure thatarlocal
is running in the background to be able to interact with the contract. To create one, simply run npx arlocal
in the command line. Learn more about arlocal
here.
{% endhint %}
evaluationOptions : object
(optional) : Evaluation options define rules on how a contract will be evaluated during reading contract state. Read more about evaluation options to learn about the available configurations and their uses.contractTxId: string
: ThecontractTxId
is a unique identifier received upon contract creation. This helps thewriteContract
to identify the contract to interact with.cacheOptions : object
(optional) : Contract state is cached for fast retrieval and seamless experience. Custom configurations can be passed in as an object for more control over the caching process. This includes aninMemory
option enabling the storage of cache in local memory. By default, the contract state is cached in a database. ThedbLocation
can be configured with the options outlined in these docs.
Example
const readResult = await readContractState({
environment: 'testnet',
contractTxId: 'CONTRACT_TRANSACTION_ID',
});
The function call returns the following data:
readContract: SortKeyCacheResult {
sortKey: '000000000000,0000000000000,0000000000000000000000000000000000000000000000000000000000000000',
cachedValue: EvalStateResult {
state: [Object],
validity: {},
errorMessages: {}
}
},
result: { status: 200, statusText: 'SUCCESSFUL' }
readContract: SortKeyCacheResult
: ThereadContract
object returns thesortKey
andcachedValue
. Read more here.sortKey: string
: ThesortKey
is a value that warp uses to sort contract transactions.cachedValue: EvalStateResult
: ThecachedValue
is an object that stores the state, the validity of the state and any errors faced while reading the state. If thereadContract
function is called immediately after awriteContract
call, the cached state is optimistically updated assuming the write call occurs successfully, while the interaction continues to be processed on the actual network (in cases of using thetestnet
andmainnet
.
result: object
: Theresult
object returns easy to understand information showing the status of the read call.status: 200
andstatusText: 'SUCCESSFUL'
indicate that information has been successfully read from the contract.