web3js支付以及哈希查寻合约状态

  • 手册 http://cw.hubwiz.com/card/c/web3.js/1/6/22/
    <script src="https://cdn.jsdelivr.net/npm/web3@latest/dist/web3.min.js"></script>
    var app=new Vue({
        el:'#app',
        data:{
            tx:'0x69f5747e0db6d52c46f1f1e2e763b57be95d0f211165842f500997a72718dfb5'
            // tx:'0xc10ee2c372319145f3690e04ed38c64a3a741701781443caf56991d1311046af'

        },
        created:function(){
            var ts = this;
            window.addEventListener('load', function() {
                // Checking if Web3 has been injected by the browser (Mist/MetaMask)
                if (typeof web3 !== 'undefined') {
                    // Use Mist/MetaMask's provider
                    web3 = new Web3(web3.currentProvider);
                } else {
                    alert('please install MetaMask');
                }
                // ts.startApp();
            });


        },
        methods:{
        	query(){
        	//查询合约执行状态
                var ts = this;
        	    web3.eth.getTransactionReceipt(ts.tx).then(function(value) {
                    // console.log(value.status,'v');
                     console.log(value,'v');
                     // value         null  为上链尚未成功
                     // value.status  true  为合约执行成功
                     // value.status  false 为合约回滚
                }, function(error) {
                    console.log(error,'e');
                });
        	  
        	    
        		
        	},
            send1() {
//             使用合约
                var MatrixInstance = new web3.eth.Contract(ContractABI,MATRIX_ADDRESS);
                var myAccount;
                var ts = this;

                web3.eth.getAccounts(function(err,res){
                    if(!err){
                        myAccount = res[0];
                        console.log(myAccount)
                        var value = 0.002;
                        var toAddress = MATRIX_ADDRESS;
                        // 2300000
                        var gasnum = 230000, gasPrice = 5000000000;
                        var params = {
                            "from": myAccount,
                            "to": MATRIX_ADDRESS,
                            "gas": gasnum,
                            "gasPrice": gasPrice,
                            "value": '0x'+((Number(value)* 1000000000000000000).toString(16))
                        };
                        // console.log('------------');
                        // console.log(MatrixInstance.methods);
                        // console.log('------------');
                        // console.log(params);


                        try {
                        // invest() 为合约方法
                        MatrixInstance.methods.invest().send(
                            params

                        ).on('transactionHash', function(hash){
                        	// 获取hash 值
							console.log(hash,'hashr-transactionHash');
							if(!hash){
								  vant.Toast.fail({ message: "充值失败", duration: 1600 });	
							}
						})
                        .on('confirmation', function(confirmationNumber, receipt){
                        		//"confirmation" 返回 Number, Object: 收到确认时触发
                        	
								// alert(1);
								// console.log(receipt,'receipt-confirmation');
								console.log(confirmationNumber,'confirmationNumber-confirmation');
								// console.log(receipt.status,'status-confirmation')
							
								
						})
                        .on('receipt', function(receipt){
                        	// 返回 Object: 交易收据有效时触发。
                        	
								// alert(2);
							     console.log(receipt,'receipt');
								 vant.Toast.fail({ message: "充值成功", duration: 1600 });
						}).on('error', function(receipt){
								// "error" 返回 Error: 交易发送过程中如果出现错误则触发此事件。
							    console.log(receipt,'error');
							    vant.Toast.fail({ message: "充值失败", duration: 1600 });
						});


                        }catch (error) {
                        	   vant.Toast.fail({ message: "充值失败33333", duration: 1600 });
                        }
             
                    }
                });
            },

            send() {
                var	val = 0.02;
                var ts = this;
                if(window.ethereum) {
                    window.web3 = new Web3(ethereum);
                    web3.eth.getAccounts(function(err,res){
                        if(!err){
                            myAccount = res[0];
                            try {
                                ethereum.enable();

                                var gasnum = 210000, gasPrice = 5000000000;
                                var params = [{
                                    "from": myAccount,
                                    "to": "0xE952961A51DE6Ce59d937caF50e38c8e1FEFc799",
                                    "gas": gasnum.toString(16),
                                    "gasPrice": gasPrice.toString(16),
                                    "value": '0x'+((Number(val)* 1000000000000000000).toString(16))
                                    //0x2386F26FC10000 //10000000000000000,
                                }]

                                console.log(params);

                                ethereum.sendAsync({
                                    method: 'eth_sendTransaction',
                                    params: params,
                                    id:10086,
                                    //from: web3.eth.accounts[0], // Provide the user's account to use.
                                }, function (err, result) {
                                    console.log(result);
                                    if(result){
                                        if(result.result){

                                            vant.Toast.success({ message: "充值成功", duration: 2600 });
                                        }else{
                                            vant.Toast.fail({ message: "充值失败", duration: 2600 });
                                            //layer.open({ content: err, skin: 'msg', time: 2 });
                                        }

                                    }else{
                                        vant.Toast.fail({ message: "充值失败", duration: 2600 });
                                        //layer.open({ content: err, skin: 'msg', time: 2 });
                                    }
                                    // console.log('result:' + result)

                                })
                            }catch (error) {
                                console.log(error, "catch (error)");
                                vant.Toast.fail({ message: "充值失败33333", duration: 1600 });
                                // updatas()
                            }
                        }
                    });
                }
            },



        },
        mounted:function(){

        }
    })
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值