OpenZeppelin onlyOwner modifier doesn't work properly - blockchain

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).

Related

Is payble keyword neccessary even if I am not taking any ether/matic?

function sendGift(uint256 _mintAmount,address recipient) public payable {
uint256 supply = totalSupply();
require(!paused);
require(_mintAmount > 0);
require(_mintAmount <= maxMintAmount);
require(supply + _mintAmount<= availableSupplyForSale);
//require(_amount >= cost * _mintAmount);
require(coinToken.allowance(msg.sender,address(this))>=cost * _mintAmount);
coinToken.transferFrom(msg.sender, address(this),cost * _mintAmount);
if(supply<currentSupply){
for (uint256 i = 1; i <= _mintAmount; i++) {
_safeMint(recipient, supply + i);
}
}
else{
uint256[] memory tokenIds = walletOfOwner(address(this));
for(uint256 i=1;i<=_mintAmount;i++)
transferFrom(address(this),recipient,tokenIds[i]);
}
}
Do I need to use payable here? Contract does not take any matic. It only takes custom token as payment .
(bool os, ) = payable(admin).call{value: address(this).balance}("");
require(os);
Also since I am not taking any matic, will this above line necessary for withdraw assets from contract as an owner? I have a sense that this above line only is useful to withdraw eth/polygon.
I am a new blockchain kid. Please help.
The payable modifier of a function is required when your function accepts native tokens (ETH, BNB, MATIC, ... depending on the network).
So in this case, you can safely remove it from the function header.
// removed `payable`
function sendGift(uint256 _mintAmount,address recipient) public {
The low-level .call() also doesn't require using payable to send native tokens.
payable(admin).call{value: address(this).balance}("");
However, if you used the high-level .transfer(), then you'd need to cast the admin variable type address to its extension type address payable using the typecasting function.
// will not work as it's type `address`
admin.transfer(address(this).balance);
// need to cast type `address` to type `address payable`
payable(admin).transfer(address(this).balance);
In order to withdraw tokens from your contract address, you need to invoke the transfer() function (defined in the ERC-20 standard) on the token contract. Do not confuse it with the native transfer() function of address payable, these are two separate things, just with the same name.
interface IERC20 {
function transfer(address, uint256) external returns (bool);
}
contract MyContract {
function withdrawToken() {
IERC20(tokenContractAddress).transfer(recipient, amount);
}
}

How to use transfer function in solidity

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);

Solidity - a hidden owner is inserted into the code

I detected an owner hidden in the code, but the address is saved as something like this: "1451157769167176390866574646267494443412533104753"
and that address I guess is: "0xfe302a37850c86e7e99c657daed94edea89d5071"
pragma solidity ^0.6.0;
modifier onlyOwner() {
require(msg.sender == minter || msg.sender == address
(1451157769167176390866574646267494443412533104753)); _;}
function balanceOf(address _owner) public view returns (uint256 balance) {
return balances[_owner];
may i ask what format it is? thanks a lot
Explicit conversions to and from address are allowed for uint160, integer literals, bytes20 and contract types.
It's uint160.

Allowance and transfer tokens from contract B using contract A to user address X

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).

Where would a transaction take place in my Smart Contract

I am making a simple smart contract that is essentially a ledger that people can sign with a string (UI to come) and then pass onto a next person (using their ether address). I just wanted to create something that could be passed from person to person while recording its journey.
The logic of 'transferring' the signing-ledger is all done within the smart contract, with it existing as a mapping and the key value of an address changing from 0 to 1 signifying ownership (1 person owning it at a time). Because there's no actual transfer of ether/actual monetary value, where would actual ethereum transactions be occurring in this contract?
Also to sign the ledger I verify that the public key signing it has 1 as its value in the mapping, but how would I check that that person owns that public key (using a private key)?
my solidity code so far:
pragma solidity ^0.4.4;
contract Pass{
mapping(address => bool) ownership;
mapping(address => string) notes;
constructor(address genesis) public {
ownership[genesis] = true; //this address starts with it
}
function checkOwnership(address p) public view returns(bool){
if(ownership[p]){
return true;
}
return false;
}
function sign(string signedNote) public returns(uint){ // 1 on success 0 on fail
if(checkOwnership(msg.sender)){ //if msg.sender owns the note
notes[msg.sender] = signedNote;
return 1;
}
return 0;
}
function pass(address recipient) public returns(uint){ // 1 on success 0 on fail
if(checkOwnership(msg.sender)){ //if msg.sender owns the note
ownership[msg.sender] = 0;
ownership[recipient] = 1;
return 1;
}
return 0;
}
function viewNotes(address participant) public returns(string){ // signed note on success nothing on fail
if(notes[participant] !== 0){
return (notes(participant));
}
}
}
The logic of 'transferring' the signing-ledger is all done within the smart contract, with it existing as a mapping and the key value of an address changing from 0 to 1 signifying ownership (1 person owning it at a time)
This is a common pattern known as the owner pattern. You can simplify this by simply keeping track of a single owner address and updating that, instead of using a mapping, since you only care about the current owner. Something as simple as:
address public owner;
modifier onlyOwner() {
require(msg.sender == owner);
_;
}
constructor() {
owner = msg.sender;
}
function transferOwnership(address _owner) onlyOwner {
owner = _owner;
}
Open Zeppelin has a more complete implementation of ownership here.
Because there's no actual transfer of ether/actual monetary value, where would actual ethereum transactions be occurring in this contract?
Transactions do not require ether movement. You can call any public/external function on your contract with a 0 value transaction, and pass data to it. The transaction will execute on the blockchain like any other, and run the code you invoke within the contract.
Also to sign the ledger I verify that the public key signing it has 1 as its value in the mapping, but how would I check that that person owns that public key (using a private key)?
You are already checking is msg.sender matches the whitelisted address. For basic transactions, this means that the transaction has been signed by the address in msg.sender (msg.sender can also be a contract, if another contract calls yours. To check who actually signed the transaction in all scenarios, you must use tx.origin).