run solidity code after every x amount of time - solidity

I am creating a dev application in which after for example every 5 minutes would like to run some code from my erc20 token's smart contract. How can I call that function after every 5 minutes in solidity?

There's no native delay (sleep, or anything that waits some amount of time) function in Solidity (or in EVM bytecode in general).
Each solidity function is executed as a part of a transaction.
So you can set the timer on your off-chain app, sending a transaction each 5 minutes. Mind that there's a delay between sending a transaction from your app and actually publishing the block (by a miner) that contains the transaction.
Example in JS:
function sendTx() {
myContract.methods.myFunction().send();
};
setInterval('sendTx', 5 * 1000 * 60);
You can also validate the delay in Solidity and perform an action only if 5 minutes passed since the last action.
pragma solidity ^0.8;
contract MyContract {
uint256 lastRun;
function myFunction() external {
require(block.timestamp - lastRun > 5 minutes, 'Need to wait 5 minutes');
// TODO perform the action
lastRun = block.timestamp;
}
}

You can use Gelato to schedule function calls in your smart contract. https://www.gelato.network/
A very useful tool that takes a smart contract address, a function name and schedule to execute your chosen tasks.

The best way to accomplish this is with Chainlink Keepers. It functions basically as a Cron job that executes a given function in your contract at every interval that you configure.
It can become expensive though, so make sure you know what the cost-benefit is. Or you can deploy on Polygon or other L2 to minimize the cost.

Related

*big.int and *types.transaction issues during interaction of solidity smart contract using Go-ethereum package

I want to interact solidity simple smart contract using Golang based Go-ethereum package, which showing me the error of *types.transaction and *big.int (returning these instead of string and uint) while functions are:
function Vote() public payable returns (string memory)
function Result() public view returns (uint)
My question is how can I manage them, so that I can get exactly the same value as required.
I think this is because transaction is being performed before this function call, which may be the cause.
By your guess, you have to wait for the transaction to be mined before you can see any resulting value of the transaction.
However, in your case the results are *types.Transaction and *big.Int, this indicates that you are using the deploy-time contract instance, or not initializing the deployed contract (need RPC connection).
You can refer section "Accessing an Ethereum contract" from here, hope you can find the answer

Is it ever more gas efficient to store address(0) as a variable constant?

I see that in many smart contracts address(0) is being used as shorthand for 0x0000000000....
However since it appears very often in almost all smart contracts, I am wondering if it is, or ever becomes gas efficient to simply create the variable as constant in the blockchain instead, and just reference it when necessary, or if it really is just cheaper to constantly write address(0) inline every time.
address public constant NULLADDRESS = address(0); vs using address(0) inline 50 times.
So i just tested this out with this code
pragma solidity >=0.4.22 <0.9.0;
contract NullContract{
address public constant NULLADDRESS = address(0);
function retrieve() public pure returns(address){
return (address(0));
}
}
constant NULLADDRESS needed 21442 gas
and retrieve function which returns value doesn't change anything in the blockchain needed 21420 gas. so its basically the same

Transferring msv.value within the body of a function in solidity

If I have a payable function in Solidity, can I transfer msg.value directly to another address without having funds in the contract. For example:
function foo() payable {
myaddr.transfer(msg.value);
}
If the contract has not received any funds yet, will this work? Or will it only work if there are at least msg.value worth of funds from a previous transaction? Thanks!
If the contract has not received any funds yet, will this work?
Yes. This snippet works as a "transaction proxy", transferring the currently received value to myaddr without having to have the balance before the transaction.
Example:
Your contract has 0 ETH balance.
Execute foo() sending along 10 ETH
myaddr receives the 10 ETH, while your contract keeps the 0 balance
User executing the foo() function pays gas fees for both the main transaction (to your contract implementing foo()) and the internal transaction (from your contract to the myaddr).

How can you simulate the pancakeswap transfer and get the output values

I'm trying to simulate a call the transfer / transferFrom function from a certain contract without submitting it to the blockchain.
This function is used by some contracts to implement some sort of taxing system before any buy/sell.
An example of some tax implementation would be:
function transfer(address recipient, uint256 amount) public override returns (bool) {
amount = amount.mul(0.8); // tax 20%
_transfer(_msgSender(), recipient, amount);
return true;
}
The leading _transfer call is then altering the internal _balances variable with the modified amount variable.
Straight of the bat, I do not want to change the state of anything as that destroys the purpose of the simulation I'm trying to achieve.
This means that I have to use address.staticcall to call this function, since it does not alter the original state. (at least that's what I think it does, correct me if I'm wrong).
The issue now is that the transfer function does not return the modified amount variable. It only returns true or false, and this is pretty much where I get stuck.
How could I retrieve the modified amount variable? Do I need to use assembly or is there some other way of achieving this?

Solidity contract not working

I'm having trouble running my first solidity contract in remix ethereum and through web3 so I'm guessing something is wrong with my contract code:
pragma solidity ^0.4.0;
contract cntrct
{
uint public aaa;
function() payable public
{
create(msg.value);
}
function create(uint _value) internal
{
require(_value>0);
aaa = _value;
}
function reader() view public returns(uint)
{
return aaa;
}
}
I succesfully deployed the contract in both remix and web3. However, after sending a transaction to the contract, the aaa variable is still 0. What I want this code to do is update the aaa variable to the last deposited amount so I can read it later by calling the reader function. In remix it does not show any input field for the aaa variable. Also, in MetaMask transactions sent to the contract stay in a pending status even if they're already completed (balances updated in remix and tx in testRPC.)
In node I'm using the following line to try to execute the reader function but I'm unsure if this will work.
contract.methods.reader().call(0, (error, result) => { if(!error){console.log(result);}});
There’s no reason to store the ether sent in a state variable unless you need to maintain a mapping of balances spread across multiple addresses. The total balance is held in the contract and is accessible with this.balance.
In addition, fallback functions are limited to 2300 gas. You can’t write to storage (ie, update a state variable) within that limit. Your variable isn’t updated because it’s failing. See the second bullet point here for more info.
In solidity contract, you can read the value of public filed by calling field as a method. here, aaa()
I have deployed with mist browser under private network and send 10 ether to this contract successfully.