How To Call a Smart Contract from another Smart Contract (Part 3/3)

Every good tutorial must end in a practical experiment, and so here’s something you may do to explore Cross Contract Calls by yourselves

Ishan Pandey
Laika Labs
4 min readFeb 15, 2023

--

We’ve spent a good time of time exploring methods to complete cross-contract calls in the past two articles in this ongoing series. We’ll culminate it all with a simple assignment task that may you complete in order to ensure that you’re all set!

In Part 1, we explored writing our own contracts and making one call a function on the other.
In Part 2, we explored the Minimum Viable OO Integration contract provided by the UMA Protocol and called the deployed Optimistic Oracle on the Goerli ETH Testnet to provide us with data.

In this Part 3, we’ll quickly go over the method to create cross-contract calls within your dapps as well. This code is made possible using the handy “Code Snippets” feature of your Laika Workspace!

Laika Code Snippets!

Note:
To complete a cross-contract call, you will need to have the address of the target contract and the ABI (Application Binary Interface) of the target contract’s functions that you wish to call. The ABI is a JSON file that describes the functions of a contract and its inputs and outputs.

Here is an example of how to complete a cross-contract call using Solidity and web3.js:

  1. First, import the web3.js library and create a new instance of the web3 object:
import Web3 from 'web3';
const web3 = new Web3(new Web3.providers.HttpProvider('http://localhost:8545'));

2. Next, define the address and ABI of the target contract:

const targetContractAddress = '0x1234567890abcdef';
const targetContractABI = [{...}];

3. Use the ABI and contract address to create a new instance of the target contract:

const targetContract = new web3.eth.Contract(targetContractABI, targetContractAddress);

4. Now you can call the target contract’s functions using the targetContract.methods object.
For example, to call the transfer function of the target contract:

const value = web3.utils.toWei('1', 'ether');
const toAddress = '0x0987654321fedcba';
targetContract.methods.transfer(toAddress, value).send({from: '0xYourAddress'}).then(console.log);

5. If the function is defined as a view function you can call it directly like so:

targetContract.methods.get().call().then(console.log);

Note: In this example, we are using the HttpProvider to connect to a local blockchain node, but you can also connect to a remote node using a different provider. Also, keep in mind that executing cross-contract calls will typically require the sender to pay gas fees.

It’s important to keep in mind that cross-contract calls can be complex and may have unintended consequences if not used carefully. Always thoroughly test and review your code before deploying to a live environment.

Now that we’ve fully explored Cross Contract Calls, we’re ready to complete an assignment!

Create a smart contract named “ProxyContract” that calls the “TargetContract” smart contract’s “transfer” function. The “transfer” function of the “TargetContract” should take two inputs: a destination address and a value in wei. The “transfer” function should transfer the specified value from the “ProxyContract” to the destination address.

The following requirements must be met:

  • The “ProxyContract” must have a public function named “executeTransfer” that takes two inputs: a destination address and a value in wei.
  • When the “executeTransfer” function is called, it should call the “transfer” function of the “TargetContract” with the provided destination address and value as inputs.
  • The address of the “TargetContract” should be stored in a public variable named “targetContractAddress” in the “ProxyContract”.
  • The ABI of the “TargetContract” should be stored in a public variable named “targetContractABI” in the “ProxyContract”.
  • Optional: The “ProxyContract” should be tested using a test suite, to ensure that it correctly calls the “transfer” function of the “TargetContract” and transfers the correct value to the destination address.

To complete the assignment, you will need to use a programming language that is compatible with smart contract development, such as Solidity, and a tool for deploying and testing smart contracts, such as Truffle or Hardhat.
The easiest way to go about this would be to use Remix IDE, as we’ve done in all the past articles.
P.S. It’ll do good to read the past two articles and revise the methods to complete the task.

And that’s it! Laika is your own ABI-first “one-stop-shop” UI for contract interaction on many if not all EVM networks! More chains are being added soon so keep an eye out! Do share your experiences of using Laika with us on our socials.
Adios for now and see you again soon! 🐶❤💛

Connect with Us

Facebook: https://www.facebook.com/getlaikaapp/
Discord:
https://discord.gg/4DzwHuxhcf
Website:
https://getlaika.app

--

--

Laika Labs
Laika Labs

Published in Laika Labs

Laika is a request builder for Web 3.0 It helps you make a request to smart contracts without a single line of code!

Ishan Pandey
Ishan Pandey

Written by Ishan Pandey

Finding Life a li’l bland, a li’l interesting. Find me building, having chats with folks or just writing about anything and everything. ✌M’encanta la Lluna💛