Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
54 commits
Select commit Hold shift + click to select a range
f3da187
validate /w team
BeroBurny Sep 26, 2024
47afc40
swap `zod` with `superstruct`
BeroBurny Sep 26, 2024
3956863
first case for api refactor
BeroBurny Sep 26, 2024
ce3b88c
move sprinter to separate file
BeroBurny Sep 26, 2024
db2a9cf
refactor
BeroBurny Sep 26, 2024
18f768b
smoll fixes
BeroBurny Sep 30, 2024
b1151a8
sprinter class docs
BeroBurny Sep 30, 2024
c72bc78
add enum :man_shrugging:
BeroBurny Sep 30, 2024
ddd45f2
micro bug fixes
BeroBurny Sep 30, 2024
c61895f
refactor POC to match new SDK
BeroBurny Sep 30, 2024
ed12547
lint
BeroBurny Sep 30, 2024
9c4272d
change sprinter sdk from dependencies to peer dependencies
BeroBurny Sep 30, 2024
2121e83
partially refactor react sdk
BeroBurny Sep 30, 2024
b8656c9
react sdk docs and smool fix
BeroBurny Oct 1, 2024
4d73265
fix yarn.lock
BeroBurny Oct 1, 2024
c84f104
fix ci
BeroBurny Oct 1, 2024
33ef698
fix yarn command
BeroBurny Oct 1, 2024
c5e921d
save
BeroBurny Oct 8, 2024
a7f93e3
save general
BeroBurny Oct 8, 2024
56231ea
refactor `sourceChains` behavior for single hop
BeroBurny Oct 8, 2024
dd1d8c8
sdk docs v1 on new approach
BeroBurny Oct 8, 2024
e46aeed
react docs v1
BeroBurny Oct 8, 2024
7f72af1
Merge branch 'master' into beroburny/api-refactor
BeroBurny Oct 8, 2024
046d431
dirty fix
BeroBurny Oct 8, 2024
a75d753
wtf is that? remove! purger!
BeroBurny Oct 8, 2024
f45864e
prettify category and push overview into index
BeroBurny Oct 9, 2024
eeb6625
fix example response for available tokens
BeroBurny Oct 9, 2024
0589132
fix network list
BeroBurny Oct 10, 2024
2efbfd7
implement remark plugins
BeroBurny Oct 10, 2024
fbe318b
fix `recipient`
BeroBurny Oct 10, 2024
7d69042
implement calldata usages
BeroBurny Oct 10, 2024
b86162e
gas tips
BeroBurny Oct 10, 2024
9acc443
smoll fix
BeroBurny Oct 10, 2024
a4ddc83
refactor gas tip
BeroBurny Oct 14, 2024
bc516e4
implement tabs in bridge and call
BeroBurny Oct 15, 2024
dfad461
ci / cd
BeroBurny Oct 15, 2024
e1fd86c
gas limit
BeroBurny Oct 15, 2024
2566a3c
aggregate
BeroBurny Oct 15, 2024
06a56ac
fix links
BeroBurny Oct 15, 2024
2ca9ae6
breaking changes docs
BeroBurny Oct 16, 2024
5c1faed
sejv
BeroBurny Oct 18, 2024
c4c7852
status
BeroBurny Oct 18, 2024
33156ae
comments
BeroBurny Oct 18, 2024
edc6911
curl example
BeroBurny Oct 18, 2024
6622bd5
address comments regarding method names
BeroBurny Oct 24, 2024
9b04fb4
fix breaking changes doc
BeroBurny Oct 24, 2024
1407c48
feat: added bridge history method
saadahmsiddiqui Oct 24, 2024
6349c33
Merge branch 'beroburny/api-refactor' of github.com:ChainSafe/sprinte…
saadahmsiddiqui Oct 24, 2024
c645b18
revert lock file changes
saadahmsiddiqui Oct 24, 2024
d404fc0
revert lock file
saadahmsiddiqui Oct 24, 2024
95ebbf9
newline add end of file `yarn.lock`
saadahmsiddiqui Oct 24, 2024
670c339
fix lint
saadahmsiddiqui Oct 24, 2024
126235c
resolve marins comments
saadahmsiddiqui Oct 24, 2024
1151874
Merge branch 'master' of github.com:ChainSafe/sprinter-ts into berobu…
saadahmsiddiqui Oct 24, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 37 additions & 15 deletions packages/react/lib/context.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import {createContext, ReactNode, useEffect, useState} from "react";
import {Sprinter} from "@chainsafe/sprinter-sdk";
import {useTokens} from "./internal/useTokens.ts";
import {useChains} from "./internal/useChains.ts";
import {useBalances} from "./internal/useBalances.ts";
import {useTransfers} from "./internal/useTransfers.ts";
import { createContext, ReactNode, useEffect, useState } from "react";
import { Sprinter } from "@chainsafe/sprinter-sdk";
import { useTokens } from "./internal/useTokens.ts";
import { useChains } from "./internal/useChains.ts";
import { useBalances } from "./internal/useBalances.ts";
import { useTransfers } from "./internal/useTransfers.ts";

type SprinterContext = ReturnType<typeof useBalances> & ReturnType<typeof useTokens> & ReturnType<typeof useChains> & ReturnType<typeof useTransfers>;
type SprinterContext = ReturnType<typeof useBalances> &
ReturnType<typeof useTokens> &
ReturnType<typeof useChains> &
ReturnType<typeof useTransfers>;

export const Context = createContext<SprinterContext | null>(null);

Expand Down Expand Up @@ -42,7 +45,7 @@ interface SprinterContextProps {
* ```
*/
export function SprinterContext({ children, baseUrl }: SprinterContextProps) {
const [sprinter] = useState(new Sprinter({baseUrl}));
const [sprinter] = useState(new Sprinter({ baseUrl }));

/** Balances */
const { balances, getUserBalances } = useBalances(sprinter);
Expand All @@ -54,18 +57,37 @@ export function SprinterContext({ children, baseUrl }: SprinterContextProps) {
const { chains, getAvailableChains } = useChains(sprinter);

/** Solutions */
const { solution, getTransfer, getTransferWithHook, getPoolAssetOnDestination, getPoolAssetOnDestinationWithHook } = useTransfers(sprinter);
const {
solution,
getTransfer,
getTransferWithHook,
getPoolAssetOnDestination,
getPoolAssetOnDestinationWithHook,
} = useTransfers(sprinter);

/** Initialization */
useEffect(() => {
getAvailableTokens();
getAvailableChains();
}, [sprinter]);

return <Context.Provider value={{
balances, getUserBalances,
tokens, getAvailableTokens,
chains, getAvailableChains,
solution, getTransfer, getTransferWithHook, getPoolAssetOnDestination, getPoolAssetOnDestinationWithHook,
}}>{children}</Context.Provider>;
return (
<Context.Provider
value={{
balances,
getUserBalances,
tokens,
getAvailableTokens,
chains,
getAvailableChains,
solution,
getTransfer,
getTransferWithHook,
getPoolAssetOnDestination,
getPoolAssetOnDestinationWithHook,
}}
>
{children}
</Context.Provider>
);
}
34 changes: 25 additions & 9 deletions packages/react/lib/hooks.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {useCallback, useContext} from "react";
import {Context} from "./context.tsx";
import {Address} from "@chainsafe/sprinter-sdk";
import {BalancesEntry} from "./internal/useBalances.ts";
import { useCallback, useContext } from "react";
import { Context } from "./context.tsx";
import { Address } from "@chainsafe/sprinter-sdk";
import { BalancesEntry } from "./internal/useBalances.ts";

/**
* A hook to access the full Sprinter context, including balances, tokens, chains, and transfer solutions.
Expand All @@ -20,7 +20,7 @@ import {BalancesEntry} from "./internal/useBalances.ts";
export function useSprinter() {
const context = useContext(Context);

if (!context) throw new Error('Sprinter Context is not defined');
if (!context) throw new Error("Sprinter Context is not defined");

return context;
}
Expand Down Expand Up @@ -77,10 +77,14 @@ const balancesEmptyState = {
* ```
*/
export function useSprinterBalances(account: Address) {
const { balances: _balances, getUserBalances: _getUserBalances } = useSprinter();
const { balances: _balances, getUserBalances: _getUserBalances } =
useSprinter();

const balances: BalancesEntry = _balances[account] || balancesEmptyState;
const getUserBalances = useCallback(() => _getUserBalances(account), [account]);
const getUserBalances = useCallback(
() => _getUserBalances(account),
[account]
);

return { balances, getUserBalances };
}
Expand Down Expand Up @@ -259,6 +263,18 @@ export function useSprinterChains() {
* ```
*/
export function useSprinterTransfers() {
const { solution, getTransfer, getTransferWithHook, getPoolAssetOnDestination, getPoolAssetOnDestinationWithHook } = useSprinter();
return { solution, getTransfer, getTransferWithHook, getPoolAssetOnDestination, getPoolAssetOnDestinationWithHook };
const {
solution,
getTransfer,
getTransferWithHook,
getPoolAssetOnDestination,
getPoolAssetOnDestinationWithHook,
} = useSprinter();
return {
solution,
getTransfer,
getTransferWithHook,
getPoolAssetOnDestination,
getPoolAssetOnDestinationWithHook,
};
}
21 changes: 14 additions & 7 deletions packages/react/src/Action.tsx
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>
);
}
46 changes: 46 additions & 0 deletions packages/sdk/src/helpers/getBridgeHistory.ts
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;
}
1 change: 1 addition & 0 deletions packages/sdk/src/helpers/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export { experimental_getTrackingUrl } from "./getTrackingUrl";
export { getBridgeHistory } from "./getBridgeHistory";
1 change: 1 addition & 0 deletions packages/sdk/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ export { setBaseUrl, BASE_URL } from "./api";
export * as api from "./api";
export { ChainType, Environment } from "./enums";
export { Sprinter } from "./sprinter";
export * from "./helpers";
29 changes: 29 additions & 0 deletions packages/sdk/src/sygma/api.ts
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;
}
2 changes: 2 additions & 0 deletions packages/sdk/src/sygma/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from "./api";
export * from "./types";
67 changes: 67 additions & 0 deletions packages/sdk/src/sygma/types.ts
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;
}