转账脚本

  1. 编写转账的js脚本,保存在transactionTest.js文件中
var Web3 = require('web3');
var web3 = new Web3(new Web3.providers.HttpProvider("http://192.168.1.114:8989"));var _from = web3.eth.accounts[0];
var _to = web3.eth.accounts[1];
var _value = 2333;web3.eth.sendTransaction({from:_from,to:_to,value:_value},(err,res)=>{if(err){console.log("Error:",err);}else{console.log("Result:",res);}
});
  1. 在shell中敲如下命令
$ node transactionTest.js
Result: 0xbae4d738ca3501d686781c10ce859b0b3eafefbeb21bf7344508017308a16587

转币合约的调用

  1. 编写solidity程序
pragma solidity ^0.4.26;contract Coin{address public minter;mapping(address=>uint) public balance;event Sent(address from,address to,uint account);constructor() public{minter = msg.sender;        }function mint(address receiver,uint account) public{require(msg.sender == minter);balance[receiver] += account;}function send(address receiver,uint account) public{require(balance[msg.sender] >= account);balance[msg.sender] -= account;balance[receiver] += account;emit Sent(msg.sender,receiver,account);}
}
  1. 使用remix部署合约到本地私链
//部署的合约地址
0x80f1A59742EF01a913Fe4B43ebaA759B9618B323
//开账minter
0x613d104e6D80ce5A06e7987D39Bbd4eE0ccD7656
//区块号
26
//abi
[{"constant":true,"inputs":[],"name":"minter","outputs":[{"name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"receiver","type":"address"},{"name":"account","type":"uint256"}],"name":"mint","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"receiver","type":"address"},{"name":"account","type":"uint256"}],"name":"send","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[{"name":"","type":"address"}],"name":"balance","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"inputs":[],"payable":false,"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":false,"name":"from","type":"address"},{"indexed":false,"name":"to","type":"address"},{"indexed":false,"name":"account","type":"uint256"}],"name":"Sent","type":"event"}]
//操作前需要在geth中指定,使msg.sender保证为该用户发起 【可选】
> eth.defaultAccount = eth.coinbase // 之后就不用输入{from:..}
  1. 编写调用的js脚本,保存在sendCoin.js文件中
var Web3 = require('web3');
var web3 = new Web3(new Web3.providers.HttpProvider("http://192.168.1.114:8989"));var _from = web3.eth.accounts[0];
var _to = web3.eth.accounts[1];
var amount = 12;var abi = [{"constant":true,"inputs":[],"name":"minter","outputs":[{"name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"receiver","type":"address"},{"name":"account","type":"uint256"}],"name":"mint","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"receiver","type":"address"},{"name":"account","type":"uint256"}],"name":"send","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[{"name":"","type":"address"}],"name":"balance","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"inputs":[],"payable":false,"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":false,"name":"from","type":"address"},{"indexed":false,"name":"to","type":"address"},{"indexed":false,"name":"account","type":"uint256"}],"name":"Sent","type":"event"}]
var CoinContract = web3.eth.contract(abi);
var contractAddr = "0x80f1A59742EF01a913Fe4B43ebaA759B9618B323";
var contractInstance = CoinContract.at(contractAddr);
contractInstance.send(_to,amount,{from:_from},(err,res)=>{if(err)console.log("Error:",err);elseconsole.log("Result",res);
});
  1. 执行shell命令,并检验
$ node sendCoin.js
Result 0x1ea44d44b45a25ed18ee1190d52175f66b832403fb121518d67ea56c4dd24025
// 之前的余额信息
> contractInstance.balance(eth.accounts[1])
25
> INFO [05-29|21:07:45.047] Submitted transaction                    fullhash=0x1ea44d44b45a25ed18ee1190d52175f66b832403fb121518d67ea56c4dd24025 recipient=0x80f1A59742EF01a913Fe4B43ebaA759B9618B323
INFO [05-29|21:07:45.047] Commit new mining work                   number=38 sealhash=12385d…3f2d34 uncles=0 txs=0 gas=0     fees=0          elapsed=41.826µs
INFO [05-29|21:07:45.048] Sealing paused, waiting for transactions
INFO [05-29|21:07:45.048] Commit new mining work                   number=38 sealhash=58cda4…ccb9dc uncles=0 txs=1 gas=35756 fees=3.5756e-14 elapsed=497.6µs
INFO [05-29|21:07:45.048] Successfully sealed new block            number=38 sealhash=58cda4…ccb9dc hash=9922c2…b07bb9 elapsed=543.138µs
INFO [05-29|21:07:45.048] 												

web3js脚本编写相关推荐

  1. 2021年大数据Kafka(三):❤️Kafka的集群搭建以及shell启动命令脚本编写❤️

    全网最详细的大数据Kafka文章系列,强烈建议收藏加关注! 新文章都已经列出历史文章目录,帮助大家回顾前面的知识重点. 目录 系列历史文章 Kafka的集群搭建以及shell启动命令脚本编写 一.搭建 ...

  2. 老李推荐:第3章3节《MonkeyRunner源码剖析》脚本编写示例: MonkeyImage API使用示例 1...

    老李推荐:第3章3节<MonkeyRunner源码剖析>脚本编写示例: MonkeyImage API使用示例 在上一节的第一个"增加日记"的示例中,我们并没有看到日记 ...

  3. Linux编写脚本查看mod,Linux shell脚本编写基础

    在进行linux测试时编写脚本是必不可少的,Shell脚本的名称可以随便定义,也不要什么后缀名,例如可以写abc,smartzip这类名称,运行时只要键入 ./smartzip就能运行脚本了.. 每行 ...

  4. 【Android 内存优化】libjpeg-turbo 函数库交叉编译与使用 ( 交叉编译脚本编写 | 函数库头文件拷贝 | 构建脚本配置 | Android Studio 测试函数库 )

    文章目录 一.交叉编译 Shell 脚本参考 二.NDK r16b 版本配置 三.libjpeg-turbo 交叉编译 Shell 脚本 四.执行 libjpeg-turbo 交叉编译 Shell 脚 ...

  5. SecureCRT中Python脚本编写学习指南

    引言 在测试网络设备中,通常使用脚本对设备端进行配置和测试以及维护:对于PE设备的测试维护人员来说使用较多是SecureCRT工具:SecureCRT支持VB.JavaScript.Python等多种 ...

  6. 使用脚本编写 Vim 编辑器,第 5 部分: 事件驱动的脚本编写和自动化

    Vim 的事件模型 Vim 编辑功能的运行方式是事件驱动的.但由于性能上的原因,实际的实现要远比这个复杂,还需要进行许多事件处理优化或者处理事件循环下面的几层,但是您仍然可以将编辑器看成一个简单循环, ...

  7. 使用脚本编写 Vim 编辑器,第 4 部分: 字典

    Vimscript 中的字典 在本质上和 AWK 关联数组.Perl 哈希表,或者 Python 字典都是一样.也就是说,这是一个无序容器,按字符串而不是整数来进行索引. Vimscript 系列 的 ...

  8. 使用脚本编写 Vim 编辑器,第 2 部分: 用户定义函数

    用户定义函数 Haskell 或 Scheme 程序员会告诉您,函数对于任何严肃的编程语言来说都是最重要的特性.对于 C 或 Perl 程序员,他们也会告诉您完全相同的观点. 函数为严肃的程序员提供了 ...

  9. 命令测试post_性能测试脚本编写之三

    >>>推荐阅读<<< 1.性能测试学习笔记-场景设计 2.性能测试的重要意义 3.性能分析流程及方法 4.应用系统性能调优之性能分析 ### web_url ### ...

最新文章

  1. 记住密码以及Android 列表的操作
  2. 【Android 安全】DEX 加密 ( 支持多 DEX 的 Android 工程结构 )
  3. 069_html统一资源定位器
  4. Mac 技术篇-查看python安装位置,查看java安装位置
  5. 备战秋招 |《百面机器学习》算法+leetcode开班报名!
  6. step1 . day2:Linux系统基础知识
  7. python浅蓝色对应的代码_浅蓝色Python模块不在m上工作
  8. leetcode360. 有序转化数组
  9. 钉钉项目任务怎么添加审批表单
  10. 计算机编程关键字一,和计算机编程有关的101条伟大的名言
  11. c jni 调用java_JNI NDK (AndroidStudio+CMake )实现C C++调用Java代码流程
  12. 【weiphp微信开发教程】留言板插件开发详解
  13. Vue学习笔记之13-webpack的配置 傻子看了都会配置的超详细教程
  14. GP2Y0E03 红外 测距 传感器 MSP430 G2553 单片机 程序
  15. 如何优雅的选择字体(font-family) 1
  16. potoshope cs5 序列号
  17. 某网吧网络布线规划设计
  18. PUG转HTML格式
  19. Web——HTML常见标签及用法
  20. html5制作波浪,技能get:用HTML5实现波浪效果

热门文章

  1. 「元」时代潮流启幕 树图生态淘派参展上海时装周回顾
  2. 虚拟机设置了桥连模式无法上网(电脑wifi上网)
  3. cordova实现点击按钮旋转屏幕插件cordova-plugin-screen-orientation
  4. C++ 中的.hpp文件
  5. 想做好用户画像?先学会这个基础操作
  6. opencv实现任意形状的内切圆
  7. ECharts系列 - 地图 实例一
  8. python求两点间的距离公式
  9. vsphere高可用
  10. linux amd显卡驱动画面撕裂,从此告别画面撕裂 AMD-FreeSync技术解析