I want to update my token smart contract and I want to send new tokens to actual holders. Any code or ideas about how to do this? Contract is deployed, but I have not idea how to send tokens to actual holders. Also, how can I blacklist those scammers that took our token from the new one?
Once a contract is deployed, you can't change it unless you have some mechanism in place to do this. If your current contract does not have this, there's nothing you can do. However, for future reference, you could try something like this:
contract SomeContract {
address public owner;
address public currentContract;
function SomeContract(address initContract){
currentVersion = initContract;
owner = msg.sender;
}
function update(address newAddress){
if(msg.sender != owner) throw;
currentVersion = newAddress;
}
function myFunction(){
currentContract.delegatecall(msg.data)
}
}
Related
the withdraw function gets me an error and it shows send and transfer are only available for object of type address payable and not address...
Confusing!!
solidity
//SPDX-License-Identifier: MIT
//this line of code was created to fund account
//show the value of fund in the address
pragma solidity ^0.8.0;
// the fundMe contract should be able to accept payment
function withdraw()payable public {
msg.sender.transfer(address(this).balance);
}
}
In Solidity, there is a difference between a normal address and a payable address so the correct way to send Ether to the sender would be payable(msg.sender).transfer(address(this).balance); this converts the normal address to a payable address. For more details take a look at this
I think up until solidity ^0.8.0, msg.sender was payable. withdraw function should be called only by the owner of the contract and you do not need to make it payable.
address payable private owner;
// set the owner when the contract is created
constructor(){
owner=payable(msg.sender)
}
function withdraw() public {
require(msg.sender==owner,"only contract owner can call this");
owner.transfer(address(this).balance);
}
However, using transfer is not safe. Because .transfer() sends more gas than 2300. Thus making it possible for reentrancy. A better way would be:
function withdraw() public {
require(msg.sender==owner,"only contract owner can call this");
(bool success, ) = owner.call{value:address(this).balance}("");
// success should be true
require(success,"Withdraw failed")
}
How can I send tokens to a token holders from inside a smart contract with solidity?
It means how can send reward to token holders?
Have a list of addresses and loop through them while calling native erc transfer method. You can't really iterate through a mapping without knowing the access keys (if you're thinking about pulling addresses from smth like balances).
I am assuming you want to send Ether to another Smart Contract or an EOA (e.g. Metamask). You can write a Smart Contract such as the below and use the Remix Ethereum (an IDE) to send Ether to an external party. Use the public function - transferEther.
//SPDX-License-Identifier: GPL-3.0
pragma solidity >= 0.6.0 < 0.9.0;
contract Sample{
address public owner;
constructor() {
owner = msg.sender;
}
receive() external payable {
}
fallback() external payable {
}
function getBalance() public view returns (uint){
return address(this).balance;
}
// this function is for sending the wei/ether OUT from this smart contract (address) to another contract/external account.
function transferEther(address payable recipient, uint amount) public returns(bool){
require(owner == msg.sender, "transfer failed because you are not the owner."); //
if (amount <= getBalance()) {
recipient.transfer(amount);
return true;
} else {
return false;
}
}
}
I currently make a test contract on ethereum.remix.org.
But how can I specify the account in the test file from which a contract call should be sent?
contract test {
MyContract contract;
address creator = address(0);
address payable account2 = address(1);
address payable account3 = address(2);
address payable account4 = address(3);
function beforeAll () public {
contract = new MyContract();
}
function makeCallFromAccount2 () {
contract.function1(10);
}
}
How can I specify that contract.function1(10) is executed from account 2?
(This is important because the contract method uses the msg.sender value)
Apparently you can do this though there is little documentation on how to use:
https://github.com/ethereum/remix/tree/master/remix-tests#use-a-different-sender-msgsender
For the following code:
contract ERC20Token {
function transferFrom(address from, address to, uint value);
}
contract MyContract {
function myFunction(address tokenAddr) {
ERC20Token tok = ERC20Token(tokenAddr);
tok.transferFrom(_owner, _recipient, 100);
}
}
Can anyone call transferFrom function of a ERC20 token?
Anyone can call any function they want, so in a literal sense, yes, anyone can call it.
But in an ERC20-compliant token contract, that transaction will revert unless the caller has been authorized to transfer enough tokens from the from address.
I'm trying to change the value of a variable in a contract that is in the blockchain. I've deducted its code and is something like this:
pragma solidity ^0.4.8;
contract Trial {
address public owner;
address public person;
uint initialEther;
function Trial(address _person) payable {
owner = msg.sender;
person = _person;
initialEther = msg.value;
}
modifier only_person() {
if (msg.sender!=person && tx.origin!=person) throw;
_;
}
function() payable {}
bytes4 public signature = bytes4(sha3("libraryFunction()"));
bool variableToBeChanged = false;
function libraryInvocation(address libraryAddress) only_person {
bool doSomething = libraryAddress.delegatecall(signature);
if (variableToBeChanged) {
.....
}
}
}
Suppose that I have the right signature of the library function, what I'm trying to do is to change the value of "variableToBeChanged" in order to execute the code inside the if.
Probably there is a way to create a library with a function with a proper name and inserting assembler code in some way to change the value of the variable. But it's like using an atomic bomb to kill an ant. I'm looking for the simpler way to do this.
I also know this contract is not safe, I'm trying to understand if this is possible and I want to understand how risky this can be for a contract.
Without a function changing the variable it is not possible. Once the contract code is deployed, it is final, no change is possible. You need to deploy a new contract.
You could control with your contract which wallet is allowed to call the function by checking the msg.sender attribute for a specific wallet address.
address owner = 0x2FD4...;
if(msg.sender != owner) throw;
//code afterwards)