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

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?

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

Solidity memory in a function

Hi I am new to solidity and I am wondering why we use the keyword memory when declaring a function, what happens if we choose not to use it? For example
function createObject(string _name, uint _dna) public {
object.push(Object(_name, _dna));
}
instead of
function createObject(string memory _name, uint _dna) public {
object.push(Object(_name, _dna));
if you don't write it, easily it will give error
Without the memory keyword, Solidity tries to declare variables in storage.
Much like RAM, Memory in Solidity is a temporary place to store data whereas Storage holds data between function calls. The Solidity Smart Contract can use any amount of memory during the execution but once the execution stops, the Memory is completely wiped off for the next execution. Whereas Storage on the other hand is persistent, each execution of the Smart contract has access to the data previously stored on the storage area.
That is, the structure of storage is set in stone at the time of contract creation based on your contract-level variable declarations and cannot be changed by future method calls. BUT -- the contents of that storage can be changed with sendTransaction calls. Such calls change “state” which is why contract-level variables are called “state variables”. So a variable uint8 storagevar; declared at the contract level can be changed to any valid value of uint8 (0-255) but that “slot” for a value of type uint8 will always be there.
If you declare variables in functions without the memory keyword, then solidity will try to use the storage structure, which currently compiles, but can produce unexpected results. memory tells solidity to create a chunk of space for the variable at method runtime, guaranteeing its size and structure for future use in that method.
memory cannot be used at the contract level. Only in methods.
The memory keyword tells Solidity to store the parameter in a certain place which allows you to change that parameter inside the function itself. Pretty much like RAM.
Calldata is the same but the parameter is immutable. It's better to use that if you don't plan on changing the parameter inside the function because it's more efficient.
Storage tells Solidity that the value is in the state of the smart contract. It's also more efficient than memory if the parameter is indeed in the state.
You can get more information about the difference between these 3 keywords in this article

Decentralized Exchanges Quotes: how to retrieve real time quotes?

Is there any API key / solidity function that would allow me to retrieve real time quotes from exchanges?
I already tried 0x API and DEX.AG but both are quite slow (1/2 calls per sec).
Yes, it is not hard but you will have to put in some work. Lets say you have 1 ETH and you want to know how much DAI the exchange will give you for it.
Example:Uniswap v2 Router:
https://etherscan.io/address/0xf164fc0ec4e93095b804a4795bbe1e041497b92a
Under 'Contracts' you can see the function getAmountsOut (Copied here to make it easier to understand)
function getAmountsOut(uint amountIn, address[] memory path) public view override returns (uint[] memory amounts) {
return UniswapV2Library.getAmountsOut(factory, amountIn, path);
}
This 'getAmountsOut' function will accept an integer amountIn, and two addresses called path.
If we input 1 for amountIn, and two addresses
WETH, DAI
[0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2, 0x6b175474e89094c44da98b954eedeac495271d0f]
the function will return the 'amounts' which is what we asked for which as of the time of writing this is 1695 DAI for 1 WETH.
Here are a couple of tutorials THAT I DID NOT WRITE that should help you get started.
UNISWAP v2: https://soliditydeveloper.com/uniswap2
MORE UNISWAP V2: https://vomtom.at/how-to-use-uniswap-v2-as-a-developer/
For automated market-makers like Uniswap, you can directly use their SDK.

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.