Executing multiple actions within a smart contract

Hello,

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

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, etc)
    ---- balanceOf function
    ---- approve function
    ---- deposit function of a token
    ---- balanceOf function
    ---- approve function
    ---- swapTokens (this can be Uniswap, 1inch, 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.

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

1 Like

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

Sorry that I am not more helpful here.

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

1 Like

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.

Hello,

Thank you for the reply. I really appreciate it. :slight_smile:

Regards,
Harish

1 Like

Hi @harishbp,

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

Thanks for being part of the community :pray: