contract_monad.mjs 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import { ethers } from 'ethers';
  2. // Monad 测试网 RPC URL
  3. const providerUrl = 'https://testnet-rpc.monad.xyz';
  4. const provider = new ethers.JsonRpcProvider(providerUrl);
  5. // 钱包私钥
  6. const privateKey = '0x3991542110242368f4770716be904b0ca6d44a8dbe4501771833b1a3642198d1';
  7. const wallet = new ethers.Wallet(privateKey, provider);
  8. // 质押合约地址
  9. const stakingContractAddress = '0x2c9c959516e9aaedb2c748224a41249202ca8be7';
  10. // 质押 $MON
  11. async function stakeMon() {
  12. try {
  13. // 检查原生代币余额
  14. const balance = await provider.getBalance(wallet.address);
  15. console.log('Native balance:', ethers.formatEther(balance));
  16. // 质押金额
  17. const amountToStake = ethers.parseEther('0.0001'); // 0.0001 原生代币
  18. if (balance < amountToStake) {
  19. throw new Error('Insufficient balance for staking');
  20. }
  21. // 低级调用,发送交易
  22. const tx = await wallet.sendTransaction({
  23. to: stakingContractAddress,
  24. value: amountToStake,
  25. data: '0xd5575982', // 函数选择器
  26. gasLimit: 100000,
  27. gasPrice: ethers.parseUnits('50', 'gwei')
  28. });
  29. console.log('Stake transaction hash:', tx.hash);
  30. // 等待交易确认
  31. const receipt = await tx.wait();
  32. console.log('Stake transaction receipt:', receipt);
  33. } catch (error) {
  34. console.error('Error staking $MON:', error);
  35. }
  36. }
  37. // 调用质押函数
  38. stakeMon();