Skip to content

Commit

Permalink
Cherry pick (#435)
Browse files Browse the repository at this point in the history
* export withTimeoutRejection (#431)

* Update index.ts

* export Web3Account and WalletProviderAccount

* test fix example (#429)

* test fix example

* fix example smart contract

* fix wallet example

* remove slice

* try fix wallet example

* fix test-example

* clean console.log

---------

Co-authored-by: julienbrs <[email protected]>

* attempt to fix sourcemap error (#432)

* move some interfaces to web3-utils (#434)

* move some interfaces to web3-utils

* update web3-utils

---------

Co-authored-by: Nathan HERVIER <[email protected]>
Co-authored-by: julienbrs <[email protected]>
  • Loading branch information
3 people authored Aug 3, 2023
1 parent ca1364c commit 647741b
Show file tree
Hide file tree
Showing 32 changed files with 125 additions and 186 deletions.
4 changes: 2 additions & 2 deletions examples/.env.example
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
DEPLOYER_PRIVATE_KEY="S12srNEAvZrTb9pktYeuePpuM4taW5dfmWAZYtdqyETWTBspkoT1"
RECEIVER_PRIVATE_KEY="S1ykLaxXyMnJoaWLYds8UntqKTamZ4vcxrZ1fdToR8WpWEpk3FC"

JSON_RPC_URL_PUBLIC=https://test.massa.net/api/v2
JSON_RPC_URL_PRIVATE=https://test.massa.net/api/v2
JSON_RPC_URL_PUBLIC=https://buildnet.massa.net/api/v2
JSON_RPC_URL_PRIVATE=https://buildnet.massa.net/api/v2

#JSON_RPC_URL_PUBLIC=http://127.0.0.1:33035
#JSON_RPC_URL_PRIVATE=http://127.0.0.1:33034
21 changes: 13 additions & 8 deletions examples/smartContracts/deployer.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/* eslint-disable @typescript-eslint/no-var-requires */
import { IAccount } from '../../src/interfaces/IAccount';
import { IContractData } from '../../src/interfaces/IContractData';
import { Client } from '../../src/web3/Client';
import { EOperationStatus } from '../../src/interfaces/EOperationStatus';
import { Args } from '../../src/utils/arguments';

import { readFileSync } from 'fs';
import { u64ToBytes, u8toByte } from '../../src/utils/serializers';
import { fromMAS } from '../../src';

import { Args, fromMAS, u64ToBytes, u8toByte } from '../../src';
import { IBaseAccount } from '../../src/interfaces/IBaseAccount';
const path = require('path');
const chalk = require('chalk');

Expand All @@ -18,12 +18,12 @@ interface ISCData {

async function checkBalance(
web3Client: Client,
account: IAccount,
account: IBaseAccount,
requiredBalance: bigint,
) {
const balance = await web3Client
.wallet()
.getAccountBalance(account.address as string);
.getAccountBalance(account.address() as string);
if (!balance?.final || balance.final < requiredBalance) {
throw new Error('Insufficient MAS balance.');
}
Expand Down Expand Up @@ -74,7 +74,7 @@ export const deploySmartContracts = async (
fee = 0n,
maxGas = 1_000_000n,
maxCoins = fromMAS(0.1),
deployerAccount?: IAccount,
deployerAccount: IBaseAccount,
): Promise<string> => {
let deploymentOperationId: string;
try {
Expand Down Expand Up @@ -141,6 +141,11 @@ export const deploySmartContracts = async (
0n,
);
console.log('Sending coins ... ', coins.toString());
const baseAccount = web3Client.wallet().getBaseAccount();

if (!baseAccount) {
throw new Error('Failed to get base account');
}

deploymentOperationId = await web3Client
.smartContracts()
Expand All @@ -154,7 +159,7 @@ export const deploySmartContracts = async (
maxGas,
maxCoins,
} as IContractData,
deployerAccount,
baseAccount,
);
console.log(
`Smart Contract ${chalk.green(
Expand Down
35 changes: 25 additions & 10 deletions examples/smartContracts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@
import { IAccount } from '../../src/interfaces/IAccount';
import { IEventFilter } from '../../src/interfaces/IEventFilter';
import { ClientFactory } from '../../src/web3/ClientFactory';
import { IEvent } from '../../src/interfaces/IEvent';
import { IReadData } from '../../src/interfaces/IReadData';
import { WalletClient } from '../../src/web3/WalletClient';
import { awaitTxConfirmation, deploySmartContracts } from './deployer';
import { Args } from '../../src/utils/arguments';
import { readFileSync } from 'fs';
import { Client } from '../../src/web3/Client';
import {
Expand All @@ -16,16 +14,20 @@ import {
} from '../../src/web3/EventPoller';
import { INodeStatus } from '../../src/interfaces/INodeStatus';
import { withTimeoutRejection } from '../../src/utils/time';
import { strToBytes } from '../../src/utils/serializers';
import { IDatastoreEntryInput } from '../../src/interfaces/IDatastoreEntryInput';
import { ICallData } from '../../src/interfaces/ICallData';
import * as dotenv from 'dotenv';
import { IProvider, ProviderType } from '../../src/interfaces/IProvider';
import { fromMAS, toMAS } from '../../src';
import {
Args,
IDeserializedResult,
IEvent,
ISerializable,
} from '../../src/interfaces/ISerializable';
fromMAS,
strToBytes,
toMAS,
} from '../../src';

const path = require('path');
const chalk = require('chalk');
const ora = require('ora');
Expand Down Expand Up @@ -185,13 +187,18 @@ const pollAsyncEvents = async (
deployerAccount,
);

const accountAddress = deployerAccount.address;

if (!accountAddress) {
throw new Error('Missing account address');
}

const deployerAccountBalance = await web3Client
.wallet()
.getAccountBalance(deployerAccount.address as string);
.getAccountBalance(accountAddress);

console.log(
`Deployer Wallet Address: ${
deployerAccount.address
} with balance (candidate, final) = (${toMAS(
`Deployer Wallet Address: ${accountAddress} with balance (candidate, final) = (${toMAS(
deployerAccountBalance?.candidate.toString() as string,
)}, ${toMAS(deployerAccountBalance?.final.toString() as string)})`,
);
Expand All @@ -200,6 +207,13 @@ const pollAsyncEvents = async (
spinner = ora(
`Running ${chalk.green('deployment')} of deployer smart contract....`,
).start();

const baseAccount = web3Client.wallet().getBaseAccount();

if (!baseAccount) {
throw new Error('Failed to get base account');
}

const deploymentOperationId = await deploySmartContracts(
[
{
Expand All @@ -214,8 +228,9 @@ const pollAsyncEvents = async (
0n,
1_000_000n,
fromMAS(0.2),
deployerAccount,
baseAccount,
);

spinner.succeed(
`Deployed Smart Contract ${chalk.green(
'successfully',
Expand Down
36 changes: 18 additions & 18 deletions examples/wallet/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import * as dotenv from 'dotenv';
import { Client } from '../../src/web3/Client';
import { IProvider, ProviderType } from '../../src/interfaces/IProvider';
import { fromMAS } from '../../src';
import { ISignature } from '../../src/interfaces/ISignature';
const path = require('path');
const chalk = require('chalk');

Expand Down Expand Up @@ -61,6 +60,8 @@ if (!receiverPrivateKey) {
deployerAccount,
);

await web3Client.wallet().addAccountsToWallet([deployerAccount]);

// get wallet balance
const deployerAccountBalance = await web3Client
.wallet()
Expand Down Expand Up @@ -91,44 +92,43 @@ if (!receiverPrivateKey) {
const senderAccount = walletAccounts[0];
const receiverAccount = walletAccounts[1];

if (!receiverAccount?.address || !senderAccount?.address) {
throw new Error('Missing receiver account address');
}

// get receiver's wallet balance
const receiverAccountBalanceBefore = await web3Client
.wallet()
.getAccountBalance(receiverAccount.address as string);
.getAccountBalance(receiverAccount.address);

console.log(
`Receiver Wallet Balance (Before): ${
receiverAccount.address
} with balance (candidate, final) = (${receiverAccountBalanceBefore?.candidate.toString()}, ${receiverAccountBalanceBefore?.final.toString()})`,
);

const message = 'hello world';

// sign a random wallet message using account2
const signedMessage = await web3Client
.wallet()
.signMessage('hello there', receiverAccount.address as string);
.signMessage(message, receiverAccount.address);
console.log('Wallet sender signing a message... ', signedMessage);

// verify a signature
const signature: ISignature = {
base58Encoded:
'B1Gy7pAstdqzjghn8fdLDtn1qLUhsxWu4x1j8N4W9wxa3hTPNsFyPeFkSkfEjVCRnCAE9jrBjernGyoDL1yt2Wgafb8uu',
};

const message = 'hello world';
if (!deployerAccount?.publicKey || !signedMessage) {
throw new Error('Missing publicKey or signed message');
}

const isVerified = await web3Client
.wallet()
.verifySignature(
message,
signature,
'P1c6udwDMs6CY2YDUm7phdrv6S5ACjTV5jW4Kriio44yDpRWK8t',
);
.verifySignature(message, signedMessage, deployerAccount.publicKey);
console.log('Signature verification: ', isVerified);

// send from base account to receiver
const txId = await web3Client.wallet().sendTransaction({
amount: fromMAS(1),
fee: 0n,
recipientAddress: receiverAccount.address as string,
recipientAddress: receiverAccount.address,
} as ITransactionData);
console.log('Money Transfer:: TxId ', txId[0]);

Expand All @@ -154,7 +154,7 @@ if (!receiverPrivateKey) {
// get receiver's wallet after
const receiverAccountBalanceAfter = await web3Client
.wallet()
.getAccountBalance(receiverAccount.address as string);
.getAccountBalance(receiverAccount.address);
console.log(
`Receiver Wallet Balance (After): ${
receiverAccount.address
Expand All @@ -164,7 +164,7 @@ if (!receiverPrivateKey) {
// get sender's wallet after
const senderAccountBalanceAfter = await web3Client
.wallet()
.getAccountBalance(senderAccount.address as string);
.getAccountBalance(senderAccount.address);
console.log(
`Sender Wallet Balance (After): ${
receiverAccount.address
Expand Down
10 changes: 5 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@
"devDependencies": {
"@massalabs/eslint-config": "^0.0.9",
"@massalabs/prettier-config-as": "^0.0.2",
"@massalabs/web3-utils": "^1.1.0",
"@massalabs/web3-utils": "^1.2.0",
"@types/bn.js": "^5.1.1",
"@types/jest": "^29.5.2",
"@types/node": "^18.13.0",
Expand All @@ -104,6 +104,7 @@
"prettier": "^2.8.4",
"prettier-eslint": "^15.0.1",
"prettier-standard": "^16.4.1",
"rimraf": "^5.0.1",
"ts-jest": "^29.1.0",
"ts-loader": "^9.4.2",
"ts-node": "^10.9.1",
Expand Down
9 changes: 4 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,8 @@ if (typeof window !== 'undefined') {

/** Exposed interfaces */
export { IAccount } from './interfaces/IAccount';
export { IEvent } from './interfaces/IEvent';
export { IEventFilter } from './interfaces/IEventFilter';
export { IEventRegexFilter } from './interfaces/IEventRegexFilter';
export { ISlot } from './interfaces/ISlot';
export { IAddressInfo } from './interfaces/IAddressInfo';
export { IBlockInfo } from './interfaces/IBlockInfo';
export { IClientConfig } from './interfaces/IClientConfig';
Expand All @@ -40,9 +38,6 @@ export { IStakingAddresses } from './interfaces/IStakingAddresses';
export { ITransactionData } from './interfaces/ITransactionData';
export { IBalance } from './interfaces/IBalance';
export { IDatastoreEntry } from './interfaces/IDatastoreEntry';
export { IContractReadOperationData } from './interfaces/IContractReadOperationData';
export { IContractReadOperationResponse } from './interfaces/IContractReadOperationResponse';
export { IReadOperationResult } from './interfaces/IReadOperationResult';
export { IExecuteReadOnlyData } from './interfaces/IExecuteReadOnlyData';
export { IExecuteReadOnlyResponse } from './interfaces/IExecuteReadOnlyResponse';
export { JsonRpcResponseData } from './interfaces/JsonRpcResponseData';
Expand Down Expand Up @@ -88,8 +83,12 @@ export {
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';

/**
* This namespace provides utility functions, such as unit conversion, serialization, encoding, etc.
*/
Expand Down
2 changes: 1 addition & 1 deletion src/interfaces/IAddressInfo.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ISlot } from './ISlot';
import { ISlot } from '@massalabs/web3-utils';

/**
* Represents the production statistics for a specific cycle.
Expand Down
2 changes: 1 addition & 1 deletion src/interfaces/IBlockInfo.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ISlot } from './ISlot';
import { ISlot } from '@massalabs/web3-utils';
import { OpType } from './OperationTypes';

/**
Expand Down
2 changes: 1 addition & 1 deletion src/interfaces/IBlockcliqueBlockBySlot.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ISlot } from './ISlot';
import { ISlot } from '@massalabs/web3-utils';

/**
* Represents the information for a block header.
Expand Down
2 changes: 1 addition & 1 deletion src/interfaces/IClientConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ import { IProvider } from './IProvider';
export interface IClientConfig {
providers: Array<IProvider>;
retryStrategyOn?: boolean;
periodOffset: number | null;
periodOffset?: number | null;
pingTimeoutMs?: number;
}
18 changes: 0 additions & 18 deletions src/interfaces/IContractReadOperationData.ts

This file was deleted.

13 changes: 0 additions & 13 deletions src/interfaces/IContractReadOperationResponse.ts

This file was deleted.

Loading

0 comments on commit 647741b

Please sign in to comment.