Skip to content

Commit cb8bf2a

Browse files
committed
Add study notes for 2025-08-19
1 parent 686d38a commit cb8bf2a

1 file changed

Lines changed: 100 additions & 0 deletions

File tree

xmhhmx.md

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,106 @@ timezone: UTC+8
1515
## Notes
1616

1717
<!-- Content_START -->
18+
# 2025-08-19
19+
20+
转账web3js功能
21+
22+
```js
23+
<script setup>
24+
import { ref } from 'vue'
25+
import Web3 from 'web3'
26+
import Tx from 'ethereumjs-tx'
27+
28+
//实例化web3
29+
var web3 = new Web3(
30+
Web3.givenProvider ||
31+
"wss://eth-sepolia.g.alchemy.com/v2/Vf-WXrhQ2Fv_u9XbSC9Mc"
32+
);
33+
//创建账户
34+
//只要执行一次 就会创建一个新的
35+
const account = web3.eth.accounts.create('123')
36+
// console.log(account.address)
37+
// console.log(account.privateKey)
38+
const address = ref('0x6191a878F0CB11E707271bce5C5e6d20dbF7f6f5')
39+
const privateKey = ref('0x5b73f024533766c257ddc575580fac09d69bb73197f7b0cdd8938ad244c4360b')
40+
41+
//获取余额
42+
const mount = ref(-1)
43+
web3.eth.getBalance(address.value).then((res) => {
44+
mount.value = Web3.utils.fromWei(res,"ether")
45+
})
46+
47+
//单位转换
48+
const num1 = web3.utils.toWei('0.3','ether')
49+
console.log(num1)
50+
const num2 =Web3.utils.fromWei('3000000',"ether");
51+
console.log(num2)
52+
53+
//转账操作
54+
const send = async() => {
55+
//1.构建转账参数
56+
//获取账户交易次数
57+
const nonce = await web3.eth.getTransactionCount(address.value)
58+
//获预计转账的gas费用
59+
// const gasPrice = await web3.eth.getGasPrice()
60+
const gasPrice = web3.utils.toWei('10', 'gwei');
61+
const gasLimit = 21000
62+
// console.log(gasPrice) //以wei为单位
63+
//转账金额 以wei作为单位
64+
const value = web3.utils.toWei('0.0005','ether')
65+
const rawTx = {
66+
from: address.value,
67+
nonce: web3.utils.toHex(nonce),
68+
gasPrice: web3.utils.toHex(gasPrice),
69+
gasLimit: web3.utils.toHex(gasLimit),
70+
to: '0xE1e55b0dc88EA14be89e218F403734bE0cADd27f',
71+
value: web3.utils.toHex(value),
72+
data: "0x",
73+
}
74+
75+
//2.生成serializedTx
76+
//转换私钥
77+
const pKey = Buffer(privateKey.value.slice(2), "hex")
78+
//gas 估算
79+
const gas = await web3.eth.estimateGas(rawTx)
80+
rawTx.gas = gas;
81+
//ethereumjs-tx 实现私钥加密
82+
const tx = new Tx(rawTx)
83+
tx.sign(pKey)
84+
//生成serializedTx
85+
const serializedTx = '0x' + tx.serializedTx().toString("hex")
86+
console.log(serializedTx)
87+
88+
//3.开始转账
89+
const trans = web3.eth.sendSignedTransaction(serializedTx)
90+
trans.on('transactionHash', (txid) => {
91+
console.log('交易ID:',txid )
92+
})
93+
94+
}
95+
96+
</script>
97+
98+
<template>
99+
<h1>账户信息:</h1>
100+
<van-divider hairline />
101+
<p>地址:{{ address }}</p>
102+
<p>私钥:{{ privateKey }}</p>
103+
<p>余额: {{ mount }}</p>
104+
<van-divider hairline />
105+
106+
<h1>转账操作:</h1>
107+
<van-button type="primary" @click="send">开始转账</van-button>
108+
</template>
109+
110+
<style lang="scss">
111+
body{
112+
padding: 20px;
113+
}
114+
</style>
115+
116+
```
117+
18118
# 2025-08-17
19119

20120
今日依旧CTF,所以没时间学习

0 commit comments

Comments
 (0)