Developers
Tutorials
Contract Calling and Execution
8min
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 \<script src="https //cdnjs cloudflare com/ajax/libs/web3/3 0 0 rc 5/web3 min js">\</script> calling to read this is the simplest type of contract call we see an example using the popular metamask wallet below // solidity contract mycontract { function myfunction() returns(uint256 mynumber, string mystring) { return (23456, "hello!%"); } } const web3 = new web3(window\ ethereum); //web3 served by metamask // web3 js var mycontract = new web3 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 given 1 '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 // 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 async function sendcmp() { input value = $('#htmlbox') val() //jquery based user input console log(input value); if (input value trim() == '') { input value = 0 } if (!isnan(input value)) { input value = parseint(input value) } var starting = string(input value); var amountwei = web3 utils towei(starting, 'ether'); var finalvalue = web3 utils tohex(amountwei); ethereum request({ method 'eth sendtransaction', params \[{ from currentaccount, to contactaddress, value finalvalue, //gasprice '0x09184e72a000', // gas '0x2710', }, ], }) then((txhash) => { console log(txhash); ethereum request({ method 'eth gettransactionreceipt', params \[txhash] }) then((transactionreceiptresult) => { console log("transaction receipt result fired"); console log(transactionreceiptresult); }) }) catch((error) => { console log("reached error"); console log(error); }); } ether js ether js is a another popular package that is used to connect to smart contracts on evm compatible blockchains calling to read let's look at an example of a contract read action, using metamask const ethereum = (window as any) ethereum; const accounts = await ethereum request({ method "eth requestaccounts", }); const provider = new ethers providers web3provider(ethereum) const walletaddress = accounts\[0] // first account in metamask const signer = provider getsigner(walletaddress) const abi = \[ "function name() public view returns (string)", "function symbol() public view returns (string)", "function decimals() public view returns (uint8)", "function totalsupply() public view returns (uint256)", "function approve(address spender, uint256 value) public returns (bool success)"] const usdtcontract = new ethers contract("0xdac17f958d2ee523a2206206994597c13d831ec7", abi, signer) const name = await usdtcontract name() const symbol = await usdtcontract symbol() const decimals = await usdtcontract decimals() const totalsupply = await usdtcontract totalsupply() console log(`${symbol} (${name}) total supply is ${ethers utils formatunits(totalsupply, decimals)}`) creating a transaction to alter state suppose a solidity function is named buy we would call it as follows through javascript \<script src="https //cdn ethers io/lib/ethers 5 0 umd min js" type="application/javascript" \>\</script> //sending data to write to blockchain state await contract buy(inputamount, { value ethersvalue }); //sending tokens function send token( contract address, send token amount, to address, send account, private key ) { let wallet = new ethers wallet(private key) let walletsigner = wallet connect(window\ ethersprovider) window\ ethersprovider getgasprice() then((currentgasprice) => { let gas price = ethers utils hexlify(parseint(currentgasprice)) console log(`gas price ${gas price}`) if (contract address) { // general token send let contract = new ethers contract( contract address, send abi, walletsigner ) // how many tokens? let numberoftokens = ethers utils parseunits(send token amount, 18) console log(`numberoftokens ${numberoftokens}`) // send tokens contract transfer(to address, numberoftokens) then((transferresult) => { console dir(transferresult) alert("sent token") }) } // ether send else { const tx = { from send account, to to address, value ethers utils parseether(send token amount), nonce window\ ethersprovider gettransactioncount( send account, "latest" ), gaslimit ethers utils hexlify(gas limit), // 100000 gasprice gas price, } console dir(tx) try { walletsigner sendtransaction(tx) then((transaction) => { console dir(transaction) alert("send finished!") }) } catch (error) { alert("failed to send!!") } } }) }