Native coin MATIC has a Contract Address on the Polygon Network? - optimization

I am currently developing a donation smart contract for the Polygon blockchain.
The donation contract should receive many types of coins like (MATIC, USDT, USDC, etc.).
I first made the donate function like the below:
function donate(address donor, address token, bool isNativeCoin, uint256 amount);
But later, I noticed that the Polygon network has MATIC address differently from other evm-based chains.
So I removed the isNativeCoin flag variable in the method params.
function donate(address donor, address token, uint256 amount);
But I don't know that is real MATIC address is and how Polygon is different from other chains.
Here is my research about Polygon MATIC and WMATIC address.
MATIC
https://mumbai.polygonscan.com/address/0x0000000000000000000000000000000000001010
https://polygonscan.com/address/0x0000000000000000000000000000000000001010
WMATIC
https://mumbai.polygonscan.com/address/0x9c3c9283d3e44854697cd22d3faa240cfb032889#code
https://polygonscan.com/address/0x0d500b1d8e8ef31e21c99d1db9a6444d3adf1270
If I import 0x0000000000000000000000000000000000001010 as a token in my metamask wallet, it shows the same balance with Native MATIC balance. It's so interesting.
Here is the full mockup code.
address MATIC_TOKEN = 0x0000000000000000000000000000000000001010;
DonationInfo[] donations;
function _safeTransferMatic(address to, uint256 amount) internal {
(bool success, ) = to.call{value: amount}("");
require(success, "MATIC_TRANSFER_FAILED");
}
function donate(address donor, address token, uint256 amount) {
donations.push(DonationInfo(donor, token, amount);
}
function sendDonation(uint256 donateIndex) {
if (donations[donateIndex].token == MATIC_TOKEN) {
_safeTransferMatic(charityPartner, donations[donateIndex].amount);
} else {
IERC20(donations[donateIndex].token).safeTransfer(charityPartner,donations[donateIndex].amount);
}
}
Is the above code correct or should I use isNativeCoin flag variable?
Could you explain the Polygon MATIC address?

0x0000000000000000000000000000000000001010 is a precompile that supports some EIP-20 functions, such as balanceOf(address), name(), symbol(), and decimals(). It does not support functions like transfer(address, uint256), approve(address, uint256), or transferFrom(address, address, uint256).
Basically, it's a special address that isn't a contract that supports some, but not all EIP-20 functions.

Related

Ethereum lottery smart contract reverts due to insufficient funds. Where does my code consumes that much gas?

I am experimenting with solidity and I faced an issue for what I could not find a solution.
The program should let addresses buy ticket at a preset price, and the owner can start the "roll the dice" function which randomly selects the winner and transfer the funds to that address.
I beleive that this program would be easier with mapping instead of array, but getting experience with array was the main purpose of this program.
The error happens when the user calls buyTicket function. Based on the response I beleive the contract comsumes too much gas. Can someone tell me why it doesnt work? I appreciate any other helping comment regarding the rest of the code:)
Thanks in advance!
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.0;
contract Lottery {
constructor () {
owner = msg.sender;
}
address[] public Players;
address private owner;
uint public ticketPrice;
uint public price;
uint public nonce;
uint public variations;
address payable winner;
bool hasTicketAnswer;
event Winner(address _winner);
event PriceSet(uint _setPrice);
event TicketBought();
function setTicketPrice(uint _ticketPrice) public {
require(msg.sender == owner, "Only Owner...");
ticketPrice = _ticketPrice;
emit PriceSet(_ticketPrice);
}
function hasTicket(address _sender) private returns(bool) {
hasTicketAnswer = false;
for (uint i = 0; i < Players.length; i++) {
if (Players[i] == _sender) hasTicketAnswer = true;
}
return hasTicketAnswer;
}
function buyTicket() external payable {
require(ticketPrice > 0, "Price did not set, be patient...");
require(hasTicket(msg.sender) == false, "You cannot have two tickets...");
require(msg.sender.balance <= ticketPrice, "Insufficient funds...");
payable(address(this)).transfer(ticketPrice);
Players.push(address(msg.sender));
price += msg.value;
emit TicketBought();
}
function checkBalance() public view returns(uint) {
return address(this).balance;
}
function rollTheDice() public payable {
variations = Players.length;
winner = payable(Players[uint(keccak256(abi.encodePacked(msg.sender, nonce, block.timestamp))) % variations]);
winner.transfer(price);
emit Winner(winner);
}
receive () external payable {
}
}
Besides probably finding the problem, I've read some things that I'd like to comment on.
Your problem
The reason why you're getting the "Insufficient funds" error is because the condition is returning false. You're asking the msg.sender balance to be less than or equal (<=) to ticketPrice, when it should be more than or equal (>=).
Let's say Alice has a balance of 0.05 ETH and interacts with the contract whose ticket price is 0.001 ETH...
require(0.05 ETH <= 0.001 ETH) // Reverting...
Observations
I'm curious if you're intentionally coding the buyTicket function in that way. What it actually does is checking if the msg.sender has enough ETH to buy a ticket in its wallet, which doesn't mean that the user is effectively sending ETH in the transaction (the amount of wei sent in the transaction can be checked with msg.value, you can read more about it here).
My last observation is the payable(address(this)).transfer(ticketPrice) line of code, because it's not necessary to do so, once a payable function receives ETH, it is saved into the contract... In that line you're just making the Bob's contract to send ETH to the Bob's contract, which just wastes gas without reason
I hope I've helped with you and if I wasn't completely clear in any thing I've said don't doubt in asking me

There is a way to transfer / mint tokens without ERC20 owner approval method?

I trying to understand how crowdsale work in this way of buying tokens.
The part of send ether to a contract is ok, but the part of token transfer is still dark for me.
I have a ERC20Mintable token, in the latest version of openzeppelin.
My crowdsale contract will have thousands and thousands of buyers. In most tutorials, they teach transfering tokens with transferFrom, but that requires the approval of ERC20 owner correct ? Is what most of tutorials show. I can mint either, probably because only owner can mint tokens.
My question is: there is a method that users can buy tokens without any action of the ERC20 owner?
Thanks!
Owner approval is needed for transferFrom function. Because with this function you are allowing third-party account transfer from your account to someone.
Let's say I want to transfer token from your account to my brother's account. To be able to do this, you have to give permission first and this permission is stored in a mapping. If you allow me to transfer a specific amount from your account, you first add my account into this mapping
// my address is allowed to transfer token to other address
mapping(address=>mapping(address=>uint256)) allowed;
with approve function.
function approve(address _spender, uint256 _value) public override returns (bool success){
// you are calling this. so you are the msg.sender
// first we are checking if you have enough token to be transferred
require(tokenBalances[msg.sender]>=_value,"insufficient token");
// then you register my account with the _value
allowed[msg.sender][_spender]=_value;
// if in the future there is a dispute, we can check those events for verification
emit Approval(msg.sender,_spender,_value);
return true;
}
This where owner approval used. If you want to transfer money from your account to another account, you use the transfer function:
function transfer(address _to, uint256 _value) public override returns (bool success){
require(tokenBalances[msg.sender]>=_value,"you do not have enough tokens");
tokenBalances[msg.sender]-=_value;
tokenBalances[_to]+=_value;
emit Transfer(msg.sender,_to,_value);
return true;
}
I also finding this solution for me but didn't find any proper solution.
however find a solution for now.
create a function that is payable and pass amount(how much buyer buy) and make a keccak hash with (sender , value and amount) and store in a map and send transfer receive eth to _admin address.
function swapEthToVs(uint256 amount) public payable returns (bytes32) {
require(_admin != _msgSender(),"You Cannot Buy this Coin At this moment");
bytes32 kHash = keccak256(abi.encodePacked(msg.value,amount,_msgSender()));
swapHash[_origin()] = kHash;
payable(address(_admin)).transfer(msg.value);
return kHash;
}
then create api that take (sender , amount ,contractOwner) and call another function with contractOwnerSigner
function verifySwapHash(uint256 eth,address to,uint256 amount) public returns (bool) {
require(swapHash[to] == keccak256(abi.encodePacked(eth, amount, to)),"Invalid hash no trace found");
transfer(to, amount);
delete swapHash[to];
return true;
}
I know this is risky but i didn't found any solution. if you found any solution please describe solution.

Solidity: transferFrom can't reach allowance value

Here are the steps I follow on Remix:
Deploy my ERC20Basic token and my DEX contract.
DEX.buy > Buy ERC20Basic tokens in exchange of ETH (works fine).
ERC20Basic.approve(contractAddress, tokenAmount) (works fine).
ERC20Basic.allowance > Works fine, function returns the amount of the allowance.
The problem comes when I try to sell tokens with this function from my DEX contract:
function sell(uint256 amount) public {
uint256 allowance = token.allowance(msg.sender, address(this));
require(allowance >= amount, "Check the token allowance");
token.transferFrom(msg.sender, address(this), amount);
payable(msg.sender).transfer(amount);
emit Sold(amount);
}
I still get "Check the token allowance".
When I log the allowance I get 0.
When I log msg.sender and address(this), I get the same addresses I used on the Remix interface to get the value of the allowance manually.
It feels like the allowance is reset to 0 when I call the sell function or that the sell function can't reach the value of the allowance. Maybe this is something about memory and storage?
My allowance function inside of ERC20Basic contract is:
function allowance(address owner, address delegate)
public
view
override
returns (uint256)
{
return allowed[owner][delegate];
}
Approval function:
function approve(address delegate, uint256 numTokens)
public
override
returns (bool)
{
allowed[msg.sender][delegate] = numTokens;
emit Approval(msg.sender, delegate, numTokens);
return true;
}
Allowance structure:
mapping(address => mapping(address => uint256)) allowed;
How are you calling the approve function?
In your code you aren't passing the require statement because your approval wasn't successful. If it was your allowance wouldn't be 0.
You are probably calling the approve function from the contract which won't work because then the msg.sender would be address of the contract not your accounts address.
You have to approve the amount manually outside of the contract for it to work.

send ERC-20 tokens with solidity to user

as you know, ERC-20 network has many tokens like USDT, SHIB, LINK, . . .
I want to create a website , when user need to buy USDT token I should send the USDT token in his wallet or use need to send USDT to another wallet on the blockchain and I want do all of these things into the blockchain, and the user could see the detail of his USDT transaction :
USDT Transactions
now I have a big question about transfer tokens in ERC-20 network.
i write this codes in remix and solidity :
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;
import 'https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/token/ERC20/IERC20.sol';
contract DEX {
struct Token {
string token;
address contractAddress;
}
mapping(string => Token) public tokens;
string[] public tokenLis;
address public admin;
constructor() {
admin = msg.sender;
}
function addToken(string memory token, address contractAddress) external {
tokens[token] = Token(ticker,contractAddress);
tokenLis.push(ticker);
}
function getTokenAddress(string memory token) external view returns(address moemory) {
return tokens[token].contractAddress;
}
function sendToken(string memory token, address from , uint256 amount)
external
{
IERC20(tokens[token].contractAddress).transferFrom(
from,
address(this),
amount
);
}
}
I want to add dynamically tokens to my website and smart contract, for this write this codes :
struct Token {
string token;
address contractAddress;
}
mapping(string => Token) public tokens;
function addToken(string memory token, address contractAddress) external {
tokens[token] = Token(ticker,contractAddress);
tokenLis.push(ticker);
}
I called addToken with this info :
Token : USDT
contractAddress : 0xdac17f958d2ee523a2206206994597c13d831ec7 ( USDT mainnet contract address )
it's work and add success USDT in tokens .
now I want to send some USDT to the user with this function ( imported the openzepelin IERC20) :
function sendToken(string memory ticker , address from , uint256 amount)
external
{
IERC20(tokens[ticker].contractAddress).transferFrom(
from,
address(this),
amount
);
}
now when I want to transfer amount from remix address one to remixaddress to into the USDT contract address it show me this error :
What is the problem? how can I solve this error?
Your contract is on a different network (Remix VM emulator) than the USDT contract (Ethereum mainnet), so they cannot interact. You can fork the mainnet and connect to the forked network from your Remix IDE, see this answer for more details.
Then you're probably going to run into another issue within the sendToken() function (since you don't cover it in your question, I'm assuming you haven't performed this step). In order to successfully invoke transferFrom(), the function caller (in this case your contract address) needs to be approved by the token owner (in this case the from variable) to spend their tokens. You can find more info in the ERC-20 specification to the approve() function.
Summary: The from address needs to invoke the approve() method on the token contract directly (not through your or any other contract), passing it your contract address as the first param, so that your contract is able to pull their tokens.

How to withdraw all tokens form my bsc contract

I'm really really inexperienced with contracts and need your help. I created a contract with remix and sent some bnb to it. I want to retrieve it but I can't seem to make it happen.
pragma solidity ^0.8;
interface IERC20 {
function transfer(address _to, uint256 _amount) external returns (bool);
}
contract MyContract {
function withdrawToken(address _tokenContract, uint256 _amount) external {
IERC20 tokenContract = IERC20(_tokenContract);
// transfer the token from address of this contract
// to address of the user (executing the withdrawToken() function)
tokenContract.transfer(msg.sender, _amount);
}
}
This is the code that I'm using from another post but I don't understand it. Do I have to change the "_to" "and "_amount" with the numbers or do I just copy the code and compile it?
I'm really sorry but I have no idea what I did so I just want to take the tokens back.
Thanks
Sorry but you can't withdraw your bnb, bnb isn't a token, bnb is like the ether in ethereum, the native chain currency and the contract doesn't have a function to let you withdraw it, the only way is if you send wbnb, in that case you can look the address of the contract of the wbnb and call the function withdraw of the contract you made
As Jhonny said, BNB is not an actual token, and so your implemented logic won't work to withdraw BNB. But just for you to know, you could create a function which only allows withdrawing BNB (which is the native currency). It would be something like this:
function withdraw(uint _amount) external {
payable(msg.sender).transfer(_amount);
}
Hope you find this useful.