Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added UnattestedTokenId type #787

Merged
merged 6 commits into from
Jan 30, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
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;
kev1n-peters marked this conversation as resolved.
Show resolved Hide resolved
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