@alephium/web3 0.12.5 → 0.13.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.
@@ -938,7 +938,7 @@ export declare class HttpClient<SecurityDataType = unknown> {
938
938
  }
939
939
  /**
940
940
  * @title Alephium API
941
- * @version 2.3.3
941
+ * @version 2.3.5
942
942
  * @baseUrl ../
943
943
  */
944
944
  export declare class Api<SecurityDataType extends unknown> extends HttpClient<SecurityDataType> {
@@ -1373,7 +1373,9 @@ export declare class Api<SecurityDataType extends unknown> extends HttpClient<Se
1373
1373
  * @summary Get the balance of an address
1374
1374
  * @request GET:/addresses/{address}/balance
1375
1375
  */
1376
- getAddressesAddressBalance: (address: string, params?: RequestParams) => Promise<Balance>;
1376
+ getAddressesAddressBalance: (address: string, query?: {
1377
+ mempool?: boolean;
1378
+ }, params?: RequestParams) => Promise<Balance>;
1377
1379
  /**
1378
1380
  * No description
1379
1381
  *
@@ -151,7 +151,7 @@ class HttpClient {
151
151
  exports.HttpClient = HttpClient;
152
152
  /**
153
153
  * @title Alephium API
154
- * @version 2.3.3
154
+ * @version 2.3.5
155
155
  * @baseUrl ../
156
156
  */
157
157
  class Api extends HttpClient {
@@ -758,9 +758,10 @@ class Api extends HttpClient {
758
758
  * @summary Get the balance of an address
759
759
  * @request GET:/addresses/{address}/balance
760
760
  */
761
- getAddressesAddressBalance: (address, params = {}) => this.request({
761
+ getAddressesAddressBalance: (address, query, params = {}) => this.request({
762
762
  path: `/addresses/${address}/balance`,
763
763
  method: 'GET',
764
+ query: query,
764
765
  format: 'json',
765
766
  ...params
766
767
  }).then(utils_1.convertHttpResponse),
@@ -1,4 +1,4 @@
1
- import { ApiRequestArguments, ApiRequestHandler, TokenMetaData } from './types';
1
+ import { ApiRequestArguments, ApiRequestHandler, FungibleTokenMetaData, NFTMetaData } from './types';
2
2
  import { Api as NodeApi } from './api-alephium';
3
3
  import { HexString } from '../contract';
4
4
  interface NodeProviderApis {
@@ -32,7 +32,9 @@ export declare class NodeProvider implements NodeProviderApis {
32
32
  request: (args: ApiRequestArguments) => Promise<any>;
33
33
  static Proxy(nodeProvider: NodeProvider): NodeProvider;
34
34
  static Remote(handler: ApiRequestHandler): NodeProvider;
35
- fetchStdTokenMetaData: (tokenId: HexString) => Promise<TokenMetaData>;
35
+ fetchFungibleTokenMetaData: (tokenId: HexString) => Promise<FungibleTokenMetaData>;
36
+ fetchNFTMetaData: (tokenId: HexString) => Promise<NFTMetaData>;
36
37
  guessStdInterfaceId: (tokenId: HexString) => Promise<HexString | undefined>;
38
+ guessStdTokenType: (tokenId: HexString) => Promise<'fungible' | 'non-fungible' | undefined>;
37
39
  }
38
40
  export {};
@@ -37,8 +37,8 @@ class NodeProvider {
37
37
  this.request = (args) => {
38
38
  return (0, types_1.request)(this, args);
39
39
  };
40
- // Only use this when the token is following the standard token interface
41
- this.fetchStdTokenMetaData = async (tokenId) => {
40
+ // Only use this when the token follows the fungible token interface, check `guessTokenType` first
41
+ this.fetchFungibleTokenMetaData = async (tokenId) => {
42
42
  const address = (0, utils_2.addressFromTokenId)(tokenId);
43
43
  const group = (0, utils_2.groupOfAddress)(address);
44
44
  const calls = Array.from([0, 1, 2, 3], (index) => ({ methodIndex: index, group: group, address: address }));
@@ -52,6 +52,19 @@ class NodeProvider {
52
52
  totalSupply: BigInt(result.results[3].returns[0].value)
53
53
  };
54
54
  };
55
+ // Only use this when the token follows the non-fungile token interface, check `guessTokenType` first
56
+ this.fetchNFTMetaData = async (tokenId) => {
57
+ const address = (0, utils_2.addressFromTokenId)(tokenId);
58
+ const group = (0, utils_2.groupOfAddress)(address);
59
+ const calls = Array.from([0, 1], (index) => ({ methodIndex: index, group: group, address: address }));
60
+ const result = await this.contracts.postContractsMulticallContract({
61
+ calls: calls
62
+ });
63
+ return {
64
+ tokenUri: (0, utils_2.hexToString)(result.results[0].returns[0].value),
65
+ collectionAddress: (0, utils_2.addressFromContractId)(result.results[1].returns[0].value)
66
+ };
67
+ };
55
68
  this.guessStdInterfaceId = async (tokenId) => {
56
69
  const address = (0, utils_2.addressFromTokenId)(tokenId);
57
70
  const group = (0, utils_2.groupOfAddress)(address);
@@ -65,6 +78,17 @@ class NodeProvider {
65
78
  return undefined;
66
79
  }
67
80
  };
81
+ this.guessStdTokenType = async (tokenId) => {
82
+ const interfaceId = await this.guessStdInterfaceId(tokenId);
83
+ switch (interfaceId) {
84
+ case '0001':
85
+ return 'fungible';
86
+ case '0003':
87
+ return 'non-fungible';
88
+ default:
89
+ return undefined;
90
+ }
91
+ };
68
92
  let nodeApi;
69
93
  if (typeof param0 === 'string') {
70
94
  nodeApi = initializeNodeApi(param0, apiKey, customFetch);
@@ -30,9 +30,13 @@ export interface ApiRequestArguments {
30
30
  export type ApiRequestHandler = (args: ApiRequestArguments) => Promise<any>;
31
31
  export declare function forwardRequests(api: Record<string, any>, handler: ApiRequestHandler): void;
32
32
  export declare function request(provider: Record<string, any>, args: ApiRequestArguments): Promise<any>;
33
- export interface TokenMetaData {
33
+ export interface FungibleTokenMetaData {
34
34
  name: string;
35
35
  symbol: string;
36
36
  decimals: number;
37
37
  totalSupply: Number256;
38
38
  }
39
+ export interface NFTMetaData {
40
+ collectionAddress: string;
41
+ tokenUri: string;
42
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@alephium/web3",
3
- "version": "0.12.5",
3
+ "version": "0.13.0",
4
4
  "description": "A JS/TS library to interact with the Alephium platform",
5
5
  "license": "GPL",
6
6
  "main": "dist/src/index.js",
@@ -27,7 +27,7 @@
27
27
  },
28
28
  "author": "Alephium dev <dev@alephium.org>",
29
29
  "config": {
30
- "alephium_version": "2.3.3",
30
+ "alephium_version": "2.3.5",
31
31
  "explorer_backend_version": "1.13.5"
32
32
  },
33
33
  "type": "commonjs",
@@ -1228,7 +1228,7 @@ export class HttpClient<SecurityDataType = unknown> {
1228
1228
 
1229
1229
  /**
1230
1230
  * @title Alephium API
1231
- * @version 2.3.3
1231
+ * @version 2.3.5
1232
1232
  * @baseUrl ../
1233
1233
  */
1234
1234
  export class Api<SecurityDataType extends unknown> extends HttpClient<SecurityDataType> {
@@ -2016,10 +2016,17 @@ export class Api<SecurityDataType extends unknown> extends HttpClient<SecurityDa
2016
2016
  * @summary Get the balance of an address
2017
2017
  * @request GET:/addresses/{address}/balance
2018
2018
  */
2019
- getAddressesAddressBalance: (address: string, params: RequestParams = {}) =>
2019
+ getAddressesAddressBalance: (
2020
+ address: string,
2021
+ query?: {
2022
+ mempool?: boolean
2023
+ },
2024
+ params: RequestParams = {}
2025
+ ) =>
2020
2026
  this.request<Balance, BadRequest | Unauthorized | NotFound | InternalServerError | ServiceUnavailable>({
2021
2027
  path: `/addresses/${address}/balance`,
2022
2028
  method: 'GET',
2029
+ query: query,
2023
2030
  format: 'json',
2024
2031
  ...params
2025
2032
  }).then(convertHttpResponse),
@@ -16,11 +16,18 @@ You should have received a copy of the GNU Lesser General Public License
16
16
  along with the library. If not, see <http://www.gnu.org/licenses/>.
17
17
  */
18
18
 
19
- import { ApiRequestArguments, ApiRequestHandler, forwardRequests, request, TokenMetaData } from './types'
19
+ import {
20
+ ApiRequestArguments,
21
+ ApiRequestHandler,
22
+ forwardRequests,
23
+ request,
24
+ FungibleTokenMetaData,
25
+ NFTMetaData
26
+ } from './types'
20
27
  import { Api as NodeApi } from './api-alephium'
21
28
  import { DEFAULT_THROTTLE_FETCH } from './utils'
22
29
  import { HexString } from '../contract'
23
- import { addressFromTokenId, groupOfAddress } from '../utils'
30
+ import { addressFromContractId, addressFromTokenId, groupOfAddress, hexToString } from '../utils'
24
31
 
25
32
  function initializeNodeApi(baseUrl: string, apiKey?: string, customFetch?: typeof fetch): NodeApi<string> {
26
33
  const nodeApi = new NodeApi<string>({
@@ -100,8 +107,8 @@ export class NodeProvider implements NodeProviderApis {
100
107
  return new NodeProvider(handler)
101
108
  }
102
109
 
103
- // Only use this when the token is following the standard token interface
104
- fetchStdTokenMetaData = async (tokenId: HexString): Promise<TokenMetaData> => {
110
+ // Only use this when the token follows the fungible token interface, check `guessTokenType` first
111
+ fetchFungibleTokenMetaData = async (tokenId: HexString): Promise<FungibleTokenMetaData> => {
105
112
  const address = addressFromTokenId(tokenId)
106
113
  const group = groupOfAddress(address)
107
114
  const calls = Array.from([0, 1, 2, 3], (index) => ({ methodIndex: index, group: group, address: address }))
@@ -116,6 +123,20 @@ export class NodeProvider implements NodeProviderApis {
116
123
  }
117
124
  }
118
125
 
126
+ // Only use this when the token follows the non-fungile token interface, check `guessTokenType` first
127
+ fetchNFTMetaData = async (tokenId: HexString): Promise<NFTMetaData> => {
128
+ const address = addressFromTokenId(tokenId)
129
+ const group = groupOfAddress(address)
130
+ const calls = Array.from([0, 1], (index) => ({ methodIndex: index, group: group, address: address }))
131
+ const result = await this.contracts.postContractsMulticallContract({
132
+ calls: calls
133
+ })
134
+ return {
135
+ tokenUri: hexToString(result.results[0].returns[0].value as any as string),
136
+ collectionAddress: addressFromContractId(result.results[1].returns[0].value as any as string)
137
+ }
138
+ }
139
+
119
140
  guessStdInterfaceId = async (tokenId: HexString): Promise<HexString | undefined> => {
120
141
  const address = addressFromTokenId(tokenId)
121
142
  const group = groupOfAddress(address)
@@ -128,4 +149,16 @@ export class NodeProvider implements NodeProviderApis {
128
149
  return undefined
129
150
  }
130
151
  }
152
+
153
+ guessStdTokenType = async (tokenId: HexString): Promise<'fungible' | 'non-fungible' | undefined> => {
154
+ const interfaceId = await this.guessStdInterfaceId(tokenId)
155
+ switch (interfaceId) {
156
+ case '0001':
157
+ return 'fungible'
158
+ case '0003':
159
+ return 'non-fungible'
160
+ default:
161
+ return undefined
162
+ }
163
+ }
131
164
  }
package/src/api/types.ts CHANGED
@@ -264,9 +264,14 @@ export async function request(provider: Record<string, any>, args: ApiRequestArg
264
264
  return call(...args.params)
265
265
  }
266
266
 
267
- export interface TokenMetaData {
267
+ export interface FungibleTokenMetaData {
268
268
  name: string
269
269
  symbol: string
270
270
  decimals: number
271
271
  totalSupply: Number256
272
272
  }
273
+
274
+ export interface NFTMetaData {
275
+ collectionAddress: string
276
+ tokenUri: string
277
+ }