@sovereign-sdk/modules 0.1.11 → 0.2.0
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 +16 -9
- package/dist/index.d.ts +16 -9
- package/dist/index.js +51 -65
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +51 -66
- package/dist/index.mjs.map +1 -1
- package/package.json +4 -3
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
|
|
43
|
+
* Gets the metadata of a specific token.
|
|
37
44
|
*
|
|
38
|
-
* If no token ID is provided, returns the
|
|
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
|
|
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
|
|
47
|
-
* console.log(`
|
|
53
|
+
* const metadata = await bank.tokenMetadata();
|
|
54
|
+
* console.log(`Token name: ${metadata.name}, decimals: ${metadata.decimals}`);
|
|
48
55
|
*
|
|
49
|
-
* // Query specific token
|
|
50
|
-
* const
|
|
56
|
+
* // Query specific token metadata
|
|
57
|
+
* const tokenMeta = await bank.tokenMetadata("token_123");
|
|
51
58
|
* ```
|
|
52
59
|
*/
|
|
53
|
-
|
|
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
|
|
43
|
+
* Gets the metadata of a specific token.
|
|
37
44
|
*
|
|
38
|
-
* If no token ID is provided, returns the
|
|
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
|
|
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
|
|
47
|
-
* console.log(`
|
|
53
|
+
* const metadata = await bank.tokenMetadata();
|
|
54
|
+
* console.log(`Token name: ${metadata.name}, decimals: ${metadata.decimals}`);
|
|
48
55
|
*
|
|
49
|
-
* // Query specific token
|
|
50
|
-
* const
|
|
56
|
+
* // Query specific token metadata
|
|
57
|
+
* const tokenMeta = await bank.tokenMetadata("token_123");
|
|
51
58
|
* ```
|
|
52
59
|
*/
|
|
53
|
-
|
|
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
|
@@ -16,26 +16,6 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
16
16
|
return to;
|
|
17
17
|
};
|
|
18
18
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
-
var __async = (__this, __arguments, generator) => {
|
|
20
|
-
return new Promise((resolve, reject) => {
|
|
21
|
-
var fulfilled = (value) => {
|
|
22
|
-
try {
|
|
23
|
-
step(generator.next(value));
|
|
24
|
-
} catch (e) {
|
|
25
|
-
reject(e);
|
|
26
|
-
}
|
|
27
|
-
};
|
|
28
|
-
var rejected = (value) => {
|
|
29
|
-
try {
|
|
30
|
-
step(generator.throw(value));
|
|
31
|
-
} catch (e) {
|
|
32
|
-
reject(e);
|
|
33
|
-
}
|
|
34
|
-
};
|
|
35
|
-
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
36
|
-
step((generator = generator.apply(__this, __arguments)).next());
|
|
37
|
-
});
|
|
38
|
-
};
|
|
39
19
|
|
|
40
20
|
// src/index.ts
|
|
41
21
|
var src_exports = {};
|
|
@@ -45,6 +25,12 @@ __export(src_exports, {
|
|
|
45
25
|
module.exports = __toCommonJS(src_exports);
|
|
46
26
|
|
|
47
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
|
+
}
|
|
48
34
|
var Bank = class {
|
|
49
35
|
/**
|
|
50
36
|
* Creates a new Bank instance.
|
|
@@ -74,46 +60,51 @@ var Bank = class {
|
|
|
74
60
|
* const tokenBalance = await bank.balance("0x123...", "token_123");
|
|
75
61
|
* ```
|
|
76
62
|
*/
|
|
77
|
-
balance(address, tokenId) {
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
}
|
|
89
|
-
});
|
|
63
|
+
async balance(address, tokenId) {
|
|
64
|
+
const token = await this.tokenIdOrElseGasTokenId(tokenId);
|
|
65
|
+
try {
|
|
66
|
+
const response = await this.rollup.http.get(
|
|
67
|
+
`/modules/bank/tokens/${token}/balances/${address}`
|
|
68
|
+
);
|
|
69
|
+
return BigInt(response.amount);
|
|
70
|
+
} catch (err) {
|
|
71
|
+
if (this.isMissingAccountError(err)) return BigInt(0);
|
|
72
|
+
throw err;
|
|
73
|
+
}
|
|
90
74
|
}
|
|
91
75
|
/**
|
|
92
|
-
* Gets the
|
|
76
|
+
* Gets the metadata of a specific token.
|
|
93
77
|
*
|
|
94
|
-
* If no token ID is provided, returns the
|
|
78
|
+
* If no token ID is provided, returns the metadata of the gas token.
|
|
95
79
|
*
|
|
96
80
|
* @param tokenId - Optional token ID. If not provided, uses the gas token
|
|
97
|
-
* @returns Promise resolving to the
|
|
81
|
+
* @returns Promise resolving to the token metadata
|
|
98
82
|
* @throws {SovereignClient.APIError} When the request fails
|
|
99
83
|
* @example
|
|
100
84
|
* ```typescript
|
|
101
85
|
* const bank = new Bank(rollup);
|
|
102
|
-
* const
|
|
103
|
-
* console.log(`
|
|
86
|
+
* const metadata = await bank.tokenMetadata();
|
|
87
|
+
* console.log(`Token name: ${metadata.name}, decimals: ${metadata.decimals}`);
|
|
104
88
|
*
|
|
105
|
-
* // Query specific token
|
|
106
|
-
* const
|
|
89
|
+
* // Query specific token metadata
|
|
90
|
+
* const tokenMeta = await bank.tokenMetadata("token_123");
|
|
107
91
|
* ```
|
|
108
92
|
*/
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
93
|
+
async tokenMetadata(tokenId) {
|
|
94
|
+
const token = await this.tokenIdOrElseGasTokenId(tokenId);
|
|
95
|
+
const response = await this.rollup.http.get(
|
|
96
|
+
`/modules/bank/state/tokens/items/${token}`
|
|
97
|
+
);
|
|
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
|
+
};
|
|
117
108
|
}
|
|
118
109
|
/**
|
|
119
110
|
* Gets the gas token identifier.
|
|
@@ -129,27 +120,22 @@ var Bank = class {
|
|
|
129
120
|
* console.log(`Gas token ID: ${gasTokenId}`);
|
|
130
121
|
* ```
|
|
131
122
|
*/
|
|
132
|
-
gasTokenId() {
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
return this._gasTokenId;
|
|
140
|
-
});
|
|
123
|
+
async gasTokenId() {
|
|
124
|
+
if (this._gasTokenId) return this._gasTokenId;
|
|
125
|
+
const response = await this.rollup.http.get(
|
|
126
|
+
"/modules/bank/tokens/gas_token"
|
|
127
|
+
);
|
|
128
|
+
this._gasTokenId = response.token_id;
|
|
129
|
+
return this._gasTokenId;
|
|
141
130
|
}
|
|
142
|
-
tokenIdOrElseGasTokenId(tokenId) {
|
|
143
|
-
return
|
|
144
|
-
return tokenId != null ? tokenId : this.gasTokenId();
|
|
145
|
-
});
|
|
131
|
+
async tokenIdOrElseGasTokenId(tokenId) {
|
|
132
|
+
return tokenId ?? this.gasTokenId();
|
|
146
133
|
}
|
|
147
134
|
// biome-ignore lint/suspicious/noExplicitAny: todo
|
|
148
135
|
isMissingAccountError(anyError) {
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
return ((_a = body == null ? void 0 : body.message) == null ? void 0 : _a.startsWith("Balance")) && ((_b = body == null ? void 0 : body.message) == null ? void 0 : _b.endsWith("not found"));
|
|
136
|
+
if (anyError?.status !== 404) return false;
|
|
137
|
+
const body = anyError?.error;
|
|
138
|
+
return body?.message?.startsWith("Balance") && body?.message?.endsWith("not found");
|
|
153
139
|
}
|
|
154
140
|
};
|
|
155
141
|
// Annotate the CommonJS export names for ESM import in node:
|
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
|
|
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,Q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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,25 +1,10 @@
|
|
|
1
|
-
var __async = (__this, __arguments, generator) => {
|
|
2
|
-
return new Promise((resolve, reject) => {
|
|
3
|
-
var fulfilled = (value) => {
|
|
4
|
-
try {
|
|
5
|
-
step(generator.next(value));
|
|
6
|
-
} catch (e) {
|
|
7
|
-
reject(e);
|
|
8
|
-
}
|
|
9
|
-
};
|
|
10
|
-
var rejected = (value) => {
|
|
11
|
-
try {
|
|
12
|
-
step(generator.throw(value));
|
|
13
|
-
} catch (e) {
|
|
14
|
-
reject(e);
|
|
15
|
-
}
|
|
16
|
-
};
|
|
17
|
-
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
18
|
-
step((generator = generator.apply(__this, __arguments)).next());
|
|
19
|
-
});
|
|
20
|
-
};
|
|
21
|
-
|
|
22
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
|
+
}
|
|
23
8
|
var Bank = class {
|
|
24
9
|
/**
|
|
25
10
|
* Creates a new Bank instance.
|
|
@@ -49,46 +34,51 @@ var Bank = class {
|
|
|
49
34
|
* const tokenBalance = await bank.balance("0x123...", "token_123");
|
|
50
35
|
* ```
|
|
51
36
|
*/
|
|
52
|
-
balance(address, tokenId) {
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
}
|
|
64
|
-
});
|
|
37
|
+
async balance(address, tokenId) {
|
|
38
|
+
const token = await this.tokenIdOrElseGasTokenId(tokenId);
|
|
39
|
+
try {
|
|
40
|
+
const response = await this.rollup.http.get(
|
|
41
|
+
`/modules/bank/tokens/${token}/balances/${address}`
|
|
42
|
+
);
|
|
43
|
+
return BigInt(response.amount);
|
|
44
|
+
} catch (err) {
|
|
45
|
+
if (this.isMissingAccountError(err)) return BigInt(0);
|
|
46
|
+
throw err;
|
|
47
|
+
}
|
|
65
48
|
}
|
|
66
49
|
/**
|
|
67
|
-
* Gets the
|
|
50
|
+
* Gets the metadata of a specific token.
|
|
68
51
|
*
|
|
69
|
-
* If no token ID is provided, returns the
|
|
52
|
+
* If no token ID is provided, returns the metadata of the gas token.
|
|
70
53
|
*
|
|
71
54
|
* @param tokenId - Optional token ID. If not provided, uses the gas token
|
|
72
|
-
* @returns Promise resolving to the
|
|
55
|
+
* @returns Promise resolving to the token metadata
|
|
73
56
|
* @throws {SovereignClient.APIError} When the request fails
|
|
74
57
|
* @example
|
|
75
58
|
* ```typescript
|
|
76
59
|
* const bank = new Bank(rollup);
|
|
77
|
-
* const
|
|
78
|
-
* console.log(`
|
|
60
|
+
* const metadata = await bank.tokenMetadata();
|
|
61
|
+
* console.log(`Token name: ${metadata.name}, decimals: ${metadata.decimals}`);
|
|
79
62
|
*
|
|
80
|
-
* // Query specific token
|
|
81
|
-
* const
|
|
63
|
+
* // Query specific token metadata
|
|
64
|
+
* const tokenMeta = await bank.tokenMetadata("token_123");
|
|
82
65
|
* ```
|
|
83
66
|
*/
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
67
|
+
async tokenMetadata(tokenId) {
|
|
68
|
+
const token = await this.tokenIdOrElseGasTokenId(tokenId);
|
|
69
|
+
const response = await this.rollup.http.get(
|
|
70
|
+
`/modules/bank/state/tokens/items/${token}`
|
|
71
|
+
);
|
|
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
|
+
};
|
|
92
82
|
}
|
|
93
83
|
/**
|
|
94
84
|
* Gets the gas token identifier.
|
|
@@ -104,27 +94,22 @@ var Bank = class {
|
|
|
104
94
|
* console.log(`Gas token ID: ${gasTokenId}`);
|
|
105
95
|
* ```
|
|
106
96
|
*/
|
|
107
|
-
gasTokenId() {
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
return this._gasTokenId;
|
|
115
|
-
});
|
|
97
|
+
async gasTokenId() {
|
|
98
|
+
if (this._gasTokenId) return this._gasTokenId;
|
|
99
|
+
const response = await this.rollup.http.get(
|
|
100
|
+
"/modules/bank/tokens/gas_token"
|
|
101
|
+
);
|
|
102
|
+
this._gasTokenId = response.token_id;
|
|
103
|
+
return this._gasTokenId;
|
|
116
104
|
}
|
|
117
|
-
tokenIdOrElseGasTokenId(tokenId) {
|
|
118
|
-
return
|
|
119
|
-
return tokenId != null ? tokenId : this.gasTokenId();
|
|
120
|
-
});
|
|
105
|
+
async tokenIdOrElseGasTokenId(tokenId) {
|
|
106
|
+
return tokenId ?? this.gasTokenId();
|
|
121
107
|
}
|
|
122
108
|
// biome-ignore lint/suspicious/noExplicitAny: todo
|
|
123
109
|
isMissingAccountError(anyError) {
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
return ((_a = body == null ? void 0 : body.message) == null ? void 0 : _a.startsWith("Balance")) && ((_b = body == null ? void 0 : body.message) == null ? void 0 : _b.endsWith("not found"));
|
|
110
|
+
if (anyError?.status !== 404) return false;
|
|
111
|
+
const body = anyError?.error;
|
|
112
|
+
return body?.message?.startsWith("Balance") && body?.message?.endsWith("not found");
|
|
128
113
|
}
|
|
129
114
|
};
|
|
130
115
|
export {
|
package/dist/index.mjs.map
CHANGED
|
@@ -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
|
|
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;AA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AAc,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.
|
|
3
|
+
"version": "0.2.0",
|
|
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": [
|
|
@@ -22,7 +22,7 @@
|
|
|
22
22
|
"tsup": "^8.3.0",
|
|
23
23
|
"typescript": "^5.6.3",
|
|
24
24
|
"vitest": "4.0.7",
|
|
25
|
-
"@sovereign-sdk/signers": "^0.5.
|
|
25
|
+
"@sovereign-sdk/signers": "^0.5.2"
|
|
26
26
|
},
|
|
27
27
|
"peerDependencies": {
|
|
28
28
|
"tsup": "^8.3.0",
|
|
@@ -32,7 +32,8 @@
|
|
|
32
32
|
"access": "public"
|
|
33
33
|
},
|
|
34
34
|
"dependencies": {
|
|
35
|
-
"
|
|
35
|
+
"bech32": "^2.0.0",
|
|
36
|
+
"@sovereign-sdk/web3": "^0.12.5",
|
|
36
37
|
"@sovereign-sdk/utils": "^0.0.4"
|
|
37
38
|
},
|
|
38
39
|
"scripts": {
|