【Web3】 Web3JS Pay Api

这篇具有很好参考价值的文章主要介绍了【Web3】 Web3JS Pay Api。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

Web3Network.eth.sendSignedTransaction(serializedTx)

参数:

  • from- String|Number:发送帐户的地址。如果未指定,则使用web3.eth.defaultAccount属性。或web3.eth.accounts.wallet中本地的地址。

  • to- String:(可选)消息的目标地址,若未定义则为发送消息。

  • value- Number|String|BN|BigNumber:(可选)为wei中的交易转移的数量,如果是发送消息,则是捐赠给地址。

  • gas - Number:(可选,默认:待定)用于交易的gas(未使用的gas会退还)。

  • gasPrice- Number|String|BN|BigNumber:(可选)此交易的gas价格,以wei为单位,默认为web3.eth.gasPrice。

  • data- String:(可选)包含合同上函数调用数据的ABI字节字符串。

  • nonce- Number:(可选)随机数的整数。

EthPayApi

介绍:实现流程一共分为两部分,一完成Api参数、二完成privateKey加密发起Pay

初始化

npm install web3

<script setup name="EthPay">
import Web3 from 'web3'

const Web3Network = new Web3(
  Web3.givenProvider || 'wss://goerli.infura.io/ws/v3/xxx'
)
</script>

'wss://goerli.infura.io/ws/v3/xxx' 网络节点需要替换为自己的网络节点,如果拿到自己网络节点查看:
【Web3】Web3连接到以太坊网络(测试网、主网)_春暖花开.,的博客-CSDN博客


一.Api参数

定义自己Eth账号

//钱包地址
const walletAddress = ref('你的ETh地址')
//钱包私钥
const walletPrivateKey = ref(
  '你的Eth私钥'
)

参数一共分为

form //发送方地址    
nonce //发送方交易次数    
gasPric //预估手续费
to //接收方地址    
value //以wei单位数量   
data //转Token代币会用到的一个字段
gas //用于交易gas

1.nonce transaction次数

  //transaction次数
  const numberTransaction = await Web3Network.eth.getTransactionCount(
    walletAddress.value
  )

2.gasPric 获取预计手续费

  //获取预计手续费
const serviceCharge = await Web3Network.eth.getGasPrice()

3. value 数量

  //以wei为单位数量
const WeiMoney = Web3.utils.toWei('0.0001')

4.合并

 const rawTx = {
    form: walletAddress.value, //发送方地址
    nonce: numberTransaction, //发送方交易次数
    gasPrice: serviceCharge, //预估手续费
    to: 'xxx', //接收方地址
    value: WeiMoney, //以wei单位数字
    data: '0x00' //转Token代币会用到的一个字段
  }

5.gas 

  //把交易数据进行gas计算
  const gas = await Web3Network.eth.estimateGas(rawTx)
  rawTx.gas = gas

二.privateKey加密调用sendSignedTransaction函数

1.私钥转数组十六进制

为什么转换?: 因为 tx.sign() 要求长度必须在32位以内所以需要转换

npm install buffer

  import { Buffer } from 'buffer'
  //把privateKey转换 数组hex
  const PrivatekeyHex = Buffer(walletPrivateKey.value, 'hex')
  console.log(`key:`, PrivatekeyHex)

2.ethereumjs-tx 库加密转换数组十六进制后Eth私钥
注意:tx.sign( ) 要求长度必须32位以内

npm install ethereumjs-tx@1.3.7

  import Tx from 'ethereumjs-tx' 
  //privateKey加密
  const tx = new Tx(rawTx)
  tx.sign(PrivatekeyHex)
  //通过ethereumjs-tx加密并转为十六进制字符串
  const serializedTx = '0x' + tx.serialize().toString('hex')
  console.log(serializedTx)

3.如果出现这样报错

 error  in ./node_modules/cipher-base/index.js

Module not found: Error: Can't resolve 'stream' in 'C:\Users\ttatt\Desktop\前端\xxx\xxx\Web3\Web3实战\web3defivue\node_modules\cipher-base'

BREAKING CHANGE: webpack < 5 used to include polyfills for node.js core modules by default.
This is no longer the case. Verify if you need this module and configure a polyfill for it.

If you want to include a polyfill, you need to:
        - add a fallback 'resolve.fallback: { "stream": require.resolve("stream-browserify") }'
        - install 'stream-browserify'
If you don't want to include a polyfill, you can use an empty module like this:
        resolve.fallback: { "stream": false }

ERROR in ./node_modules/cipher-base/index.js 3:16-43
Module not found: Error: Can't resolve 'stream' in 'C:\Users\ttatt\Desktop\xxx\xxx\85期\Web3\Web3实战\web3defivue\node_modules\cipher-base'

BREAKING CHANGE: webpack < 5 used to include polyfills for node.js core modules by default.
This is no longer the case. Verify if you need this module and configure a polyfill for it.

If you want to include a polyfill, you need to:
        - add a fallback 'resolve.fallback: { "stream": require.resolve("stream-browserify") }'
        - install 'stream-browserify'
If you don't want to include a polyfill, you can use an empty module like this:
        resolve.fallback: { "stream": false }
 @ ./node_modules/create-hash/browser.js 7:11-33
 @ ./node_modules/ethereumjs-util/dist/index.js 18:17-39
 @ ./node_modules/ethereumjs-tx/es5/index.js 9:14-40
 @ ./node_modules/babel-loader/lib/index.js??clonedRuleSet-40.use[0]!./node_modules/vue-loader/dist/index.js??ruleSet[0].use[0]!./src/App.vue?vue&type=script&setup=true&name=web3&lang=js 4:0-31 68:21-23 88:15-17
 @ ./src/App.vue?vue&type=script&setup=true&name=web3&lang=js 1:0-210 1:0-210 1:211-410 1:211-410
 @ ./src/App.vue 2:0-75 3:0-70 3:0-70 6:49-55
 @ ./src/main.js 2:0-28 6:22-25

webpack compiled with 1 error

要解决在 vue.config.js

npm install node-polyfill-webpack-plugin@2.0.1

const { defineConfig } = require('@vue/cli-service')
// 引入插件
const NodePolyfillWebpackPlugin = require('node-polyfill-webpack-plugin')

module.exports = defineConfig({
  transpileDependencies: true,
  configureWebpack: {
      //使用插件
    plugins: [new NodePolyfillWebpackPlugin()]
  }
})

4.调用ETHPay函数sendSignedTransaction 开始EthPay是否成功文章来源地址https://www.toymoban.com/news/detail-558838.html

  //开始执行
  const Transfer = Web3Network.eth.sendSignedTransaction(serializedTx)

  // 监听是否成功 -- 加载中
  Transfer.on('transactionHash', txid => {
    console.log(
      `查看----https://goerli.etherscan.io/tx/${txid}`
    )
  })

代码

  //一.第一部分
  //transaction次数
  const numberTransaction = await Web3Network.eth.getTransactionCount(
    walletAddress.value
  )
  console.log(numberTransaction)
  //获取预计手续费
  const serviceCharge = await Web3Network.eth.getGasPrice()
  console.log(serviceCharge)

  //以wei为单位数量
  const WeiMoney = Web3.utils.toWei('0.0001')
  console.log(WeiMoney)
  //Api参数
  const rawTx = {
    form: walletAddress.value, //发送方地址
    nonce: numberTransaction, //发送方transaction次数
    gasPrice: serviceCharge, //预估手续费
    to: 'xxxx', //接收方地址
    value: WeiMoney, //以wei单位数量
    data: '0x00' //转Token会用到的一个字段
  }
  //把transaction数据进行gas计算
  const gas = await Web3Network.eth.estimateGas(rawTx)
  rawTx.gas = gas

  //二.第二部分
  //把privateKey转换 数组hex
  //因为 tx.sign() 要求长度必须在32位以内所以需要转换
  const PrivatekeyHex = Buffer(walletPrivateKey.value, 'hex')
  console.log(`key:`, PrivatekeyHex)

  //privateKey加密
  const tx = new Tx(rawTx)
  tx.sign(PrivatekeyHex)
  //通过ethereumjs-tx加密并转为十六进制字符串
  const serializedTx = '0x' + tx.serialize().toString('hex')
  //开始执行
  const Transfer = Web3Network.eth.sendSignedTransaction(serializedTx)

  // 监听是否成功 -- 加载中
  Transfer.on('transactionHash', txid => {
    console.log(txid)
    console.log(
      `查看----https://goerli.etherscan.io/tx/${txid}`
    )
  })

到了这里,关于【Web3】 Web3JS Pay Api的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 以太坊Dapp通过web3js部署调用智能合约

    参考视频:https://www.bilibili.com/video/BV14z4y1Z7Jd?p=1 https://remix.ethereum.org/ 创建一个新的文件夹 mkdir MyDapp2 启动 ganache-cli 下载web3 npm install web3 ,注:ganache的启动和deploy.js要在同一个目录。 先进行测试 node deploy.js 部署合约 web3的版本:1.7.1 复制 WEB3DEPLOY 的内容到deploy.js == ganache需要

    2023年04月23日
    浏览(33)
  • HTML页面通过Web3JS连接智能合约并调用其中接口

    之前我们学习solidity 并用它编写了智能合约 登上区块链 然后也做了基本的测试 但是 其实在web3时代 前端依旧扮演者非常重要的角色 我们现在就来打通web3 从合约到页面的一个管理 首先 我们还是将自己的ganache环境起起来 然后 在我们之前智能合约的项目终端执行 将我们的智

    2024年02月16日
    浏览(39)
  • 使用nodejs和web3js实现链接metamask钱包并实现合约交互

    在以太坊区块链上,metamask钱包是一个非常常用的钱包,用以管理以太币和其他以太坊资产。同时,它也是一个重要的以太坊智能合约交互工具。在本文中,我们将介绍如何使用nodejs和web3js实现链接metamask钱包并实现合约交互。 在开始之前,首先需要安装NodeJS和Web3JS。 NodeJS是

    2024年02月04日
    浏览(67)
  • 使用nodejs和web3js来实现链接MetaMask钱包并取消风险代币授权

    随着区块链技术的快速发展,数字货币资产在投资及交易市场中得到了广泛的应用。而在数字货币交易过程中,钱包是一个非常重要的环节。比如MetaMask就是一款非常流行的区块链钱包,它可以让用户在浏览器中安全地管理自己的数字资产。 然而,在数字货币交易过程中也存

    2024年01月23日
    浏览(40)
  • web3Js(干货)(多签的流程原理)看完这一篇就懂了(波场网络-请勿用于除学习外其他用途)

    连接波场网络: 其中APIKEY可以在官网获取; 可以使用tronWeb.isConnected()判断是否连接成功 创建离线波场地址: 该地址未激活,如果需要激活, 通常需要一定数量的 TRX(TRON 的本地代币)用于支付激活费用; 等待区块确定 就可以查看激活信息; 创建随机助记词与私钥: 如何让

    2024年02月04日
    浏览(61)
  • 连接区块链节点的 JavaScript 库 web3.js

    通过前面的文章我们可以知道基于区块链开发一个DApp,而DApp结合了智能合约和用户界面(客户端),那客户端是如何与区块链进行连接交互的、如何调用智能合约的、如何发送一个交易到区块链、如何获取区块链上的数据,这就是本文要介绍的 web3.js。 如下图,区块链网络

    2024年02月15日
    浏览(46)
  • 使用 Web3.js 连接以太坊节点并查询区块链数据

    Web3.js 是一个用于连接以太坊网络的 JavaScript 库。在本文中,我们将介绍如何使用 Web3.js 来连接以太坊节点,并且查询以太坊区块链上的数据。 1. 安装 Web3.js 首先,我们需要安装 Web3.js。在命令行中,输入以下命令: 2. 连接以太坊节点 在使用 Web3.js 之前,我们需要先连接到以

    2023年04月26日
    浏览(38)
  • 以太坊标准JS API库——浅谈Web3.js与Ethers.js

    版本号: Web3.js v1.7.3 Ethers.js v.5.6 web3.js是一个标准的以太坊JavaScript API库,该JS库由以太坊基金会开发维护,同时它也是最早且使用最广泛的一个ETH API库。由于Moonbeam完全兼容以太坊EVM,因此web3.js可以在Moonbeam上进行正常交互 web3.js(v1.7.3)库官方使用文档:https://web3js.readthedoc

    2024年01月23日
    浏览(30)
  • 【区块链 | Solidity】Solidity开发教程:用Web3.js构建第一个Dapp

    Web3.js 使用的实用介绍 原文 作者:wissal haji 欢迎订阅《Solidity智能合约零基础开发教程专栏》系列文章。 如果你一直在跟着这个系列学习,那么你已经掌握了编写自己的智能合约。 因此,今天给大家介绍一下构建去中心化应用的全貌,并向大家介绍一下web3.js,这是构建da

    2024年01月24日
    浏览(41)
  • vue3 使用 web3.js;钱包转账;唤醒钱包.......

    web3.js基本使用 本篇文章不适合没有经验的小白,需要对web3有一定的了解,所以有些参数需要自己懂! 准备 安装web3.js依赖包 npm install web3 文档教程地址:https://web3.tryblockchain.org/ 这是我自己封装的类 web3.js 直接复制过去就可以用

    2024年02月03日
    浏览(40)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包