@ethersproject/contracts#BaseContract TypeScript Examples

The following examples show how to use @ethersproject/contracts#BaseContract. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: entities-test.ts    From sdk with ISC License 5 votes vote down vote up
describe("Entities tests", function(this: Mocha.Suite) {
    enum EntityKind {
        SynapseBridge    = "SynapseBridge",
        L1BridgeZap      = "L1BridgeZap",
        L2BridgeZap      = "L2BridgeZap",
        GenericZapBridge = "GenericZapBridge",
        BridgeConfigV3   = "BridgeConfigV3",
    }

    interface fnArgs {
        chainId:          number,
        signerOrProvider: SignerOrProvider,
    }

    interface TestCase {
        chainId: number,
        fn:      (args: fnArgs) => BaseContract,
        kind:    EntityKind,
    }

    function makeTestCase(chainId: number, kind: EntityKind): TestCase {
        let fn: (args: fnArgs) => BaseContract;

        switch (kind) {
            case EntityKind.SynapseBridge:
                fn = SynapseBridgeContractInstance;
                break;
            case EntityKind.L1BridgeZap:
                fn = L1BridgeZapContractInstance;
                break;
            case EntityKind.L2BridgeZap:
                fn = L2BridgeZapContractInstance;
                break;
            case EntityKind.GenericZapBridge:
                fn = GenericZapBridgeContractInstance;
                break;
            case EntityKind.BridgeConfigV3:
                fn = BridgeConfigV3ContractInstance;
                break;
        }

        return {chainId, fn, kind}
    }

    [
        makeTestCase(ChainId.FANTOM, EntityKind.SynapseBridge),
        makeTestCase(ChainId.FANTOM, EntityKind.L2BridgeZap),
        makeTestCase(ChainId.BSC,    EntityKind.SynapseBridge),
        makeTestCase(ChainId.BSC,    EntityKind.L2BridgeZap),
        makeTestCase(ChainId.ETH,    EntityKind.SynapseBridge),
        makeTestCase(ChainId.ETH,    EntityKind.L1BridgeZap),
        makeTestCase(ChainId.ETH,    EntityKind.BridgeConfigV3),
    ].forEach(tc => {
        describe(Networks.networkName(tc.chainId), function(this: Mocha.Suite) {
            const
                provider                = rpcProviderForChain(tc.chainId),
                newInstanceArgs: fnArgs = {chainId: tc.chainId, signerOrProvider: provider};

            let instance: BaseContract = tc.fn(newInstanceArgs);

            it(
                `Test ${EntityKind[tc.kind]} instance`,
                wrapExpect(expectNull(instance, false))
            )
        })
    })
})
Example #2
Source File: SwapRate-test.ts    From sdk with ISC License 4 votes vote down vote up
describe("TokenSwap -- Asynchronous Tests", function(this: Mocha.Suite) {
    describe("calculateSwapRate() tests", function(this: Mocha.Suite) {
        interface TestCase {
            chainId:   number,
            tokenFrom: Token,
            tokenTo:   Token,
            amountIn:  BigNumber,
            wantError: boolean,
        }

        const makeTestCase = (c: number, t1: Token, t2: Token, amt?: string, wantError?: boolean): TestCase =>
            ({
                chainId:   c,
                tokenFrom: t1,
                tokenTo:   t2,
                amountIn:  getTestAmount(t1, c, amt),
                wantError: wantError ?? false,
            });

        [
            makeTestCase(ChainId.ETH,        Tokens.DAI,        Tokens.USDC),
            makeTestCase(ChainId.ETH,        Tokens.ETH,        Tokens.NETH, null, true),
            makeTestCase(ChainId.OPTIMISM,   Tokens.WETH,       Tokens.NETH),
            makeTestCase(ChainId.BSC,        Tokens.BUSD,       Tokens.USDT),
            makeTestCase(ChainId.BSC,        Tokens.NUSD,       Tokens.BUSD),
            makeTestCase(ChainId.BSC,        Tokens.NUSD,       Tokens.DAI,  null, true),
            makeTestCase(ChainId.ARBITRUM,   Tokens.NEWO,       Tokens.UST,  null, true),
        ].forEach((tc: TestCase) => {
            const
                titleSuffix: string = tc.wantError ? "should fail" : "should pass",
                tokFrom: string     = tc.tokenFrom.symbol,
                tokTo: string       = tc.tokenTo.symbol,
                testTitle: string   = `for ${tokFrom} => ${tokTo} on ${Networks.networkName(tc.chainId)} ${titleSuffix}`,
                testTitle1: string  = `calculateSwapRate ${testTitle}`,
                testTitle2: string  = `buildSwapTokensTransaction ${testTitle}`,
                testTitle3: string  = `swapSetup ${testTitle}`;

            let amountOut: BigNumber;

            step(testTitle1, async function(this: Mocha.Context) {
                this.timeout(DEFAULT_TEST_TIMEOUT);

                let prom: Promise<TokenSwap.EstimatedSwapRate> = Promise.resolve(TokenSwap.calculateSwapRate({
                    chainId:   tc.chainId,
                    tokenFrom: tc.tokenFrom,
                    tokenTo:   tc.tokenTo,
                    amountIn:  tc.amountIn,
                })).then((res) => {
                    amountOut = res.amountOut;
                    return res
                });

                return tc.wantError
                    ? await expectRejected(prom)
                    : expectProperty(await prom, "amountOut").that.is.gt(Zero.toNumber())
            })

            step(testTitle2, async function(this: Mocha.Context) {
                this.timeout(DEFAULT_TEST_TIMEOUT);

                const args: TokenSwap.SwapTokensParams = {
                    ...tc,
                    minAmountOut: amountOut,
                };

                let prom = TokenSwap.buildSwapTokensTransaction(args);

                return (await (
                    tc.wantError
                        ? expectRejected(prom)
                        : expectFulfilled(prom)
                ))
            })

            step(testTitle3, async function(this: Mocha.Context) {
                this.timeout(DEFAULT_TEST_TIMEOUT);

                let prom = TokenSwap.swapSetup(
                    tc.tokenFrom,
                    tc.tokenTo,
                    tc.chainId,
                )

                try {
                    let res = await prom;
                    expect(res).to.have.property("swapInstance");
                    expect(res.swapInstance).to.be.an.instanceof(BaseContract);
                    return
                } catch (e) {
                    if (tc.wantError) {
                        return (await expect(prom).to.eventually.be.rejected);
                    } else {
                        expect.fail(e);
                    }
                }
            })
        })
    })
})