// 大多数 Provider 在页面加载时都可用作 window.ethereum。// 这只是一种约定,而不是标准,在实践中可能并非如此。// 请查阅 Provider 实现的文档。constethereum=window.ethereum;consttransactionParameters={...}// 填写参数ethereum.request({method:'eth_sendTransaction',params:[transactionParameters],}).then((txHash)=>{ethereum.on('tx_cancel',(info)=>{const{oldTx,newTx,nonce,from}=message.data;console.log(`Tx ${oldTx} with nonce ${nonce} from ${from} was cancelled, the new hash is ${newTx}`)// 交易 ${oldTx},nonce 为 ${nonce},来自 ${from} 已被取消,新的哈希是 ${newTx}});ethereum.on('tx_speedup',(info)=>{const{oldTx,newTx,nonce,from}=message.data;console.log(`Tx ${oldTx} with nonce ${nonce} from ${from} was sped up, the new hash is ${newTx}`)// 交易 ${oldTx},nonce 为 ${nonce},来自 ${from} 已被加速,新的哈希是 ${newTx}});ethereum.on('tx_replacement',(info)=>{const{oldTx,newTx,nonce,from}=message.data;console.log(`Tx ${oldTx} with nonce ${nonce} from ${from} was replaced, the new hash is ${newTx}`)// 交易 ${oldTx},nonce 为 ${nonce},来自 ${from} 已被替换,新的哈希是 ${newTx}});console.log(`Transaction hash ${txHash}`)// 交易哈希 ${txHash}}).catch((error)=>{console.error(`Error sending transaction: ${error.code}: ${error.message}`);// 发送交易时出错: ${error.code}: ${error.message}});