Web3Network.eth.sendSignedTransaction(serializedTx)
參數(shù):
-
from
-String|Number
:發(fā)送帳戶的地址。如果未指定,則使用web3.eth.defaultAccount屬性。或web3.eth.accounts.wallet中本地的地址。 -
to
-String
:(可選)消息的目標地址,若未定義則為發(fā)送消息。 -
value
-Number|String|BN|BigNumber
:(可選)為wei中的交易轉(zhuǎn)移的數(shù)量,如果是發(fā)送消息,則是捐贈給地址。 -
gas
-Number
:(可選,默認:待定)用于交易的gas(未使用的gas會退還)。 -
gasPrice
-Number|String|BN|BigNumber
:(可選)此交易的gas價格,以wei為單位,默認為web3.eth.gasPrice。 -
data
-String
:(可選)包含合同上函數(shù)調(diào)用數(shù)據(jù)的ABI字節(jié)字符串。 -
nonce
-Number
:(可選)隨機數(shù)的整數(shù)。
EthPayApi
介紹:實現(xiàn)流程一共分為兩部分,一完成Api參數(shù)、二完成privateKey加密發(fā)起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' 網(wǎng)絡(luò)節(jié)點需要替換為自己的網(wǎng)絡(luò)節(jié)點,如果拿到自己網(wǎng)絡(luò)節(jié)點查看:
【W(wǎng)eb3】Web3連接到以太坊網(wǎng)絡(luò)(測試網(wǎng)、主網(wǎng))_春暖花開.,的博客-CSDN博客
一.Api參數(shù)
定義自己Eth賬號
//錢包地址
const walletAddress = ref('你的ETh地址')
//錢包私鑰
const walletPrivateKey = ref(
'你的Eth私鑰'
)
參數(shù)一共分為
form //發(fā)送方地址 ? ?
nonce //發(fā)送方交易次數(shù) ? ?
gasPric //預(yù)估手續(xù)費
to //接收方地址 ? ?
value //以wei單位數(shù)量? ?
data //轉(zhuǎn)Token代幣會用到的一個字段
gas //用于交易gas
1.nonce transaction次數(shù)
//transaction次數(shù)
const numberTransaction = await Web3Network.eth.getTransactionCount(
walletAddress.value
)
2.gasPric?獲取預(yù)計手續(xù)費
//獲取預(yù)計手續(xù)費
const serviceCharge = await Web3Network.eth.getGasPrice()
3. value 數(shù)量
//以wei為單位數(shù)量
const WeiMoney = Web3.utils.toWei('0.0001')
4.合并
const rawTx = {
form: walletAddress.value, //發(fā)送方地址
nonce: numberTransaction, //發(fā)送方交易次數(shù)
gasPrice: serviceCharge, //預(yù)估手續(xù)費
to: 'xxx', //接收方地址
value: WeiMoney, //以wei單位數(shù)字
data: '0x00' //轉(zhuǎn)Token代幣會用到的一個字段
}
5.gas?
//把交易數(shù)據(jù)進行g(shù)as計算
const gas = await Web3Network.eth.estimateGas(rawTx)
rawTx.gas = gas
二.privateKey加密調(diào)用sendSignedTransaction函數(shù)
1.私鑰轉(zhuǎn)數(shù)組十六進制
為什么轉(zhuǎn)換?:?因為 tx.sign() 要求長度必須在32位以內(nèi)所以需要轉(zhuǎn)換
npm install?buffer
import { Buffer } from 'buffer'
//把privateKey轉(zhuǎn)換 數(shù)組hex
const PrivatekeyHex = Buffer(walletPrivateKey.value, 'hex')
console.log(`key:`, PrivatekeyHex)
2.ethereumjs-tx?庫加密轉(zhuǎn)換數(shù)組十六進制后Eth私鑰
注意:tx.sign( ) 要求長度必須32位以內(nèi)
npm install?ethereumjs-tx@1.3.7
import Tx from 'ethereumjs-tx'
//privateKey加密
const tx = new Tx(rawTx)
tx.sign(PrivatekeyHex)
//通過ethereumjs-tx加密并轉(zhuǎn)為十六進制字符串
const serializedTx = '0x' + tx.serialize().toString('hex')
console.log(serializedTx)
3.如果出現(xiàn)這樣報錯
?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實戰(zhàn)\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實戰(zhàn)\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-25webpack compiled with 1 error
要解決在 vue.config.js
npm install?node-polyfill-webpack-plugin@2.0.1文章來源:http://www.zghlxwxcb.cn/news/detail-558838.html
const { defineConfig } = require('@vue/cli-service')
// 引入插件
const NodePolyfillWebpackPlugin = require('node-polyfill-webpack-plugin')
module.exports = defineConfig({
transpileDependencies: true,
configureWebpack: {
//使用插件
plugins: [new NodePolyfillWebpackPlugin()]
}
})
4.調(diào)用ETHPay函數(shù)sendSignedTransaction?開始EthPay是否成功文章來源地址http://www.zghlxwxcb.cn/news/detail-558838.html
//開始執(zhí)行
const Transfer = Web3Network.eth.sendSignedTransaction(serializedTx)
// 監(jiān)聽是否成功 -- 加載中
Transfer.on('transactionHash', txid => {
console.log(
`查看----https://goerli.etherscan.io/tx/${txid}`
)
})
代碼
//一.第一部分
//transaction次數(shù)
const numberTransaction = await Web3Network.eth.getTransactionCount(
walletAddress.value
)
console.log(numberTransaction)
//獲取預(yù)計手續(xù)費
const serviceCharge = await Web3Network.eth.getGasPrice()
console.log(serviceCharge)
//以wei為單位數(shù)量
const WeiMoney = Web3.utils.toWei('0.0001')
console.log(WeiMoney)
//Api參數(shù)
const rawTx = {
form: walletAddress.value, //發(fā)送方地址
nonce: numberTransaction, //發(fā)送方transaction次數(shù)
gasPrice: serviceCharge, //預(yù)估手續(xù)費
to: 'xxxx', //接收方地址
value: WeiMoney, //以wei單位數(shù)量
data: '0x00' //轉(zhuǎn)Token會用到的一個字段
}
//把transaction數(shù)據(jù)進行g(shù)as計算
const gas = await Web3Network.eth.estimateGas(rawTx)
rawTx.gas = gas
//二.第二部分
//把privateKey轉(zhuǎn)換 數(shù)組hex
//因為 tx.sign() 要求長度必須在32位以內(nèi)所以需要轉(zhuǎn)換
const PrivatekeyHex = Buffer(walletPrivateKey.value, 'hex')
console.log(`key:`, PrivatekeyHex)
//privateKey加密
const tx = new Tx(rawTx)
tx.sign(PrivatekeyHex)
//通過ethereumjs-tx加密并轉(zhuǎn)為十六進制字符串
const serializedTx = '0x' + tx.serialize().toString('hex')
//開始執(zhí)行
const Transfer = Web3Network.eth.sendSignedTransaction(serializedTx)
// 監(jiān)聽是否成功 -- 加載中
Transfer.on('transactionHash', txid => {
console.log(txid)
console.log(
`查看----https://goerli.etherscan.io/tx/${txid}`
)
})
到了這里,關(guān)于【W(wǎng)eb3】 Web3JS Pay Api的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!