Skip to content

Commit

Permalink
Add constants to web3-utils
Browse files Browse the repository at this point in the history
  • Loading branch information
Ben-Rey committed Nov 15, 2023
1 parent de986d4 commit 11e9e62
Show file tree
Hide file tree
Showing 14 changed files with 66 additions and 53 deletions.
4 changes: 2 additions & 2 deletions packages/massa-web3/examples/smartContracts/deployer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export async function awaitTxConfirmation(
.smartContracts()
.awaitRequiredOperationStatus(
deploymentOperationId,
EOperationStatus.FINAL_SUCCESS,
EOperationStatus.SPECULATIVE_SUCCESS,
);
console.log(
`Transaction with Operation ID ${chalk.yellow(
Expand All @@ -57,7 +57,7 @@ export async function awaitTxConfirmation(
throw new Error(ex);
}

if (status !== EOperationStatus.FINAL_SUCCESS) {
if (status !== EOperationStatus.SPECULATIVE_SUCCESS) {
const msg = chalk.red(
`Transaction ${chalk.yellow(
deploymentOperationId,
Expand Down
3 changes: 1 addition & 2 deletions packages/massa-web3/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,7 @@ export {
MASSA_PROTOFILE_KEY,
PROTO_FILE_SEPARATOR,
} from './web3/SmartContractsClient';
export * from '@massalabs/web3-utils';
export { withTimeoutRejection } from './utils/time';
export { fromMAS, toMAS, MassaUnits } from './utils/converters';

export { Web3Account } from './web3/accounts/Web3Account';
export { WalletProviderAccount } from './web3/accounts/WalletProviderAccount';
Expand All @@ -95,3 +93,4 @@ export { WalletProviderAccount } from './web3/accounts/WalletProviderAccount';
*/
export * as utils from './utils';
export * from './utils/keyAndAddresses';
export * from '@massalabs/web3-utils';
4 changes: 1 addition & 3 deletions packages/massa-web3/src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
export * as arguments from '@massalabs/web3-utils';

export * as bytes from './bytes';

export * as converters from './converters';
export * as converters from '@massalabs/web3-utils/src/converters';

export * as keyAndAddresses from './keyAndAddresses';

Expand Down
15 changes: 6 additions & 9 deletions packages/massa-web3/src/utils/keyAndAddresses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,12 @@ import {

import * as ed from '@noble/ed25519';
import { getBytesPublicKey, getBytesSecretKey } from './bytes';

/**
* Prefixes for secret keys, public keys, and addresses.
* Prefixes are used as a convention to differentiate one key from another.
*/
const PUBLIC_KEY_PREFIX = 'P';
const ADDRESS_PREFIX_LENGTH = 2;
const ADDRESS_USER_PREFIX = 'AU';
const ADDRESS_CONTRACT_PREFIX = 'AS';
import {
ADDRESS_CONTRACT_PREFIX,
ADDRESS_PREFIX_LENGTH,
ADDRESS_USER_PREFIX,
PUBLIC_KEY_PREFIX,
} from '@massalabs/web3-utils';

/**
* A secret key.
Expand Down
14 changes: 6 additions & 8 deletions packages/massa-web3/src/web3/SmartContractsClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { IOperationData } from '../interfaces/IOperationData';
import { IReadData } from '../interfaces/IReadData';
import { ISmartContractsClient } from '../interfaces/ISmartContractsClient';
import { JSON_RPC_REQUEST_METHOD } from '../interfaces/JsonRpcMethods';
import { fromMAS } from '../utils/converters';
import { fromMAS } from '@massalabs/web3-utils/src/converters';
import { trySafeExecute } from '../utils/retryExecuteFunction';
import { BaseClient } from './BaseClient';
import { PublicApiClient } from './PublicApiClient';
Expand All @@ -31,11 +31,10 @@ import {
IContractReadOperationData,
IEvent,
Args,
MAX_GAS_CALL,
} from '@massalabs/web3-utils';
import { wait } from '../utils/time';

export const MAX_READ_BLOCK_GAS = BigInt(4_294_967_295);

const WAIT_STATUS_TIMEOUT = 60000;
const TX_POLL_INTERVAL_MS = 1000;

Expand Down Expand Up @@ -122,9 +121,9 @@ export class SmartContractsClient
throw new Error(`No tx sender available`);
}
// check the max. allowed gas
if (callData.maxGas > MAX_READ_BLOCK_GAS) {
if (callData.maxGas > MAX_GAS_CALL) {
throw new Error(
`The gas submitted ${callData.maxGas.toString()} exceeds the max. allowed block gas of ${MAX_READ_BLOCK_GAS.toString()}`,
`The gas submitted ${callData.maxGas.toString()} exceeds the max. allowed block gas of ${MAX_GAS_CALL.toString()}`,
);
}

Expand Down Expand Up @@ -154,9 +153,9 @@ export class SmartContractsClient
readData: IReadData,
): Promise<IContractReadOperationResponse> {
// check the max. allowed gas
if (readData.maxGas > MAX_READ_BLOCK_GAS) {
if (readData.maxGas > MAX_GAS_CALL) {
throw new Error(
`The gas submitted ${readData.maxGas.toString()} exceeds the max. allowed block gas of ${MAX_READ_BLOCK_GAS.toString()}`,
`The gas submitted ${readData.maxGas.toString()} exceeds the max. allowed block gas of ${MAX_GAS_CALL.toString()}`,
);
}

Expand Down Expand Up @@ -406,7 +405,6 @@ export class SmartContractsClient
*
* @returns A promise that resolves to the status of the operation.
*
* @private
*/
private async awaitOperationStatusHelper(
opId: string,
Expand Down
9 changes: 4 additions & 5 deletions packages/massa-web3/src/web3/WalletClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,13 @@ import { IRollsData } from '../interfaces/IRollsData';
import { IBalance } from '../interfaces/IBalance';
import * as ed from '@noble/ed25519';
import { IWalletClient } from '../interfaces/IWalletClient';
import { fromMAS } from '../utils/converters';
import { fromMAS } from '@massalabs/web3-utils/src/converters';

import { Address, SecretKey, PublicKey } from '../utils/keyAndAddresses';
import { IBaseAccount } from '../interfaces/IBaseAccount';
import { Web3Account } from './accounts/Web3Account';
import { KEYS_VERSION_NUMBER, SECRET_KEY_PREFIX } from '@massalabs/web3-utils';

const SECRET_KEY_PREFIX = 'S';
const VERSION_NUMBER = 0;
const MAX_WALLET_ACCOUNTS = 256;

/**
Expand Down Expand Up @@ -285,15 +284,15 @@ export class WalletClient extends BaseClient implements IWalletClient {

/**
* Generates a new wallet account.
* @param version_number - The version number of the secret key to be generated, to create a new account.
* @param KEYS_VERSION_NUMBER - The version number of the secret key to be generated, to create a new account.
*
* @returns A Promise that resolves to an {@link IAccount} object, which represents the newly created account.
*/
public static async walletGenerateNewAccount(): Promise<IAccount> {
// generate private key
const secretKeyArray: Uint8Array = ed.utils.randomPrivateKey();

const version = Buffer.from(varintEncode(VERSION_NUMBER));
const version = Buffer.from(varintEncode(KEYS_VERSION_NUMBER));
const secretKeyBase58Encoded: string =
SECRET_KEY_PREFIX +
base58Encode(Buffer.concat([version, secretKeyArray]));
Expand Down
2 changes: 0 additions & 2 deletions packages/massa-web3/src/web3/accounts/Web3Account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ import { ICallData } from '../../interfaces/ICallData';
import { IContractData } from '../../interfaces/IContractData';
import { trySafeExecute } from '../../utils/retryExecuteFunction';

export const MAX_READ_BLOCK_GAS = BigInt(4_294_967_295);

export class Web3Account extends BaseClient implements IBaseAccount {
private account: IAccount;
private publicApiClient: IPublicApiClient;
Expand Down
22 changes: 11 additions & 11 deletions packages/massa-web3/test/utils/conversion.spec.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,32 @@
import { expect, it, describe } from '@jest/globals';
import BigNumber from 'bignumber.js';
import * as converters from '../../src/utils/converters';
import { fromMAS, toMAS } from '@massalabs/web3-utils';

describe('conversion tests', () => {
it('test conversions to and from (all ranges and formats)', () => {
const nanoMassa1 = converters.fromMAS('1.5234');
const nanoMassa1 = fromMAS('1.5234');
expect(nanoMassa1.toString()).toStrictEqual('1523400000');
const massa1 = converters.toMAS(nanoMassa1);
const massa1 = toMAS(nanoMassa1);
expect(massa1.toString()).toStrictEqual('1.5234');

const nanoMassa2 = converters.fromMAS(1.5234);
const nanoMassa2 = fromMAS(1.5234);
expect(nanoMassa2.toString()).toStrictEqual('1523400000');
const massa2 = converters.toMAS(nanoMassa2);
const massa2 = toMAS(nanoMassa2);
expect(massa2.toString()).toStrictEqual('1.5234');

const nanoMassa3 = converters.fromMAS(new BigNumber('1.5234'));
const nanoMassa3 = fromMAS(new BigNumber('1.5234'));
expect(nanoMassa3.toString()).toStrictEqual('1523400000');
const massa3 = converters.toMAS(nanoMassa3);
const massa3 = toMAS(nanoMassa3);
expect(massa3.toString()).toStrictEqual('1.5234');

const nanoMassa4 = converters.fromMAS(BigInt(2));
const nanoMassa4 = fromMAS(BigInt(2));
expect(nanoMassa4.toString()).toStrictEqual('2000000000');
const massa4 = converters.toMAS(nanoMassa4);
const massa4 = toMAS(nanoMassa4);
expect(massa4.toString()).toStrictEqual('2');

const nanoMassa5 = converters.fromMAS('1.1234567899');
const nanoMassa5 = fromMAS('1.1234567899');
expect(nanoMassa5.toString()).toStrictEqual('1123456790');
const massa5 = converters.toMAS(nanoMassa5);
const massa5 = toMAS(nanoMassa5);
expect(massa5.toString()).toStrictEqual('1.12345679');
});
});
15 changes: 6 additions & 9 deletions packages/massa-web3/test/web3/smartContractsClient.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,9 @@
import { IBalance } from '../../src/interfaces/IBalance';
import { JSON_RPC_REQUEST_METHOD } from '../../src/interfaces/JsonRpcMethods';
import { EOperationStatus } from '../../src/interfaces/EOperationStatus';
import { fromMAS } from '../../src/utils/converters';
import { fromMAS, MAX_GAS_CALL } from '@massalabs/web3-utils';
import { PublicApiClient } from '../../src/web3/PublicApiClient';
import {
MAX_READ_BLOCK_GAS,
SmartContractsClient,
} from '../../src/web3/SmartContractsClient';
import { SmartContractsClient } from '../../src/web3/SmartContractsClient';
import { WalletClient } from '../../src/web3/WalletClient';
import {
mockClientConfig,
Expand Down Expand Up @@ -320,12 +317,12 @@ describe('SmartContractsClient', () => {

test('should throw error when maxGas is superior to the maximum gas allowed', async () => {
const modifiedMockCallData = { ...mockCallData };
modifiedMockCallData.maxGas = MAX_READ_BLOCK_GAS + BigInt(1);
modifiedMockCallData.maxGas = MAX_GAS_CALL + BigInt(1);

await expect(
smartContractsClient.callSmartContract(modifiedMockCallData),
).rejects.toThrow(
`The gas submitted ${modifiedMockCallData.maxGas.toString()} exceeds the max. allowed block gas of ${MAX_READ_BLOCK_GAS.toString()}`,
`The gas submitted ${modifiedMockCallData.maxGas.toString()} exceeds the max. allowed block gas of ${MAX_GAS_CALL.toString()}`,
);
});

Expand Down Expand Up @@ -378,12 +375,12 @@ describe('SmartContractsClient', () => {
const mockReadDataWithLargeMaxGas = {
...mockReadData,
maxGas: BigInt(4_294_967_296),
}; // value > MAX_READ_BLOCK_GAS
}; // value > MAX_GAS_CALL

await expect(
smartContractsClient.readSmartContract(mockReadDataWithLargeMaxGas),
).rejects.toThrow(
`The gas submitted ${mockReadDataWithLargeMaxGas.maxGas.toString()} exceeds the max. allowed block gas of ${MAX_READ_BLOCK_GAS.toString()}`,
`The gas submitted ${mockReadDataWithLargeMaxGas.maxGas.toString()} exceeds the max. allowed block gas of ${MAX_GAS_CALL.toString()}`,
);
});

Expand Down
2 changes: 1 addition & 1 deletion packages/massa-web3/test/web3/walletClient.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { OperationTypeId } from '../../src/interfaces/OperationTypes';
import { JSON_RPC_REQUEST_METHOD } from '../../src/interfaces/JsonRpcMethods';
import { mockAddressesInfo, mockNodeStatusInfo, mockOpIds } from './mockData';
import { IRollsData } from '../../src/interfaces/IRollsData';
import { fromMAS } from '../../src/utils/converters';
import { fromMAS } from '@massalabs/web3-utils';
import { Web3Account } from '../../src/web3/accounts/Web3Account';
import { IBaseAccount } from '../../src/interfaces/IBaseAccount';

Expand Down
2 changes: 1 addition & 1 deletion packages/massa-web3/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"outDir": "./dist"
},
"include": [
"./src/**/*.ts"
"./src/**/*.ts",
],
"exclude": [
"node_modules",
Expand Down
25 changes: 25 additions & 0 deletions packages/web3-utils/src/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { fromMAS } from './converters';

/* -------------------------------------------------------------------------- */
/* KEYS */
/* -------------------------------------------------------------------------- */
export const SECRET_KEY_PREFIX = 'S';
export const PUBLIC_KEY_PREFIX = 'P';
export const ADDRESS_USER_PREFIX = 'AU';
export const ADDRESS_CONTRACT_PREFIX = 'AS';
export const ADDRESS_PREFIX_LENGTH = 2;
export const KEYS_VERSION_NUMBER = 0;

/* -------------------------------------------------------------------------- */
/* STORAGE COST */
/* -------------------------------------------------------------------------- */
export const STORAGE_BYTE_COST = fromMAS(0.0001);
export const NEW_LEDGER_ENTRY_COST = STORAGE_BYTE_COST * 4n;
export const BASE_ACCOUNT_CREATION_COST = fromMAS(0.001);

/* -------------------------------------------------------------------------- */
/* GAS LIMIT */
/* -------------------------------------------------------------------------- */
export const MAX_GAS_EXECUTE_SC = 3_980_167_295n;
export const MAX_GAS_DEPLOYMENT = MAX_GAS_EXECUTE_SC;
export const MAX_GAS_CALL = 4_294_967_295n;
File renamed without changes.
2 changes: 2 additions & 0 deletions packages/web3-utils/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
export * from './arguments';
export * from './serializers';
export * from './interfaces';
export * from './constants';
export * from './converters';

0 comments on commit 11e9e62

Please sign in to comment.