# Executing multiple actions within a smart contract

**URL:** <https://forum.openzeppelin.com/t/executing-multiple-actions-within-a-smart-contract/4120>\
**Category:** Support\
**Created:** [October 13, 2020, 4:21am UTC](https://forum.openzeppelin.com/t/executing-multiple-actions-within-a-smart-contract/4120 "2020-10-13T04:21:53Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![harishbp](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/harishbp/32/3132_2.png) [@harishbp](https://forum.openzeppelin.com/u/harishbp)\
**Post date:** [October 13, 2020, 4:21am UTC](https://forum.openzeppelin.com/t/executing-multiple-actions-within-a-smart-contract/4120/1 "2020-10-13T04:21:53Z")

</div>

Hello,

I have been trying to create a smart contract which will execute multiple actions, similar to the following example.

> **[Ethereum Transaction Hash (Txhash) Details | Etherscan](https://etherscan.io/tx/0x7d639fe1ea5f36dfa6763bfc3ba2876cf2c3359adaa24b96bef5487e5488e0a5)**
>
> Ethereum (ETH) detailed transaction info for txhash 0x7d639fe1ea5f36dfa6763bfc3ba2876cf2c3359adaa24b96bef5487e5488e0a5. The transaction status, block confirmation, gas fee, Ether (ETH), and token transfer are shown.

I was able to do execute my smart contract successfully using with ganache mainnet fork. But, when I implement this on the mainnet, I always run out of gas or I use up HIGH gas.

This is how I am doing this. I am NOT an advanced programmer, hence, I think my implementation is wrong.

1. I call a function present in my smart contact and passing 5 arguments to this function.

2. Based on the arguments, there are about 6 different functions which the smart contract can call within itself.

3. Based on the arguments, the smart contract will in turn call a respective function present within the smart contract and execute a series of actions.

4. Inside this, each of this function in the smart contract, I execute the following sequence of actions.  
---- balanceOf function  
---- approve function  
---- swapTokens (this can be Uniswap, 1inch, [Curve.fi](http://Curve.fi), etc)  
---- balanceOf function  
---- approve function  
---- deposit function of a token  
---- balanceOf function  
---- approve function  
---- swapTokens (this can be Uniswap, 1inch, [Curve.fi](http://Curve.fi), etc)  
---- balanceOf  
---- withdraw function

I know that the above is NOT the correct implementation. Due to this, when I call the smart contract, its using up too much of GAS or its always Out of GAS to execute.

How can I solve this? How can I make sure that I am following the proper structure to implement the smart contract with the sequence of actions provided above?

Also, I came across DSProxy and your own minimal proxy contract implementation. Here is one example of DSProxy transaction.

> **[Ethereum Transaction Hash (Txhash) Details | Etherscan](https://etherscan.io/tx/0xe4eb694e297a381456e9bcfdbd1e53117ae42bc5043428a0f0bb85820bfecffc)**
>
> Ethereum (ETH) detailed transaction info for txhash 0xe4eb694e297a381456e9bcfdbd1e53117ae42bc5043428a0f0bb85820bfecffc. The transaction status, block confirmation, gas fee, Ether (ETH), and token transfer are shown.

In the above example, how are the sequence of actions passed on in byte code? It calls the following function.

execute(bytes \_code, bytes \_data)

I know I am asking too much in this post. But, currently I am lost and I have wasted a lot of dollars to get this working. This is because everything works fine with ganache mainnet fork, but, not on mainnet itself.

Thank you for even if you consider to reply to long post.

Regards,  
Harish

---

<div class="post-metadata">

**Author:** ![abcoathup](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/abcoathup/32/415_2.png) [@abcoathup](https://forum.openzeppelin.com/u/abcoathup)\
**Post date:** [October 13, 2020, 5:56am UTC](https://forum.openzeppelin.com/t/executing-multiple-actions-within-a-smart-contract/4120/2 "2020-10-13T05:56:31Z")

</div>

Hi @harishbp,

I assume that you are creating a contract for trading. Unfortunately this is not my area of expertise.

Ideally you would be able to test locally and calculate the gas used and then try on a public testnet before trying on a fork of mainnet and finally mainnet.  
If you are already setup to test on a fork of mainnet then you should estimate the gas of the transaction and then calculate the cost of the transaction.

I assume the purpose of using DELEGATECALL is to off load any logic that can be done offchain so that you only execute the minimum amount of calls on chain.

I found the documentation for DSProxy: [https://dapp.tools/dappsys/ds-proxy.html](https://dapp.tools/dappsys/ds-proxy.html)

Sorry that I am not more helpful here.

---

<div class="post-metadata">

**Author:** ![harishbp](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/harishbp/32/3132_2.png) [@harishbp](https://forum.openzeppelin.com/u/harishbp)\
**Post date:** [October 13, 2020, 6:09am UTC](https://forum.openzeppelin.com/t/executing-multiple-actions-within-a-smart-contract/4120/3 "2020-10-13T06:09:19Z")

</div>

Thank you for the reply.

If its possible, can you give me a good example, on how to encode two functions and execute the transaction? I am doing something like this for 1 function, but, how to do it with 2 or more functions?

```
const tx = token.methods.approve(address, amount);
    const data = tx.encodeABI();

```

How do I add 1 more function to this above code? Something like -

```
const tx1 = token.methods.approve(address, amount);
    	const tx2 = token.methods.deposit(amount); 
    const data = tx1.encodeABI();
    const data = tx2.encodeABI();

```

Is it possible?

Regards,  
Harish

---

<div class="post-metadata">

**Author:** ![abcoathup](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/abcoathup/32/415_2.png) [@abcoathup](https://forum.openzeppelin.com/u/abcoathup)\
**Post date:** [October 14, 2020, 5:58am UTC](https://forum.openzeppelin.com/t/executing-multiple-actions-within-a-smart-contract/4120/4 "2020-10-14T05:58:30Z")

</div>

Hi @harishbp,

This is not something I have done or played with unfortunately.

I am wondering if you could pass in the data for each action as a parameter to call in order, or concatenate the actions as a single lot of data, and then decode each action. Though it gets tricky if you need the output of one action as the input of another.

Which is why I think you may be better off (at least to start with) as specifying each action to take in your contract.

Sorry I can’t be of much help.

---

<div class="post-metadata">

**Author:** ![harishbp](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/harishbp/32/3132_2.png) [@harishbp](https://forum.openzeppelin.com/u/harishbp)\
**Post date:** [October 15, 2020, 1:48am UTC](https://forum.openzeppelin.com/t/executing-multiple-actions-within-a-smart-contract/4120/5 "2020-10-15T01:48:04Z")

</div>

Hello,

Thank you for the reply. I really appreciate it. 🙂

Regards,  
Harish

---

<div class="post-metadata">

**Author:** ![abcoathup](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/abcoathup/32/415_2.png) [@abcoathup](https://forum.openzeppelin.com/u/abcoathup)\
**Post date:** [October 15, 2020, 7:40am UTC](https://forum.openzeppelin.com/t/executing-multiple-actions-within-a-smart-contract/4120/6 "2020-10-15T07:40:00Z")

</div>

Hi @harishbp,

Feel free to keep asking questions, as a community member may be able to assist.

Thanks for being part of the community 🙏
