Skip to content

Commit

Permalink
core function tests update
Browse files Browse the repository at this point in the history
  • Loading branch information
RomeroYang committed Jun 30, 2023
1 parent 6b06081 commit eda7050
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 82 deletions.
4 changes: 2 additions & 2 deletions src/api-provider.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { ApiProvider } from "./api-provider";
import { ChainId } from "./configs";

describe("api-provider", () => {
jest.setTimeout(30000);
jest.setTimeout(300000);

const provider = new ApiProvider();

Expand All @@ -16,7 +16,7 @@ describe("api-provider", () => {

const res = await firstValueFrom(
provider.connectFromChain(chains, {
karura: ["wss://karura.api.onfinality.io/public-ws"],
karura: ["wss://karura-rpc.dwellir.com", "wss://karura.api.onfinality.io/public-ws", "wss://karura.polkawallet.io"],
})
);

Expand Down
56 changes: 4 additions & 52 deletions src/bridge.spec.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
import { firstValueFrom } from "rxjs";

import { ApiProvider } from "./api-provider";
import { BaseCrossChainAdapter } from "./base-chain-adapter";
import { PolkadotAdapter } from "./adapters/polkadot";
import { ChainId } from "./configs";
import { Bridge } from "./bridge";
import { AcalaAdapter } from "./adapters/acala";
import { FN } from "./types";

describe("Bridge sdk usage", () => {
jest.setTimeout(30000);
jest.setTimeout(300000);

const provider = new ApiProvider();

Expand Down Expand Up @@ -51,55 +48,10 @@ describe("Bridge sdk usage", () => {
expect(bridge.router.getAvailableTokens({ from: "acala", to: "polkadot" }).length).toBeGreaterThanOrEqual(0);
});

test("2. connect fromChain should be ok", async () => {
test("2. find adapter should be ok", async () => {
const chains = Object.keys(availableAdapters) as ChainId[];

expect(provider.getApi(chains[0])).toEqual(undefined);
expect(provider.getApi(chains[1])).toEqual(undefined);

// connect all adapters
const connected = await firstValueFrom(provider.connectFromChain(chains, undefined));
// and set apiProvider for each adapter
await Promise.all(chains.map((chain) => availableAdapters[chain].init(provider.getApi(chain))));

expect(connected.length).toEqual(chains.length);

expect(connected[0]).toEqual(chains[0]);
expect(connected[1]).toEqual(chains[1]);

expect(provider.getApi(chains[0])).toBeDefined();
expect(provider.getApi(chains[1])).toBeDefined();

expect((await firstValueFrom(provider.getApi(chains[0]).rpc.system.chain())).toLowerCase()).toEqual(chains[0]);
expect((await firstValueFrom(provider.getApi(chains[1]).rpc.system.chain())).toLowerCase()).toEqual(chains[1]);

setTimeout(async () => {
expect((await provider.getApiPromise(chains[0]).rpc.system.chain()).toLowerCase()).toEqual(chains[0]);
expect((await provider.getApiPromise(chains[1]).rpc.system.chain()).toLowerCase()).toEqual(chains[1]);
}, 1000);
});

test("3. token balance query & create tx should be ok", async () => {
const chain: ChainId = "acala";
const toChain: ChainId = "polkadot";
const token = "DOT";
const testAddress = "23M5ttkmR6Kco7bReRDve6bQUSAcwqebatp3fWGJYb4hDSDJ";

const balance = await firstValueFrom(availableAdapters[chain].subscribeTokenBalance(token, testAddress));

expect(balance.free.toNumber()).toBeGreaterThanOrEqual(0);
expect(balance.available.toNumber()).toBeGreaterThanOrEqual(0);

const inputConfig = await firstValueFrom(
availableAdapters[chain].subscribeInputConfig({ to: toChain, token, address: testAddress, signer: testAddress })
);

expect(BigInt(inputConfig.estimateFee)).toBeGreaterThanOrEqual(BigInt(0));
expect(inputConfig.minInput.toNumber()).toBeGreaterThan(0);
expect(inputConfig.maxInput.toNumber()).toBeLessThanOrEqual(balance.available.toNumber());

const tx = availableAdapters[chain].createTx({ to: toChain, token, amount: FN.fromInner("10000000000", 10), address: testAddress });

expect(tx.args.length).toBeGreaterThan(1);
expect(bridge.findAdapter(chains[0])).toEqual(availableAdapters[chains[0]]);
expect(bridge.findAdapter(chains[1])).toEqual(availableAdapters[chains[1]]);
});
});
29 changes: 1 addition & 28 deletions src/cross-chain-router.spec.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1,18 @@
import { options } from "@acala-network/api";
import { Wallet } from "@acala-network/sdk/wallet";

import { ApiPromise } from "@polkadot/api";
import { WsProvider } from "@polkadot/rpc-provider";

import { isChainEqual } from "./utils/is-chain-equal";
import { chains } from "./configs";
import { BridgeRouterManager } from "./cross-chain-router";
import { RouteConfigs } from "./types";

describe("cross-chain-router-manager", () => {
let manager: BridgeRouterManager;
let api: ApiPromise;
let wallet: Wallet;

jest.setTimeout(30000);
jest.setTimeout(300000);

const initSDK = async () => {
if (manager) {
return manager;
}

const endpoint = "wss://karura.api.onfinality.io/public-ws";
const provider = new WsProvider(endpoint) as any;

api = await ApiPromise.create(options({ provider }));

await api.isReady;

wallet = new Wallet(api);

await wallet.isReady;

manager = new BridgeRouterManager();

await manager.addRouters(
Expand Down Expand Up @@ -125,12 +106,4 @@ describe("cross-chain-router-manager", () => {
// ignore node disconnect issue
}
});

afterAll(async () => {
if (api) {
await api.disconnect();
}

await new Promise((resolve) => setTimeout(() => resolve(undefined), 5000));
});
});

0 comments on commit eda7050

Please sign in to comment.