Skip to content

Commit

Permalink
async
Browse files Browse the repository at this point in the history
  • Loading branch information
TateB committed Feb 15, 2024
1 parent 4e140d4 commit 4beda88
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 14 deletions.
10 changes: 10 additions & 0 deletions src/async.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,23 @@ test("evm coin name", async () => {
expect(coder.coinType).toBe(2147483658);
expect(coder.name).toBe("op");
expect(coder.evmChainId).toBe(10);
expect("isUnknownChain" in coder).toBeFalse();
});

test("evm coin type", async () => {
const coder = await getCoderByCoinTypeAsync(2147483658);
expect(coder.coinType).toBe(2147483658);
expect(coder.name).toBe("op");
expect(coder.evmChainId).toBe(10);
expect(coder.isUnknownChain).toBeFalse();
});

test("unknown evm coin type", async () => {
const coder = await getCoderByCoinTypeAsync(2147483659);
expect(coder.coinType).toBe(2147483659);
expect(coder.name).toBe("Unknown Chain (11)");
expect(coder.evmChainId).toBe(11);
expect(coder.isUnknownChain).toBeTrue();
});

const nonEvmCoinNames = Object.keys(nonEvmCoinNameToTypeMap);
Expand Down
12 changes: 6 additions & 6 deletions src/async.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,24 +46,24 @@ export const getCoderByCoinTypeAsync = async <
const names =
coinTypeToNameMap[String(coinType) as keyof typeof coinTypeToNameMap];

if (!names) throw new Error(`Unsupported coin type: ${coinType}`);

const [name] = names;

if (coinType >= SLIP44_MSB) {
// EVM coin
const evmChainId = coinTypeToEvmChainId(coinType);
const isUnknownChain = !names;
const name = isUnknownChain ? `Unknown Chain (${evmChainId})` : names[0];
return {
name,
coinType: coinType as EvmCoinType,
evmChainId,
isUnknownChain,
encode: eth.encode,
decode: eth.decode,
} as GetCoderByCoinType<TCoinType>;
}
const mod = await import(`./coin/${name}`);

if (!names) throw new Error(`Unsupported coin type: ${coinType}`);
const [name] = names;
const mod = await import(`./coin/${name}`);
if (!mod) throw new Error(`Failed to load coin: ${name}`);

return mod[name];
};
2 changes: 2 additions & 0 deletions src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ test("evm coin name", () => {
expect(coder.coinType).toBe(2147483658);
expect(coder.name).toBe("op");
expect(coder.evmChainId).toBe(10);
expect("isUnknownChain" in coder).toBeFalse();
expect(coder.encode).toBeFunction();
expect(coder.decode).toBeFunction();
});
Expand All @@ -40,6 +41,7 @@ test("evm coin type", () => {
expect(coder.coinType).toBe(2147483658);
expect(coder.name).toBe("op");
expect(coder.evmChainId).toBe(10);
expect(coder.isUnknownChain).toBeFalse();
expect(coder.encode).toBeFunction();
expect(coder.decode).toBeFunction();
});
Expand Down
15 changes: 8 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ export const getCoderByCoinType = <
const names =
coinTypeToNameMap[String(coinType) as keyof typeof coinTypeToNameMap];
// https://docs.ens.domains/ens-improvement-proposals/ensip-11-evmchain-address-resolution

if (coinType >= SLIP44_MSB) {
// EVM coin
const evmChainId = coinTypeToEvmChainId(coinType);
Expand All @@ -87,12 +88,12 @@ export const getCoderByCoinType = <
encode: ethFormat.encode,
decode: ethFormat.decode,
} as GetCoderByCoinType<TCoinType>;
} else {
if (!names) {
throw new Error(`Unsupported coin type: ${coinType}`);
}
const [name] = names;
const format = formats[name as keyof typeof formats];
return format as GetCoderByCoinType<TCoinType>;
}

if (!names) {
throw new Error(`Unsupported coin type: ${coinType}`);
}
const [name] = names;
const format = formats[name as keyof typeof formats];
return format as GetCoderByCoinType<TCoinType>;
};
4 changes: 3 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ export type CoinTypeToFormatMap = {
: never;
};
export type CoinNameToFormatMap = {
[key in CoinName]: CoinTypeToFormatMap[CoinNameToTypeMap[key]];
[key in CoinName]: Prettify<
Omit<CoinTypeToFormatMap[CoinNameToTypeMap[key]], "isUnknownChain">
>;
};

export type EvmCoinMap = typeof evmCoinNameToTypeMap;
Expand Down

0 comments on commit 4beda88

Please sign in to comment.