Fetch pair price from Uniswap using smart contract function - ethereum

I am relatively new to Solidity, I am trying to fetch the price of a pair with the following contract:
contract Uniswap {
constructor () public payable {
}
function getBalance() public view returns (uint) {
return address(this).balance;
}
// calculate price based on pair reserves
function getTokenPrice(address pairAddress, uint amount) public payable returns(uint)
{
IUniswapV2Pair pair = IUniswapV2Pair(pairAddress);
IERC20 token1 = IERC20(pair.token1());
(uint Res0, uint Res1,) = pair.getReserves();
// decimals
uint res0 = Res0*(10**token1.decimals());
return((amount*res0)/Res1); // return amount of token0 needed to buy token1
}
}
but when I try to run on Remix the getTokenPriceit returns me the following error message:
The transaction has been reverted to the initial state. Note: The called function should be payable if you send value and the value you send should be less than your current balance. Debug the transaction to get more information.
Any idea why? I am trying to fetch the price of ETH/USDT so the using this website (https://v2.info.uniswap.org/pair/0x0d4a11d5eeaac28ec3f61d100daf4d40471f1852) and with the pair address at the end of the URL. Thanks for the help!

When you deploy a contract in Remix, it deploys to a local network. (By default. You can change it and deploy to mainnet as well, if you inject a production Infura provider for example.)
The Uniswap pair contract 0x0d4a11... does not exist on the local network.
The easiest solution is to deploy your contract on the mainnet (where the 0x0d4a11... Uniswap pair contract exists).
Or you can find another Uniswap pair on a public testnet (well-known are Rinkeby, Ropsten, Goerli, and Kovan), and deploy your contract on the same testnet. Don't forget to change the testnet address of the uniswap pair that you pass to your getTokenPrice() function.
Or you can deploy the Uniswap pair contract and all of its dependencies and change the dependencies addreses (which is a lot of work - my guess is that it's dependend on the Uniswap router contract, the actual token contracts of the pair, maybe they are dependend on some other contract, ...) to your local network. From here it's the same - deploy your contract to the local network, and call the getTokenPrice() with the correct pair address.

You may also invoke uniswap's Subgraph using javascript

Related

Uniswap v3 custom ERC20 token swap

I am trying to implement a token swap of my custom ERC20 token via UniswapV3
I use Rinkeby Ethereum network.
I deployed the token under address: 0x4646CB39EA04d4763BED770F80F0e0dE8efcdF0f
I added the liquidity to Uniswap for this token and ETH.
Now, I try to execute swap in my contract, but it doesn't work. I get the error:
Gas estimation errored with the following message (see below). The transaction execution will likely fail. Do you want to force sending?
execution reverted
My Swap.sol contract takes an address of the token to swap with ETH as a constructor parameter. When I deploy it using DAI token address, the swap works just fine.
I assume this is a Uniswap liquidity related problem, but I added liquidity manually and I can swap my token inside their app.
Contract code:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.10;
pragma abicoder v2;
import "#uniswap/v3-periphery/contracts/libraries/TransferHelper.sol";
import "#uniswap/v3-periphery/contracts/interfaces/ISwapRouter.sol";
import "#uniswap/v3-periphery/contracts/interfaces/IQuoter.sol";
contract Swap {
address private constant SWAP_ROUTER =
0xE592427A0AEce92De3Edee1F18E0157C05861564;
address private constant WETH = 0xc778417E063141139Fce010982780140Aa0cD5Ab;
address public tokenAddress;
address public immutable _owner;
ISwapRouter public immutable swapRouter;
constructor(address token) {
_owner = msg.sender;
swapRouter = ISwapRouter(SWAP_ROUTER);
tokenAddress = token;
}
function swapExactInputSingle() external payable {
require(msg.value > 0, "Must pass non 0 ETH amount");
ISwapRouter.ExactInputSingleParams memory params = ISwapRouter
.ExactInputSingleParams({
tokenIn: WETH,
tokenOut: tokenAddress,
fee: 3000,
recipient: msg.sender,
deadline: block.timestamp,
amountIn: msg.value,
amountOutMinimum: 1,
sqrtPriceLimitX96: 0
});
swapRouter.exactInputSingle{value: msg.value}(params);
}
receive() external payable {}
}
I had the same issue with the swapExactInputMultihop function on uniswap. For each pools/paths you're going through, you need to make sure you've got the correct pool fee set.
You can checkout the swap fees on the uniswap website: V3-overview/fees
or on a video tutorial, going through the whole process:
Blockchain With Wisdom on YouTube
Managed to fix it.
I set the fee: 3000 in contract, but I created liquidity with 1% fee, so I had to change it to fee: 10000 according to docs: fee The fee tier of the pool, used to determine the correct pool contract in which to execute the swap

How can I do a proxy call on anchor protocol terra ethereum transfer. error: UNPREDICTABLE_GAS_LIMIT

I want to make a smart contract that deposits UST on the eth anchor protocol, by using the deposit function. Here you can find the docs of this function I try to call. https://docs.anchorprotocol.com/ethanchor/ethanchor-contracts/router . The function gets called trough a proxy contract. I found the implementation address by reading the proxy contract on etherscan.
TestContracts on Ropsten testnet:
proxy: 0x7537aC093cE1315BCE08bBF0bf6f9b86B7475008
etherscan: https://ropsten.etherscan.io/address/0x7537aC093cE1315BCE08bBF0bf6f9b86B7475008
implementation (ROUTERV2): 0x2ba0010394eb3e41454c7d79c0fb6e0deb67284b
etherscan: https://ropsten.etherscan.io/address/0x2ba0010394eb3e41454c7d79c0fb6e0deb67284b
I imported the ROUTERV2 contract for calling the proxy with the abi of the implementation.
my execution order is:
deploying the contract
approving the contract for spending the tokens from the sender using ethers.
approveUST function on smart contract (I am not sure if this is necessary)
calling the depositStable function.
I get the following error when calling the depositStale function UNPREDICTABLE_GAS_LIMIT;
Below You see the contract I try to make. It should take the UST of the user and deposit it on eth anchor.
Here is a github link to the hardhat repo of the contract so you can simulate the bug and hopefully help me solving this issue.
https://github.com/tijgersoftware/Terra-anchor
I thank you for taking the time to read my question!
contract Anchor {
address public proxy;
address public operator;
address public USTAddress;
constructor(
address proxy_,
address operator_,
address USTAddress_
) public {
proxy = proxy_;
operator = operator_;
USTAddress = USTAddress_;
}
function approveUST() public {
ERC20(USTAddress).approve(
address(this),
0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
);
}
function initDepositStable(uint256 amount) public {
RouterV2(proxy).initDepositStable(amount);
}
function depositUST(uint256 amount) public {
// InterfaceOfImplementationContract(proxyAddress).MethodBeingCalled(
// parameter,
// parameter,
// parameter
// );
ERC20(USTAddress).approve(
address(this),
0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
);
RouterV2(proxy).depositStable(operator, amount);
}
}
tags: solidity, smart contracts, ethereum, anchor protocol, terra, brdige trasfer.

Is my Solidity code correct and safe to use

I am very new using Solidity to create Ethereum contracts and would like to know if the contract I have created is first correct and safe to use.
What I am doing in this contract is allowing the user to send some ETH from their wallet to a friends wallet but also take a $0.05 fee (194740000000000 wei at time of writing) for using my services.
Now when testing the contract using remix all works fine, but as I am new to solidity contracts I just wanted to get some expert opinions to make sure I am doing everything correctly and safety.
My contract code is:
pragma solidity ^0.5.1;
contract Forwarder {
address payable public receiversAddress = 0x14723A09ACff6D2A60DcdF7aA4AFf308FDDC160C;
address payable public feeAddress = 0xdD870fA1b7C4700F2BD7f44238821C26f7392148;
constructor() payable public {
uint amountToSendReceiver = msg.value-194740000000000;
receiversAddress.transfer(amountToSendReceiver);
feeAddress.transfer(194740000000000);
}
}
As mentioned everything works fine in remix, but is my contract code correct and safe to use? There is no way to use the contract after to maybe steal funds or have any vulnerabilities?
Yes, your code is pretty safe. The only thing I would like to add is line with require condition for code maintainability
And if your vars are not changeable, its better to use constant to make your contract cheaper for deploy ;)
Functions can be declared constant in which case they promise not to
modify the state.
pragma solidity ^0.5.1;
contract Forwarder {
address payable public constant receiversAddress = 0x14723A09ACff6D2A60DcdF7aA4AFf308FDDC160C;
address payable public constant feeAddress = 0xdD870fA1b7C4700F2BD7f44238821C26f7392148;
uint256 constant feeAmount = 194740000000000;
constructor() payable public {
require(msg.value >= feeAmount); // for maintainability, your tx will be reverted anyway, just to show why
feeAddress.transfer(feeAmount); // transfer fee
receiversAddress.transfer(address(this).balance); // transfer remaining amount of msg.value
}
}

How ERC20 tokens control access to their functions?

Here is my question (basically, I want to know if I am right or mistaken):
First of all, we have standard ERC20:
contract ERC20Interface
{
function transfer(address _to, uint _value) public returns (bool success);
function transferFrom(address _from, address _to, uint _value) public returns (bool success);
function approve(address _spender, uint _value) public returns (bool success);
.....
}
Then, in a CrowdSale, we have a token, derived from it:
contract ICO is Ownable {
...
MyToken public m_token;
...
function ICO() public {
m_token = new MyToken();
...
Then the crowdsale ends. The token with list of all people (addresses) that bought it becomes "free" of crowd sale contract:
function finish() onlyOwner public {
if(goalReached()) {
m_token.transferOwnership(m_addrOwner);
My questions:
1. For this token to be traded on exchanges, I have to somehow provide these exchanges with token's address. Where should I get it?
2. Am I correct? Is it how it supposed to work?
In other words, if a token is a separate contract, passed to ICO contract by address, I can see how exchanges can trade it after the end of crowd sale: they have that same address.
But I see a lot of crowdsales on Etherscan, that create contract internally (like in code snippets above). How do I make it "tradable" after the end of a crowd sale in this case?
Thanks.
Most exchanges will only support your token if they see value in it. You'd have to contact the exchanges and they will evaluate if it's worth it to them based on the demand for your token. Even then, they will most likely ask for payment up front before listing.
For example, Bittrex has their submission process documented here. They offer free listing once you pass their verification process (along with some non-free services). Other exchanges follow a similar process.
The contract address you're looking for that you'd have to provide is returned to your client when you deploy your token contract. If you somehow lose the address in your client, you can find it on etherscan.io by searching for the token name/symbol (assuming your token contract follows the ERC20 spec) or by transaction hash. You can also retrieve the address in code:
function getTokenAddress() constant returns (address) {
return address(m_token);
}
Outside of an exchange, you can also do private transfers of tokens by simply adding the token contract address to a wallet like Mist or Parity and sending tokens between addresses.
Note that all of this is independent of the crowd sale. The crowd sale is a separate contract that is used to raise money and do the initial token distribution.

Creating instance of contract inside another contract and calling it's methods results in thrown exception

I have 2 basic contracts: one is for token and the second is for sale.
Token сontract:
contract MyToken is StandardToken, Ownable {
string public constant name = "My Sample Token";
string public constant symbol = "MST";
uint32 public constant decimals = 18;
function MyToken(uint _totalSupply) {
require (_totalSupply > 0);
totalSupply = _totalSupply;
balances[msg.sender] = totalSupply;
}
}
Sale Contract
contract Sale {
address owner;
address public founderAddress;
uint256 public constant foundersAmount = 50;
MyToken public token = new MyToken(1000);
uint256 public issuedTokensAmount = 0;
function Sale() {
owner = msg.sender;
founderAddress = 0x14723a09acff6d2a60dcdf7aa4aff308fddc160c;
token.transfer(founderAddress, foundersAmount);
}
function() external payable {
token.transfer(msg.sender, 1);
owner.transfer(msg.value);
}
}
StandardToken and Ownable are all standard implementations from OpenZeppelin repository. Full contract source is available here.
So basically in my Sale Contract I create an instance of my token contract with fixed supply and assign all of the tokens to the caller. Then I transfer some amount of tokens to founder address. When I try to send some ethereum to Sale contract I'm attempting to transfer some of my tokens to the sender (Running all code in Remix browser, I create an instance of Sale contract and call "fallback" method specifying some ether amount). However, this fails with "Exception during execution. (invalid opcode). Please debug the transaction for more information." message. All that I can see when debugging is that code fails in payable method at line:
token.transfer(msg.sender, 1);
I can't see the exact reason for this as I'm not able to step into this method and see whats going on inside.
Interesting thing is that when I remove a call to transfer method on token instance in Sale Contract constructor - code seems to run fine without any exceptions.
What am I missing?
I debugged into the the contract using remix, and the invalid opcode is thrown by:
290 DUP8
291 DUP1
292 EXTCODESIZE
293 ISZERO
294 ISZERO
295 PUSH2 012f
298 JUMPI
299 PUSH1 00
301 DUP1
302 INVALID
I left out the rest, but essentially it loads the address of the token contract and calls EXTCODESIZE which retrieves the contracts code size, and checks that it's not equal to 0 (the token contract exists), unfortunately, it did equate to 0. At this point, I'm unsure whether this is a limitation in remix or I've misunderstood the setup.
I tried the identical contract setup on truffle + testrpc and it deployed, accepted the currency successfully. Do note however that testrpc indicated:
Gas usage: 59137
Meaning that this is above the default sendTransaction w/ no data default (21,000 gas). This means that in a live environment, ensure that you inform users to include extra gas, otherwise the fallback function would probably fail due to OOG errors.
The reason behind this is that you're using a fallback function. Try using a normal function and it should happen.