Transfer the gas value of contract to my own address - ethereum

What happens ?
msg.sender.transfer(msg.value);
remix.eth is just telling me
errored: Cannot read property 'op' of undefined

Works fine for me:
pragma solidity ^0.4.0;
contract Test {
function () payable {
msg.sender.transfer(msg.value);
}
}
I get invalid opcode if I don't specify the function is payable.

Related

Is it possible to call methods of contract's initializer during its initialization?

I tried to deploy following smart contracts
pragma solidity 0.8.17;
contract Contract1 {
constructor() {
new Contract2(address(this));
}
function foo() public pure returns (string memory) {
return "bar";
}
}
contract Contract2 {
Contract1 contract1;
constructor(address _contract1) {
contract1 = Contract1(_contract1);
contract1.foo();
}
}
but it fails with following message:
The transaction has been reverted to the initial state. Note: The
called function should be payable if you send value and the value you
send should be less than your current balance. Debug the transaction
to get more information.
Seems like it's impossible to call methods of contract's initializer (the contract that created an instance of current one) during its initialization.
My question is how it happens that address of the Contract1 is already exists, but it's impossible to call any of its methods from the constructor of Contract2?
The address of Contract1 is not yet available when the constructor of Contract2 is called. The address of Contract1 is only available after the constructor of Contract1 has finished executing.
This means that the constructor of Contract2 cannot call any methods of Contract1 until after the constructor of Contract1 has finished executing.
In this case you can deploy the Contract1, wait for the tx complete and after deploy the Contract2 using the address of Contract1 in constructor arguments.

How to transfer ether to smart contract by msg.value?

I want to transfer some ether form my address to the smart contract, I have tried the code below but it doesn't work. How to transfer ether through msg.sender?
pragma solidity >=0.7.0 <0.9.0;
contract Test {
function testTransfer() external payable {
bool sent = payable(address(this)).send(msg.value);
require(sent, "invalid balance");
}
}
Once I trigger the function with the value of 1 ether, the error output is as below:
I'm sure that I have enough Ether in my address. What should I do to transfer the Ether? Thanks!
The transaction fails because the smart contract is trying to transfer the ether to himself, and the smart contract doesn't have defined the receive function so it can receive ether that way, for your example you could simply remove all the code inside the function and make another function to check the contract balance and it will work
pragma solidity >=0.7.0 <0.9.0;
contract Test {
function testTransfer() external payable {}
function getBalance() external view returns (uint256) {
return address(this).balance;
}
}

How to fund Chainlink smart contract with other contract?

I'm trying to fund a smart contract with LINK tokens. I get an error "VM error: revert. revert The transaction has been reverted to the initial state. Note: The called function should be payable if you send value and the value you send should be less than your current balance. Debug the transaction to get more information." My code is simple:
// SPDX-License-Identifier: MIT
pragma solidity >=0.5 <0.9.0;
//Remix Imports
import "https://github.com/smartcontractkit/chainlink/blob/develop/evm-contracts/src/v0.6/ChainlinkClient.sol";
import "https://github.com/smartcontractkit/chainlink/blob/develop/evm-contracts/src/v0.6/vendor/Ownable.sol";
import "https://github.com/smartcontractkit/chainlink/blob/develop/evm-contracts/src/v0.6/vendor/SafeMathChainlink.sol";
import "https://github.com/smartcontractkit/chainlink/blob/develop/evm-contracts/src/v0.6/interfaces/LinkTokenInterface.sol";
import "https://github.com/smartcontractkit/chainlink/blob/master/evm-contracts/src/v0.6/interfaces/AggregatorV3Interface.sol";
contract SimpleStorage {
SimpleStorage2[] simpleStorage2s;
function set() public payable returns(address) {
SimpleStorage2 a = new SimpleStorage2();
simpleStorage2s.push(a);
LinkTokenInterface link = LinkTokenInterface(a.getChainlinkToken());
link.transfer(address(a), 100);
return address(a);
}
}
contract SimpleStorage2 is ChainlinkClient, Ownable {
function getChainlinkToken() public view returns (address) {
return chainlinkTokenAddress();
}
}
Solidity compiler 0.6.12. What am I doing wrong? How do I get this to work?
Get the address of the contract that you've deployed, and send it LINK.
You can read the Chainlink documentation for more information.
It looks like you're using remix.
Get the address of your contract.
Paste it into your Metamask
3. Send your contract LINK
You'll get a few notifications to confirm.

Why does the truffle console not print correct variable in solidity smart contract?

I am a beginner in programming Ethereum smart contracts using solidity and truffle. I made a simple smart contract to deploy that sets a variable equal to 1000000, as shown below:
pragma solidity >=0.4.22 <0.8.0;
contract CelesteToken{
uint256 public totalSupply;
function ClesteToken()public{
totalSupply = 1000000;
}
}
In the truffle console I used the following commands:
CelesteToken.deployed().then(function(i){token =i;})
token.totalSupply().then(function(s){totalSupply =s;})
totalSupply.toNumber()
However instead of returning 1000000 as per the code, it returns 0. I am not sure why this is happening, can anyone help?
You can try replacing
function ClesteToken()public{
totalSupply = 1000000;
}
to
constructor () public {
totalSupply = 1000000;
}

Calling External Contract throws error : VM Exception while processing transaction: revert

I've deployed the ScoreStore contract to test RPC, and it works fine. This is ScoreStore contract:
pragma solidity ^0.4.4;
contract ScoreStore
{
mapping(string => int) PersonScores;
function SetScore(string name, int score) {
if(PersonScores[name]>0){
throw;
}
else{
PersonScores[name] = score;
}
}
function GetScore(string name) returns (int){
return PersonScores[name];
}
}
Now I want to use this contract on another contract named MyGame, the contract code is as follows:
pragma solidity ^0.4.4;
contract IScoreStore{
function GetScore(string name) returns (int);
}
contract MyGame{
function ShowScore(string name) returns (int){
// Interface takes an address of the existing contract as parameter
IScoreStore ss = IScoreStore(0x6c38cfb90e8fb1922e61ea4fbe09d29c7751bf82);
return ss.GetScore(name);
}
}
When I give this command on truffle console, mg.ShowScore.call("Anna")
it thorws this:
Error: VM Exception while processing transaction: revert
at XMLHttpRequest._onHttpResponseEnd (C:\Users\Fariha.Abbasi\AppData\Roaming\npm\node_modules\truffle\build\webpack:\~\xhr2\lib\xhr2.js:509:1)
at XMLHttpRequest._setReadyState (C:\Users\Fariha.Abbasi\AppData\Roaming\npm\node_modules\truffle\build\webpack:\~\xhr2\lib\xhr2.js:354:1)
at XMLHttpRequestEventTarget.dispatchEvent (C:\Users\Fariha.Abbasi\AppData\Roaming\npm\node_modules\truffle\build\webpack:\~\xhr2\lib\xhr2.js:64:1)
at XMLHttpRequest.request.onreadystatechange (C:\Users\Fariha.Abbasi\AppData\Roaming\npm\node_modules\truffle\build\webpack:\~\web3\lib\web3\httpprovider.
at C:\Users\Fariha.Abbasi\AppData\Roaming\npm\node_modules\truffle\build\webpack:\~\truffle-provider\wrapper.js:134:1
at C:\Users\Fariha.Abbasi\AppData\Roaming\npm\node_modules\truffle\build\webpack:\~\web3\lib\web3\requestmanager.js:86:1
at Object.InvalidResponse (C:\Users\Fariha.Abbasi\AppData\Roaming\npm\node_modules\truffle\build\webpack:\~\web3\lib\web3\errors.js:38:1)
Any idea, what i am doing wrong?
Any help is appreciated, P.S: testrpc is already running.
I was able to compile both the contracts in remix and call the ShowScore function after setting some value from ScoreStore contract successfully.
Are you sure the address given to the interface is correct? Because I got the same revert error when I gave an invalid address.