Caduceus smart contracts are implemented on the Caduceus EVM. Therefore, it is easy to use widely available libraries such as Web3.js and Ether.js to call deployed contracts.
We will look at three possibilities when calling or executing.
Web3.JS Example
First you must include Web3.js in your Javascript code. You can include it from a CDN such as Cloudflare as in the following example:
This is the simplest type of contract call. We see an example using the popular Metamask wallet below:
JS
|
// Solidity
contract MyContract {functionmyFunction()returns(uint256 myNumber, string myString){return(23456,"Hello!%");}}const web3 =newWeb3(window.ethereum);//web3 served by Metamask// web3.jsvar MyContract =newweb3.eth.Contract(abi, address);
MyContract.methods.myFunction().call().then(console.log);> Result {myNumber:'23456',myString:'Hello!%',0:'23456',// these are here as fallbacks if the name is not know or given1:'Hello!%'}
Creating a transaction to alter state
This is equivalent to writing to the Caduceus blockchain. A smart contract's methods may accept parametes such as integers, strings and booleans. These valuse will be persisted to the blockchain state upon a successful write.
JS
|
// using the callback//.methodName represents the function name in the smart contract//123 represents the parameter that is set
myContract.methods.methodName(123).send({from:'0xde0B295669a9FD93d5F28D9Ec85E40f4cb697BAe'},function(error, transactionHash){...});// using the promise
myContract.methods.methodName(123).send({from:'0xde0B295669a9FD93d5F28D9Ec85E40f4cb697BAe'}).then(function(receipt){// receipt can also be a new contract instance, when coming from a "contract.deploy({...}).send()"});
Creating a transaction to send CMP tokens
Creating a transaction to send CMP involves first converting the amount to Wei, and then converting the Wei to a Hex value.
The ethereum.request object is available when Metamask is active for the user.
In this example, we have an error catcher within the function.