Commit 5efbb9ab authored by Developer's avatar Developer

add test script

parent b40aa685
const { ContractFactory } = require("ethers");
const hre = require("hardhat");
async function forceRevert() {
// 部署合约
const factory = await hre.ethers.getContractFactory("RevertContract");
const contract = await factory.deploy();
await contract.waitForDeployment();
const contractAddress = await contract.getAddress();
console.log("Contract deployed at:", contractAddress);
const signer = new hre.ethers.Wallet(process.env.DEPLOY_PRIVATE_KEY, hre.ethers.provider);
const transferAmount = hre.ethers.parseEther("1"); // 小于 1 ETH
var nonce = await hre.ethers.provider.getTransactionCount(signer.address)
const chainid = await hre.ethers.provider.getNetwork().then(network => network.chainId);
// Encode the function call
const abi = ["function sendViaTransfer(address payable _to, uint256 _amount)"];
const iface = new hre.ethers.Interface(abi);
const data = iface.encodeFunctionData("sendViaTransfer", ['0xAD4143Df8Fe9E31B1C4318e8dE555872085f481d', transferAmount]);
var balance = await hre.ethers.provider.getBalance(signer.address);
console.log("Sender balance:", hre.ethers.formatEther(balance), " ETH");
// 签名交易
const signedTx = await signer.signTransaction({
type: 0,
chainId: chainid,
nonce: nonce,
to: contractAddress,
data: data,
value: transferAmount,
gasLimit: 1000000, // 设置 gas 限制
gasPrice: hre.ethers.parseUnits("10000", "gwei"), // 设置 gas 价格
});
// 发送交易
const txResponse = await hre.ethers.provider.broadcastTransaction(signedTx);
console.log("Transaction sent:", txResponse.hash);
// wait receipt
const receipt = await txResponse.wait();
console.log("Transaction receipt:", receipt);
balance = await hre.ethers.provider.getBalance(signer.address);
console.log("Sender balance:", hre.ethers.formatEther(balance), " ETH");
}
// Define the script
async function main() {
await forceRevert()
}
// Run the script
main().catch((error) => {
console.error("Error:", error);
});
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment