目录下多了两个账号的配置文件,后续也可以利用这两个文件将账户导入其他钱包
请求连接钱包:
export const requestMetamaskAccount = async () => {
if (!ethereum) {
return;
}
const accounts = await ethereum.request({ method: "eth_accounts" });
if (accounts.length === 0) {
currentAccount = null;
} else if (accounts[0] !== currentAccount) {
currentAccount = accounts[0];
}
return currentAccount;
};
发送交易:
const toAddress = document.getElementById("toAddress").value.trim();
const amount = document.getElementById("amount").value.trim();
const contractAddress = '0x74FE09B3bA8AdEa31f6448f4c742e9148A262d9b';
const abiInterface = new utils.Interface(abi);
const functionData = abiInterface.encodeFunctionData("transfer", [
toAddress,
parseInt(amount),
]);
const transactionParameters = {
nonce: "0x00", // ignored by MetaMask
gasPrice: "0x94810dee", // customizable by user during MetaMask confirmation.
gas: "0x8a82", // customizable by user during MetaMask confirmation.
to: contractAddress, // Required except during contract publications.
from: account, // must match user's active address.
value: "0x00", // Only required to send ether to the recipient from the initiating external account.
data: functionData,
chainId: "0x5", // Used to prevent transaction reuse across blockchains. Auto-filled by MetaMask.
};
const txHash = await ethereum.request({
method: "eth_sendTransaction",
params: [transactionParameters],
});
对静态资源使用 webpack 打包构建,然后通过 ipfs 将页面资源部署到去中心化的网络中
为了让大家对 Web3.0 常用的一些底层技术有所了解,本文的案例没有使用任何框架来开发,但实际开发中我们可以利用一些工具来帮助开发提效,例如Home – Truffle Suite[11]、Embark into the Ether. | Embark[12]、Fleek: Build on the New Internet[13]、Remix – Ethereum IDE[14]。
郑重声明:本文版权归原作者所有,转载文章仅为传播更多信息之目的,如作者信息标记有误,请第一时间联系我们修改或删除,多谢。
郑重声明:本文版权归原作者所有,转载文章仅为传播更多信息之目的,如作者信息标记有误,请第一时间联系我们修改或删除,多谢。