send_somnia.mjs 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. // import { ethers } from 'ethers';
  2. import { ethers } from "https://cdn-cors.ethers.io/lib/ethers-5.6.9.esm.min.js";
  3. // 设置 RPC URL
  4. const providerUrl = 'https://dream-rpc.somnia.network';
  5. const provider = new ethers.JsonRpcProvider(providerUrl);
  6. // 替换为您的钱包私钥(发送方钱包)
  7. const privateKey = '0x3991542110242368f4770716be904b0ca6d44a8dbe4501771833b1a3642198d1'; // 私钥
  8. const wallet = new ethers.Wallet(privateKey, provider);
  9. // 接收方钱包地址
  10. const recipientAddress = '0x70D5EE1DfddD3726f0D71F4CD5a8ef43aC651a75';
  11. // 发送代币的函数
  12. async function sendToken() {
  13. try {
  14. const amountToSend = ethers.parseEther('0.0000000000001'); // 发送数量
  15. // 构造交易对象
  16. const tx = {
  17. to: recipientAddress,
  18. value: amountToSend
  19. };
  20. // 发送交易
  21. const txResponse = await wallet.sendTransaction(tx);
  22. console.log('Transaction hash:', txResponse.hash);
  23. // 等待交易确认
  24. const receipt = await txResponse.wait();
  25. console.log('Transaction receipt:', receipt);
  26. } catch (error) {
  27. console.error('Error sending Token:', error);
  28. }
  29. }
  30. // 调用发送Token的函数
  31. sendToken();