EExcel 丞燕快速查詢2

EExcel 丞燕快速查詢2
EExcel 丞燕快速查詢2 https://sandk.ffbizs.com/

ethereum smart contract payable

https://remix.ethereum.org/

Method 1: No function name



    function () payable external {
        t = msg.value;
        emit Buyvalue(msg.value);
    }

function () payable external {

 OR

function () external payable {


pragma solidity >=0.4.0 <0.6.0;

contract SimpleStorage {
    uint storedData;
    uint256 t;
    event Set(address indexed _from, uint value);
    event Buyvalue(uint tokens);
    
    function set(uint x) public {
        storedData = x;
        emit Set(msg.sender, x);
    }

    function get() public view returns (uint) {
        return storedData;
    }
    
    function gett() public view returns (uint) {
        return t;
    }
    
    function () payable external {
        t = msg.value;
        emit Buyvalue(msg.value);
    }
}

MetaMask can not director send contract address eth, so use console


web3.eth.sendTransaction({
  to:'0xc356362efcce609fbe141b9bbda9e5ddc8a25526', 
  from:'0x5921a4C1B13afbD4b61d63e9c7BD47741C47B176', 
  value:web3.toWei("1", "ether")
}, console.log)

https://ethereum.stackexchange.com/questions/40862/how-to-set-msg-value-in-metamask








This method have warning about

Fallback function of contract SimpleStorage requires too much gas (21202). If the fallback function requires more than 2300 gas, the contract cannot receive Ether.


Method 2:buy function


https://www.ethereum.org/token


    function buy() payable public {
        t = msg.value;
        emit Buyvalue(msg.value);
    }


https://ethereum.stackexchange.com/questions/45398/call-a-payable-function-and-pay-ether-with-metamask



pragma solidity >=0.4.0 <0.6.0;

contract SimpleStorage {
    uint storedData;
    uint256 t;
    event Set(address indexed _from, uint value);
    event Buyvalue(uint tokens);
    
    function set(uint x) public {
        storedData = x;
        emit Set(msg.sender, x);
    }

    function get() public view returns (uint) {
        return storedData;
    }
    
    function gett() public view returns (uint) {
        return t;
    }
    
    function buy() payable public {
        t = msg.value;
        emit Buyvalue(msg.value);
    }
}

MetaMask can not director send contract address eth, so use console


var thecontract = web3.eth.contract([{"constant":false,"inputs":[{"name":"x","type":"uint256"}],"name":"set","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"get","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"gett","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[],"name":"buy","outputs":[],"payable":true,"stateMutability":"payable","type":"function"},{"anonymous":false,"inputs":[{"indexed":true,"name":"_from","type":"address"},{"indexed":false,"name":"value","type":"uint256"}],"name":"Set","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"name":"tokens","type":"uint256"}],"name":"Buyvalue","type":"event"}]);
var MyContract = thecontract.at('0x75e65c1f6de85183ba4937dafb62f54450f314b6');

MyContract.buy({
  from:'0x5921a4C1B13afbD4b61d63e9c7BD47741C47B176', 
  value:web3.toWei("1", "ether")
}, console.log) 

Here have one problem about line 1: web3.eth.contract( ooxxooxx )

ooxxooxx How to get






result same method 1