I am new to blockchain technology and trying to understand somethings(Etheruem, Truffle, DApps etc.) here and there from the internet.
From Ethereum official website,
Create a cryptocurrency contract in Ethereum
I am trying to specifically understand these functions
approve(address _spender, uint _value) returns (bool success) {...}
approveAndCall(address _spender, uint _value, bytes _extraData) returns (bool success) {...}
transferFrom(address _from, address _to, uint _value) returns (bool success) {...}
and lastly this function() {throw;}
This contract code
contract tokenRecipient {
function receiveApproval(address _from, uint _value, address _token, bytes _extraData);
}
and this declaration mapping (address => mapping (address => uint) ) public allowance;
First function basically takes address to where coin is to send and the value to be sent and return boolean value. Other function is for transfer value and takes sender and reciever address and returns again bookean value for success or failure.
I posted this question on Ethereum Stackchange and got the answer. Follow the comments also.
How does a token contract work?
Related
In the Remix, getICOMembers function works appropriately and returns all members of a specific ICO type after they are added to the icoMembers mapping.
mapping(uint256 => address[]) private icoMembers;
function getICOMembers(uint256 _icoType) external view onlyOwner returns (address[] memory)
{
return icoMembers[_icoType];
}
0 1
But Etherscan gives this error even if I connected by owner address.
deployer address owner address etherscan error
Also, this function has onlyOwner modifier too but it works.
ICOdata[] private ICOdatas;
function getICODatas() external view onlyOwner returns (ICOdata[] memory)
{
return ICOdatas;
}
getICODatas function output
I tried to add members by hardcoding instead of joining the token sale function, changing external to public but it still gives the same error.
Also, this is the constructor.
constructor(
address _token,
address payable _usdtWallet,
address _vestingContract
) {
require(
address(_token) != address(0),
"ERROR at Crowdsale constructor: Token contract address shouldn't be zero address."
);
require(
_usdtWallet != address(0),
"ERROR at Crowdsale constructor: USDT wallet address shouldn't be zero address."
);
require(
_vestingContract != address(0),
"ERROR at Crowdsale constructor: Vesting contract address shouldn't be zero address."
);
token = ERC20(_token);
usdtWallet = _usdtWallet;
totalAllocation = 0;
vestingContract = IVesting(_vestingContract);
transferOwnership(msg.sender);
}
A view function is usually invoked using a read-only call - not using a transaction.
Unlike transaction, a call is not signed by any private key. So you can specify any from address that is reflected as the value of msg.sender.
Then it depends on the specific implementation of the UI and of the node that processes the call. My guess is that Remix IDE uses the currently selected address from the accounts list as msg.sender (which passes the onlyOwner validation), while Etherscan uses the zero address (which fails the validation).
I created smart contract to transfer ETH from one account to another, ETH deduction occurs but send to other address(which is not specified). Please help me to resolve this problem.
//SPDX-License-Identifier:MIT
pragma solidity ^0.8.12;
contract ETH{
address public buyer;
uint public amt;
address public seller;
constructor(){
seller = msg.sender;
}
function add(address payable _buyer) payable public{
buyer = _buyer;
payable(buyer).transfer(amt);
}
function bal() view public returns(uint){
return buyer.balance;
}
function s() payable public returns(uint){
return msg.value;
}
}
The default value of amt is 0 and your code does not set this property (to a non-zero value) anywhere.
Which effectively makes your .transfer(amt) function to send 0 ETH to the buyer and keep all the ETH, that you sent along with the transaction, in the contract.
If you want to redirect the sent amount, there's the msg.value global variable, which reflects the current value sent with the transaction.
payable(buyer).transfer(msg.value);
I have deployed contract A.
Now I am creating gateway contract B and I want to send some tokens of contract A to user address X using owner address.
Worth to mention that contract A owner is the same as contract B.
I do the following
contract A is Ownable { // this one already deployed by owner
constructor() {
owner = msg.sender; // owner address is 0x123
approve(msg.sender, totalSupply); // let's approve all tokens for owner
}
function transferFrom(address from, address to, uint256 value) public returns (bool) {
require(value <= allowed[from][msg.sender], "Not allowed!");
// let's skip other logic
}
}
contract B is Ownable { // gateway contract will be deployed and executed by same owner
A contractA = ETC20(0x111);
address payable X = 0x333;
constructor() {
owner = msg.sender; // owner address is 0x123
}
function giveAwayTokens(uint256 value) {
contractA.transferFrom(owner, X, value);
}
}
When I execute "giveAwayTokens" function from owner address (0x123), I get error "Not allowed!".
So I am a bit confused right now, because the allowance of owner from owner is max supply.
Or maybe the msg.sender is contractB itself?
Please enlighten me what I am doing wrong here, thank you
When ContractB calls ContractA, the msg.sender in ContractA is ContractB address.
Based on the code and the error message, the owner (0x123) didn't allow ContractB to spend their tokens.
You need to set the value of allowed[<owner>][<ContractB>] to be at least the amount of tokens that you want to send.
Most likely you have an approve() function (defined in the token standard) that you can use. In the linked example, the caller of the function would be the owner, the spender would be the ContractB and the value would be any value equal or higher than the amount of tokens that you want to send (mind the decimals).
I have written smart contract for basic ICO using ERC223 token Standard.
When I verified the code in securify.ch(https://securify.chainsecurity.com/) it is showing some security errors. Here is the code where I am getting those errors
Code:
function transfer(address _to, uint _value, bytes _data) public {
uint codeLength;
assembly {
codeLength := extcodesize(_to)
}
balances[msg.sender] = balances[msg.sender].sub(_value);
balances[_to] = balances[_to].add(_value);
if(codeLength>0) {
ERC223ReceivingContract receiver = ERC223ReceivingContract(_to);
receiver.tokenFallback(msg.sender, _value, _data);
}
emit Transfer(msg.sender, _to, _value, _data);
}
In the below line, I am getting the errors.
receiver.tokenFallback(msg.sender, _value, _data);
Error : This contract allows an arbitrary user to execute untrusted code on behalf of the contract. This is because any user can submit an arbitrary address _spender and arbitrary data _data which are provided as argument to the method. The address spender determines the target of the call instruction, while _data determines the function called in the untrusted contract.
Considering below code
mapping (address => uint256) public balanceOf;
event Transfer(address indexed from, address indexed to, uint256 value);
function _transfer(address _from, address _to, uint _value) internal {
require(_to != 0x0);
require(balanceOf[_from] >= _value);
balanceOf[_from] -= _value;
balanceOf[_to] += _value;
Transfer(_from, _to, _value);
}
function sell(uint256 amount) public {
require(this.balance >= amount * sellPrice);
_transfer(msg.sender, this, amount);
msg.sender.transfer(amount * sellPrice);
}
function sendTokens(address _to, uint value) public {
_transfer(msg.sender,_to,value);
msg.sender.transfer(value * sellPrice);
}
function buy() payable public {
uint amount = (msg.value / buyPrice) * 1 wei;
_transfer(this, msg.sender, amount);
}
Here sell() and buy() function sends/receive tokens to/from contract but when function with almost same signature sendTokens() is called it is not able to send or are sending 0 tokens. Contract also has ethers and tokens. But it always consider tokens to send and receive. But sendTokens is not behaving same with almost same code.
Also I have an another question related to this. Every account has ethers and multiple tokens. Then how will it identify that here this specific tokens needs to be send or here ether is to be send and not any tokens.
Can anyone help. Thanks in advance.
Well there is no correction in the code. Perhaps Metamask and remix is not supporting this type of transaction at this moment. When I call this method from Web3js it worked. So couldn't find the exact reason but yes it worked at end.