@alephium/web3 0.19.1 → 0.20.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.
@@ -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;
@@ -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 {
@@ -16,3 +16,4 @@ export declare function prettifyNumber(amount: Number256, decimals: number, conf
16
16
  export declare function convertAmountWithDecimals(amount: string | number, decimals: number): bigint | undefined;
17
17
  export declare function convertAlphAmountWithDecimals(amount: string | number): bigint | undefined;
18
18
  export declare function number256ToBigint(number: Number256): bigint;
19
+ export declare function number256ToNumber(number: Number256, decimals: number): number;
@@ -20,7 +20,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
20
20
  return (mod && mod.__esModule) ? mod : { "default": mod };
21
21
  };
22
22
  Object.defineProperty(exports, "__esModule", { value: true });
23
- exports.number256ToBigint = exports.convertAlphAmountWithDecimals = exports.convertAmountWithDecimals = exports.prettifyNumber = exports.prettifyExactAmount = exports.prettifyTokenAmount = exports.prettifyAttoAlphAmount = exports.prettifyNumberConfig = exports.isNumeric = void 0;
23
+ exports.number256ToNumber = exports.number256ToBigint = exports.convertAlphAmountWithDecimals = exports.convertAmountWithDecimals = exports.prettifyNumber = exports.prettifyExactAmount = exports.prettifyTokenAmount = exports.prettifyAttoAlphAmount = exports.prettifyNumberConfig = exports.isNumeric = void 0;
24
24
  // Credits:
25
25
  // 1. https://github.com/argentlabs/argent-x/blob/e63affa7f28b27333dca4081a3dcd375bb2da40b/packages/extension/src/shared/utils/number.ts
26
26
  // 2. https://github.com/ethers-io/ethers.js/blob/724881f34d428406488a1c9f9dbebe54b6edecda/src.ts/utils/fixednumber.ts
@@ -138,7 +138,7 @@ function convertAmountWithDecimals(amount, decimals) {
138
138
  }
139
139
  }
140
140
  exports.convertAmountWithDecimals = convertAmountWithDecimals;
141
- // E.g. `1.23 ALPH` will be converted to `1230000000000000000`
141
+ // E.g. `1.23` ALPH will be converted to `1230000000000000000`
142
142
  function convertAlphAmountWithDecimals(amount) {
143
143
  return convertAmountWithDecimals(amount, 18);
144
144
  }
@@ -147,3 +147,7 @@ function number256ToBigint(number) {
147
147
  return typeof number === 'string' ? BigInt(number) : number;
148
148
  }
149
149
  exports.number256ToBigint = number256ToBigint;
150
+ function number256ToNumber(number, decimals) {
151
+ return parseFloat(toFixedNumber(number256ToBigint(number), decimals));
152
+ }
153
+ exports.number256ToNumber = number256ToNumber;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@alephium/web3",
3
- "version": "0.19.1",
3
+ "version": "0.20.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",
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
@@ -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 {
@@ -153,7 +153,7 @@ export function convertAmountWithDecimals(amount: string | number, decimals: num
153
153
  }
154
154
  }
155
155
 
156
- // E.g. `1.23 ALPH` will be converted to `1230000000000000000`
156
+ // E.g. `1.23` ALPH will be converted to `1230000000000000000`
157
157
  export function convertAlphAmountWithDecimals(amount: string | number): bigint | undefined {
158
158
  return convertAmountWithDecimals(amount, 18)
159
159
  }
@@ -161,3 +161,7 @@ export function convertAlphAmountWithDecimals(amount: string | number): bigint |
161
161
  export function number256ToBigint(number: Number256): bigint {
162
162
  return typeof number === 'string' ? BigInt(number) : number
163
163
  }
164
+
165
+ export function number256ToNumber(number: Number256, decimals: number): number {
166
+ return parseFloat(toFixedNumber(number256ToBigint(number), decimals))
167
+ }