Skip to content

Commit

Permalink
feat: wire up deposit address listener
Browse files Browse the repository at this point in the history
  • Loading branch information
canhtrinh committed Sep 17, 2023
1 parent 9fffb99 commit a8c892e
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 16 deletions.
10 changes: 9 additions & 1 deletion packages/deposit-address/src/getDepositAddress.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@ import getDepositAddress from "./getDepositAddress";

describe("getDepositAddress", () => {
test("get deposit address", async () => {
expect(getDepositAddress).toBeTruthy();
const res = await getDepositAddress({
sourceChain: "Fantom",
destinationChain: "ethereum-2",
asset: "uaxl",
destinationAddress: "0xB8Cd93C83A974649D76B1c19f311f639e62272BC",
module: "evm",
});
console.log({ res });
expect(res).toBeTruthy();
});
});
28 changes: 21 additions & 7 deletions packages/deposit-address/src/getDepositAddress.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,30 @@
import { waitForDepositAddress } from "./helpers";

export type SendOptions = {};
export type SendOptions = {
sourceChain: string;
destinationChain: string;
destinationAddress: string;
asset: string;
module: string;
};

async function getDepositAddress(sendOptions: SendOptions) {
async function getDepositAddress({
sourceChain,
destinationAddress,
destinationChain,
asset,
module,
}: SendOptions) {
//invoke api to retrieve deposit address

//wait for deposit address
const depositAddress = await waitForDepositAddress();
return {
depositAddress,
sendOptions,
};
return waitForDepositAddress({
sourceChain,
destinationAddress,
destinationChain,
asset,
module,
});
}

export default getDepositAddress;
65 changes: 57 additions & 8 deletions packages/deposit-address/src/helpers/waitForDepositAddress.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,65 @@
export function waitForDepositAddress() {
const result = poll();
import { LinkRequestResponse } from "@axelarjs/api";
import { createAxelarscanNodeClient } from "@axelarjs/api/axelarscan/node";

export type ListenerParams = {
sourceChain: string;
destinationChain: string;
destinationAddress: string;
asset: string;
module: string;
};

const isStrEqual = (a: string | undefined, b: string) =>
a?.toLowerCase() === b?.toLowerCase();

const findLinkRequest = (
params: ListenerParams,
results: LinkRequestResponse[]
) => {
const foundEntry = results.find(
(linkRequest) =>
params.module === "axelarnet" ||
(isStrEqual(linkRequest.sourceChain, params.sourceChain) &&
isStrEqual(linkRequest.destinationAddress, params.destinationAddress) &&
isStrEqual(linkRequest.destinationChain, params.destinationChain) &&
isStrEqual(linkRequest.asset, params.asset))
);
return foundEntry;
};

export async function waitForDepositAddress(params: ListenerParams) {
const apiClient = createAxelarscanNodeClient({
prefixUrl: "https://testnet.api.axelarscan.io",
});

return await poll(
params,
() => apiClient.getRecentLinkTransactions({ size: 10 }),
(res: LinkRequestResponse[]) => !findLinkRequest(params, res),
5_000,
5
);
}

async function poll(fn: any, fnCondition: any, ms: number) {
let result = await fn();
while (fnCondition(result)) {
async function poll(
params: ListenerParams,
fn: any,
keepGoingCondition: any,
ms: number,
maxTries: number
) {
let tries = 0;
let results = await fn();

while (keepGoingCondition(results) && tries < maxTries) {
await sleep(ms);
result = await fn();
results = await fn();
tries++;
}
return result;
const depositAddress = findLinkRequest(params, results);
return depositAddress ? Promise.resolve(depositAddress) : Promise.reject("");
}

function sleep(ms = 1000) {
async function sleep(ms = 1000) {
return new Promise((res) => setTimeout(res, ms));
}

0 comments on commit a8c892e

Please sign in to comment.