以太坊Web3学习笔记

// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;

contract Coin{
    
    address public minter;
    
    mapping(address=>uint256) public balances;
    
    event Sent(address from, address to, uint256 amount);
    
    constructor(uint256 initalSupply) public{
        minter = msg.sender;
        balances[msg.sender] = initalSupply;
    }
    
    function mint(address receiver, uint256 amount) public {
        require(msg.sender == minter);
        balances[receiver] += amount;
    }
    
    function send(address receiver, uint256 amount) public {
        require(balances[msg.sender] >= amount);
        require(balances[receiver] + amount > balances[receiver]);
        balances[msg.sender] -= amount;
        balances[receiver] += amount;
        //return true;
        emit Sent(msg.sender, receiver, amount);
    }
    
}

1. 发送交易

var Web3 = require("web3");
var web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));

var arguments = process.argv.splice(2);

var _from = "0x662d722458f7970d73e94d33b9422a08af5f48bc";
var _to = arguments[0];

var _value = arguments[1];

web3.eth.sendTransaction({
    from: _from,
    to: _to,
    value: _value
})
.then((err, receipt)=>{
   if(err)
      console.log(err);
   else
      console.log(receipt);
});

2. 部署合约

var abi = [{"inputs":[{"internalType":"uint256","name":"initalSupply","type":"uint256"}],"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"address","name":"from","type":"address"},{"indexed":false,"internalType":"address","name":"to","type":"address"},{"indexed":false,"internalType":"uint256","name":"amount","type":"uint256"}],"name":"Sent","type":"event"},{"inputs":[{"internalType":"address","name":"","type":"address"}],"name":"balances","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"receiver","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"mint","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"minter","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"receiver","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"send","outputs":[],"stateMutability":"nonpayable","type":"function"}]
var bin = "0x60806040523480156100115760006000fd5b506040516107a63803806107a6833981810160405281019061003391906100e2565b5b33600060006101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff16021790555080600160005060003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000206000508190909055505b5061013456610133565b6000815190506100db81610118565b5b92915050565b6000602082840312156100f55760006000fd5b6000610103848285016100cc565b9150505b92915050565b60008190505b919050565b6101218161010d565b8114151561012f5760006000fd5b505b565b5b610663806101436000396000f3fe60806040523480156100115760006000fd5b50600436106100515760003560e01c8063075461721461005757806327e235e31461007557806340c10f19146100a5578063d0679d34146100c157610051565b60006000fd5b61005f6100dd565b60405161006c919061048a565b60405180910390f35b61008f600480360381019061008a9190610400565b610103565b60405161009c91906104de565b60405180910390f35b6100bf60048036038101906100ba919061042b565b61011e565b005b6100db60048036038101906100d6919061042b565b6101dd565b005b600060009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1681565b60016000506020528060005260406000206000915090505481565b600060009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff1614151561017b5760006000fd5b80600160005060008473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020600082828250546101cf91906104fa565b9250508190909055505b5050565b80600160005060003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060005054101515156102325760006000fd5b600160005060008373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000206000505481600160005060008573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020600050546102c991906104fa565b1115156102d65760006000fd5b80600160005060003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000206000828282505461032a9190610551565b92505081909090555080600160005060008473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000206000828282505461038791906104fa565b9250508190909055507f3990db2d31862302a685e8086b5755072a6e2b5b780af1ee81ece35ee3cd33453383836040516103c3939291906104a6565b60405180910390a15b50505661062c565b6000813590506103e3816105f6565b5b92915050565b6000813590506103f981610611565b5b92915050565b6000602082840312156104135760006000fd5b6000610421848285016103d4565b9150505b92915050565b60006000604083850312156104405760006000fd5b600061044e858286016103d4565b925050602061045f858286016103ea565b9150505b9250929050565b61047381610586565b825250505b565b610483816105ba565b825250505b565b600060208201905061049f600083018461046a565b5b92915050565b60006060820190506104bb600083018661046a565b6104c8602083018561046a565b6104d5604083018461047a565b5b949350505050565b60006020820190506104f3600083018461047a565b5b92915050565b6000610505826105ba565b9150610510836105ba565b9250827fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff03821115610545576105446105c5565b5b82820190505b92915050565b600061055c826105ba565b9150610567836105ba565b92508282101561057a576105796105c5565b5b82820390505b92915050565b600061059182610599565b90505b919050565b600073ffffffffffffffffffffffffffffffffffffffff821690505b919050565b60008190505b919050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052601160045260246000fd5b565b6105ff81610586565b8114151561060d5760006000fd5b505b565b61061a816105ba565b811415156106285760006000fd5b505b565bfea2646970667358221220d2ffa2293b8f1b97db91acfc29e27c57450769b93af8c4c55ab806732f38963f64736f6c63430008040033"
var deloyObject = {from:"0x662d722458F7970d73e94D33b9422a08aF5f48Bc", gas:1000000}
var c2 =new web3.eth.Contract(abi)

c2.deploy({data: bin, arguments: ["0x662d722458F7970d73e94D33b9422a08aF5f48Bc"]}).send(deloyObject, function(error, hash){console.log(hash)})

c2.methods.send("0x8c35154266a4c72f360b8557e80a6fa232de633d", 2000000).send({from: "0x662d722458f7970d73e94d33b9422a08af5f48bc"}).then(console.log)
c2.methods.balances("0x662d722458F7970d73e94D33b9422a08aF5f48Bc").call().then(console.log)
c2.methods.balances("0x8c35154266a4c72f360b8557e80a6fa232de633d").call().then(console.log)

3. 匹配合约

var Web3 = require("web3");
var web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));

//从第三个开始截取
var arguments = process.argv.slice(2);

//var _from = "0x662d722458f7970d73e94d33b9422a08af5f48bc";
//var _to = "0x0dc734c5bea7a45f195a9f37f6d9f5fb043aaa5e";
//var amount = 50000;

var _addr = arguments[0];

var abi = [{"inputs":[{"internalType":"uint256","name":"initalSupply","type":"uint256"}],"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"address","name":"from","type":"address"},{"indexed":false,"internalType":"address","name":"to","type":"address"},{"indexed":false,"internalType":"uint256","name":"amount","type":"uint256"}],"name":"Sent","type":"event"},{"inputs":[{"internalType":"address","name":"","type":"address"}],"name":"balances","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"receiver","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"mint","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"minter","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"receiver","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"send","outputs":[],"stateMutability":"nonpayable","type":"function"}];

var myC = new web3.eth.Contract(abi, "0xFA72617606CF87041Acaf3c15d552302500Ffc4C");

myC.methods.balances(_addr).call().then(console.log);

4. 调用合约

var Web3 = require("web3");
var web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));

//从第三个开始截取
var arguments = process.argv.slice(2);

var _from = "0x662d722458f7970d73e94d33b9422a08af5f48bc";
//var _to = "0x0dc734c5bea7a45f195a9f37f6d9f5fb043aaa5e";
//var amount = 50000;

var _to = arguments[0];
var amount = arguments[1];

var abi = [{"inputs":[{"internalType":"uint256","name":"initalSupply","type":"uint256"}],"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"address","name":"from","type":"address"},{"indexed":false,"internalType":"address","name":"to","type":"address"},{"indexed":false,"internalType":"uint256","name":"amount","type":"uint256"}],"name":"Sent","type":"event"},{"inputs":[{"internalType":"address","name":"","type":"address"}],"name":"balances","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"receiver","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"mint","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"minter","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"receiver","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"send","outputs":[],"stateMutability":"nonpayable","type":"function"}];

var myC = new web3.eth.Contract(abi, "0xFA72617606CF87041Acaf3c15d552302500Ffc4C");

myC.methods.send(_to, amount).send({from: _from}).then(console.log);

5. 跟踪事件

var Web3 = require("web3");
var web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));

//从第三个开始截取
//var arguments = process.argv.slice(2);

var abi = [{"inputs":[{"internalType":"uint256","name":"initalSupply","type":"uint256"}],"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"address","name":"from","type":"address"},{"indexed":false,"internalType":"address","name":"to","type":"address"},{"indexed":false,"internalType":"uint256","name":"amount","type":"uint256"}],"name":"Sent","type":"event"},{"inputs":[{"internalType":"address","name":"","type":"address"}],"name":"balances","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"receiver","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"mint","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"minter","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"receiver","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"send","outputs":[],"stateMutability":"nonpayable","type":"function"}];

var myC = new web3.eth.Contract(abi, "0xFA72617606CF87041Acaf3c15d552302500Ffc4C");

myC.events.Sent({filter: {}, fromBlock:"pending"}, (err, re)=>{console.log(re)})
.on("connected", function(subscriptionId){
    console.log(subscriptionId);
})
.on('data', function(event){
    console.log(event); // same results as the optional callback above
})
.on('changed', function(event){
    // remove event from local database
});

你可能感兴趣的:(以太坊Web3学习笔记)