ethers.js/lib.commonjs/_tests/test-contract.js
2022-11-09 22:45:17 -05:00

279 lines
13 KiB
JavaScript

"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const assert_1 = __importDefault(require("assert"));
const create_provider_js_1 = require("./create-provider.js");
const index_js_1 = require("../index.js");
describe("Test Contract", function () {
const addr = "0x99417252Aad7B065940eBdF50d665Fb8879c5958";
const abi = [
"error CustomError1(uint256 code, string message)",
"event EventUint256(uint256 indexed value)",
"event EventAddress(address indexed value)",
"event EventString(string value)",
"event EventBytes(bytes value)",
"function testCustomError1(bool pass, uint code, string calldata message) pure returns (uint256)",
"function testErrorString(bool pass, string calldata message) pure returns (uint256)",
"function testPanic(uint256 code) returns (uint256)",
"function testEvent(uint256 valueUint256, address valueAddress, string valueString, bytes valueBytes) public",
"function testCallAdd(uint256 a, uint256 b) pure returns (uint256 result)"
];
it("tests contract calls", async function () {
this.timeout(10000);
const provider = (0, create_provider_js_1.getProvider)("InfuraProvider", "goerli");
const contract = new index_js_1.Contract(addr, abi, provider);
assert_1.default.equal(await contract.testCallAdd(4, 5), BigInt(9), "testCallAdd(4, 5)");
assert_1.default.equal(await contract.testCallAdd(6, 0), BigInt(6), "testCallAdd(6, 0)");
});
it("tests events", async function () {
this.timeout(60000);
const provider = (0, create_provider_js_1.getProvider)("InfuraProvider", "goerli");
assert_1.default.ok(provider);
const contract = new index_js_1.Contract(addr, abi, provider);
const signer = new index_js_1.Wallet((process.env.FAUCET_PRIVATEKEY), provider);
const contractSigner = contract.connect(signer);
const vUint256 = 42;
const vAddrName = "ethers.eth";
const vAddr = "0x228568EA92aC5Bc281c1E30b1893735c60a139F1";
const vString = "Hello";
const vBytes = "0x12345678";
let hash = null;
// Test running a listener for a specific event
const specificEvent = new Promise((resolve, reject) => {
contract.on("EventUint256", async (value, event) => {
// Triggered by someone else
if (hash == null || hash !== event.log.transactionHash) {
return;
}
try {
assert_1.default.equal(event.filter, "EventUint256", "event.filter");
assert_1.default.equal(event.fragment.name, "EventUint256", "event.fragment.name");
assert_1.default.equal(event.log.address, addr, "event.log.address");
assert_1.default.equal(event.args.length, 1, "event.args.length");
assert_1.default.equal(event.args[0], BigInt(42), "event.args[0]");
const count = await contract.listenerCount("EventUint256");
await event.removeListener();
assert_1.default.equal(await contract.listenerCount("EventUint256"), count - 1, "decrement event count");
resolve(null);
}
catch (e) {
event.removeListener();
reject(e);
}
});
});
// Test running a listener on all (i.e. "*") events
const allEvents = new Promise((resolve, reject) => {
const waitingFor = {
EventUint256: vUint256,
EventAddress: vAddr,
EventString: vString,
EventBytes: vBytes
};
contract.on("*", (event) => {
// Triggered by someone else
if (hash == null || hash !== event.log.transactionHash) {
return;
}
try {
const name = event.eventName;
assert_1.default.equal(event.args[0], waitingFor[name], `${name}`);
delete waitingFor[name];
if (Object.keys(waitingFor).length === 0) {
event.removeListener();
resolve(null);
}
}
catch (error) {
reject(error);
}
});
});
// Send a transaction to trigger some events
const tx = await contractSigner.testEvent(vUint256, vAddr, vString, vBytes);
hash = tx.hash;
const checkEvent = (filter, event) => {
const values = {
EventUint256: vUint256,
EventString: vString,
EventAddress: vAddr,
EventBytes: vBytes
};
assert_1.default.ok(event instanceof index_js_1.EventLog, `queryFilter(${filter}):isEventLog`);
const name = event.eventName;
assert_1.default.equal(event.address, addr, `queryFilter(${filter}):address`);
assert_1.default.equal(event.args[0], values[name], `queryFilter(${filter}):args[0]`);
};
const checkEventFilter = async (filter) => {
const events = (await contract.queryFilter(filter, -10)).filter((e) => (e.transactionHash === hash));
assert_1.default.equal(events.length, 1, `queryFilter(${filter}).length`);
checkEvent(filter, events[0]);
return events[0];
};
const receipt = await tx.wait();
// Check the logs in the receipt
for (const log of receipt.logs) {
checkEvent("receipt", log);
}
// Various options for queryFilter
await checkEventFilter("EventUint256");
await checkEventFilter(["EventUint256"]);
await checkEventFilter([["EventUint256"]]);
await checkEventFilter("EventUint256(uint)");
await checkEventFilter(["EventUint256(uint)"]);
await checkEventFilter([["EventUint256(uint)"]]);
await checkEventFilter([["EventUint256", "EventUint256(uint)"]]);
await checkEventFilter("0x85c55bbb820e6d71c71f4894e57751de334b38c421f9c170b0e66d32eafea337");
// Query by Event
await checkEventFilter(contract.filters.EventUint256);
// Query by Deferred Topic Filter; address
await checkEventFilter(contract.filters.EventUint256(vUint256));
// Query by Deferred Topic Filter; address
await checkEventFilter(contract.filters.EventAddress(vAddr));
// Query by Deferred Topic Filter; ENS name => address
await checkEventFilter(contract.filters.EventAddress(vAddrName));
// Multiple Methods
{
const filter = [["EventUint256", "EventString"]];
const events = (await contract.queryFilter(filter, -10)).filter((e) => (e.transactionHash === hash));
assert_1.default.equal(events.length, 2, `queryFilter(${filter}).length`);
for (const event of events) {
checkEvent(filter, event);
}
}
await specificEvent;
await allEvents;
});
});
describe("Test Typed Contract Interaction", function () {
const tests = [
{
types: ["uint8", "uint16", "uint24", "uint32", "uint40", "uint48", "uint56", "uint64", "uint72", "uint80", "uint88", "uint96", "uint104", "uint112", "uint120", "uint128", "uint136", "uint144", "uint152", "uint160", "uint168", "uint176", "uint184", "uint192", "uint200", "uint208", "uint216", "uint224", "uint232", "uint240", "uint248", "uint256", "int8", "int16", "int24", "int32", "int40", "int48", "int56", "int64", "int72", "int80", "int88", "int96", "int104", "int112", "int120", "int128", "int136", "int144", "int152", "int160", "int168", "int176", "int184", "int192", "int200", "int208", "int216", "int224", "int232", "int240", "int248", "int256"],
valueFunc: (type) => { return 42; }
},
{
types: [
"bytes1", "bytes2", "bytes3", "bytes4", "bytes5", "bytes6", "bytes7", "bytes8", "bytes9", "bytes10", "bytes11", "bytes12", "bytes13", "bytes14", "bytes15", "bytes16", "bytes17", "bytes18", "bytes19", "bytes20", "bytes21", "bytes22", "bytes23", "bytes24", "bytes25", "bytes26", "bytes27", "bytes28", "bytes29", "bytes30", "bytes31", "bytes32",
"bytes"
],
valueFunc: (type) => {
const length = type.substring(5);
if (length) {
const value = new Uint8Array(parseInt(length));
value.fill(42);
return value;
}
return "0x123456";
}
}, {
types: ["bool"],
valueFunc: (type) => { return true; }
}, {
types: ["address"],
valueFunc: (type) => { return "0x643aA0A61eADCC9Cc202D1915D942d35D005400C"; }
}, {
types: ["string"],
valueFunc: (type) => { return "someString"; }
}
];
const abi = [];
for (let i = 1; i <= 32; i++) {
abi.push(`function testTyped(uint${i * 8}) public pure returns (string memory)`);
abi.push(`function testTyped(int${i * 8}) public pure returns (string memory)`);
abi.push(`function testTyped(bytes${i}) public pure returns (string memory)`);
}
abi.push(`function testTyped(address) public pure returns (string memory)`);
abi.push(`function testTyped(bool) public pure returns (string memory)`);
abi.push(`function testTyped(bytes memory) public pure returns (string memory)`);
abi.push(`function testTyped(string memory) public pure returns (string memory)`);
const addr = "0x838f41545DA5e18AA0e1ab391085d22E172B7B02";
const provider = (0, create_provider_js_1.getProvider)("InfuraProvider", "goerli");
const contract = new index_js_1.Contract(addr, abi, provider);
for (const { types, valueFunc } of tests) {
for (const type of types) {
const value = valueFunc(type);
it(`tests typed value: Typed.from(${type})`, async function () {
const v = index_js_1.Typed.from(type, value);
const result = await contract.testTyped(v);
assert_1.default.equal(result, type);
});
it(`tests typed value: Typed.${type}()`, async function () {
const v = index_js_1.Typed[type](value);
const result = await contract.testTyped(v);
assert_1.default.equal(result, type);
});
}
}
});
/*
describe("Test Contract Calls", function() {
it("finds typed methods", async function() {
const contract = new Contract("0xC18360217D8F7Ab5e7c516566761Ea12Ce7F9D72", [
"function foo(string s) view returns (uint)",
"function foo(uint8) view returns (uint)",
"function foo(uint u, bool b) view returns (uint)",
]);
const value = Typed.string("42");
await contract.foo.populateTransaction(value, Typed.overrides({ value: 100 }))
contract["foo(string)"].fragment
});
});
describe("Test Contract Interface", function() {
it("builds contract interfaces", async function() {
this.timeout(60000);
interface Erc20Interface {
// Constant Methods
balanceOf: ConstantContractMethod<[ address: string | Addressable ], bigint>;
decimals: ConstantContractMethod<[ ], bigint>;
name: ConstantContractMethod<[ ], string>;
symbol: ConstantContractMethod<[ ], string>;
// Mutation Methods
transferFrom: ContractMethod<[ address: string | Addressable,
address: string | Addressable, amount: BigNumberish ], boolean>;
// Events
filters: {
Transfer: ContractEvent<[ from: Addressable | string, to: BigNumberish ]>;
}
}
const erc20Abi = [
"function balanceOf(address owner) view returns (uint)",
"function decimals() view returns (uint)",
"function name() view returns (string)",
"function symbol() view returns (string)",
"function transferFrom(address from, address to, uint amount) returns (boolean)",
"event Transfer(address indexed from, address indexed to, uint amount)"
];
class Erc20Contract extends BaseContract.buildClass<Erc20Interface>(erc20Abi) { };
const provider = new providers.InfuraProvider();
// ENS
//const addr = "0xC18360217D8F7Ab5e7c516566761Ea12Ce7F9D72";
// DAI
const addr = "0x6B175474E89094C44Da98b954EedeAC495271d0F";
const contract = new Erc20Contract(addr, provider);
console.log("SYMBOL", await contract.symbol());
console.log("DECIMALS", await contract.decimals());
console.log(await contract.balanceOf("0x5555763613a12D8F3e73be831DFf8598089d3dCa"));
console.log(await contract.balanceOf("ricmoo.eth"));
await contract.on(contract.filters.Transfer, (from, to, value, event) => {
console.log("HELLO!", { from, to, value, event });
event.removeListener();
});
const logs = await contract.queryFilter("Transfer", -10);
console.log(logs, logs[0], logs[0].args.from);
});
});
*/
//# sourceMappingURL=test-contract.js.map