首页 > 其他分享 >npm的web3

npm的web3

时间:2023-02-22 11:22:06浏览次数:32  
标签:npm function err Web3 web3 var new

1.获取账户地址

var localhost = "http://127.0.0.1:8545"
var Web3 = require("web3")
var web3 = new Web3(new Web3.providers.HttpProvider(localhost))
web3.eth.getAccounts(function (error, result) {
    console.log("账户列表地址:");
    console.log(result);
});

部署合约测试

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

async function getAccount(){
    return new Promise((resolve,reject)=>{
        web3.eth.getAccounts((err,result)=>{
            if(err){reject(err);return}
            resolve(result)
        })
    })
}

async function readContractFile(fileName){
    return new Promise((resolve, reject) => {
       let filePath =   path.resolve(__dirname,fileName)
       fs.readFile(filePath,'utf8',(err,res)=>{
            if(err){reject(err);return}
            resolve(res)
       })
    })
}

async function deploy(AbiInfo,ByteCode,arguments,account,gas,){
   var currentContract = new web3.eth.Contract(JSON.parse(AbiInfo))
   currentContract.deploy({
    data:JSON.parse(ByteCode).object,
    arguments:arguments
   }).send({
        from:account,
        gas:gas
   },function(e,contract){
        console.log(e, contract);
        if (typeof contract.address !== 'undefined') {
            console.log('Contract mined! address: ' + contract.address + ' transactionHash: ' + contract.transactionHash);
       }
   })
}
//部署测试
async function deployContract(){
    let account = (await getAccount())[0]
    let AbiInfo = await readContractFile("file/ABI.json")
    let ByteCode = await readContractFile("file/ByteCode.json")
    await deploy(AbiInfo,ByteCode,[],account,'4700000')

}
deployContract()

和合约交互

合约部署完后得到合约地址,把地址放在web3.eth.Contract​里

标签:npm,function,err,Web3,web3,var,new
From: https://www.cnblogs.com/myfriend/p/npm-web3-1moenl.html

相关文章

  • npm install --global、--save、 --save-dev的区别和使用场景
    初学者使用npm安装时总是会有疑问,怎么在安装的模块时使用的命令不一样,一会加--global(-g),一会加--save(-S),一会又变成--save-dev(-D),这些参数是干什么的,什么时候用呢?接下来......
  • NPM记录
    前言:加密的插件废了,不想管他,就那样吧。。。。。本地安装npm环境参考:https://blog.csdn.net/qq_37242720/article/details/120280038安装nodejshttps://nodejs.org......
  • 常用包管理工具, 国内换源操作以及源地址收集(npm, yarn, pip, docker, linux, github
    常用包管理工具的换源本文将保持更新,以适应不同时代软件源的变化;如发现软件源失效请留言提醒常用包管理工具,国内换源操作以及镜像源地址收集(npm,yarn,pip,doc......
  • ES6之导入NPM包
    NPM和模块化结合使用 前提是要先安装node.js如果在vscode的终端中一直不成功的话,可以在cmd里,以管理员身份来安装jquery:  使用方法://修改背景颜色为粉色//ES......
  • npm install sentry-cli 失败
      npmsetENTRYCLI_CDNURL=https://cdn.npm.taobao.org/dist/sentry-clinpmsetsentrycli_cdnurl=https://cdn.npm.taobao.org/dist/sentry-cli......
  • npm代理网络链接失败
     解决方法:1、取消npm代理设置,输入命令:npmconfigsetproxynull2、输入命令:npmconfigsethttps-proxynull3、添加淘宝镜像,输入命令:npm--registryhttps://regist......
  • Vue系列---【自定义vue组件发布npm仓库】
    自定义vue组件发布npm仓库参考链接:自定义vue组件发布npm仓库......
  • 发布npm包
    要先注册npm账号,并且npm的源地址必须是https://registry.npmjs.org/ 参考一小满包https://blog.csdn.net/qq1195566313/article/details/125958100 参考二https:......
  • npm -D与-S
    --save==-S-S,--save安装包信息将加入到dependencies(生产阶段的依赖,也就是项目运行时的依赖,就是程序上线后仍然需要依赖)--save-dev==-D-D,--save-dev安......
  • pnpm的基本原理及快速使用
    基本原理前置知识:软件链接与硬链接软链接(符号链接Symboliclink):是一类特殊的文件,其包含有一条以绝对路径或者相对路径的形式指向其它文件或者目录的引用。在window快捷......