@alephium/web3 0.17.0 → 0.18.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/alephium-web3.min.js +1 -1
- package/dist/alephium-web3.min.js.map +1 -1
- package/dist/src/api/api-alephium.d.ts +9 -3
- package/dist/src/api/api-alephium.js +1 -1
- package/dist/src/api/node-provider.js +11 -12
- package/dist/src/api/types.d.ts +0 -1
- package/dist/src/contract/contract.d.ts +1 -0
- package/dist/src/contract/contract.js +16 -8
- package/package.json +2 -2
- package/src/api/api-alephium.ts +10 -2
- package/src/api/node-provider.ts +11 -13
- package/src/api/types.ts +0 -1
- package/src/contract/contract.ts +15 -7
- package/std/nft_interface.ral +0 -2
|
@@ -297,7 +297,12 @@ export interface CallContract {
|
|
|
297
297
|
existingContracts?: string[];
|
|
298
298
|
inputAssets?: TestInputAsset[];
|
|
299
299
|
}
|
|
300
|
-
export interface
|
|
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.
|
|
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<
|
|
1586
|
+
postContractsCallContract: (data: CallContract, params?: RequestParams) => Promise<CallContractFailed | CallContractSucceeded>;
|
|
1581
1587
|
/**
|
|
1582
1588
|
* No description
|
|
1583
1589
|
*
|
|
@@ -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:
|
|
49
|
-
name:
|
|
50
|
-
decimals: Number(
|
|
51
|
-
totalSupply: BigInt(
|
|
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
|
|
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.
|
|
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)(
|
|
75
|
-
totalSupply: BigInt(
|
|
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) => {
|
package/dist/src/api/types.d.ts
CHANGED
|
@@ -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
|
|
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
|
-
|
|
743
|
+
callResult.contracts.forEach((contract) => addressToCodeHash.set(contract.address, contract.codeHash));
|
|
743
744
|
return {
|
|
744
745
|
returns: returns,
|
|
745
|
-
gasUsed:
|
|
746
|
-
contracts:
|
|
747
|
-
txInputs:
|
|
748
|
-
txOutputs:
|
|
749
|
-
events: Contract.fromApiEvents(
|
|
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;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@alephium/web3",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.18.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.
|
|
30
|
+
"alephium_version": "2.5.0",
|
|
31
31
|
"explorer_backend_version": "1.15.1"
|
|
32
32
|
},
|
|
33
33
|
"type": "commonjs",
|
package/src/api/api-alephium.ts
CHANGED
|
@@ -338,7 +338,14 @@ export interface CallContract {
|
|
|
338
338
|
inputAssets?: TestInputAsset[]
|
|
339
339
|
}
|
|
340
340
|
|
|
341
|
-
export interface
|
|
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.
|
|
1239
|
+
* @version 2.5.0
|
|
1232
1240
|
* @baseUrl ../
|
|
1233
1241
|
*/
|
|
1234
1242
|
export class Api<SecurityDataType extends unknown> extends HttpClient<SecurityDataType> {
|
package/src/api/node-provider.ts
CHANGED
|
@@ -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:
|
|
121
|
-
name:
|
|
122
|
-
decimals: Number(
|
|
123
|
-
totalSupply: BigInt(
|
|
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
|
|
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.
|
|
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(
|
|
149
|
-
totalSupply: BigInt(
|
|
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
package/src/contract/contract.ts
CHANGED
|
@@ -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
|
|
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
|
-
|
|
1096
|
+
callResult.contracts.forEach((contract) => addressToCodeHash.set(contract.address, contract.codeHash))
|
|
1096
1097
|
return {
|
|
1097
1098
|
returns: returns,
|
|
1098
|
-
gasUsed:
|
|
1099
|
-
contracts:
|
|
1100
|
-
txInputs:
|
|
1101
|
-
txOutputs:
|
|
1102
|
-
events: Contract.fromApiEvents(
|
|
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
|
+
}
|