Last active
December 1, 2023 00:15
-
-
Save ericlee42/de052ce07a6b321c342e2b9b1daeba98 to your computer and use it in GitHub Desktop.
Metis Stardust WebSocket issue test scripts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// node --experimental-json-modules read.mjs | |
// node 17 only | |
import { ethers } from "ethers"; | |
import { readFile } from "fs/promises"; | |
import ERC20ABI from "./ERC20.json" assert { type: "json" }; | |
const privateKey = await readFile("key.txt", { encoding: "utf-8" }); | |
const rpcclient = new ethers.providers.WebSocketProvider( | |
"wss://stardust-ws.metis.io/" | |
); | |
const wallet = new ethers.Wallet(privateKey, rpcclient); | |
const token = new ethers.Contract( | |
"0x844C472e6f0950a65276ffAE9503Ba309C36EF3E", | |
ERC20ABI, | |
wallet | |
); | |
process.on("SIGINT", () => token.removeAllListeners()); | |
token.on("Transfer", (from, to, amount) => { | |
console.log(from, to, amount); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// node --experimental-json-modules read.mjs | |
// node 17 only | |
import { BigNumber, ethers } from "ethers"; | |
import { setTimeout } from "timers/promises"; | |
import { readFile } from "fs/promises"; | |
import ERC20ABI from "./ERC20.json" assert { type: "json" }; | |
const privateKey = await readFile("key.txt", { encoding: "utf-8" }); | |
const rpcclient = new ethers.providers.WebSocketProvider( | |
"wss://stardust-ws.metis.io/" | |
); | |
const wallet = new ethers.Wallet(privateKey, rpcclient); | |
const token = new ethers.Contract( | |
"0x844C472e6f0950a65276ffAE9503Ba309C36EF3E", | |
ERC20ABI, | |
wallet | |
); | |
const ac = new AbortController(); | |
process.on("SIGINT", ac.abort.bind(ac)); | |
const stopped = new Promise((res) => { | |
ac.signal.addEventListener("abort", () => res(true)); | |
}); | |
let shutdown = false; | |
while (!shutdown) { | |
const tx = await token.transfer( | |
wallet.address, | |
BigNumber.from(Math.trunc(Math.random() * 1000)) | |
); | |
const txr = await tx.wait(1); | |
console.log("Tx", tx.hash, "is comfirmed at", txr.blockNumber); | |
shutdown = await Promise.race([ | |
stopped, | |
setTimeout(5000, false, { signal: ac.signal }), | |
]); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[ | |
{ | |
"anonymous": false, | |
"inputs": [ | |
{ | |
"indexed": true, | |
"internalType": "address", | |
"name": "from", | |
"type": "address" | |
}, | |
{ | |
"indexed": true, | |
"internalType": "address", | |
"name": "to", | |
"type": "address" | |
}, | |
{ | |
"indexed": true, | |
"internalType": "uint256", | |
"name": "amount", | |
"type": "uint256" | |
} | |
], | |
"name": "Transfer", | |
"type": "event" | |
}, | |
{ | |
"inputs": [], | |
"name": "decimal", | |
"outputs": [ | |
{ | |
"internalType": "uint8", | |
"name": "", | |
"type": "uint8" | |
} | |
], | |
"stateMutability": "view", | |
"type": "function" | |
}, | |
{ | |
"inputs": [], | |
"name": "symbol", | |
"outputs": [ | |
{ | |
"internalType": "string", | |
"name": "", | |
"type": "string" | |
} | |
], | |
"stateMutability": "view", | |
"type": "function" | |
}, | |
{ | |
"inputs": [ | |
{ | |
"internalType": "address", | |
"name": "to", | |
"type": "address" | |
}, | |
{ | |
"internalType": "uint256", | |
"name": "amount", | |
"type": "uint256" | |
} | |
], | |
"name": "transfer", | |
"outputs": [ | |
{ | |
"internalType": "bool", | |
"name": "", | |
"type": "bool" | |
} | |
], | |
"stateMutability": "nonpayable", | |
"type": "function" | |
} | |
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// SPDX-License-Identifier: MIT | |
pragma solidity ^0.8.0; | |
contract ERC20 { | |
string public constant symbol = "ERC20"; | |
uint8 public constant decimal = 2; | |
event Transfer(address indexed from, address indexed to, uint256 indexed amount); | |
function transfer(address to, uint256 amount) public returns (bool) { | |
emit Transfer(msg.sender, to, amount); | |
return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment