以太坊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
});
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 206,723评论 6 481
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 88,485评论 2 382
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 152,998评论 0 344
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 55,323评论 1 279
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 64,355评论 5 374
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,079评论 1 285
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,389评论 3 400
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,019评论 0 259
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 43,519评论 1 300
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,971评论 2 325
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,100评论 1 333
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,738评论 4 324
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,293评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,289评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,517评论 1 262
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 45,547评论 2 354
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,834评论 2 345

推荐阅读更多精彩内容