I want to charge a token value to perform a function within my contract.
I created the token in the mist, did the deploy on the private network and everything is running.
my code :
contract my_contract{
function my_function() public {
do_something;
// token other contract below
token.transferFrom(msg_sender, to_address, amount);
}
}
How exactly do I do this?
How do I instantiate a token in my contract?
Include the interface for the contract you want to call in your client contract and pass in the contract address when you deploy.
Example:
pragma solidity ^0.4.18;
contract ERC20 {
function allowance(address owner, address spender) public view returns (uint256);
function transferFrom(address from, address to, uint256 value) public returns (bool);
function approve(address spender, uint256 value) public returns (bool);
function totalSupply() public view returns (uint256);
function balanceOf(address who) public view returns (uint256);
function transfer(address to, uint256 value) public returns (bool);
event Transfer(address indexed from, address indexed to, uint256 value);
event Approval(address indexed owner, address indexed spender, uint256 value);
}
contract my_contract {
ERC20 _token;
function my_contract(address tokenAddress) public {
_token = ERC20(tokenAddress);
}
function my_function(address to, uint amount) public {
//do_something
_token.transferFrom(msg.sender, to, amount);
}
}
Related
Gas estimation errored with the following message (see below). The transaction execution will likely fail. Do you want to force sending?
Internal JSON-RPC error. { "code": -32000, "message": "execution reverted" }
I have implemented the safemoon contract but when I deployed the code in the BSC testnet it's given me an error.
contract SafemoonDemo is Context, IERC20, Ownable {
using SafeMath for uint256;
using Address for address;
mapping (address => uint256) private _rOwned;
mapping (address => uint256) private _tOwned;
mapping (address => mapping (address => uint256)) private _allowances;
mapping (address => bool) private _isExcludedFromFee;
mapping (address => bool) private _isExcluded;
address[] private _excluded;
uint256 private constant MAX = ~uint256(0);
uint256 private _tTotal = 1000 * 10**6 * 10**9;
uint256 private _rTotal = (MAX - (MAX % _tTotal));
uint256 private _tFeeTotal;
string private _name = "SafeMoonDemo";
string private _symbol = "SFD";
uint8 private _decimals = 9;
uint256 public _taxFee = 5;
uint256 private _previousTaxFee = _taxFee;
uint256 public _liquidityFee = 5;
uint256 private _previousLiquidityFee = _liquidityFee;
IUniswapV2Router02 public immutable uniswapV2Router;
address public immutable uniswapV2Pair;
bool inSwapAndLiquify;
bool public swapAndLiquifyEnabled = true;
uint256 public _maxTxAmount = 5000 * 10**6 * 10**9;
uint256 private numTokensSellToAddToLiquidity = 5000 * 10**6 * 10**9;
event MinTokensBeforeSwapUpdated(uint256 minTokensBeforeSwap);
event SwapAndLiquifyEnabledUpdated(bool enabled);
event SwapAndLiquify(
uint256 tokensSwapped,
uint256 ethReceived,
uint256 tokensIntoLiqudity
);
modifier lockTheSwap {
inSwapAndLiquify = true;
_;
inSwapAndLiquify = false;
}
constructor () public {
_rOwned[_msgSender()] = _rTotal;
IUniswapV2Router02 _uniswapV2Router = IUniswapV2Router02(0x10ED43C718714eb63d5aA57B78B54704E256024E);
// Create a uniswap pair for this new token
uniswapV2Pair = IUniswapV2Factory(_uniswapV2Router.factory())
.createPair(address(this), _uniswapV2Router.WETH());
// set the rest of the contract variables
uniswapV2Router = _uniswapV2Router;
//exclude owner and this contract from fee
_isExcludedFromFee[owner()] = true;
_isExcludedFromFee[address(this)] = true;
emit Transfer(address(0), _msgSender(), _tTotal);
}
function name() public view returns (string memory) {
return _name;
}
function symbol() public view returns (string memory) {
return _symbol;
}
function decimals() public view returns (uint8) {
return _decimals;
}
function totalSupply() public view override returns (uint256) {
return _tTotal;
}
function balanceOf(address account) public view override returns (uint256) {
if (_isExcluded[account]) return _tOwned[account];
return tokenFromReflection(_rOwned[account]);
}
function transfer(address recipient, uint256 amount) public override returns (bool) {
_transfer(_msgSender(), recipient, amount);
return true;
}
function allowance(address owner, address spender) public view override returns (uint256) {
return _allowances[owner][spender];
}
function approve(address spender, uint256 amount) public override returns (bool) {
_approve(_msgSender(), spender, amount);
return true;
}
function transferFrom(address sender, address recipient, uint256 amount) public override returns (bool) {
_transfer(sender, recipient, amount);
_approve(sender, _msgSender(), _allowances[sender][_msgSender()].sub(amount, "ERC20: transfer amount exceeds allowance"));
return true;
}
function increaseAllowance(address spender, uint256 addedValue) public virtual returns (bool) {
_approve(_msgSender(), spender, _allowances[_msgSender()][spender].add(addedValue));
return true;
}
Your constructor is trying to call the factory() function on the 0x10ED43C718714eb63d5aA57B78B54704E256024E address, expecting a 20-byte response that it would convert to an address type.
Even though there is the PancakeSwap router contract deployed on this address on the BSC mainnet, the BSC testnet doesn't hold any contract on this address.
So your constructor fails as it doesn't get the 20-byte response (from the non-contract address) that it expects.
The Pancakeswap docs page doesn't list any testnet addresses, so it's possible that there are no official Pancakeswap contracts on the testnet.
interface IPinkLock01 {
function totalLockCountForToken(address token) external view returns (uint256);
function getLocksForToken(address _token, uint256 start, uint256 end) external view returns (
uint256 id,
address token,
address owner,
uint256 amount,
uint256 lockDate,
uint256 unlockDate
);
}
function test() external view returns (address) {
address lockContract = 0x7ee058420e5937496F5a2096f04caA7721cF70cc;
address tokenPair = 0x689a3bfE8b61CBe2C8938EbDfb11FeEaE7358359;
uint256 _id;
address _token;
address _owner;
uint256 _amount;
uint256 _lockDate;
uint256 _unlockDate;
(_id, _token, _owner, _amount, _lockDate, _unlockDate) = IPinkLock01(lockContract).getLocksForToken(tokenPair,0,0);
return _owner;
}
The environment is on the bsc mainnet, I don't understand why it returns 0x000000000000000000000000000000000008805
I have problem on transfer USDT to Smart Contract .
Please help me.
pragma solidity ^0.6.0;
pragma experimental ABIEncoderV2;
interface IERC20 {
function totalSupply() external view returns (uint256);
function balanceOf(address account) external view returns (uint256);
function allowance(address owner, address spender)
external
view
returns (uint256);
function transfer(address recipient, uint256 amount)
external
returns (bool);
function approve(address spender, uint256 amount) external returns (bool);
function transferFrom(
address sender,
address recipient,
uint256 amount
) external returns (bool);
function increaseApproval(address spender, uint256 amount) external returns(bool);
function decreaseApproval(address spender, uint256 amount) external returns(bool);
event Transfer(address indexed from, address indexed to, uint256 value);
event Approval(
address indexed owner,
address indexed spender,
uint256 value
);
}
contract SimpleBank{
IERC20 tokenContract;
constructor(IERC20 _tokenContract) public {
tokenContract = _tokenContract;
}
function transfer(uint256 amount) external {
address sender = msg.sender;
address reciever = address(this);
uint256 balance = tokenContract.balanceOf(sender);
require(balance >= amount, "balance is low");
bool success1 = tokenContract.approve(sender , amount);
require(success1 , "error on approve");
bool success = tokenContract.transferFrom(sender, reciever, amount);
require(success, "transfer Error");
}
}
The approve() function on the tokenContract needs to be invoked from the token sender directly - not through another contract.
Your snippet effectively approves sender to spend SimpleBank's tokens, as SimpleBank is the msg.sender in the tokenContract.approve() function.
Here is my ERC20 implementation code. When I deploy to Ropsten testing network, I can't add token to my metamask by demonstrating token symbol automatically. I still can't send it to the other account in MEW wallet by error message. All of the function in contract follow the erc20 interface.
contract TWTD {
mapping (address => uint256) public ownerTobalance;
mapping (address => mapping (address => uint256)) public allowance;
event Approval(address indexed owner, address indexed spender, uint256 value);
event Transfer(address indexed from, address indexed to, uint256 value);
string TokenName;
string Symbol;
uint8 decimals = 18;
uint256 totalsupply;
using SafeMath for uint256;
function TWTD(string _name, string _symbol, uint256 _initialsupply) public {
ownerTobalance[msg.sender] = _initialsupply;
totalsupply = _initialsupply;
TokenName = _name;
Symbol = _symbol;
}
function totalSupply() public view returns (uint256){
return totalsupply;
}
function balanceOf(address who) public view returns (uint256) {
return ownerTobalance[who];
}
function _transfer(address from, address to, uint256 value) internal {
require ( to != 0x0);
require (ownerTobalance[from] >= value);
uint256 Balance = ownerTobalance[from] + ownerTobalance[to];
ownerTobalance[from].sub(value);
ownerTobalance[to].add(value);
assert(ownerTobalance[from] + ownerTobalance[to] == Balance);
emit Transfer(from,to,value);
}
function transfer(address to, uint256 value) public returns (bool) {
_transfer(msg.sender,to,value);
return true;
}
function transferFrom(address from, address to, uint256 value) public returns (bool) {
require (allowance[from][msg.sender] >= value);
allowance[from][msg.sender].sub(value);
_transfer(from,to,value);
return true;
}
function approve(address spender, uint256 value) public returns (bool) {
require(value > 0);
allowance[msg.sender][spender] = value;
emit Approval(msg.sender,spender,value);
return true;
}
function allowance(address owner, address spender)
public view returns (uint256) {
return allowance[owner][spender];
}
}
Error:
browser/IERC20.sol:4:53: Warning: This declaration shadows an existing
declaration.
function totalSupply() public constant returns (uint256 totalSupply);
browser/IERC20.sol:4:5: The shadowed declaration is here:
function totalSupply() public constant returns (uint256 totalSupply);
IERC20.sol
pragma solidity ^0.4.17;
interface IERC20 {
function totalSupply() public constant returns (uint256 totalSupply);
function balanceOf(address _owner) public constant returns (uint256 balance);
function transfer(address _to, uint256 _value) public returns (bool success);
function transferFrom(address _from, address _to, uint256 _value) public returns (bool success);
function approve(address _spender, uint256 _value) public returns (bool success);
function allowance(address _owner, address _spender) public constant returns (uint256 remaining);
event Transfer(address indexed _from, address indexed _to, uint256 _value);
event Approval(address indexed _owner, address indexed _spender, uint256 _value);
}
INDToken.sol
pragma solidity ^0.4.11;
import './IERC20.sol';
contract INDToken is IERC20 {
uint public constant _totalSupply = 1000000;
string public constant symbol = "IND";
string public constant name = "Indonesia Token";
uint8 public constant decimals = 3;
mapping(address => uint256) balances;
mapping(address => mapping(address => uint256)) allowed;
function INDToken() public {
balances[msg.sender] = _totalSupply;
}
function totalSupply() public constant returns (uint256 totalSupply){
return _totalSupply;
}
function balanceOf(address _owner) public constant returns (uint256 balance){
return balances[_owner];
}
function transfer(address _to, uint256 _value) public returns (bool success){
require(
balances[msg.sender] >= _value
&& _value > 0
);
balances[msg.sender] -= _value;
balances[_to] += _value;
Transfer(msg.sender, _to, _value);
return true;
}
function transferFrom(address _from, address _to, uint256 _value) public returns (bool success){
require(
allowed[_from][msg.sender] >= _value
&& balances[_from] >= _value
&& _value > 0
);
balances[_from] -= _value;
balances[_to] += _value;
allowed[_from][msg.sender] -= _value;
Transfer(_from, _to, _value);
return true;
}
function approve(address _spender, uint256 _value) public returns (bool success) {
allowed[msg.sender][_spender] = _value;
Approval(msg.sender, _spender, _value);
return true;
}
function allowance(address _owner, address _spender) public constant returns (uint256 remaining){
return allowed[_owner][_spender];
}
event Transfer(address indexed _from, address indexed _to, uint256 _value);
event Approval(address indexed _owner, address indexed _spender, uint256 _value);
}
I believe the issue is the reuse of the function name as the return value name. I would simply delete the name of the return value, since you're not using it anyway:
function totalSupply() public constant returns (uint256) {
As an alternative, you can name it something different:
function totalSupply() public constant returns (uint256 total) {