web3js使用metamask调用合约查询和发起交易

4 篇文章 3 订阅
3 篇文章 1 订阅

web3通过合约查询的方法

使用web3调用合约的方式,官网文档给出的方法是

new web3.eth.Contract(jsonInterface[, address][, options])

jsonInterface - Object: 要实例化的合约的json接口
address - String: 可选,要调用的合约的地址,也可以在之后使用 myContract.options.address = '0x1234..' 来指定该地址
options - Object : 可选,合约的配置对象,其中某些字段用作调用和交易的回调:
from - String: 交易发送方地址
gasPrice - String: 用于交易的gas价格,单位:wei
gas - Number: 交易可用的最大gas量,即gas limit
data - String: 合约的字节码,部署合约时需要

但实际情况中这只是合约构造函数并不能直接查询

直接查询的使用方法

myContract = new web3.eth.Contract(ABI, Address);
//这里也可用直接打印myContract查看有没有 myMethod() 这个方法
myContract.methods.myMethod(//一般传值就在这里传).call().then((res) => {
         console.log(res);//res就是调合约查询出来的数据
 });
 ABI -为指定的合约方法进行ABI编码,可用于发送交易、调用方法或向另一个合约方法传递参数。
 Address -合约地址

实际情况中是有非常多的查询,一般是要通过封装配合回调来使用的

配合metamask实现交易

function GetAngleRewards(Address, useraddr, callback) {
    myContract = new web3.eth.Contract(AngelABI, Address);
    const account = useraddr;
    sendEtherFrom(account, function(err, transaction) {
        if (err) {
            Message.error("I'm sorry you can't contribute!");
        }
    })

    function sendEtherFrom(account) {
        const method = 'eth_sendTransaction'
        let data = web3.eth.abi.encodeFunctionCall({
            name: "send",
            type: "function",
            inputs: [],
            outputs: [],
        }, []);
        const parameters = [{
            from: account,
            to: Address,
            data: data
        }]
        const from = account;
        const payload = {
            method: method,
            params: parameters,
            from: from,
        }
        callback(null);
        ethereum.sendAsync(payload, function(err, response) {
            const rejected = 'User denied transaction signature.'
            if (response.error && response.error.message.includes(rejected)) {
                callback("refuse");
            }
            if (response.code == "-32603") {
                callback("fail");
            }
            if (response.error && response.error.code == "-32603") {
                callback("fail");
            }
            if (response.result) {
                timer_takeGain = setInterval(() => {
                    number_takeGain++
                    // 查询交易是否完成,这里要通过这个方法去一直查询交易是否完成
                    web3.eth.getTransactionReceipt(response.result).then(function(res) {
                        if (res == null) {
                            callback(res)
                        } else if (res.status) {
                            callback(res.status)
                            clearInterval(timer_takeGain);
                        } else {
                            clearInterval(timer_takeGain);
                        }

                    });
                    if (number_takeGain > 10) {
                        clearInterval(timer_takeGain);
                        callback("timeout")
                        number_takeGain = 1;
                    }
                }, 2000);
            }
        })
    }

}
  • 1
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值