@alephium/web3 0.19.2 → 0.21.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/types.d.ts +1 -0
- package/dist/src/api/types.js +17 -1
- package/dist/src/contract/contract.d.ts +1 -0
- package/dist/src/contract/contract.js +13 -0
- package/package.json +1 -1
- package/src/api/types.ts +13 -0
- package/src/contract/contract.ts +17 -1
package/dist/src/api/types.d.ts
CHANGED
|
@@ -21,6 +21,7 @@ export declare function toApiVal(v: Val, tpe: string): node.Val;
|
|
|
21
21
|
export declare function fromApiVals(vals: node.Val[], names: string[], types: string[], systemEvent?: boolean): NamedVals;
|
|
22
22
|
export declare function fromApiArray(vals: node.Val[], types: string[]): Val[];
|
|
23
23
|
export declare function fromApiVal(v: node.Val, tpe: string): Val;
|
|
24
|
+
export declare function getDefaultValue(tpe: string): Val;
|
|
24
25
|
export declare function typeLength(tpe: string): number;
|
|
25
26
|
export interface ApiRequestArguments {
|
|
26
27
|
path: string;
|
package/dist/src/api/types.js
CHANGED
|
@@ -17,7 +17,8 @@ You should have received a copy of the GNU Lesser General Public License
|
|
|
17
17
|
along with the library. If not, see <http://www.gnu.org/licenses/>.
|
|
18
18
|
*/
|
|
19
19
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
20
|
-
exports.StdInterfaceIds = exports.request = exports.forwardRequests = exports.typeLength = exports.fromApiVal = exports.fromApiArray = exports.fromApiVals = exports.toApiVal = exports.toApiArray = exports.toApiAddress = exports.toApiByteVec = exports.fromApiNumber256 = exports.toApiNumber256Optional = exports.toApiNumber256 = exports.toApiBoolean = exports.fromApiTokens = exports.fromApiToken = exports.toApiTokens = exports.toApiToken = void 0;
|
|
20
|
+
exports.StdInterfaceIds = exports.request = exports.forwardRequests = exports.typeLength = exports.getDefaultValue = exports.fromApiVal = exports.fromApiArray = exports.fromApiVals = exports.toApiVal = exports.toApiArray = exports.toApiAddress = exports.toApiByteVec = exports.fromApiNumber256 = exports.toApiNumber256Optional = exports.toApiNumber256 = exports.toApiBoolean = exports.fromApiTokens = exports.fromApiToken = exports.toApiTokens = exports.toApiToken = void 0;
|
|
21
|
+
const constants_1 = require("../constants");
|
|
21
22
|
const utils_1 = require("../utils");
|
|
22
23
|
utils_1.assertType;
|
|
23
24
|
function toApiToken(token) {
|
|
@@ -224,6 +225,21 @@ function decodeArrayType(tpe) {
|
|
|
224
225
|
return [subType, [dim]];
|
|
225
226
|
}
|
|
226
227
|
}
|
|
228
|
+
function getDefaultValue(tpe) {
|
|
229
|
+
if (tpe === 'U256' || tpe === 'I256')
|
|
230
|
+
return 0n;
|
|
231
|
+
if (tpe === 'Bool')
|
|
232
|
+
return false;
|
|
233
|
+
if (tpe === 'ByteVec')
|
|
234
|
+
return '';
|
|
235
|
+
if (tpe === 'Address')
|
|
236
|
+
return constants_1.ZERO_ADDRESS;
|
|
237
|
+
// array type
|
|
238
|
+
const [baseType, dims] = decodeArrayType(tpe);
|
|
239
|
+
const defaultBaseValue = getDefaultValue(baseType);
|
|
240
|
+
return dims.reduceRight((acc, length) => Array(length).fill(acc), defaultBaseValue);
|
|
241
|
+
}
|
|
242
|
+
exports.getDefaultValue = getDefaultValue;
|
|
227
243
|
function foldVals(vals, dims) {
|
|
228
244
|
if (dims.length == 1) {
|
|
229
245
|
return vals;
|
|
@@ -120,6 +120,7 @@ export declare class Contract extends Artifact {
|
|
|
120
120
|
static fromCompileResult(result: node.CompileContractResult): Contract;
|
|
121
121
|
static fromArtifactFile(path: string, bytecodeDebugPatch: string, codeHashDebug: string): Promise<Contract>;
|
|
122
122
|
toString(): string;
|
|
123
|
+
getInitialFieldsWithDefaultValues(): Fields;
|
|
123
124
|
toState<T extends Fields>(fields: T, asset: Asset, address?: string): ContractState<T>;
|
|
124
125
|
static randomAddress(): string;
|
|
125
126
|
printDebugMessages(funcName: string, messages: DebugMessage[]): void;
|
|
@@ -552,6 +552,19 @@ class Contract extends Artifact {
|
|
|
552
552
|
}
|
|
553
553
|
return JSON.stringify(object, null, 2);
|
|
554
554
|
}
|
|
555
|
+
getInitialFieldsWithDefaultValues() {
|
|
556
|
+
const fields = this.stdInterfaceId === undefined
|
|
557
|
+
? this.fieldsSig
|
|
558
|
+
: {
|
|
559
|
+
names: this.fieldsSig.names.slice(-1),
|
|
560
|
+
types: this.fieldsSig.types.slice(-1),
|
|
561
|
+
isMutable: this.fieldsSig.isMutable.slice(-1)
|
|
562
|
+
};
|
|
563
|
+
return fields.names.reduce((acc, key, index) => {
|
|
564
|
+
acc[`${key}`] = (0, api_1.getDefaultValue)(fields.types[`${index}`]);
|
|
565
|
+
return acc;
|
|
566
|
+
}, {});
|
|
567
|
+
}
|
|
555
568
|
toState(fields, asset, address) {
|
|
556
569
|
const addressDef = typeof address !== 'undefined' ? address : Contract.randomAddress();
|
|
557
570
|
return {
|
package/package.json
CHANGED
package/src/api/types.ts
CHANGED
|
@@ -16,6 +16,7 @@ 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 { ZERO_ADDRESS } from '../constants'
|
|
19
20
|
import { assertType, bs58, Eq, isBase58, isHexString } from '../utils'
|
|
20
21
|
import * as node from './api-alephium'
|
|
21
22
|
|
|
@@ -224,6 +225,18 @@ function decodeArrayType(tpe: string): [baseType: string, dims: number[]] {
|
|
|
224
225
|
}
|
|
225
226
|
}
|
|
226
227
|
|
|
228
|
+
export function getDefaultValue(tpe: string): Val {
|
|
229
|
+
if (tpe === 'U256' || tpe === 'I256') return 0n
|
|
230
|
+
if (tpe === 'Bool') return false
|
|
231
|
+
if (tpe === 'ByteVec') return ''
|
|
232
|
+
if (tpe === 'Address') return ZERO_ADDRESS
|
|
233
|
+
|
|
234
|
+
// array type
|
|
235
|
+
const [baseType, dims] = decodeArrayType(tpe)
|
|
236
|
+
const defaultBaseValue = getDefaultValue(baseType)
|
|
237
|
+
return dims.reduceRight((acc, length) => Array(length).fill(acc), defaultBaseValue)
|
|
238
|
+
}
|
|
239
|
+
|
|
227
240
|
function foldVals(vals: Val[], dims: number[]): Val {
|
|
228
241
|
if (dims.length == 1) {
|
|
229
242
|
return vals
|
package/src/contract/contract.ts
CHANGED
|
@@ -33,7 +33,8 @@ import {
|
|
|
33
33
|
Val,
|
|
34
34
|
fromApiTokens,
|
|
35
35
|
fromApiVals,
|
|
36
|
-
typeLength
|
|
36
|
+
typeLength,
|
|
37
|
+
getDefaultValue
|
|
37
38
|
} from '../api'
|
|
38
39
|
import {
|
|
39
40
|
SignDeployContractTxParams,
|
|
@@ -844,6 +845,21 @@ export class Contract extends Artifact {
|
|
|
844
845
|
return JSON.stringify(object, null, 2)
|
|
845
846
|
}
|
|
846
847
|
|
|
848
|
+
getInitialFieldsWithDefaultValues(): Fields {
|
|
849
|
+
const fields =
|
|
850
|
+
this.stdInterfaceId === undefined
|
|
851
|
+
? this.fieldsSig
|
|
852
|
+
: {
|
|
853
|
+
names: this.fieldsSig.names.slice(-1),
|
|
854
|
+
types: this.fieldsSig.types.slice(-1),
|
|
855
|
+
isMutable: this.fieldsSig.isMutable.slice(-1)
|
|
856
|
+
}
|
|
857
|
+
return fields.names.reduce((acc, key, index) => {
|
|
858
|
+
acc[`${key}`] = getDefaultValue(fields.types[`${index}`])
|
|
859
|
+
return acc
|
|
860
|
+
}, {})
|
|
861
|
+
}
|
|
862
|
+
|
|
847
863
|
toState<T extends Fields>(fields: T, asset: Asset, address?: string): ContractState<T> {
|
|
848
864
|
const addressDef = typeof address !== 'undefined' ? address : Contract.randomAddress()
|
|
849
865
|
return {
|