Skip to content

Commit ffa1edf

Browse files
authored
Added UnattestedTokenId type (#787)
* 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
1 parent 9890739 commit ffa1edf

File tree

3 files changed

+29
-3
lines changed

3 files changed

+29
-3
lines changed

connect/src/routes/request.ts

+8-2
Original file line numberDiff line numberDiff line change
@@ -86,15 +86,21 @@ export class RouteTransferRequest<N extends Network> {
8686
params: {
8787
source: TokenId<FC>;
8888
destination: TokenId<TC>;
89+
sourceDecimals?: number;
90+
destinationDecimals?: number;
8991
},
9092
fromChain?: ChainContext<N, FC>,
9193
toChain?: ChainContext<N, TC>,
9294
) {
9395
fromChain = fromChain ?? wh.getChain(params.source.chain);
9496
toChain = toChain ?? wh.getChain(params.destination.chain);
9597

96-
const sourceDetails = await getTokenDetails(fromChain, params.source);
97-
const destDetails = await getTokenDetails(toChain, params.destination);
98+
const sourceDetails = await getTokenDetails(fromChain, params.source, params.sourceDecimals);
99+
const destDetails = await getTokenDetails(
100+
toChain,
101+
params.destination,
102+
params.destinationDecimals,
103+
);
98104

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

connect/src/routes/token.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ export function tokenAddresses(tokens: TokenId[]): string[] {
3131
export async function getTokenDetails<N extends Network>(
3232
chain: ChainContext<N>,
3333
token: TokenId,
34+
decimals?: number,
3435
): Promise<TokenDetails> {
3536
const address = canonicalAddress(token);
3637

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

4142
const symbol = details ? details.symbol : undefined;
4243
const wrapped = isNative(token.address) ? await chain.getNativeWrappedTokenId() : undefined;
43-
const decimals = Number(await chain.getDecimals(token.address));
44+
decimals = decimals ?? (await chain.getDecimals(token.address));
4445

4546
return {
4647
id: token,

core/definitions/src/types.ts

+19
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,25 @@ export function isTokenId<C extends Chain>(thing: any): thing is TokenId<C> {
6363
);
6464
}
6565

66+
/**
67+
* An UnattestedTokenId is used to represent a token that has not yet been attested / created
68+
*
69+
* @interface UnattestedTokenId
70+
*/
71+
export type UnattestedTokenId<C extends Chain = Chain> = TokenId<C> & {
72+
isUnattested: true;
73+
decimals: number; // expected decimals for the token
74+
originalTokenId: TokenId;
75+
};
76+
export function isUnattestedTokenId<C extends Chain>(thing: any): thing is UnattestedTokenId<C> {
77+
return (
78+
isTokenId(thing) &&
79+
(<UnattestedTokenId<C>>thing).isUnattested === true &&
80+
(<UnattestedTokenId<C>>thing).decimals !== undefined &&
81+
(<UnattestedTokenId<C>>thing).originalTokenId !== undefined
82+
);
83+
}
84+
6685
export function isSameToken(a: TokenId, b: TokenId): boolean {
6786
if (a.chain !== b.chain) return false;
6887
if (isNative(a.address) && isNative(b.address)) return true;

0 commit comments

Comments
 (0)