Skip to content

Commit

Permalink
Added doc for invokeContract
Browse files Browse the repository at this point in the history
  • Loading branch information
Hjort committed Jun 10, 2022
1 parent 9de82c1 commit bf4be38
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
40 changes: 40 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -913,6 +913,46 @@ const name = instanceInfo.name;
Note that only version 0 contracts returns the model. (use `isInstanceInfoV0`/`isInstanceInfoV1` to check the version)
## InvokeContract
Used to simulate a contract update, and to trigger view functions.
```js
const blockHash = "7f7409679e53875567e2ae812c9fcefe90ced8961d08554756f42bf268a42749";
const contractAddress = { index: 1n, subindex: 0n };
const invoker = new AccountAddress('3tXiu8d4CWeuC12irAB7YVb1hzp3YxsmmmNzzkdujCPqQ9EjDm');
const result = await client.invokeContract(
blockHash,
{
invoker: invoker,
contract: contractAddress,
method: 'PiggyBank.smash',
amount: undefined,
parameter: undefined,
energy: 30000n,
}
);

if (!result) {
// The node could not attempt the invocation, most likely the contract doesn't exist.
}

if (result.tag === 'failure') {
// Invoke was unsuccesful
const rejectReason = result.reason; // Describes why the update failed;
...
} else {
const events = result.events; // a list of events that would be generated by the update
const returnValue = result.returnValue; // If the invoked method has return value
...
}
```
Note that some of the parts of the context are optional:
- amount: defaults to 0
- energy: defaults to 10 million
- parameter: defaults to no parameters
- invoker: uses the zero account address, which can be used instead of finding a random address.
## Deserialize contract state
The following example demonstrates how to deserialize a contract's state:
Expand Down
6 changes: 6 additions & 0 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -805,6 +805,12 @@ export default class ConcordiumNodeClient {
return response;
}

/**
* Simulates a smart contract update, and returns the updated state of smart contract instance (if V0 contract) or returnValue of the invoked method (if the method has a return value) .
* @param blockHash the block hash at which the contract should be invoked at.
* @param context the collection of details used to invoke the contract. Must include the address of the contract and the method invoked.
* @returns If the node was able to invoke, then the outcome is returned. If the contract is not defined at the given block hash, then undefined is returned.
*/
async invokeContract(
blockHash: string,
contractContext: ContractContext
Expand Down

0 comments on commit bf4be38

Please sign in to comment.