以太坊上钱包开发示例:创建账户,余额获取和转账

在以太坊上开发钱包涉及使用以太坊的编程接口和工具。以下是一个简单的示例,展示如何使用JavaScript和Web3.js库在以太坊上创建账户、获取余额和进行转账。这个示例假设你已经安装了Node.js和npm。
### 1. 安装依赖
首先,你需要安装Web3.js库。在你的项目目录中打开终端并运行以下命令:
```bash npm install web3 ```
### 2. 创建账户
以下是一个简单的JavaScript脚本,展示如何使用Web3.js创建一个新的以太坊账户。
```javascript const Web3 = require('web3');
// 连接到以太坊节点(例如Infura) const web3 = new Web3('https://mainnet.infura.io/v3/YOUR_INFURA_PROJECT_ID');
async function createAccount() { try { // 生成新的账户 const account = await web3.eth.generateAccount(); console.log('New account:', account.address); console.log('Private key:', account.privateKey); return account; } catch (error) { console.error('Error creating account:', error); } }
createAccount(); ```
### 3. 获取账户余额
以下是一个函数,展示如何获取账户的余额。
```javascript async function getAccountBalance(accountAddress) { try { const balance = await web3.eth.getBalance(accountAddress); console.log(`Balance for ${accountAddress}:

相关内容:

整个实现过程:首先创建一个账户,得到新账户的address和privatekey两个值,现在获取到的余额肯定是0,从metamask里面给新建的账户转点钱,再次获取余额。然后再从新建的账户给metamask转点钱。

有一些基本知识:Metamask是一个流行的加密货币钱包和浏览器扩展,主要用于管理以太坊(Ethereum)及其上运行的数字资产和去中心化应用(DApps)。它充当了用户与以太坊区块链之间的桥梁,允许用户轻松进行交易、存储和管理加密货币。刚开始metamask上肯定是没有钱的,这就需要我们往里面转钱,可以通过欧易往真实的以太坊主网(Ethereum Mainnet)上充钱,也可以通过Sepolia-faucet挖矿往以太坊测试网(Sepolia)上充测试币。当然也有一些别的测试网络可以选择,我这里用的是Sepolia。

转账过程中需要知道两个概念gas和gasprice:Gas 是以太坊网络中用于衡量执行特定操作或智能合约所需计算工作量的单位。每个操作(如转账、部署智能合约、调用函数等)都有对应的 gas 消耗量。Gas price 是你愿意为每个单位的 gas 支付的价格,通常以 Gwei(1 Gwei = 0.000000001 Ether)为单位。Gwei 是 Ether 的十亿分之一。假设你有一个简单的转账操作,消耗 21,000 gas,你设置的 gas price 是 50 Gwei。那么,这次转账的总费用将是:总费用=21,000 gas×50 Gwei/gas=1,050,000 Gwei=1.05 Ether总费用=21,000gas×50Gwei/gas=1,050,000Gwei=1.05Eth

我这个测试代码里,要从我自己新建的账户给metamask转账,就是先从网络上获取现在的Gas price,乘以最小的gas限额,就可以算出总费用,这个费用一定要小于你现在账户里面的数值,当然在代码实现时,要注意把所有的数值使用BigInt() 进行转换,确保所有涉及运算的数值保持一致性。

1,创建账户

npm install web3
import Web3 from "web3";
//创建 Web3 实例,优先使用 Web3.givenProvider(如浏览器扩展),否则使用 Infura 提供的 WebSocket端点连接Sepolia测试网络
var web3= new Web3(Web3.givenProvider||"wss://sepolia.infura.io/ws/v3/401c8c4fc37440068881de3787afcd91");
// 创建新的以太坊账户,使用 '123' 作为种子生成私钥和地址
const account=web3.eth.accounts.create("123");
console.log(account);

得到的账户主要包括address和privatekey两个值。每跑一次程序就会新生成一个账户,所以跑一次就把账户记下来,把这段代码注释掉

2,余额获取

现在获取到的余额肯定是0,需要往里面转点测试币。

var web3 = new Web3(Web3.givenProvider || "wss://sepolia.infura.io/ws/v3/401c8c4fc37440068881de3787afcd91");

// 输出提供者的信息,确认连接到正确的网络
console.log("Given provider is", Web3.givenProvider);
console.log("Web3 instance:", web3);

// 检查WebSocket是否连接成功
web3.eth.net.isListening()
.then(() => console.log('Web3 connected via WebSocket!'))
.catch(e => console.log('Failed to connect via WebSocket:', e));

// 创建以太坊地址的引用
const address = ref("0x0e5B745F6621fbe013f5178f6E9F0114cE044a00");
const mount = ref(-1);
console.log("Address is", address.value);

// 获取余额
web3.eth.getBalance(address.value).then((res) => {
console.log("Balance in Wei:", res); // 输出原始的余额(Wei单位)
mount.value = web3.utils.fromWei(res, 'ether'); // 转换成以太币单位
console.log("Balance in Ether:", mount.value); // 确保在异步操作完成后输出
}).catch(err => {
console.error("Error fetching balance:", err); // 捕获错误并输出
});

3,转账

从新建的账户往metamask里面转测试币

const send = async() =>{
const valueToSend = BigInt(web3.utils.toWei('0.00000001', 'ether')); // 发送0.1 ETHvalueToSend=10000000Wei
const nonce = await web3.eth.getTransactionCount(address.value); // nonce是地址的交易次数,用于确保交易顺序。
console.log("Nonce:", nonce);
const gasPrice = BigInt(await web3.eth.getGasPrice());// 获取当前 gas 价格
console.log("gasPrice value is", gasPrice.toString());//gasPrice value is 6404208594
const gasLimit = BigInt(21000);
const gasFee = BigInt(gasPrice)* gasLimit;
console.log("gasFee value is", gasFee.toString());//gasFee value is 134488380474000
const totalValue = valueToSend + gasFee;
console.log("totalValue value is", totalValue);//totalValue value is 134498380474000n
const balance = BigInt(await web3.eth.getBalance(address.value));
console.log("balance value is", balance);//balance value is 165482219930039000n
if (balance < totalValue) {
throw new Error("Insufficient funds for transfer");
}
const tx = {
from: address.value,// 发送方地址
to: "0xF2FB8e02B042d97B32618061b698D6A2B10fEaEb",// 接收方地址
value: web3.utils.toHex(valueToSend),
gas: web3.utils.toHex(21000), // 发送 ETH 的最小 gas 限额
gasPrice: web3.utils.toHex(gasPrice),
nonce: web3.utils.toHex(nonce)
};
const priKey = Buffer.from(privateKey.value.slice(2), "hex");
const gas=await web3.eth.estimateGas(tx);
console.log("estimateGasPrice value is", gas);//estimateGasPrice value is 21000n
tx.gas = web3.utils.toHex(gas);
console.log("estimateGasPricetoHex value is", tx.gas);//estimateGasPricetoHex value is 0x5208
const newtx=new Tx(tx);
console.log("newtx value is", newtx);
newtx.sign(priKey);
const serializedTX = '0x' + newtx.serialize().toString("hex");
console.log("serialiazedTX value is", serializedTX);
const trans=web3.eth.sendSignedTransaction(serializedTX);
trans.on("transactionHash",(txid)=>{
console.log("交易id",txid);
});
trans.on("receipt",(res)=>{
console.log("第一个节点确认",res);
});
trans.on("confimation",(res)=>{
console.log("第n个节点确认",res);
});
}
</script>

网页上写个发送按钮,成功转账

<template>
<p>test</p>
<p>{{address}}</p>
<p>{{mount}}</p>
<h1>transfer account</h1>
<van-pider/>
<van-button type="primary" @click="send">transfer</van-button>
</template>

关于作者: 网站小编

码农网专注IT技术教程资源分享平台,学习资源下载网站,58码农网包含计算机技术、网站程序源码下载、编程技术论坛、互联网资源下载等产品服务,提供原创、优质、完整内容的专业码农交流分享平台。

热门文章