0

I've deployed a smart contract to the blockchain with few functions.

I want to know how I can interact with the code e.g. > Contract has 2 functions - deposit and withdraw. Now, instead of me depositing and withdarwing 100 times manually, how can I write code which calls those functions automatically for me?

Let's say the code creates news Ether wallet, sends Ethereum to that wallet, then, from that wallet deposits the funds and withdraws it, then creates new wallet and repeat.

3

2 Answers 2

0

Code in Smart Contracts cannot be invoked automatically from within the blockchain, it has to be called externally.

This means that you need to write up a script or some service which would run and interact with your smart contract. Once you have this service you can run it as many times as you want, or you could schedule it to run at specific intervals.

A good place to start is to read into Web3.js which is a well known library to integrate with the Ethereum blockchain.

3
  • That's not true, there are plenty of smart contracts that use on-chain methods to execute code. Auto rebase tokens, Sett/Alpha vaults. You can import an oracle library to have certain features execute based on off-chain data or use on-chain data like specific block intervals to invoke methods, or even the arbitrage bots that run are all automated. You can create the script that executes the main contract using another contract inside the directory when it's deployed as well. Aug 9, 2021 at 19:08
  • I believe you misunderstood what I was saying. Every single entry point of an execution flow has to start from outside the blockchain ecosystem. The automation you mentioned is possible with scripts, oracles, etc. but each one of them starts from an outside source. Basically you cannot write a smart contract which executes on it's own at any given time (e.g. something similar to a cron job) without any external force initiating the execution.
    – valdi.k
    Aug 10, 2021 at 13:38
  • Sorry oracles and rebasing were poor examples. You can perform internally executed cron jobs. @InstaDev/cron - Period.sol takes an index of the number of blocks since initialized. Period _cron = new Period(T) can be executed internally with uint256 currentPeriod = _cron.getCurrentPeriod(); incrementing each period T. PeriodicStages(T) pushes stages to the stack FIFO. You just want to make sure you have checks in place so it does not eternally drain an account of ETH, gas, or whatever is being utilized. I use it in a survival game I'm developing with no issue. Sep 2, 2021 at 5:53
-2

Found this tutorial on how to automate smart contract:

"Let’s say you already have a smart contract deployed. And you want Gelato to call the smart contract at a certain time. All you need to do is to deploy a resolver contract which will tell Gelato."

“Hey call this function here at every hour”

Here is an example of the function buy()which I want Gelato to call. This function buys ETH through Uniswap V2:

uint256 public lastBought;
function buy(
    uint256 amountIn,
    uint256 amountOutMin,
    address[] calldata path,
    address to,
    uint256 deadline
  ) external {
    require(block.timestamp >= lastBought + 1 hours);
    IERC20(path[0]).approve(address(router), amountIn);
    router.swapExactTokensForETH(
           amountIn, 
           amountOutMin, 
           path, 
           to,     
           deadline
    );
    lastBought = block.timestamp;
  }

So more in the tutorial. Hope it helps!

1
  • 1
    It seems you are part of Gelato network. The site has a rule that if you use a link in the answer you should disclose your affiliation or it will be considered as spam. Also the answer is quite poor just a copy and past from the link.
    – Ismael
    Feb 25 at 5:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.