@sovereign-sdk/modules 0.1.12 → 0.2.1

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.
package/dist/index.d.mts CHANGED
@@ -1,5 +1,12 @@
1
1
  import { Rollup } from '@sovereign-sdk/web3';
2
2
 
3
+ type TokenMetadata = {
4
+ name: string;
5
+ decimals: number;
6
+ totalSupply: bigint;
7
+ supplyCap: bigint;
8
+ admins: string[];
9
+ };
3
10
  /**
4
11
  * Bank class for interacting with the Sovereign SDK Bank module.
5
12
  */
@@ -33,24 +40,24 @@ declare class Bank {
33
40
  */
34
41
  balance(address: string, tokenId?: string): Promise<bigint>;
35
42
  /**
36
- * Gets the total supply of a specific token.
43
+ * Gets the metadata of a specific token.
37
44
  *
38
- * If no token ID is provided, returns the total supply of the gas token.
45
+ * If no token ID is provided, returns the metadata of the gas token.
39
46
  *
40
47
  * @param tokenId - Optional token ID. If not provided, uses the gas token
41
- * @returns Promise resolving to the total supply as a bigint
48
+ * @returns Promise resolving to the token metadata
42
49
  * @throws {SovereignClient.APIError} When the request fails
43
50
  * @example
44
51
  * ```typescript
45
52
  * const bank = new Bank(rollup);
46
- * const totalSupply = await bank.totalSupply();
47
- * console.log(`Total gas token supply: ${totalSupply}`);
53
+ * const metadata = await bank.tokenMetadata();
54
+ * console.log(`Token name: ${metadata.name}, decimals: ${metadata.decimals}`);
48
55
  *
49
- * // Query specific token supply
50
- * const tokenSupply = await bank.totalSupply("token_123");
56
+ * // Query specific token metadata
57
+ * const tokenMeta = await bank.tokenMetadata("token_123");
51
58
  * ```
52
59
  */
53
- totalSupply(tokenId?: string): Promise<bigint>;
60
+ tokenMetadata(tokenId?: string): Promise<TokenMetadata>;
54
61
  /**
55
62
  * Gets the gas token identifier.
56
63
  *
@@ -70,4 +77,4 @@ declare class Bank {
70
77
  private isMissingAccountError;
71
78
  }
72
79
 
73
- export { Bank };
80
+ export { Bank, type TokenMetadata };
package/dist/index.d.ts CHANGED
@@ -1,5 +1,12 @@
1
1
  import { Rollup } from '@sovereign-sdk/web3';
2
2
 
3
+ type TokenMetadata = {
4
+ name: string;
5
+ decimals: number;
6
+ totalSupply: bigint;
7
+ supplyCap: bigint;
8
+ admins: string[];
9
+ };
3
10
  /**
4
11
  * Bank class for interacting with the Sovereign SDK Bank module.
5
12
  */
@@ -33,24 +40,24 @@ declare class Bank {
33
40
  */
34
41
  balance(address: string, tokenId?: string): Promise<bigint>;
35
42
  /**
36
- * Gets the total supply of a specific token.
43
+ * Gets the metadata of a specific token.
37
44
  *
38
- * If no token ID is provided, returns the total supply of the gas token.
45
+ * If no token ID is provided, returns the metadata of the gas token.
39
46
  *
40
47
  * @param tokenId - Optional token ID. If not provided, uses the gas token
41
- * @returns Promise resolving to the total supply as a bigint
48
+ * @returns Promise resolving to the token metadata
42
49
  * @throws {SovereignClient.APIError} When the request fails
43
50
  * @example
44
51
  * ```typescript
45
52
  * const bank = new Bank(rollup);
46
- * const totalSupply = await bank.totalSupply();
47
- * console.log(`Total gas token supply: ${totalSupply}`);
53
+ * const metadata = await bank.tokenMetadata();
54
+ * console.log(`Token name: ${metadata.name}, decimals: ${metadata.decimals}`);
48
55
  *
49
- * // Query specific token supply
50
- * const tokenSupply = await bank.totalSupply("token_123");
56
+ * // Query specific token metadata
57
+ * const tokenMeta = await bank.tokenMetadata("token_123");
51
58
  * ```
52
59
  */
53
- totalSupply(tokenId?: string): Promise<bigint>;
60
+ tokenMetadata(tokenId?: string): Promise<TokenMetadata>;
54
61
  /**
55
62
  * Gets the gas token identifier.
56
63
  *
@@ -70,4 +77,4 @@ declare class Bank {
70
77
  private isMissingAccountError;
71
78
  }
72
79
 
73
- export { Bank };
80
+ export { Bank, type TokenMetadata };
package/dist/index.js CHANGED
@@ -25,6 +25,12 @@ __export(src_exports, {
25
25
  module.exports = __toCommonJS(src_exports);
26
26
 
27
27
  // src/bank.ts
28
+ var import_bech32 = require("bech32");
29
+ function getHolderAddress(holder) {
30
+ if ("user" in holder) return holder.user;
31
+ if ("module" in holder) return holder.module;
32
+ return holder.derived;
33
+ }
28
34
  var Bank = class {
29
35
  /**
30
36
  * Creates a new Bank instance.
@@ -67,29 +73,38 @@ var Bank = class {
67
73
  }
68
74
  }
69
75
  /**
70
- * Gets the total supply of a specific token.
76
+ * Gets the metadata of a specific token.
71
77
  *
72
- * If no token ID is provided, returns the total supply of the gas token.
78
+ * If no token ID is provided, returns the metadata of the gas token.
73
79
  *
74
80
  * @param tokenId - Optional token ID. If not provided, uses the gas token
75
- * @returns Promise resolving to the total supply as a bigint
81
+ * @returns Promise resolving to the token metadata
76
82
  * @throws {SovereignClient.APIError} When the request fails
77
83
  * @example
78
84
  * ```typescript
79
85
  * const bank = new Bank(rollup);
80
- * const totalSupply = await bank.totalSupply();
81
- * console.log(`Total gas token supply: ${totalSupply}`);
86
+ * const metadata = await bank.tokenMetadata();
87
+ * console.log(`Token name: ${metadata.name}, decimals: ${metadata.decimals}`);
82
88
  *
83
- * // Query specific token supply
84
- * const tokenSupply = await bank.totalSupply("token_123");
89
+ * // Query specific token metadata
90
+ * const tokenMeta = await bank.tokenMetadata("token_123");
85
91
  * ```
86
92
  */
87
- async totalSupply(tokenId) {
93
+ async tokenMetadata(tokenId) {
88
94
  const token = await this.tokenIdOrElseGasTokenId(tokenId);
89
95
  const response = await this.rollup.http.get(
90
- `/modules/bank/tokens/${token}/total-supply`
96
+ `/modules/bank/state/tokens/items/${token}`
91
97
  );
92
- return BigInt(response.amount);
98
+ const decoded = import_bech32.bech32m.decode(token);
99
+ const bytes = import_bech32.bech32m.fromWords(decoded.words);
100
+ const decimals = bytes[31];
101
+ return {
102
+ name: response.value.name,
103
+ decimals,
104
+ totalSupply: BigInt(response.value.total_supply),
105
+ supplyCap: BigInt(response.value.supply_cap),
106
+ admins: response.value.admins.map(getHolderAddress)
107
+ };
93
108
  }
94
109
  /**
95
110
  * Gets the gas token identifier.
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/index.ts","../src/bank.ts"],"sourcesContent":["export * from \"./bank\";\n","import { type Rollup, SovereignClient } from \"@sovereign-sdk/web3\";\nimport type { ErrorResponse } from \"./types\";\n\ntype GasTokenIdPayload = {\n token_id: string;\n};\n\ntype BalancePayload = {\n amount: string;\n token_id: string;\n};\n\ntype TotalSupplyPayload = {\n amount: string;\n token_id: string;\n};\n\n/**\n * Bank class for interacting with the Sovereign SDK Bank module.\n */\nexport class Bank {\n // biome-ignore lint/suspicious/noExplicitAny: types arent used\n private readonly rollup: Rollup<any, any>;\n private _gasTokenId?: string;\n\n /**\n * Creates a new Bank instance.\n *\n * @param rollup - The rollup instance to use for HTTP requests\n */\n // biome-ignore lint/suspicious/noExplicitAny: types arent used\n constructor(rollup: Rollup<any, any>) {\n this.rollup = rollup;\n }\n\n /**\n * Gets the balance of a specific token for a given address.\n *\n * If no token ID is provided, the gas token balance will be returned.\n *\n * @param address - The address to query the balance for\n * @param tokenId - Optional token ID. If not provided, uses the gas token\n * @returns Promise resolving to the token balance as a bigint\n * @throws {SovereignClient.APIError} When the request fails for reasons other than missing account\n * @example\n * ```typescript\n * const bank = new Bank(rollup);\n * const balance = await bank.balance(\"0x123...\");\n * console.log(`Balance: ${balance}`);\n *\n * // Query specific token balance\n * const tokenBalance = await bank.balance(\"0x123...\", \"token_123\");\n * ```\n */\n async balance(address: string, tokenId?: string): Promise<bigint> {\n const token = await this.tokenIdOrElseGasTokenId(tokenId);\n\n try {\n const response: BalancePayload = await this.rollup.http.get(\n `/modules/bank/tokens/${token}/balances/${address}`,\n );\n\n return BigInt(response.amount);\n } catch (err) {\n if (this.isMissingAccountError(err)) return BigInt(0);\n\n throw err;\n }\n }\n\n /**\n * Gets the total supply of a specific token.\n *\n * If no token ID is provided, returns the total supply of the gas token.\n *\n * @param tokenId - Optional token ID. If not provided, uses the gas token\n * @returns Promise resolving to the total supply as a bigint\n * @throws {SovereignClient.APIError} When the request fails\n * @example\n * ```typescript\n * const bank = new Bank(rollup);\n * const totalSupply = await bank.totalSupply();\n * console.log(`Total gas token supply: ${totalSupply}`);\n *\n * // Query specific token supply\n * const tokenSupply = await bank.totalSupply(\"token_123\");\n * ```\n */\n async totalSupply(tokenId?: string): Promise<bigint> {\n const token = await this.tokenIdOrElseGasTokenId(tokenId);\n const response: TotalSupplyPayload = await this.rollup.http.get(\n `/modules/bank/tokens/${token}/total-supply`,\n );\n return BigInt(response.amount);\n }\n\n /**\n * Gets the gas token identifier.\n *\n * The gas token ID is cached after the first request to avoid repeated API calls.\n *\n * @returns Promise resolving to the gas token ID as a string\n * @throws {Error} When the gas token ID cannot be retrieved\n * @example\n * ```typescript\n * const bank = new Bank(rollup);\n * const gasTokenId = await bank.gasTokenId();\n * console.log(`Gas token ID: ${gasTokenId}`);\n * ```\n */\n async gasTokenId(): Promise<string> {\n if (this._gasTokenId) return this._gasTokenId;\n const response: GasTokenIdPayload = await this.rollup.http.get(\n \"/modules/bank/tokens/gas_token\",\n );\n this._gasTokenId = response.token_id;\n return this._gasTokenId;\n }\n\n private async tokenIdOrElseGasTokenId(tokenId?: string): Promise<string> {\n return tokenId ?? this.gasTokenId();\n }\n\n // biome-ignore lint/suspicious/noExplicitAny: todo\n private isMissingAccountError(anyError: any): boolean {\n if (anyError?.status !== 404) return false;\n const body = anyError?.error as ErrorResponse;\n return (\n body?.message?.startsWith(\"Balance\") &&\n body?.message?.endsWith(\"not found\")\n );\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACoBO,IAAM,OAAN,MAAW;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAWhB,YAAY,QAA0B;AACpC,SAAK,SAAS;AAAA,EAChB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAqBA,MAAM,QAAQ,SAAiB,SAAmC;AAChE,UAAM,QAAQ,MAAM,KAAK,wBAAwB,OAAO;AAExD,QAAI;AACF,YAAM,WAA2B,MAAM,KAAK,OAAO,KAAK;AAAA,QACtD,wBAAwB,KAAK,aAAa,OAAO;AAAA,MACnD;AAEA,aAAO,OAAO,SAAS,MAAM;AAAA,IAC/B,SAAS,KAAK;AACZ,UAAI,KAAK,sBAAsB,GAAG,EAAG,QAAO,OAAO,CAAC;AAEpD,YAAM;AAAA,IACR;AAAA,EACF;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAoBA,MAAM,YAAY,SAAmC;AACnD,UAAM,QAAQ,MAAM,KAAK,wBAAwB,OAAO;AACxD,UAAM,WAA+B,MAAM,KAAK,OAAO,KAAK;AAAA,MAC1D,wBAAwB,KAAK;AAAA,IAC/B;AACA,WAAO,OAAO,SAAS,MAAM;AAAA,EAC/B;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAgBA,MAAM,aAA8B;AAClC,QAAI,KAAK,YAAa,QAAO,KAAK;AAClC,UAAM,WAA8B,MAAM,KAAK,OAAO,KAAK;AAAA,MACzD;AAAA,IACF;AACA,SAAK,cAAc,SAAS;AAC5B,WAAO,KAAK;AAAA,EACd;AAAA,EAEA,MAAc,wBAAwB,SAAmC;AACvE,WAAO,WAAW,KAAK,WAAW;AAAA,EACpC;AAAA;AAAA,EAGQ,sBAAsB,UAAwB;AACpD,QAAI,UAAU,WAAW,IAAK,QAAO;AACrC,UAAM,OAAO,UAAU;AACvB,WACE,MAAM,SAAS,WAAW,SAAS,KACnC,MAAM,SAAS,SAAS,WAAW;AAAA,EAEvC;AACF;","names":[]}
1
+ {"version":3,"sources":["../src/index.ts","../src/bank.ts"],"sourcesContent":["export * from \"./bank\";\n","import { type Rollup, SovereignClient } from \"@sovereign-sdk/web3\";\nimport { bech32m } from \"bech32\";\nimport type { ErrorResponse } from \"./types\";\n\n// TokenHolder is an enum: { user: string } | { module: string } | { derived: string }\ntype TokenHolderPayload =\n | { user: string }\n | { module: string }\n | { derived: string };\n\ntype TokenStatePayload = {\n key: string;\n value: {\n name: string;\n total_supply: string;\n supply_cap: string;\n admins: TokenHolderPayload[];\n };\n};\n\nexport type TokenMetadata = {\n name: string;\n decimals: number;\n totalSupply: bigint;\n supplyCap: bigint;\n admins: string[];\n};\n\nfunction getHolderAddress(holder: TokenHolderPayload): string {\n if (\"user\" in holder) return holder.user;\n if (\"module\" in holder) return holder.module;\n return holder.derived;\n}\n\ntype GasTokenIdPayload = {\n token_id: string;\n};\n\ntype BalancePayload = {\n amount: string;\n token_id: string;\n};\n\n/**\n * Bank class for interacting with the Sovereign SDK Bank module.\n */\nexport class Bank {\n // biome-ignore lint/suspicious/noExplicitAny: types arent used\n private readonly rollup: Rollup<any, any>;\n private _gasTokenId?: string;\n\n /**\n * Creates a new Bank instance.\n *\n * @param rollup - The rollup instance to use for HTTP requests\n */\n // biome-ignore lint/suspicious/noExplicitAny: types arent used\n constructor(rollup: Rollup<any, any>) {\n this.rollup = rollup;\n }\n\n /**\n * Gets the balance of a specific token for a given address.\n *\n * If no token ID is provided, the gas token balance will be returned.\n *\n * @param address - The address to query the balance for\n * @param tokenId - Optional token ID. If not provided, uses the gas token\n * @returns Promise resolving to the token balance as a bigint\n * @throws {SovereignClient.APIError} When the request fails for reasons other than missing account\n * @example\n * ```typescript\n * const bank = new Bank(rollup);\n * const balance = await bank.balance(\"0x123...\");\n * console.log(`Balance: ${balance}`);\n *\n * // Query specific token balance\n * const tokenBalance = await bank.balance(\"0x123...\", \"token_123\");\n * ```\n */\n async balance(address: string, tokenId?: string): Promise<bigint> {\n const token = await this.tokenIdOrElseGasTokenId(tokenId);\n\n try {\n const response: BalancePayload = await this.rollup.http.get(\n `/modules/bank/tokens/${token}/balances/${address}`,\n );\n\n return BigInt(response.amount);\n } catch (err) {\n if (this.isMissingAccountError(err)) return BigInt(0);\n\n throw err;\n }\n }\n\n /**\n * Gets the metadata of a specific token.\n *\n * If no token ID is provided, returns the metadata of the gas token.\n *\n * @param tokenId - Optional token ID. If not provided, uses the gas token\n * @returns Promise resolving to the token metadata\n * @throws {SovereignClient.APIError} When the request fails\n * @example\n * ```typescript\n * const bank = new Bank(rollup);\n * const metadata = await bank.tokenMetadata();\n * console.log(`Token name: ${metadata.name}, decimals: ${metadata.decimals}`);\n *\n * // Query specific token metadata\n * const tokenMeta = await bank.tokenMetadata(\"token_123\");\n * ```\n */\n async tokenMetadata(tokenId?: string): Promise<TokenMetadata> {\n const token = await this.tokenIdOrElseGasTokenId(tokenId);\n const response: TokenStatePayload = await this.rollup.http.get(\n `/modules/bank/state/tokens/items/${token}`,\n );\n\n // Decimals are encoded in the last byte of the token ID (byte 31 of the 32-byte hash)\n const decoded = bech32m.decode(token);\n const bytes = bech32m.fromWords(decoded.words);\n const decimals = bytes[31];\n\n return {\n name: response.value.name,\n decimals,\n totalSupply: BigInt(response.value.total_supply),\n supplyCap: BigInt(response.value.supply_cap),\n admins: response.value.admins.map(getHolderAddress),\n };\n }\n\n /**\n * Gets the gas token identifier.\n *\n * The gas token ID is cached after the first request to avoid repeated API calls.\n *\n * @returns Promise resolving to the gas token ID as a string\n * @throws {Error} When the gas token ID cannot be retrieved\n * @example\n * ```typescript\n * const bank = new Bank(rollup);\n * const gasTokenId = await bank.gasTokenId();\n * console.log(`Gas token ID: ${gasTokenId}`);\n * ```\n */\n async gasTokenId(): Promise<string> {\n if (this._gasTokenId) return this._gasTokenId;\n const response: GasTokenIdPayload = await this.rollup.http.get(\n \"/modules/bank/tokens/gas_token\",\n );\n this._gasTokenId = response.token_id;\n return this._gasTokenId;\n }\n\n private async tokenIdOrElseGasTokenId(tokenId?: string): Promise<string> {\n return tokenId ?? this.gasTokenId();\n }\n\n // biome-ignore lint/suspicious/noExplicitAny: todo\n private isMissingAccountError(anyError: any): boolean {\n if (anyError?.status !== 404) return false;\n const body = anyError?.error as ErrorResponse;\n return (\n body?.message?.startsWith(\"Balance\") &&\n body?.message?.endsWith(\"not found\")\n );\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACCA,oBAAwB;AA2BxB,SAAS,iBAAiB,QAAoC;AAC5D,MAAI,UAAU,OAAQ,QAAO,OAAO;AACpC,MAAI,YAAY,OAAQ,QAAO,OAAO;AACtC,SAAO,OAAO;AAChB;AAcO,IAAM,OAAN,MAAW;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAWhB,YAAY,QAA0B;AACpC,SAAK,SAAS;AAAA,EAChB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAqBA,MAAM,QAAQ,SAAiB,SAAmC;AAChE,UAAM,QAAQ,MAAM,KAAK,wBAAwB,OAAO;AAExD,QAAI;AACF,YAAM,WAA2B,MAAM,KAAK,OAAO,KAAK;AAAA,QACtD,wBAAwB,KAAK,aAAa,OAAO;AAAA,MACnD;AAEA,aAAO,OAAO,SAAS,MAAM;AAAA,IAC/B,SAAS,KAAK;AACZ,UAAI,KAAK,sBAAsB,GAAG,EAAG,QAAO,OAAO,CAAC;AAEpD,YAAM;AAAA,IACR;AAAA,EACF;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAoBA,MAAM,cAAc,SAA0C;AAC5D,UAAM,QAAQ,MAAM,KAAK,wBAAwB,OAAO;AACxD,UAAM,WAA8B,MAAM,KAAK,OAAO,KAAK;AAAA,MACzD,oCAAoC,KAAK;AAAA,IAC3C;AAGA,UAAM,UAAU,sBAAQ,OAAO,KAAK;AACpC,UAAM,QAAQ,sBAAQ,UAAU,QAAQ,KAAK;AAC7C,UAAM,WAAW,MAAM,EAAE;AAEzB,WAAO;AAAA,MACL,MAAM,SAAS,MAAM;AAAA,MACrB;AAAA,MACA,aAAa,OAAO,SAAS,MAAM,YAAY;AAAA,MAC/C,WAAW,OAAO,SAAS,MAAM,UAAU;AAAA,MAC3C,QAAQ,SAAS,MAAM,OAAO,IAAI,gBAAgB;AAAA,IACpD;AAAA,EACF;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAgBA,MAAM,aAA8B;AAClC,QAAI,KAAK,YAAa,QAAO,KAAK;AAClC,UAAM,WAA8B,MAAM,KAAK,OAAO,KAAK;AAAA,MACzD;AAAA,IACF;AACA,SAAK,cAAc,SAAS;AAC5B,WAAO,KAAK;AAAA,EACd;AAAA,EAEA,MAAc,wBAAwB,SAAmC;AACvE,WAAO,WAAW,KAAK,WAAW;AAAA,EACpC;AAAA;AAAA,EAGQ,sBAAsB,UAAwB;AACpD,QAAI,UAAU,WAAW,IAAK,QAAO;AACrC,UAAM,OAAO,UAAU;AACvB,WACE,MAAM,SAAS,WAAW,SAAS,KACnC,MAAM,SAAS,SAAS,WAAW;AAAA,EAEvC;AACF;","names":[]}
package/dist/index.mjs CHANGED
@@ -1,4 +1,10 @@
1
1
  // src/bank.ts
2
+ import { bech32m } from "bech32";
3
+ function getHolderAddress(holder) {
4
+ if ("user" in holder) return holder.user;
5
+ if ("module" in holder) return holder.module;
6
+ return holder.derived;
7
+ }
2
8
  var Bank = class {
3
9
  /**
4
10
  * Creates a new Bank instance.
@@ -41,29 +47,38 @@ var Bank = class {
41
47
  }
42
48
  }
43
49
  /**
44
- * Gets the total supply of a specific token.
50
+ * Gets the metadata of a specific token.
45
51
  *
46
- * If no token ID is provided, returns the total supply of the gas token.
52
+ * If no token ID is provided, returns the metadata of the gas token.
47
53
  *
48
54
  * @param tokenId - Optional token ID. If not provided, uses the gas token
49
- * @returns Promise resolving to the total supply as a bigint
55
+ * @returns Promise resolving to the token metadata
50
56
  * @throws {SovereignClient.APIError} When the request fails
51
57
  * @example
52
58
  * ```typescript
53
59
  * const bank = new Bank(rollup);
54
- * const totalSupply = await bank.totalSupply();
55
- * console.log(`Total gas token supply: ${totalSupply}`);
60
+ * const metadata = await bank.tokenMetadata();
61
+ * console.log(`Token name: ${metadata.name}, decimals: ${metadata.decimals}`);
56
62
  *
57
- * // Query specific token supply
58
- * const tokenSupply = await bank.totalSupply("token_123");
63
+ * // Query specific token metadata
64
+ * const tokenMeta = await bank.tokenMetadata("token_123");
59
65
  * ```
60
66
  */
61
- async totalSupply(tokenId) {
67
+ async tokenMetadata(tokenId) {
62
68
  const token = await this.tokenIdOrElseGasTokenId(tokenId);
63
69
  const response = await this.rollup.http.get(
64
- `/modules/bank/tokens/${token}/total-supply`
70
+ `/modules/bank/state/tokens/items/${token}`
65
71
  );
66
- return BigInt(response.amount);
72
+ const decoded = bech32m.decode(token);
73
+ const bytes = bech32m.fromWords(decoded.words);
74
+ const decimals = bytes[31];
75
+ return {
76
+ name: response.value.name,
77
+ decimals,
78
+ totalSupply: BigInt(response.value.total_supply),
79
+ supplyCap: BigInt(response.value.supply_cap),
80
+ admins: response.value.admins.map(getHolderAddress)
81
+ };
67
82
  }
68
83
  /**
69
84
  * Gets the gas token identifier.
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/bank.ts"],"sourcesContent":["import { type Rollup, SovereignClient } from \"@sovereign-sdk/web3\";\nimport type { ErrorResponse } from \"./types\";\n\ntype GasTokenIdPayload = {\n token_id: string;\n};\n\ntype BalancePayload = {\n amount: string;\n token_id: string;\n};\n\ntype TotalSupplyPayload = {\n amount: string;\n token_id: string;\n};\n\n/**\n * Bank class for interacting with the Sovereign SDK Bank module.\n */\nexport class Bank {\n // biome-ignore lint/suspicious/noExplicitAny: types arent used\n private readonly rollup: Rollup<any, any>;\n private _gasTokenId?: string;\n\n /**\n * Creates a new Bank instance.\n *\n * @param rollup - The rollup instance to use for HTTP requests\n */\n // biome-ignore lint/suspicious/noExplicitAny: types arent used\n constructor(rollup: Rollup<any, any>) {\n this.rollup = rollup;\n }\n\n /**\n * Gets the balance of a specific token for a given address.\n *\n * If no token ID is provided, the gas token balance will be returned.\n *\n * @param address - The address to query the balance for\n * @param tokenId - Optional token ID. If not provided, uses the gas token\n * @returns Promise resolving to the token balance as a bigint\n * @throws {SovereignClient.APIError} When the request fails for reasons other than missing account\n * @example\n * ```typescript\n * const bank = new Bank(rollup);\n * const balance = await bank.balance(\"0x123...\");\n * console.log(`Balance: ${balance}`);\n *\n * // Query specific token balance\n * const tokenBalance = await bank.balance(\"0x123...\", \"token_123\");\n * ```\n */\n async balance(address: string, tokenId?: string): Promise<bigint> {\n const token = await this.tokenIdOrElseGasTokenId(tokenId);\n\n try {\n const response: BalancePayload = await this.rollup.http.get(\n `/modules/bank/tokens/${token}/balances/${address}`,\n );\n\n return BigInt(response.amount);\n } catch (err) {\n if (this.isMissingAccountError(err)) return BigInt(0);\n\n throw err;\n }\n }\n\n /**\n * Gets the total supply of a specific token.\n *\n * If no token ID is provided, returns the total supply of the gas token.\n *\n * @param tokenId - Optional token ID. If not provided, uses the gas token\n * @returns Promise resolving to the total supply as a bigint\n * @throws {SovereignClient.APIError} When the request fails\n * @example\n * ```typescript\n * const bank = new Bank(rollup);\n * const totalSupply = await bank.totalSupply();\n * console.log(`Total gas token supply: ${totalSupply}`);\n *\n * // Query specific token supply\n * const tokenSupply = await bank.totalSupply(\"token_123\");\n * ```\n */\n async totalSupply(tokenId?: string): Promise<bigint> {\n const token = await this.tokenIdOrElseGasTokenId(tokenId);\n const response: TotalSupplyPayload = await this.rollup.http.get(\n `/modules/bank/tokens/${token}/total-supply`,\n );\n return BigInt(response.amount);\n }\n\n /**\n * Gets the gas token identifier.\n *\n * The gas token ID is cached after the first request to avoid repeated API calls.\n *\n * @returns Promise resolving to the gas token ID as a string\n * @throws {Error} When the gas token ID cannot be retrieved\n * @example\n * ```typescript\n * const bank = new Bank(rollup);\n * const gasTokenId = await bank.gasTokenId();\n * console.log(`Gas token ID: ${gasTokenId}`);\n * ```\n */\n async gasTokenId(): Promise<string> {\n if (this._gasTokenId) return this._gasTokenId;\n const response: GasTokenIdPayload = await this.rollup.http.get(\n \"/modules/bank/tokens/gas_token\",\n );\n this._gasTokenId = response.token_id;\n return this._gasTokenId;\n }\n\n private async tokenIdOrElseGasTokenId(tokenId?: string): Promise<string> {\n return tokenId ?? this.gasTokenId();\n }\n\n // biome-ignore lint/suspicious/noExplicitAny: todo\n private isMissingAccountError(anyError: any): boolean {\n if (anyError?.status !== 404) return false;\n const body = anyError?.error as ErrorResponse;\n return (\n body?.message?.startsWith(\"Balance\") &&\n body?.message?.endsWith(\"not found\")\n );\n }\n}\n"],"mappings":";AAoBO,IAAM,OAAN,MAAW;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAWhB,YAAY,QAA0B;AACpC,SAAK,SAAS;AAAA,EAChB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAqBA,MAAM,QAAQ,SAAiB,SAAmC;AAChE,UAAM,QAAQ,MAAM,KAAK,wBAAwB,OAAO;AAExD,QAAI;AACF,YAAM,WAA2B,MAAM,KAAK,OAAO,KAAK;AAAA,QACtD,wBAAwB,KAAK,aAAa,OAAO;AAAA,MACnD;AAEA,aAAO,OAAO,SAAS,MAAM;AAAA,IAC/B,SAAS,KAAK;AACZ,UAAI,KAAK,sBAAsB,GAAG,EAAG,QAAO,OAAO,CAAC;AAEpD,YAAM;AAAA,IACR;AAAA,EACF;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAoBA,MAAM,YAAY,SAAmC;AACnD,UAAM,QAAQ,MAAM,KAAK,wBAAwB,OAAO;AACxD,UAAM,WAA+B,MAAM,KAAK,OAAO,KAAK;AAAA,MAC1D,wBAAwB,KAAK;AAAA,IAC/B;AACA,WAAO,OAAO,SAAS,MAAM;AAAA,EAC/B;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAgBA,MAAM,aAA8B;AAClC,QAAI,KAAK,YAAa,QAAO,KAAK;AAClC,UAAM,WAA8B,MAAM,KAAK,OAAO,KAAK;AAAA,MACzD;AAAA,IACF;AACA,SAAK,cAAc,SAAS;AAC5B,WAAO,KAAK;AAAA,EACd;AAAA,EAEA,MAAc,wBAAwB,SAAmC;AACvE,WAAO,WAAW,KAAK,WAAW;AAAA,EACpC;AAAA;AAAA,EAGQ,sBAAsB,UAAwB;AACpD,QAAI,UAAU,WAAW,IAAK,QAAO;AACrC,UAAM,OAAO,UAAU;AACvB,WACE,MAAM,SAAS,WAAW,SAAS,KACnC,MAAM,SAAS,SAAS,WAAW;AAAA,EAEvC;AACF;","names":[]}
1
+ {"version":3,"sources":["../src/bank.ts"],"sourcesContent":["import { type Rollup, SovereignClient } from \"@sovereign-sdk/web3\";\nimport { bech32m } from \"bech32\";\nimport type { ErrorResponse } from \"./types\";\n\n// TokenHolder is an enum: { user: string } | { module: string } | { derived: string }\ntype TokenHolderPayload =\n | { user: string }\n | { module: string }\n | { derived: string };\n\ntype TokenStatePayload = {\n key: string;\n value: {\n name: string;\n total_supply: string;\n supply_cap: string;\n admins: TokenHolderPayload[];\n };\n};\n\nexport type TokenMetadata = {\n name: string;\n decimals: number;\n totalSupply: bigint;\n supplyCap: bigint;\n admins: string[];\n};\n\nfunction getHolderAddress(holder: TokenHolderPayload): string {\n if (\"user\" in holder) return holder.user;\n if (\"module\" in holder) return holder.module;\n return holder.derived;\n}\n\ntype GasTokenIdPayload = {\n token_id: string;\n};\n\ntype BalancePayload = {\n amount: string;\n token_id: string;\n};\n\n/**\n * Bank class for interacting with the Sovereign SDK Bank module.\n */\nexport class Bank {\n // biome-ignore lint/suspicious/noExplicitAny: types arent used\n private readonly rollup: Rollup<any, any>;\n private _gasTokenId?: string;\n\n /**\n * Creates a new Bank instance.\n *\n * @param rollup - The rollup instance to use for HTTP requests\n */\n // biome-ignore lint/suspicious/noExplicitAny: types arent used\n constructor(rollup: Rollup<any, any>) {\n this.rollup = rollup;\n }\n\n /**\n * Gets the balance of a specific token for a given address.\n *\n * If no token ID is provided, the gas token balance will be returned.\n *\n * @param address - The address to query the balance for\n * @param tokenId - Optional token ID. If not provided, uses the gas token\n * @returns Promise resolving to the token balance as a bigint\n * @throws {SovereignClient.APIError} When the request fails for reasons other than missing account\n * @example\n * ```typescript\n * const bank = new Bank(rollup);\n * const balance = await bank.balance(\"0x123...\");\n * console.log(`Balance: ${balance}`);\n *\n * // Query specific token balance\n * const tokenBalance = await bank.balance(\"0x123...\", \"token_123\");\n * ```\n */\n async balance(address: string, tokenId?: string): Promise<bigint> {\n const token = await this.tokenIdOrElseGasTokenId(tokenId);\n\n try {\n const response: BalancePayload = await this.rollup.http.get(\n `/modules/bank/tokens/${token}/balances/${address}`,\n );\n\n return BigInt(response.amount);\n } catch (err) {\n if (this.isMissingAccountError(err)) return BigInt(0);\n\n throw err;\n }\n }\n\n /**\n * Gets the metadata of a specific token.\n *\n * If no token ID is provided, returns the metadata of the gas token.\n *\n * @param tokenId - Optional token ID. If not provided, uses the gas token\n * @returns Promise resolving to the token metadata\n * @throws {SovereignClient.APIError} When the request fails\n * @example\n * ```typescript\n * const bank = new Bank(rollup);\n * const metadata = await bank.tokenMetadata();\n * console.log(`Token name: ${metadata.name}, decimals: ${metadata.decimals}`);\n *\n * // Query specific token metadata\n * const tokenMeta = await bank.tokenMetadata(\"token_123\");\n * ```\n */\n async tokenMetadata(tokenId?: string): Promise<TokenMetadata> {\n const token = await this.tokenIdOrElseGasTokenId(tokenId);\n const response: TokenStatePayload = await this.rollup.http.get(\n `/modules/bank/state/tokens/items/${token}`,\n );\n\n // Decimals are encoded in the last byte of the token ID (byte 31 of the 32-byte hash)\n const decoded = bech32m.decode(token);\n const bytes = bech32m.fromWords(decoded.words);\n const decimals = bytes[31];\n\n return {\n name: response.value.name,\n decimals,\n totalSupply: BigInt(response.value.total_supply),\n supplyCap: BigInt(response.value.supply_cap),\n admins: response.value.admins.map(getHolderAddress),\n };\n }\n\n /**\n * Gets the gas token identifier.\n *\n * The gas token ID is cached after the first request to avoid repeated API calls.\n *\n * @returns Promise resolving to the gas token ID as a string\n * @throws {Error} When the gas token ID cannot be retrieved\n * @example\n * ```typescript\n * const bank = new Bank(rollup);\n * const gasTokenId = await bank.gasTokenId();\n * console.log(`Gas token ID: ${gasTokenId}`);\n * ```\n */\n async gasTokenId(): Promise<string> {\n if (this._gasTokenId) return this._gasTokenId;\n const response: GasTokenIdPayload = await this.rollup.http.get(\n \"/modules/bank/tokens/gas_token\",\n );\n this._gasTokenId = response.token_id;\n return this._gasTokenId;\n }\n\n private async tokenIdOrElseGasTokenId(tokenId?: string): Promise<string> {\n return tokenId ?? this.gasTokenId();\n }\n\n // biome-ignore lint/suspicious/noExplicitAny: todo\n private isMissingAccountError(anyError: any): boolean {\n if (anyError?.status !== 404) return false;\n const body = anyError?.error as ErrorResponse;\n return (\n body?.message?.startsWith(\"Balance\") &&\n body?.message?.endsWith(\"not found\")\n );\n }\n}\n"],"mappings":";AACA,SAAS,eAAe;AA2BxB,SAAS,iBAAiB,QAAoC;AAC5D,MAAI,UAAU,OAAQ,QAAO,OAAO;AACpC,MAAI,YAAY,OAAQ,QAAO,OAAO;AACtC,SAAO,OAAO;AAChB;AAcO,IAAM,OAAN,MAAW;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAWhB,YAAY,QAA0B;AACpC,SAAK,SAAS;AAAA,EAChB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAqBA,MAAM,QAAQ,SAAiB,SAAmC;AAChE,UAAM,QAAQ,MAAM,KAAK,wBAAwB,OAAO;AAExD,QAAI;AACF,YAAM,WAA2B,MAAM,KAAK,OAAO,KAAK;AAAA,QACtD,wBAAwB,KAAK,aAAa,OAAO;AAAA,MACnD;AAEA,aAAO,OAAO,SAAS,MAAM;AAAA,IAC/B,SAAS,KAAK;AACZ,UAAI,KAAK,sBAAsB,GAAG,EAAG,QAAO,OAAO,CAAC;AAEpD,YAAM;AAAA,IACR;AAAA,EACF;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAoBA,MAAM,cAAc,SAA0C;AAC5D,UAAM,QAAQ,MAAM,KAAK,wBAAwB,OAAO;AACxD,UAAM,WAA8B,MAAM,KAAK,OAAO,KAAK;AAAA,MACzD,oCAAoC,KAAK;AAAA,IAC3C;AAGA,UAAM,UAAU,QAAQ,OAAO,KAAK;AACpC,UAAM,QAAQ,QAAQ,UAAU,QAAQ,KAAK;AAC7C,UAAM,WAAW,MAAM,EAAE;AAEzB,WAAO;AAAA,MACL,MAAM,SAAS,MAAM;AAAA,MACrB;AAAA,MACA,aAAa,OAAO,SAAS,MAAM,YAAY;AAAA,MAC/C,WAAW,OAAO,SAAS,MAAM,UAAU;AAAA,MAC3C,QAAQ,SAAS,MAAM,OAAO,IAAI,gBAAgB;AAAA,IACpD;AAAA,EACF;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAgBA,MAAM,aAA8B;AAClC,QAAI,KAAK,YAAa,QAAO,KAAK;AAClC,UAAM,WAA8B,MAAM,KAAK,OAAO,KAAK;AAAA,MACzD;AAAA,IACF;AACA,SAAK,cAAc,SAAS;AAC5B,WAAO,KAAK;AAAA,EACd;AAAA,EAEA,MAAc,wBAAwB,SAAmC;AACvE,WAAO,WAAW,KAAK,WAAW;AAAA,EACpC;AAAA;AAAA,EAGQ,sBAAsB,UAAwB;AACpD,QAAI,UAAU,WAAW,IAAK,QAAO;AACrC,UAAM,OAAO,UAAU;AACvB,WACE,MAAM,SAAS,WAAW,SAAS,KACnC,MAAM,SAAS,SAAS,WAAW;AAAA,EAEvC;AACF;","names":[]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sovereign-sdk/modules",
3
- "version": "0.1.12",
3
+ "version": "0.2.1",
4
4
  "description": "A package providing convenient helpers for interacting with core Sovereign SDK modules",
5
5
  "repository": "github:Sovereign-Labs/sovereign-sdk",
6
6
  "files": [
@@ -32,8 +32,9 @@
32
32
  "access": "public"
33
33
  },
34
34
  "dependencies": {
35
- "@sovereign-sdk/utils": "^0.0.4",
36
- "@sovereign-sdk/web3": "^0.12.4"
35
+ "bech32": "^2.0.0",
36
+ "@sovereign-sdk/web3": "^0.12.6",
37
+ "@sovereign-sdk/utils": "^0.0.4"
37
38
  },
38
39
  "scripts": {
39
40
  "build": "tsup",