generated from ChainSafe/yarn-workspaces-typescript-template
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: get bridge history for an address (#68)
# Summary Added method to fetch bridge transactions history of an address # Closes: #67 # Description The method should return aggregated data from all bridges that are supported by Sprinter API. Right now, only Sygma is added but later other bridge will be added # Chores: - [ ] Unit tests - [x] TS Docs / Comments --------- Co-authored-by: Saad Ahmed Siddiqui <[email protected]>
- Loading branch information
1 parent
d6b1e9c
commit 4d524c6
Showing
9 changed files
with
222 additions
and
31 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
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,10 +1,17 @@ | ||
import {useSprinterBalances} from "../lib/hooks.ts"; | ||
import { useSprinterBalances } from "../lib/hooks.ts"; | ||
|
||
export function Action() { | ||
const hook = useSprinterBalances("0x3E101Ec02e7A48D16DADE204C96bFF842E7E2519"); | ||
const hook = useSprinterBalances( | ||
"0x3E101Ec02e7A48D16DADE204C96bFF842E7E2519" | ||
); | ||
|
||
return (<button onClick={() => { | ||
hook.getUserBalances(); | ||
hook.getUserBalances(); | ||
}}>Action</button>) | ||
} | ||
return ( | ||
<button | ||
onClick={() => { | ||
hook.getUserBalances(); | ||
}} | ||
> | ||
Action | ||
</button> | ||
); | ||
} |
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 |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { Environment } from "../enums"; | ||
import { getTransfers } from "../sygma/api"; | ||
import type { Status, SygmaTransfer } from "../sygma/types"; | ||
import type { Address } from "../types"; | ||
|
||
interface History { | ||
originTx: string; | ||
originName: string; | ||
destinationTx?: string; | ||
destinationName: string; | ||
amount: string; | ||
tokenSymbol: string; | ||
status: Status; | ||
} | ||
|
||
function handleSygmaResponseEntry(entry: SygmaTransfer): History { | ||
return { | ||
originTx: entry.deposit?.txHash || "0x0", | ||
originName: entry.fromDomain.name, | ||
destinationTx: entry.execution?.txHash, | ||
destinationName: entry.toDomain.name, | ||
amount: entry.amount, | ||
tokenSymbol: entry.fee.tokenSymbol, | ||
status: entry.status, | ||
}; | ||
} | ||
|
||
/** | ||
* Returns bridging history | ||
* for an address | ||
* @param {Address} address | ||
* @param {Environment} environment | ||
* @returns {Promise<History[]>} | ||
*/ | ||
export async function getBridgeHistory( | ||
address: Address, | ||
environment: Environment = Environment.MAINNET, | ||
): Promise<History[]> { | ||
// TODO: add logic for all supported bridges | ||
const transactions = await getTransfers(address, environment).then( | ||
(sygmaTransfers) => | ||
sygmaTransfers.map((transfer) => handleSygmaResponseEntry(transfer)), | ||
); | ||
|
||
return transactions; | ||
} |
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 +1,2 @@ | ||
export { experimental_getTrackingUrl } from "./getTrackingUrl"; | ||
export { getBridgeHistory } from "./getBridgeHistory"; |
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 |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { Environment } from "../enums"; | ||
|
||
import type { SygmaTransfer } from "./types"; | ||
|
||
const SYGMA_API_ENDPOINT: Record<Environment, string> = { | ||
[Environment.MAINNET]: "https://api.buildwithsygma.com/", | ||
[Environment.TESTNET]: "https://api.test.buildwithsygma.com/", | ||
}; | ||
|
||
/** | ||
* Returns list of sygma transfers for an address | ||
* @param {`0x${string}`} address EVM address | ||
* @param {Environment} environment TESTNET or MAINNET | ||
* @returns {Promise<Array<SygmaTransfer>>} | ||
*/ | ||
export async function getTransfers( | ||
address: string, | ||
environment: Environment, | ||
): Promise<Array<SygmaTransfer>> { | ||
const transfersPath = `/api/sender/${address}/transfers`; | ||
const url = new URL(transfersPath, SYGMA_API_ENDPOINT[environment]); | ||
url.searchParams.set("limit", "100"); | ||
|
||
const response: SygmaTransfer[] = await fetch(url.toString()).then( | ||
(response): Promise<SygmaTransfer[]> => response.json(), | ||
); | ||
|
||
return response; | ||
} |
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 |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from "./api"; | ||
export * from "./types"; |
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 |
---|---|---|
@@ -0,0 +1,67 @@ | ||
export enum Status { | ||
pending = "pending", | ||
executed = "executed", | ||
failed = "failed", | ||
} | ||
|
||
export interface SygmaTransfer { | ||
id: string; | ||
depositNonce: number; | ||
resource: Resource; | ||
fromDomain: Domain; | ||
fromDomainId: number; | ||
toDomain: Domain; | ||
toDomainId: number; | ||
sender: string; | ||
destination: string; | ||
amount: string; | ||
timestamp?: string; | ||
status: Status; | ||
deposit?: Deposit; | ||
execution?: Execution; | ||
fee: Fee; | ||
resourceID: string; | ||
usdValue: number; | ||
accountId: string; | ||
} | ||
|
||
export interface Resource { | ||
id: string; | ||
type: string; | ||
} | ||
|
||
export interface Domain { | ||
id: string; | ||
name: string; | ||
lastIndexedBlock: string; | ||
} | ||
|
||
export interface Deposit { | ||
id: string; | ||
transferId: string; | ||
type: string; | ||
txHash: string; | ||
blockNumber: string; | ||
depositData: string; | ||
handlerResponse: string; | ||
timestamp: string; | ||
} | ||
|
||
export interface Execution { | ||
id: string; | ||
transferId: string; | ||
type: string; | ||
txHash: string; | ||
blockNumber: string; | ||
executionEvent: string; | ||
timestamp: string; | ||
} | ||
|
||
export interface Fee { | ||
amount: string; | ||
id: string; | ||
tokenAddress: string; | ||
tokenSymbol: string; | ||
transferId: string; | ||
decimals: number; | ||
} |