@alephium/web3 0.17.1 → 0.18.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.
@@ -297,7 +297,12 @@ export interface CallContract {
297
297
  existingContracts?: string[];
298
298
  inputAssets?: TestInputAsset[];
299
299
  }
300
- export interface CallContractResult {
300
+ export interface CallContractFailed {
301
+ error: string;
302
+ type: string;
303
+ }
304
+ export type CallContractResult = CallContractFailed | CallContractSucceeded;
305
+ export interface CallContractSucceeded {
301
306
  returns: Val[];
302
307
  /** @format int32 */
303
308
  gasUsed: number;
@@ -305,6 +310,7 @@ export interface CallContractResult {
305
310
  txInputs: string[];
306
311
  txOutputs: Output[];
307
312
  events: ContractEventByTxId[];
313
+ type: string;
308
314
  }
309
315
  export interface ChainInfo {
310
316
  /** @format int32 */
@@ -938,7 +944,7 @@ export declare class HttpClient<SecurityDataType = unknown> {
938
944
  }
939
945
  /**
940
946
  * @title Alephium API
941
- * @version 2.3.5
947
+ * @version 2.5.0
942
948
  * @baseUrl ../
943
949
  */
944
950
  export declare class Api<SecurityDataType extends unknown> extends HttpClient<SecurityDataType> {
@@ -1577,7 +1583,7 @@ export declare class Api<SecurityDataType extends unknown> extends HttpClient<Se
1577
1583
  * @summary Call contract
1578
1584
  * @request POST:/contracts/call-contract
1579
1585
  */
1580
- postContractsCallContract: (data: CallContract, params?: RequestParams) => Promise<CallContractResult>;
1586
+ postContractsCallContract: (data: CallContract, params?: RequestParams) => Promise<CallContractFailed | CallContractSucceeded>;
1581
1587
  /**
1582
1588
  * No description
1583
1589
  *
@@ -151,7 +151,7 @@ class HttpClient {
151
151
  exports.HttpClient = HttpClient;
152
152
  /**
153
153
  * @title Alephium API
154
- * @version 2.3.5
154
+ * @version 2.5.0
155
155
  * @baseUrl ../
156
156
  */
157
157
  class Api extends HttpClient {
@@ -20,6 +20,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
20
20
  exports.NodeProvider = void 0;
21
21
  const types_1 = require("./types");
22
22
  const api_alephium_1 = require("./api-alephium");
23
+ const contract_1 = require("../contract");
23
24
  const utils_1 = require("../utils");
24
25
  function initializeNodeApi(baseUrl, apiKey, customFetch) {
25
26
  const nodeApi = new api_alephium_1.Api({
@@ -44,24 +45,21 @@ class NodeProvider {
44
45
  const result = await this.contracts.postContractsMulticallContract({
45
46
  calls: calls
46
47
  });
48
+ const callResults = result.results.map((r) => (0, contract_1.tryGetCallResult)(r));
47
49
  return {
48
- symbol: result.results[0].returns[0].value,
49
- name: result.results[1].returns[0].value,
50
- decimals: Number(result.results[2].returns[0].value),
51
- totalSupply: BigInt(result.results[3].returns[0].value)
50
+ symbol: callResults[0].returns[0].value,
51
+ name: callResults[1].returns[0].value,
52
+ decimals: Number(callResults[2].returns[0].value),
53
+ totalSupply: BigInt(callResults[3].returns[0].value)
52
54
  };
53
55
  };
54
56
  // Only use this when the token follows the non-fungile token interface, check `guessTokenType` first
55
57
  this.fetchNFTMetaData = async (tokenId) => {
56
58
  const address = (0, utils_1.addressFromTokenId)(tokenId);
57
59
  const group = (0, utils_1.groupOfAddress)(address);
58
- const calls = Array.from([0, 1], (index) => ({ methodIndex: index, group: group, address: address }));
59
- const result = await this.contracts.postContractsMulticallContract({
60
- calls: calls
61
- });
60
+ const result = await this.contracts.postContractsCallContract({ methodIndex: 0, group, address });
62
61
  return {
63
- tokenUri: (0, utils_1.hexToString)(result.results[0].returns[0].value),
64
- collectionAddress: (0, utils_1.addressFromContractId)(result.results[1].returns[0].value)
62
+ tokenUri: (0, utils_1.hexToString)((0, contract_1.tryGetCallResult)(result).returns[0].value)
65
63
  };
66
64
  };
67
65
  // Only use this when the contract follows the NFT collection interface, check `guessFollowsNFTCollectionStd` first
@@ -70,9 +68,10 @@ class NodeProvider {
70
68
  const group = (0, utils_1.groupOfAddress)(address);
71
69
  const calls = Array.from([0, 1], (index) => ({ methodIndex: index, group: group, address: address }));
72
70
  const result = await this.contracts.postContractsMulticallContract({ calls });
71
+ const callResults = result.results.map((r) => (0, contract_1.tryGetCallResult)(r));
73
72
  return {
74
- collectionUri: (0, utils_1.hexToString)(result.results[0].returns[0].value),
75
- totalSupply: BigInt(result.results[1].returns[0].value)
73
+ collectionUri: (0, utils_1.hexToString)(callResults[0].returns[0].value),
74
+ totalSupply: BigInt(callResults[1].returns[0].value)
76
75
  };
77
76
  };
78
77
  this.guessStdInterfaceId = async (tokenId) => {
@@ -42,7 +42,6 @@ export interface FungibleTokenMetaData {
42
42
  totalSupply: Number256;
43
43
  }
44
44
  export interface NFTMetaData {
45
- collectionAddress: string;
46
45
  tokenUri: string;
47
46
  }
48
47
  export interface NFTCollectionMetaData {
@@ -307,4 +307,5 @@ export declare function multicallMethods<I extends ContractInstance, F extends F
307
307
  export declare function getContractEventsCurrentCount(contractAddress: Address): Promise<number>;
308
308
  export declare const getContractIdFromUnsignedTx: (nodeProvider: NodeProvider, unsignedTx: string) => Promise<HexString>;
309
309
  export declare const getTokenIdFromUnsignedTx: (nodeProvider: NodeProvider, unsignedTx: string) => Promise<HexString>;
310
+ export declare function tryGetCallResult(result: node.CallContractResult): node.CallContractSucceeded;
310
311
  export {};
@@ -43,7 +43,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
43
43
  return (mod && mod.__esModule) ? mod : { "default": mod };
44
44
  };
45
45
  Object.defineProperty(exports, "__esModule", { value: true });
46
- exports.getTokenIdFromUnsignedTx = exports.getContractIdFromUnsignedTx = exports.getContractEventsCurrentCount = exports.multicallMethods = exports.callMethod = exports.subscribeContractEvents = exports.subscribeContractEvent = exports.decodeEvent = exports.subscribeContractDestroyedEvent = exports.subscribeContractCreatedEvent = exports.fetchContractState = exports.ContractInstance = exports.testMethod = exports.addStdIdToFields = exports.subscribeEventsFromContract = exports.decodeContractDestroyedEvent = exports.decodeContractCreatedEvent = exports.DestroyContractEventAddress = exports.CreateContractEventAddress = exports.ExecutableScript = exports.ContractFactory = exports.randomTxId = exports.toApiVals = exports.Script = exports.Contract = exports.Artifact = exports.Project = exports.ProjectArtifact = exports.DEFAULT_COMPILER_OPTIONS = exports.DEFAULT_NODE_COMPILER_OPTIONS = exports.StdIdFieldName = void 0;
46
+ exports.tryGetCallResult = exports.getTokenIdFromUnsignedTx = exports.getContractIdFromUnsignedTx = exports.getContractEventsCurrentCount = exports.multicallMethods = exports.callMethod = exports.subscribeContractEvents = exports.subscribeContractEvent = exports.decodeEvent = exports.subscribeContractDestroyedEvent = exports.subscribeContractCreatedEvent = exports.fetchContractState = exports.ContractInstance = exports.testMethod = exports.addStdIdToFields = exports.subscribeEventsFromContract = exports.decodeContractDestroyedEvent = exports.decodeContractCreatedEvent = exports.DestroyContractEventAddress = exports.CreateContractEventAddress = exports.ExecutableScript = exports.ContractFactory = exports.randomTxId = exports.toApiVals = exports.Script = exports.Contract = exports.Artifact = exports.Project = exports.ProjectArtifact = exports.DEFAULT_COMPILER_OPTIONS = exports.DEFAULT_NODE_COMPILER_OPTIONS = exports.StdIdFieldName = void 0;
47
47
  const buffer_1 = require("buffer/");
48
48
  const fs_1 = __importDefault(require("fs"));
49
49
  const fs_2 = require("fs");
@@ -736,17 +736,18 @@ class Contract extends Artifact {
736
736
  }
737
737
  fromApiCallContractResult(result, txId, methodIndex, getContractByCodeHash) {
738
738
  const returnTypes = this.functions[`${methodIndex}`].returnTypes;
739
- const rawReturn = (0, api_1.fromApiArray)(result.returns, returnTypes);
739
+ const callResult = tryGetCallResult(result);
740
+ const rawReturn = (0, api_1.fromApiArray)(callResult.returns, returnTypes);
740
741
  const returns = rawReturn.length === 0 ? null : rawReturn.length === 1 ? rawReturn[0] : rawReturn;
741
742
  const addressToCodeHash = new Map();
742
- result.contracts.forEach((contract) => addressToCodeHash.set(contract.address, contract.codeHash));
743
+ callResult.contracts.forEach((contract) => addressToCodeHash.set(contract.address, contract.codeHash));
743
744
  return {
744
745
  returns: returns,
745
- gasUsed: result.gasUsed,
746
- contracts: result.contracts.map((state) => Contract.fromApiContractState(state, getContractByCodeHash)),
747
- txInputs: result.txInputs,
748
- txOutputs: result.txOutputs.map((output) => fromApiOutput(output)),
749
- events: Contract.fromApiEvents(result.events, addressToCodeHash, txId, getContractByCodeHash)
746
+ gasUsed: callResult.gasUsed,
747
+ contracts: callResult.contracts.map((state) => Contract.fromApiContractState(state, getContractByCodeHash)),
748
+ txInputs: callResult.txInputs,
749
+ txOutputs: callResult.txOutputs.map((output) => fromApiOutput(output)),
750
+ events: Contract.fromApiEvents(callResult.events, addressToCodeHash, txId, getContractByCodeHash)
750
751
  };
751
752
  }
752
753
  }
@@ -1184,3 +1185,10 @@ const getContractIdFromUnsignedTx = async (nodeProvider, unsignedTx) => {
1184
1185
  exports.getContractIdFromUnsignedTx = getContractIdFromUnsignedTx;
1185
1186
  // This function only works in the simple case where a single non-subcontract is created in the tx
1186
1187
  exports.getTokenIdFromUnsignedTx = exports.getContractIdFromUnsignedTx;
1188
+ function tryGetCallResult(result) {
1189
+ if (result.type === 'CallContractFailed') {
1190
+ throw new Error(`Failed to call contract, error: ${result.error}`);
1191
+ }
1192
+ return result;
1193
+ }
1194
+ exports.tryGetCallResult = tryGetCallResult;
@@ -233,6 +233,9 @@ function stringToHex(str) {
233
233
  }
234
234
  exports.stringToHex = stringToHex;
235
235
  function hexToString(str) {
236
+ if (!isHexString(str)) {
237
+ throw new Error(`Invalid hex string: ${str}`);
238
+ }
236
239
  return buffer_1.Buffer.from(str, 'hex').toString();
237
240
  }
238
241
  exports.hexToString = hexToString;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@alephium/web3",
3
- "version": "0.17.1",
3
+ "version": "0.18.1",
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.5",
30
+ "alephium_version": "2.5.0",
31
31
  "explorer_backend_version": "1.15.1"
32
32
  },
33
33
  "type": "commonjs",
@@ -338,7 +338,14 @@ export interface CallContract {
338
338
  inputAssets?: TestInputAsset[]
339
339
  }
340
340
 
341
- export interface CallContractResult {
341
+ export interface CallContractFailed {
342
+ error: string
343
+ type: string
344
+ }
345
+
346
+ export type CallContractResult = CallContractFailed | CallContractSucceeded
347
+
348
+ export interface CallContractSucceeded {
342
349
  returns: Val[]
343
350
  /** @format int32 */
344
351
  gasUsed: number
@@ -346,6 +353,7 @@ export interface CallContractResult {
346
353
  txInputs: string[]
347
354
  txOutputs: Output[]
348
355
  events: ContractEventByTxId[]
356
+ type: string
349
357
  }
350
358
 
351
359
  export interface ChainInfo {
@@ -1228,7 +1236,7 @@ export class HttpClient<SecurityDataType = unknown> {
1228
1236
 
1229
1237
  /**
1230
1238
  * @title Alephium API
1231
- * @version 2.3.5
1239
+ * @version 2.5.0
1232
1240
  * @baseUrl ../
1233
1241
  */
1234
1242
  export class Api<SecurityDataType extends unknown> extends HttpClient<SecurityDataType> {
@@ -27,7 +27,7 @@ import {
27
27
  StdInterfaceIds
28
28
  } from './types'
29
29
  import { Api as NodeApi } from './api-alephium'
30
- import { HexString } from '../contract'
30
+ import { HexString, tryGetCallResult } from '../contract'
31
31
  import { addressFromContractId, addressFromTokenId, groupOfAddress, hexToString } from '../utils'
32
32
 
33
33
  function initializeNodeApi(baseUrl: string, apiKey?: string, customFetch?: typeof fetch): NodeApi<string> {
@@ -116,11 +116,12 @@ export class NodeProvider implements NodeProviderApis {
116
116
  const result = await this.contracts.postContractsMulticallContract({
117
117
  calls: calls
118
118
  })
119
+ const callResults = result.results.map((r) => tryGetCallResult(r))
119
120
  return {
120
- symbol: result.results[0].returns[0].value as any as string,
121
- name: result.results[1].returns[0].value as any as string,
122
- decimals: Number(result.results[2].returns[0].value as any as string),
123
- totalSupply: BigInt(result.results[3].returns[0].value as any as string)
121
+ symbol: callResults[0].returns[0].value as any as string,
122
+ name: callResults[1].returns[0].value as any as string,
123
+ decimals: Number(callResults[2].returns[0].value as any as string),
124
+ totalSupply: BigInt(callResults[3].returns[0].value as any as string)
124
125
  }
125
126
  }
126
127
 
@@ -128,13 +129,9 @@ export class NodeProvider implements NodeProviderApis {
128
129
  fetchNFTMetaData = async (tokenId: HexString): Promise<NFTMetaData> => {
129
130
  const address = addressFromTokenId(tokenId)
130
131
  const group = groupOfAddress(address)
131
- const calls = Array.from([0, 1], (index) => ({ methodIndex: index, group: group, address: address }))
132
- const result = await this.contracts.postContractsMulticallContract({
133
- calls: calls
134
- })
132
+ const result = await this.contracts.postContractsCallContract({ methodIndex: 0, group, address })
135
133
  return {
136
- tokenUri: hexToString(result.results[0].returns[0].value as any as string),
137
- collectionAddress: addressFromContractId(result.results[1].returns[0].value as any as string)
134
+ tokenUri: hexToString(tryGetCallResult(result).returns[0].value as any as string)
138
135
  }
139
136
  }
140
137
 
@@ -144,9 +141,10 @@ export class NodeProvider implements NodeProviderApis {
144
141
  const group = groupOfAddress(address)
145
142
  const calls = Array.from([0, 1], (index) => ({ methodIndex: index, group: group, address: address }))
146
143
  const result = await this.contracts.postContractsMulticallContract({ calls })
144
+ const callResults = result.results.map((r) => tryGetCallResult(r))
147
145
  return {
148
- collectionUri: hexToString(result.results[0].returns[0].value as any as string),
149
- totalSupply: BigInt(result.results[1].returns[0].value as any as string)
146
+ collectionUri: hexToString(callResults[0].returns[0].value as any as string),
147
+ totalSupply: BigInt(callResults[1].returns[0].value as any as string)
150
148
  }
151
149
  }
152
150
 
package/src/api/types.ts CHANGED
@@ -284,7 +284,6 @@ export interface FungibleTokenMetaData {
284
284
  }
285
285
 
286
286
  export interface NFTMetaData {
287
- collectionAddress: string
288
287
  tokenUri: string
289
288
  }
290
289
 
@@ -1088,18 +1088,19 @@ export class Contract extends Artifact {
1088
1088
  getContractByCodeHash?: (codeHash: string) => Contract
1089
1089
  ): CallContractResult<unknown> {
1090
1090
  const returnTypes = this.functions[`${methodIndex}`].returnTypes
1091
- const rawReturn = fromApiArray(result.returns, returnTypes)
1091
+ const callResult = tryGetCallResult(result)
1092
+ const rawReturn = fromApiArray(callResult.returns, returnTypes)
1092
1093
  const returns = rawReturn.length === 0 ? null : rawReturn.length === 1 ? rawReturn[0] : rawReturn
1093
1094
 
1094
1095
  const addressToCodeHash = new Map<string, string>()
1095
- result.contracts.forEach((contract) => addressToCodeHash.set(contract.address, contract.codeHash))
1096
+ callResult.contracts.forEach((contract) => addressToCodeHash.set(contract.address, contract.codeHash))
1096
1097
  return {
1097
1098
  returns: returns,
1098
- gasUsed: result.gasUsed,
1099
- contracts: result.contracts.map((state) => Contract.fromApiContractState(state, getContractByCodeHash)),
1100
- txInputs: result.txInputs,
1101
- txOutputs: result.txOutputs.map((output) => fromApiOutput(output)),
1102
- events: Contract.fromApiEvents(result.events, addressToCodeHash, txId, getContractByCodeHash)
1099
+ gasUsed: callResult.gasUsed,
1100
+ contracts: callResult.contracts.map((state) => Contract.fromApiContractState(state, getContractByCodeHash)),
1101
+ txInputs: callResult.txInputs,
1102
+ txOutputs: callResult.txOutputs.map((output) => fromApiOutput(output)),
1103
+ events: Contract.fromApiEvents(callResult.events, addressToCodeHash, txId, getContractByCodeHash)
1103
1104
  }
1104
1105
  }
1105
1106
  }
@@ -1811,3 +1812,10 @@ export const getContractIdFromUnsignedTx = async (
1811
1812
 
1812
1813
  // This function only works in the simple case where a single non-subcontract is created in the tx
1813
1814
  export const getTokenIdFromUnsignedTx = getContractIdFromUnsignedTx
1815
+
1816
+ export function tryGetCallResult(result: node.CallContractResult): node.CallContractSucceeded {
1817
+ if (result.type === 'CallContractFailed') {
1818
+ throw new Error(`Failed to call contract, error: ${(result as node.CallContractFailed).error}`)
1819
+ }
1820
+ return result as node.CallContractSucceeded
1821
+ }
@@ -242,6 +242,9 @@ export function stringToHex(str: string): string {
242
242
  }
243
243
 
244
244
  export function hexToString(str: string): string {
245
+ if (!isHexString(str)) {
246
+ throw new Error(`Invalid hex string: ${str}`)
247
+ }
245
248
  return Buffer.from(str, 'hex').toString()
246
249
  }
247
250
 
@@ -22,6 +22,4 @@ Interface INFT {
22
22
  // }
23
23
  // }
24
24
  pub fn getTokenUri() -> ByteVec
25
-
26
- pub fn getCollectionId() -> ByteVec
27
25
  }