Skip to content

Commit

Permalink
Added UnattestedTokenId type (#787)
Browse files Browse the repository at this point in the history
* Added UnattestedTokenId type

Represents a token that does not exist yet

* added decimals to UnattestedToken type

* added originalTokenId to UnattestedTokenId type

* non-existent token catch fetch balance error

* remove try/catch balance check

* remove await
  • Loading branch information
kev1n-peters authored Jan 30, 2025
1 parent 9890739 commit ffa1edf
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 3 deletions.
10 changes: 8 additions & 2 deletions connect/src/routes/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,15 +86,21 @@ export class RouteTransferRequest<N extends Network> {
params: {
source: TokenId<FC>;
destination: TokenId<TC>;
sourceDecimals?: number;
destinationDecimals?: number;
},
fromChain?: ChainContext<N, FC>,
toChain?: ChainContext<N, TC>,
) {
fromChain = fromChain ?? wh.getChain(params.source.chain);
toChain = toChain ?? wh.getChain(params.destination.chain);

const sourceDetails = await getTokenDetails(fromChain, params.source);
const destDetails = await getTokenDetails(toChain, params.destination);
const sourceDetails = await getTokenDetails(fromChain, params.source, params.sourceDecimals);
const destDetails = await getTokenDetails(
toChain,
params.destination,
params.destinationDecimals,
);

const rtr = new RouteTransferRequest(fromChain, toChain, sourceDetails, destDetails);

Expand Down
3 changes: 2 additions & 1 deletion connect/src/routes/token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export function tokenAddresses(tokens: TokenId[]): string[] {
export async function getTokenDetails<N extends Network>(
chain: ChainContext<N>,
token: TokenId,
decimals?: number,
): Promise<TokenDetails> {
const address = canonicalAddress(token);

Expand All @@ -40,7 +41,7 @@ export async function getTokenDetails<N extends Network>(

const symbol = details ? details.symbol : undefined;
const wrapped = isNative(token.address) ? await chain.getNativeWrappedTokenId() : undefined;
const decimals = Number(await chain.getDecimals(token.address));
decimals = decimals ?? (await chain.getDecimals(token.address));

return {
id: token,
Expand Down
19 changes: 19 additions & 0 deletions core/definitions/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,25 @@ export function isTokenId<C extends Chain>(thing: any): thing is TokenId<C> {
);
}

/**
* An UnattestedTokenId is used to represent a token that has not yet been attested / created
*
* @interface UnattestedTokenId
*/
export type UnattestedTokenId<C extends Chain = Chain> = TokenId<C> & {
isUnattested: true;
decimals: number; // expected decimals for the token
originalTokenId: TokenId;
};
export function isUnattestedTokenId<C extends Chain>(thing: any): thing is UnattestedTokenId<C> {
return (
isTokenId(thing) &&
(<UnattestedTokenId<C>>thing).isUnattested === true &&
(<UnattestedTokenId<C>>thing).decimals !== undefined &&
(<UnattestedTokenId<C>>thing).originalTokenId !== undefined
);
}

export function isSameToken(a: TokenId, b: TokenId): boolean {
if (a.chain !== b.chain) return false;
if (isNative(a.address) && isNative(b.address)) return true;
Expand Down

0 comments on commit ffa1edf

Please sign in to comment.