@swapkit/helpers 1.0.0-rc.78 → 1.0.0-rc.79

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,8 @@
1
1
  import { describe, expect, test } from "vitest";
2
+ import { Chain } from "@swapkit/types";
2
3
 
3
4
  import { derivationPathToString, getTHORNameCost, validateTHORName } from "../others.ts";
5
+ import { findAssetBy } from "../asset.ts";
4
6
 
5
7
  describe("derivationPathToString", () => {
6
8
  test("should return the correct string for a full path", () => {
@@ -57,3 +59,28 @@ describe("validateTHORName", () => {
57
59
  });
58
60
  }
59
61
  });
62
+
63
+ describe("getAssetBy", () => {
64
+ test("find asset by identifier", async () => {
65
+ const assetByIdentifier = await findAssetBy({ identifier: "ETH.ETH" });
66
+ expect(assetByIdentifier).toBe("ETH.ETH");
67
+ });
68
+
69
+ test("find asset by chain and contract", async () => {
70
+ const assetByChainAndContract = await findAssetBy({
71
+ chain: Chain.Ethereum,
72
+ contract: "0XA0B86991C6218B36C1D19D4A2E9EB0CE3606EB48",
73
+ });
74
+ expect(assetByChainAndContract).toBe("ETH.USDC-0XA0B86991C6218B36C1D19D4A2E9EB0CE3606EB48");
75
+ });
76
+
77
+ test("return undefined if asset can't be found", async () => {
78
+ const assetByIdentifier = await findAssetBy({ identifier: "ARB.NOTEXISTINGTOKEN" });
79
+ const assetByChainAndContract = await findAssetBy({
80
+ chain: Chain.Ethereum,
81
+ contract: "NOTFOUND",
82
+ });
83
+ expect(assetByIdentifier).toBe(undefined);
84
+ expect(assetByChainAndContract).toBe(undefined);
85
+ });
86
+ });
@@ -1,6 +1,7 @@
1
1
  import { RequestClient } from "@swapkit/api";
2
2
  import type { EVMChain } from "@swapkit/types";
3
3
  import { BaseDecimal, Chain, ChainToRPC, EVMChains, FeeOption } from "@swapkit/types";
4
+ import type { TokenNames } from "../types.ts";
4
5
 
5
6
  const getDecimalMethodHex = "0x313ce567";
6
7
 
@@ -194,3 +195,27 @@ export const filterAssets = (
194
195
  !potentialScamRegex.test(assetString) && evmAssetHasAddress(assetString) && value !== "0"
195
196
  );
196
197
  });
198
+
199
+ export async function findAssetBy(
200
+ params: { chain: EVMChain; contract: string } | { identifier: `${Chain}.${string}` },
201
+ ) {
202
+ const tokenPackages = await import("@swapkit/tokens");
203
+
204
+ for (const tokenList of Object.values(tokenPackages)) {
205
+ for (const { identifier, chain: tokenChain, ...rest } of tokenList.tokens) {
206
+ if ("identifier" in params && identifier === params.identifier) {
207
+ return identifier as TokenNames;
208
+ }
209
+
210
+ if (
211
+ "address" in rest &&
212
+ "chain" in params &&
213
+ tokenChain === params.chain &&
214
+ rest.address.toLowerCase() === params.contract.toLowerCase()
215
+ )
216
+ return identifier as TokenNames;
217
+ }
218
+ }
219
+
220
+ return;
221
+ }