How to invoke a pre-installed contract in another contract - ethereum

First I pre-install a contract A(address(A)). Then I install a contract B (address(B)) to invoke A by using call interface. Lastly I invoke the contract by using his address(x). I meant to use the address(x) to invoke A, but actually the address(B) invoke A. So how can I use address(x) to invoke A?"
pragma solidity ^0.4.21;
contract TransferERC20 {
event TransferEvent (
bool _flag,
string _invoiceId,
address _erc20ContractHash,
address indexed _from,
address indexed _to,
uint256 _value
);
function transfer(string _invoiceId, address _erc20ContractHash,address _from, address _to, uint256 _amount) public returns (bool) {
bytes4 methodTransfer = bytes4(keccak256("transfer(address,uint256)"));
if(_erc20ContractHash.call(methodTransfer, _to, _amount)) {
emit TransferEvent(true, _invoiceId, _erc20ContractHash, _from, _to, _amount);
return true;
}
emit TransferEvent(false, _invoiceId, _erc20ContractHash, _from, _to, _amount);
return false;
}
}
The above code intends to call ERC20 contract which is pre-installed on ethereum testnet. However I invoke failed because the address is changed to the the TransferERC20 address. How can I realize the transfer function by using the TransferERC20 callor's address. THKS.

The token holder first needs to set an allowance using approve on the ERC20 token for the TransferERC20 contract and only then can they call transfer on the TransferERC20 contract which should call transferFrom on the ERC20 token.
This requires two transactions, one for approve and one for transferFrom.
If you are creating ERC20 tokens you may want to look at the OpenZeppelin Contracts implementation to see if this meets your needs.
See the documentation for details: https://docs.openzeppelin.com/contracts/2.x/tokens#ERC20
Alternatively you could look at creating ERC777 tokens (no need to do approve and transferFrom in two separate transactions). See the documentation for details: https://docs.openzeppelin.com/contracts/2.x/tokens#ERC777
If you have questions on using OpenZeppelin you can ask in the Community Forum: https://forum.openzeppelin.com/
Disclosure: I am the Community Manager at OpenZeppelin

Related

how to implement the ERC20 token transferFrom method in solidity smartcontract

Assuming I have a wallet, I want to authorize another wallet to send a transfer on my behalf, how do I implement the above using the following practice.
pragma solidity ^0.8.0;
import 'Token.sol';
contract TransferToken{
function transferFrom(address recipient, uint amount) external {
Token token = Token(0xd9145CCE52D386f254917e481eB44e9943F3555);
token.transferFrom(msg.sender, recipient, amount);
}
}
contract Owner {
function transfer(address recipient, uint amount) external {
Token token = Token(0xd9145CCE52D386f254917e481eB44e9943F39138);
token.approve(0x617F2E2fD72FD9D5503197092aC168c91465E7f2, amount);
TransferToken transferToken = TransferToken(0x617F2E2fD72FD9D5503197092aC168c91465E7f2);
transferToken.transferFrom(recipient, amount);}}
in the above example, i am using the openZappelin implementation for ERC20 on the import (Token.sol)
now my question is this ... since I want to use a real wallet with ERC20 token how do I implement the above example, I am thinking for this I don't need the import anymore, but I don't know how to implement it without too many mistakes already, I very new to solidity and I am using remix for the above implementation
will be glad if i could get a help on this

How to transfer an ERC721 token

I'm trying to transfer an ERC721 token, but I'm getting the error ERC721: transfer caller is not owner nor approved for the transferToken method.
Main.sol
import "./ERC721.sol";
import "./Counters.sol";
contract Main is ERC721 {
using Counters for Counters.Counter;
Counters.Counter internal _tokenIds;
address payable internal admin;
constructor() ERC721("MyToken", "TOKEN") {
admin = payable(msg.sender);
}
}
Auction.sol
import "./Main.sol";
contract Auction is Main {
struct AuctionInfo {
uint256 tokenId;
address highestBidder;
uint highestBid;
}
mapping(string => AuctionInfo) private _auctionInfo;
function createAuction(string memory id) public {
_tokenIds.increment();
uint256 newTokenId = _tokenIds.current();
_mint(msg.sender, newTokenId);
_auctionInfo[id].tokenId = newTokenId;
}
function transferToken(string memory id) public {
require(msg.sender == _auctionInfo[id].highestBidder, "You are not the highest bidder");
safeTransferFrom(address(this), _auctionInfo[id].highestBidder, _auctionInfo[id].tokenId);
}
// other methods...
}
The minting contract is this and the owner of the token is the msg.sender of the minting method if I'm not mistaken. Am I to use the approve (or setApprovalForAll) for this each time before transferring? I've tried this, payable(this), and address(this) for the safeTransferFrom method, but none seem to be working.
For example, I tried the following, but get the same revert message:
approve(address(this), _auctionInfo[id].tokenId);
this.safeTransferFrom(address(this), _auctionInfo[id].highestBidder, _auctionInfo[id].tokenId);
The main principle behind any Blockchain is that nobody on the blockchain network should be trusted, and still the transactions should happen fool proof, with no possibility of any cheating being done (barring of course of some hacking).
If you invoke the approve method from the Auction contract, then the msg.sender for the approve function in the ERC721 token contract is your auction contract address. So, in other words, your Auction Contract is trying to approve itself to sell someone else's NFTs, which is not very trustworthy.
What should really happen is that owner of the NFT should invoke the approve method of the ERC721 contract - i.e. the transaction that you send for the approve function call, should be signed by the NFT owner wallet address. This way, the msg.sender for the approve function in the ERC721 contract will be the owner of the NFT. As per the ERC721 standards, the owner of the NFT can approve anyone they want, to sell their NFT(s), as the no-trust in the network is still maintained (At least I should be able to trust myself). The approve method should be invoked from within your DAPP, before the transferToken function is invoked from the DAPP.
Hope that explains why you are unable to transfer your ERC721 tokens.
Because of the internal visibility of the ERC721._approve() function, you can effectively perform the approval for the user.
Then you'll be able to execute the safeTransferFrom(tokenOwner, receiver, tokenId) from your contract, because your contract address is approved to operate this specific token even though it belongs to the tokenOwner.
This snippet mints the token, assigning the ownership to the msg.sender. But then it also calls the _approve() function that doesn't contain any validations and simply assigns the approval of the token to the Auction address.
pragma solidity ^0.8;
import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/token/ERC721/ERC721.sol";
contract Auction is ERC721 {
constructor() ERC721("CollectionName", "Symbol") {}
function createAuction() public {
uint256 newTokenId = 1;
_mint(msg.sender, newTokenId);
_approve(address(this), newTokenId);
}
}
You can see from the screenshot that the owner is 0x5B... (the user address) and that the token is approved for 0xd9... (the contract address).
Note: The _approve() function is internal - it can be called from the ERC721 contract and contracts deriving from it (in your case Main and Auction), but it can't be called from external contracts or end user addresses.

Why can't I use this transferEther function to send Ether to the smart contract?

I have this code I have entered into Remix IDE, as ReceivedEther.sol, a standalone smart contract.
I've transferred 0.02 Ether to the smart contract, using MetaMask.
When I checked the smart contract's balance, it returns 200000000000000000, as expected.
If I try to use the transferEther function, however, and enter a number smaller than this - say, 0.005 ETH, or 50000000000000000 as the amount - it doesn't work using MetaMask.
When MetaMask prompts me it's never for that amount. It's for 0 ETH and 0.00322 gas fee (or whatever the gas is). Basically it always set the amount of ETH at 0 and only charges the fee.
Why can't I transfer an amount of ETH using this function in the Remix IDE with MetaMask?
pragma solidity ^0.8.0;
contract ReceivedEther {
function transferEther(address payable _recipient, uint _amount) external returns (bool) {
require(address(this).balance >= _amount, 'Not enough Ether in contract!');
_recipient.transfer(_amount);
return true;
}
/**
* #return contract balance
*/
function contractBalance() external view returns (uint) {
return address(this).balance;
}
}
Your code sends ETH (stated in the _amount variable) from the smart contract to the _recipient. So it doesn't require any ETH to be sent in order to execute the transferEther() function.
If you want your contract to accept ETH, the function that accepts it (or the general fallback() or receive() function) needs to be marked as payable.
Example:
pragma solidity ^0.8.0;
contract ReceivedEther {
receive() external payable {} // note the `payable` keyword
// rest of your implementation
}
Then you can send whathever amount of ETH to the smart contract address (without specifying any function to execute).
See more at https://docs.soliditylang.org/en/v0.8.5/contracts.html#receive-ether-function
If you want to prefill the amount in MetaMask from Remix IDE, you can use the "Value" input in the "Deploy & Run Transactions" tab.

Fetch pair price from Uniswap using smart contract function

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

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.