|
| 1 | +import assert from "assert"; |
| 2 | + |
| 3 | +import { |
| 4 | + isError, makeError, |
| 5 | + |
| 6 | + AbstractProvider, FallbackProvider, Network, |
| 7 | +} from "../index.js"; |
| 8 | + |
| 9 | +import type { |
| 10 | + PerformActionRequest |
| 11 | +} from "../index.js"; |
| 12 | + |
| 13 | + |
| 14 | + |
| 15 | +const network = Network.from("mainnet"); |
| 16 | + |
| 17 | +function stall(duration: number): Promise<void> { |
| 18 | + return new Promise((resolve) => { setTimeout(resolve, duration); }); |
| 19 | +} |
| 20 | + |
| 21 | + |
| 22 | +export type Performer = (req: PerformActionRequest) => Promise<any>; |
| 23 | + |
| 24 | +export class MockProvider extends AbstractProvider { |
| 25 | + readonly _perform: Performer; |
| 26 | + |
| 27 | + constructor(perform: Performer) { |
| 28 | + super(network, { cacheTimeout: -1 }); |
| 29 | + this._perform = perform; |
| 30 | + } |
| 31 | + |
| 32 | + async _detectNetwork(): Promise<Network> { return network; } |
| 33 | + |
| 34 | + async perform(req: PerformActionRequest): Promise<any> { |
| 35 | + return await this._perform(req); |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +describe("Test Fallback broadcast", function() { |
| 40 | + |
| 41 | + const txHash = "0x33017397ef7c7943dee3b422aec52b0a210de58d73d49c1b3ce455970f01c83a"; |
| 42 | + |
| 43 | + async function test(actions: Array<{ timeout: number, error?: Error }>): Promise<any> { |
| 44 | + // https://sepolia.etherscan.io/tx/0x33017397ef7c7943dee3b422aec52b0a210de58d73d49c1b3ce455970f01c83a |
| 45 | + const tx = "0x02f87683aa36a7048459682f00845d899ef982520894b5bdaa442bb34f27e793861c456cd5bdc527ac8c89056bc75e2d6310000080c001a07503893743e94445b2361a444343757e6f59d52e19e9b3f65eb138d802eaa972a06e4e9bc10ff55474f9aac0a4c284733b4195cb7b273de5e7465ce75a168e0c38"; |
| 46 | + |
| 47 | + const providers: Array<MockProvider> = actions.map(({ timeout, error }) => { |
| 48 | + return new MockProvider(async (r) => { |
| 49 | + if (r.method === "getBlockNumber") { return 1; } |
| 50 | + if (r.method === "broadcastTransaction") { |
| 51 | + await stall(timeout); |
| 52 | + if (error) { throw error; } |
| 53 | + return txHash; |
| 54 | + } |
| 55 | + throw new Error(`unhandled method: ${ r.method }`); |
| 56 | + }); |
| 57 | + });; |
| 58 | + |
| 59 | + const provider = new FallbackProvider(providers); |
| 60 | + return await provider.broadcastTransaction(tx); |
| 61 | + } |
| 62 | + |
| 63 | + it("picks late non-failed broadcasts", async function() { |
| 64 | + const result = await test([ |
| 65 | + { timeout: 200, error: makeError("already seen", "UNKNOWN_ERROR") }, |
| 66 | + { timeout: 4000, error: makeError("already seen", "UNKNOWN_ERROR") }, |
| 67 | + { timeout: 400 }, |
| 68 | + ]); |
| 69 | + assert(result.hash === txHash, "result.hash === txHash"); |
| 70 | + }); |
| 71 | + |
| 72 | + it("picks late non-failed broadcasts with quorum-met red-herrings", async function() { |
| 73 | + const result = await test([ |
| 74 | + { timeout: 200, error: makeError("bad nonce", "NONCE_EXPIRED") }, |
| 75 | + { timeout: 400, error: makeError("bad nonce", "NONCE_EXPIRED") }, |
| 76 | + { timeout: 1000 }, |
| 77 | + ]); |
| 78 | + assert(result.hash === txHash, "result.hash === txHash"); |
| 79 | + }); |
| 80 | + |
| 81 | + it("insufficient funds short-circuit broadcast", async function() { |
| 82 | + await assert.rejects(async function() { |
| 83 | + const result = await test([ |
| 84 | + { timeout: 200, error: makeError("is broke", "INSUFFICIENT_FUNDS") }, |
| 85 | + { timeout: 400, error: makeError("is broke", "INSUFFICIENT_FUNDS") }, |
| 86 | + { timeout: 800 }, |
| 87 | + { timeout: 1000 }, |
| 88 | + ]); |
| 89 | + console.log(result); |
| 90 | + }, function(error: unknown) { |
| 91 | + assert(isError(error, "INSUFFICIENT_FUNDS")); |
| 92 | + return true; |
| 93 | + }); |
| 94 | + }); |
| 95 | +}); |
0 commit comments