-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: wire up deposit address listener
- Loading branch information
Showing
3 changed files
with
87 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
65
packages/deposit-address/src/helpers/waitForDepositAddress.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} |