Not able to verify and publish contract on BSC Scan - ethereum

I'm trying to to verify and publish a contract on BSC Scan testnet.
I'm using Open Zepellin and Remix - ETH IDE, however I'm getting the following error:
not found: File import callback not supported
I believe the same issue is true if I try verifying it on Etherscan.
what am I doing wrong?
Contract Link
This is the code I pasted on BSC Scan to verify and publish it.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "#openzeppelin/contracts/token/ERC20/ERC20.sol";
import "#openzeppelin/contracts/access/Ownable.sol";
contract Presidente is ERC20, Ownable {
constructor() ERC20("Presidente", "PRES") {
_mint(msg.sender, 1000000 * 10 ** decimals());
}
function mint(address to, uint256 amount) public onlyOwner {
_mint(to, amount);
}
}
this is the full error I get

i see that you are trying to verify while the contract you have needs to be flattened,
here is what you need to do:
Go to remix.ethereum.org and create a new file token.sol
copy/paste your code in it and save it
go to the last tab plugin manager and look for FLATTENER install it
FLATTENER will show as a tab click on it and press on Flatten token.sol
press on Save as token_flat.sol
go back to the first tab and you'll find the new file
remove all of the extra license showen "// SPDX-License-Identifier: MIT"
move the second part to become the theird part
pragma solidity ^0.8.0;
/**
#dev Interface for the optional metadata functions from the ERC20 standard.
Available since v4.1.
/
interface IERC20Metadata is IERC20 {
/*
#dev Returns the name of the token.
*/
function name() external view returns (string memory);
/**
#dev Returns the symbol of the token.
*/
function symbol() external view returns (string memory);
/**
#dev Returns the decimals places of the token.
*/
function decimals() external view returns (uint8);
}
// File: #openzeppelin/contracts/token/ERC20/IERC20.sol
after this part
pragma solidity ^0.8.0;
/**
* #dev Interface of the ERC20 standard as defined in the EIP.
*/
interface IERC20 {
/**
* #dev Returns the amount of tokens in existence.
*/
function totalSupply() external view returns (uint256);
/**
* #dev Returns the amount of tokens owned by `account`.
*/
function balanceOf(address account) external view returns (uint256);
/**
* #dev Moves `amount` tokens from the caller's account to `recipient`.
*
* Returns a boolean value indicating whether the operation succeeded.
*
* Emits a {Transfer} event.
*/
function transfer(address recipient, uint256 amount) external returns (bool);
/**
* #dev Returns the remaining number of tokens that `spender` will be
* allowed to spend on behalf of `owner` through {transferFrom}. This is
* zero by default.
*
* This value changes when {approve} or {transferFrom} are called.
*/
function allowance(address owner, address spender) external view returns (uint256);
/**
* #dev Sets `amount` as the allowance of `spender` over the caller's tokens.
*
* Returns a boolean value indicating whether the operation succeeded.
*
* IMPORTANT: Beware that changing an allowance with this method brings the risk
* that someone may use both the old and the new allowance by unfortunate
* transaction ordering. One possible solution to mitigate this race
* condition is to first reduce the spender's allowance to 0 and set the
* desired value afterwards:
* https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729
*
* Emits an {Approval} event.
*/
function approve(address spender, uint256 amount) external returns (bool);
/**
* #dev Moves `amount` tokens from `sender` to `recipient` using the
* allowance mechanism. `amount` is then deducted from the caller's
* allowance.
*
* Returns a boolean value indicating whether the operation succeeded.
*
* Emits a {Transfer} event.
*/
function transferFrom(
address sender,
address recipient,
uint256 amount
) external returns (bool);
/**
* #dev Emitted when `value` tokens are moved from one account (`from`) to
* another (`to`).
*
* Note that `value` may be zero.
*/
event Transfer(address indexed from, address indexed to, uint256 value);
/**
* #dev Emitted when the allowance of a `spender` for an `owner` is set by
* a call to {approve}. `value` is the new allowance.
*/
event Approval(address indexed owner, address indexed spender, uint256 value);
}
// File: #openzeppelin/contracts/token/ERC20/ERC20.sol
compile and you will be good to go

Related

Remix error message 'Unused function parameter' with fulfillRandomness using Chainlink VRF Smart Contract

The issue is a repeated error message upon attempting to compile a verifiable random number smart contract in remix. This particular function (line 48 in remix or last line of code below) yields the error message consistently across compiler versions (I checked the imported documents versions too), cross brave browser+firefox, and even after checking grammar. I do not understand what this error message is conveying (I am brand new to programming) and I cannot find another thread with this same issue. I suspect the function to be faulty in some sense of how it is either defined or interacting with the other imported documents (VRFConsumerBase.sol).
//SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "#chainlink/contracts/src/v0.8/VRFConsumerBase.sol";
/**
* Request testnet LINK and ETH here: https://faucets.chain.link/
* Find information on LINK Token Contracts and get the latest ETH and LINK faucets here:
* https://docs.chain.link/docs/link-token-contracts/
*/
contract RandomNumberConsumer is VRFConsumerBase {
bytes32 public keyHash;
uint256 public fee;
uint256 public randomResult;
/**
* Constructor inherits VRFConsumerBase
*
* Network: Rinkeby
* Chainlink VRF Coordinator address: 0xb3dCcb4Cf7a26f6cf6B120Cf5A73875B7BBc655B
* LINK token address: 0x01BE23585060835E02B77ef475b0Cc51aA1e0709
* Key Hash: 0x2ed0feb3e7fd2022120aa84fab1945545a9f2ffc9076fd6156fa96eaff4c1311
*/
constructor() VRFConsumerBase(
0xb3dCcb4Cf7a26f6cf6B120Cf5A73875B7BBc655B, //VRF Coordinator
0x01BE23585060835E02B77ef475b0Cc51aA1e0709) //Rinkeby LINK Token
{
keyHash = 0x2ed0feb3e7fd2022120aa84fab1945545a9f2ffc9076fd6156fa96eaff4c1311;
fee = 0.1 * 10 ** 18; //0.1 LINK (varies by network)
}
/**
* Requests fulfillRandomness
*/
function getRandomNumber() public returns (bytes32 requestId) {
require(LINK.balanceOf(address(this)) >= fee, "Not enough LINK - fill contract with faucet");
return requestRandomness(keyHash, fee);
}
/**
* Callback function used by VRF Coordinator
*/
function fulfillRandomness(bytes32 requestId, uint256 randomness) internal override {
randomResult = randomness;
}
}
This near identical code can be found at https://docs.chain.link/docs/get-a-random-number/ (line 52).
Here is the error message below,
from solidity:
Warning: Unused function parameter. Remove or comment out the variable name to silence this warning.
--> docs.chain.link/samples/VRF/RandomNumberConsumer.sol:52:32: function fulfillRandomness(bytes32 requestId, uint256 randomness) internal override {
I am following the 'Getting A Random Number with Chainlink VRF' tutorial from Chainlink
https://www.youtube.com/watch?v=JqZWariqh5s (timestamp # 9:30) except I am testing on the Rinkeby network not Kovan. I also double checked with this tutorial.
https://www.youtube.com/watch?v=_aXumgdpnPU (timestamp # 12:00)
Neither tutorial encounters this specific error message and both are able to compile and deploy the smart contract successfully. I also receive the error message when I directly open and attempt to compile the smart contract from the Chainlink website without touching the code.
i had the same error i fixed it by leaving the bytes32 parameter empty
your code
(bytes32 requestId , uint256 randomness)
instead use:
(bytes32, uint256 randomness)

How do you transfer an ERC-721 token using an impersonated address on an Ethereum mainnet fork?

I'm writing a contract that involves transferring an ERC-721 token from one user to another. In order to test that this works with existing NFT collections, I'm using ganache-cli to fork mainnet and impersonate the holder of the ERC-721 token in question. I've confirmed on Etherscan that the address I'm unlocking is indeed the holder of the ERC-721 token that I'm trying to transfer.
First, I'm forking mainnet using ganache-cli:
ganache-cli -f <INFURA_MAINNET_ENDPOINT> -d -i 66 1 --unlock <HOLDER_ADDRESS>
My smart contract code includes:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
interface IERC721 {
function ownerOf(uint256 _tokenId) external returns (address);
function safeTransferFrom(address _from, address _to, uint256 _tokenId) external payable;
}
interface CryptopunkInterface {
function transferPunk(address _to, uint _tokenId) external;
}
and contains this function:
/// #dev Sells NFT into a bid (i.e., "hits" the bid)
/// #param _bidderAddress Address of the bidder
/// #param _nftAddress Address of collection to which the bid applies
/// #param _tokenId Token id of the NFT in question
/// #param _expectedWeiPriceEach Price (in wei) that seller expects to receive for each NFT
/// #return Proceeds remitted to seller
function hitBid(address _bidderAddress, address _nftAddress, uint256 _tokenId, uint256 _expectedWeiPriceEach) public returns (uint256) {
console.log("msg.sender of hitBid: ", msg.sender);
// Initialize bid
Bid memory bid = bids[_bidderAddress][_nftAddress];
// Require that bid exists
require(bid.quantity > 0, "This bid does not exist.");
// Require that bid amount is at least what the seller expects
require(bid.weiPriceEach >= _expectedWeiPriceEach, "Bid is insufficient.");
// Decrement bidder's bid quantity for this collection
bids[_bidderAddress][_nftAddress].quantity = bid.quantity - 1;
// Compute platform fee proceeds
uint256 platformFeeProceeds = bid.weiPriceEach * platformFee / 10000;
// Remit platform fee proceeds to owner
sendValue(OWNER, platformFeeProceeds);
// Transfer NFT to bidder
// Check whether _nftAddress is Cryptopunks address
if (_nftAddress == 0xb47e3cd837dDF8e4c57F05d70Ab865de6e193BBB) {
CryptopunkInterface(_nftAddress).transferPunk(_bidderAddress, _tokenId);
} else {
console.log("ownerOf NFT being sold: ", IERC721(_nftAddress).ownerOf(_tokenId));
IERC721(_nftAddress).safeTransferFrom(msg.sender, _bidderAddress, _tokenId);
}
// Compute seller proceeds
uint256 sellerProceeds = bid.weiPriceEach - platformFeeProceeds;
// Remit seller proceeds to seller
sendValue(payable(msg.sender), sellerProceeds);
// Emit new trade event
emit NewTrade(_bidderAddress, msg.sender, _nftAddress, bid.weiPriceEach, 1, _tokenId);
// Return seller proceeds
return sellerProceeds;
}
When I run truffle test, executing the function on behalf of the unlocked holder address, I get this error:
Error: Returned error: VM Exception while processing transaction: revert ERC721: transfer caller is not owner nor approved -- Reason given: ERC721: transfer caller is not owner nor approved.
UPDATE:
I switched from using ganache-cli to using Hardhat to fork mainnet. I'm impersonating the relevant addresses in my test.js file:
const BAYC_HOLDER_ADDRESS = "0x54BE3a794282C030b15E43aE2bB182E14c409C5e";
await hre.network.provider.request({
method: "hardhat_impersonateAccount",
params: [BAYC_HOLDER_ADDRESS],
});
I've also verified that msg.sender of hitBid is indeed the ownerOf the NFT in question with the console.log statements above.
msg.sender of hitBid: 0x54be3a794282c030b15e43ae2bb182e14c409c5e
ownerOf NFT being sold: 0x54be3a794282c030b15e43ae2bb182e14c409c5e
Nonetheless, I'm still getting the same error:
Error: VM Exception while processing transaction: reverted with reason string 'ERC721: transfer caller is not owner nor approved'
The reason you are getting this error is because msg.sender of hitBid() is not the same as msg.sender of IERC721(_nftAddress).safeTransferFrom().
The seller needs to sign two transactions:
The user needs to sign approve(yourContractAddress, tokenId)
The user needs to sign hitBid()
That will prevent hitBid() from reverting, since your contract address (msg.sender of safeTransferFrom()) will then be approved to make the transfer.

Managing gas fees in solidity smart contract

I have a ERC20 smart contract with edited transfer function
function transfer(address recipient, uint256 amount) public virtual override returns (bool) {
if(_transactionMaxValue > 0){
require(_transactionMaxValue >= amount, "You can not transfer more than 1000000 tokens at once!");
}
transfer(recipient, amount);
}
I have added if statement in transfer function, in case user indicates limit per single transaction before deploying. I was wondering if this would affect gas fees when transferring tokens, in order to decide weather to leave the "universal" ERC20 smart contract template with indictable transaction limit or compose new one, with no if statement, if no transaction limit was indicated.
I was wondering if this would affect gas fees when transferring tokens
Adding the (if and require) conditions increases the total amount of gas used, but the increase is small in the context of gas usage of other operations of the parent transfer() function. It's because in the overriding function, you're performing "just" one more storage read and few other operations in memory.
Execution of your transfer() function costs 54,620 gas (including the parent function; assuming the require() condition doesn't fail). While execution of just the parent transfer() function costs 52,320 gas.
You need to use super.transfer(recipient, amount); if you want to invoke the parent transfer() function. Without the super keyword, you'd lock the script in an infinite recursion, always invoking itself.
Also, in order to correctly override the parent function, you need to state the override modifier (and virtual if you are planning to override this function as well) before the public visibility modifier, and to return the value as declared.
pragma solidity ^0.8.0;
import "#openzeppelin/contracts/token/ERC20/ERC20.sol";
contract MyToken is ERC20 {
uint256 _transactionMaxValue = 1000000;
constructor() ERC20("MyToken", "MyT") {
_mint(msg.sender, 1000000);
}
// moved the `override virtual` before the `public` modifier
function transfer(address recipient, uint256 amount) override virtual public returns (bool) {
if(_transactionMaxValue > 0){
require(_transactionMaxValue >= amount, "You can not transfer more than 1000000 tokens at once!");
}
// added `return super.`
return super.transfer(recipient, amount);
}
}
Everything #Petr Hejda suggested is correct.
Additional improvement here would be to make a require statement fail reason string smaller and more precise, as it will result in the smaller bytecode, and therefore cheaper deployment.

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.

Cannot mint ERC223 token

Hello I have a simple implementation for a ERC223 Token. I use this repository: ERC223-token-standard
Here is my contract Code:
pragma solidity ^0.5.8;
import "./ERC223-token-standard-development/token/ERC223/ERC223Burnable.sol";
import "./ERC223-token-standard-development/token/ERC223/ERC223Mintable.sol";
contract MyToken is ERC223Burnable, ERC223Mintable{
}
The first strange Thing is that I get this warning when compiling:
Warning: Function state mutability can be restricted to view
function mint(address account, uint256 amount) public onlyMinter returns (bool) {
^ (Relevant source part starts here and spans across multiple lines).
I think this is weird because to my understanding it changes the state of the contract and should not be a view function
However after creating my contract and using the mint function the totalSupply is still 0 even though isMinter(myaddr) returns true. I thought that I was just doing something wrong in the truffle testfile but after I pushed my code into Remix I still get the same warning and the total supply is still 0.
I hope you can help me with this issue.
thanks for your question, it looks like a bug in your _mint function:
function mint(address account, uint256 amount) public onlyMinter returns (bool) {
_totalSupply.add(amount);
balances[msg.sender].add(amount);
return true;
}
Let's see how add method of SafeMath lib works:
function add(uint256 a, uint256 b) internal pure returns (uint256) {
uint256 c = a + b;
require(c >= a, "SafeMath: addition overflow");
return c;
}
First of all, its pure function, which doesn't modify the state of contract. It just returns amount of addition. So, it's expected behavior for your code ‘cause you don't save a result
But if you take a look at open-zeppelin's implementation of _mint method, they change contract's state
function _mint(address account, uint256 amount) internal {
require(account != address(0), "ERC20: mint to the zero address");
_totalSupply = _totalSupply.add(amount);
_balances[account] = _balances[account].add(amount);
emit Transfer(address(0), account, amount);
}
I believe it's better to use OpenZeppelin's contracts for your development 'cause they have a lot of auditors
P.S. Also you don't use address account variable in _mint() and always add amount to msg.sender (yourself) in your implementation