Skip to content

Commit

Permalink
Created deployment, deposit and withdraw scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
leoloco committed Apr 5, 2024
1 parent 4917c90 commit 61d4798
Show file tree
Hide file tree
Showing 7 changed files with 2,593 additions and 18 deletions.
4 changes: 3 additions & 1 deletion smart-contracts/.env.example
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
WALLET_PRIVATE_KEY=""

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

WMAS_ADRS=""
2,414 changes: 2,397 additions & 17 deletions smart-contracts/package-lock.json

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions smart-contracts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@
"@massalabs/eslint-config": "^0.0.9",
"@massalabs/massa-as-sdk": "^2.5.5-dev",
"@massalabs/massa-sc-compiler": "^0.1.1-dev",
"@massalabs/massa-sc-deployer": "^1.3.0",
"@massalabs/massa-web3": "^3.0.4-dev",
"@massalabs/prettier-config-as": "^0.0.2",
"@massalabs/web3-utils": "^1.4.8",
"@types/node": "^18.11.10",
"as-bignum": "^0.3.0",
"assemblyscript": "^0.25.2",
Expand Down
36 changes: 36 additions & 0 deletions smart-contracts/scripts/deploy-wmas.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { readFileSync } from 'fs';
import path from 'path';
import { deploySC } from '@massalabs/massa-sc-deployer';
import { Args, fromMAS } from '@massalabs/web3-utils';
import { getClient } from './utils';
import { fileURLToPath } from 'url';
import { config } from 'dotenv';
config();

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(path.dirname(__filename));

const { account, chainId } = await getClient();

if (!process.env.JSON_RPC_URL_PUBLIC) {
throw new Error('JSON_RPC_URL_PUBLIC env variable is not set');
}
await deploySC(
process.env.JSON_RPC_URL_PUBLIC,
account,
[
{
data: readFileSync(path.join(__dirname, '..', 'smart-contracts/build', 'WMAS.wasm')),
coins: fromMAS(0.0255),
args: new Args()
.addString('Wrapped Massa')
.addString('WMAS')
.addU8(9)
.addU256(0n),
},
],
chainId,
0n,
1_000_000_000n,
true,
);
24 changes: 24 additions & 0 deletions smart-contracts/scripts/deposit-wmas.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { getClient, waitFinalOperation } from './utils';
import { config } from 'dotenv';
config();

export async function deposit(fee:bigint, amount:bigint): Promise<void> {
if(!process.env.WMAS_ADRS) {
throw new Error('WMAS_ADRS env variable is not set');
}
const { client, baseAccount } = await getClient();
let operationId = await client.smartContracts().callSmartContract(
{
fee: fee,
maxGas: 2_100_000n,
coins: amount,
targetAddress: process.env.WMAS_ADRS,
targetFunction: 'deposit',
parameter: [],
},
baseAccount,
);
console.log(`deposit operationId: ${operationId}`);
waitFinalOperation(client, operationId);

}
103 changes: 103 additions & 0 deletions smart-contracts/scripts/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
import {
Client,
WalletClient,
IAccount,
IBaseAccount,
PublicApiClient,
ProviderType,
Web3Account,
IEvent,
EOperationStatus,
} from '@massalabs/massa-web3';
import { scheduler } from 'timers/promises';

export const WAIT_STATUS_TIMEOUT = 300000;
export const STATUS_POLL_INTERVAL_MS = 1000;

export const getClient = async (): Promise<{
client: Client;
account: IAccount;
baseAccount: IBaseAccount;
chainId: bigint;
}> => {
if (!process.env.WALLET_PRIVATE_KEY) {
throw new Error('WALLET_PRIVATE_KEY env variable is not set');
}
if (!process.env.JSON_RPC_URL_PUBLIC) {
throw new Error('JSON_RPC_URL_PUBLIC env variable is not set');
}
const account = await WalletClient.getAccountFromSecretKey(
process.env.WALLET_PRIVATE_KEY,
);

const clientConfig = {
retryStrategyOn: true,
providers: [
{ url: process.env.JSON_RPC_URL_PUBLIC, type: ProviderType.PUBLIC },
],
periodOffset: 9,
};

const publicApi = new PublicApiClient(clientConfig);
const status = await publicApi.getNodeStatus();

const web3account = new Web3Account(account, publicApi, status.chain_id);
const client = new Client(clientConfig, web3account, publicApi);
return {
client,
account,
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
baseAccount: client.wallet().getBaseAccount()!,
chainId: status.chain_id,
};
};


export const waitFinalOperation = async (
client: Client,
opId: string,
): Promise<IEvent[]> => {
const start = Date.now();
let counterMs = 0;
while (counterMs < WAIT_STATUS_TIMEOUT) {
const status = await client.smartContracts().getOperationStatus(opId);
if (status === EOperationStatus.FINAL_SUCCESS) {
console.log(`Operation ${opId} finished successfully!`);
return getOperationEvents(client, opId);
}
if (
status === EOperationStatus.FINAL_ERROR ||
status === EOperationStatus.SPECULATIVE_ERROR
) {
const events = await getOperationEvents(client, opId);
if (!events.length) {
console.log(
`Operation ${opId} failed with no events! Try to increase maxGas`,
);
}
events.map((l) => console.log(`>>>> New event: ${l.data}`));
throw new Error(`Operation ended with errors...`);
}

await scheduler.wait(STATUS_POLL_INTERVAL_MS);
counterMs = Date.now() - start;
}
const status = await client.smartContracts().getOperationStatus(opId);
throw new Error(
`Wait operation timeout... status=${EOperationStatus[status]}`,
);
};

export async function getOperationEvents(
client: Client,
opId: string,
): Promise<IEvent[]> {
return client.smartContracts().getFilteredScOutputEvents({
start: null,
end: null,
original_caller_address: null,
original_operation_id: opId,
emitter_address: null,
is_final: null,
});
}
27 changes: 27 additions & 0 deletions smart-contracts/scripts/withdraw-wmas.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { Args } from '@massalabs/massa-web3';
import { getClient, waitFinalOperation } from './utils';
import { config } from 'dotenv';
config();

export async function withdraw(fee: bigint, amount:bigint, withdrawer: string): Promise<void> {
if(!process.env.WMAS_ADRS) {
throw new Error('WMAS_ADRS env variable is not set');
}
const { client, baseAccount } = await getClient();
let operationId = await client.smartContracts().callSmartContract(
{
fee: fee,
maxGas: 2_100_000n,
coins: 0n,
targetAddress: process.env.WMAS_ADRS,
targetFunction: 'withdraw',
parameter: new Args()
.addU64(amount)
.addString(withdrawer)
.serialize(),
},
baseAccount,
);
console.log(`withdraw operationId: ${operationId}`);
waitFinalOperation(client, operationId);
}

0 comments on commit 61d4798

Please sign in to comment.