Skip to content

Commit

Permalink
Add Deploy Sc from Wallets (#272)
Browse files Browse the repository at this point in the history
* add bearby deploy

* add MS deploy

* add types

* remove public from execute sc function

* add to string

* add description

* add deploySc Description
  • Loading branch information
pivilartisant authored Dec 10, 2024
1 parent c6334b5 commit f319128
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 8 deletions.
39 changes: 34 additions & 5 deletions src/bearbyWallet/BearbyAccount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ import { operationType } from '../utils/constants';
import {
Address,
CallSCParams,
DEPLOYER_BYTECODE,
DeploySCParams,
EventFilter,
formatNodeStatusObject,
JsonRPCClient,
Mas,
MAX_GAS_CALL,
MAX_GAS_DEPLOYMENT,
Network,
NodeStatusInfo,
Operation,
Expand All @@ -24,12 +26,14 @@ import {
ReadSCParams,
SignedData,
SmartContract,
StorageCost,
strToBytes,
rpcTypes,
} from '@massalabs/massa-web3';
import { networkInfos } from './utils/network';
import { WalletName } from '../wallet';
import isEqual from 'lodash.isequal';
import { uint8ArrayToBase64 } from '../utils/base64';

export class BearbyAccount implements Provider {
public constructor(public address: string) {}
Expand Down Expand Up @@ -82,7 +86,6 @@ export class BearbyAccount implements Provider {
if (data instanceof Uint8Array) {
strData = new TextDecoder().decode(data);
}

try {
const signature = await web3.wallet.signMessage(strData);

Expand Down Expand Up @@ -236,10 +239,36 @@ export class BearbyAccount implements Provider {
}
}

// eslint-disable-next-line @typescript-eslint/no-unused-vars
public async deploySC(_params: DeploySCParams): Promise<SmartContract> {
// TODO: Implement deploySC using web3.contract.deploy
throw new Error('Method not implemented.');
public async deploySC(params: DeploySCParams): Promise<SmartContract> {
try {
const fee = Number(params.fee ?? (await this.minimalFee()));
const totalCost =
StorageCost.smartContract(params.byteCode.length) + params.coins;

const args = {
...params,
maxCoins: totalCost,
maxGas: params.maxGas || MAX_GAS_DEPLOYMENT,
coins: params.coins,
fee: fee,
gasPrice: 10000n, // dummy value waiting for (https://github.com/bearby-wallet/bearby-web3/pull/25)
contractDataBase64: uint8ArrayToBase64(params.byteCode),
deployerBase64: uint8ArrayToBase64(DEPLOYER_BYTECODE),
};

const operationId = await web3.contract.deploy(args);

const operation = new Operation(this, operationId);

const deployedAddress = await operation.getDeployedAddress(
params.waitFinalExecution,
);

return new SmartContract(this, deployedAddress);
} catch (error) {
console.error('Error deploying smart contract:', error);

Check warning on line 269 in src/bearbyWallet/BearbyAccount.ts

View workflow job for this annotation

GitHub Actions / format

Unexpected console statement

Check warning on line 269 in src/bearbyWallet/BearbyAccount.ts

View workflow job for this annotation

GitHub Actions / test / format / format

Unexpected console statement
throw new Error(`Failed to deploy smart contract: ${error.message}`);
}
}

executeSC(): Promise<Operation> {
Expand Down
46 changes: 43 additions & 3 deletions src/massaStation/MassaStationAccount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
uint8ArrayToBase64,
} from '../utils/base64';
import {
DeploySCFunctionBody,
ExecuteFunctionBody,
MSAccountSignPayload,
MSAccountSignResp,
Expand Down Expand Up @@ -39,6 +40,8 @@ import {
SmartContract,
strToBytes,
rpcTypes,
StorageCost,
formatMas,
} from '@massalabs/massa-web3';
import { getClient, networkInfos } from './utils/network';
import { WalletName } from '../wallet';
Expand Down Expand Up @@ -239,9 +242,46 @@ export class MassaStationAccount implements Provider {
return client.executeReadOnlyCall(readOnlyParams);
}

// eslint-disable-next-line @typescript-eslint/no-unused-vars
public async deploySC(_params: DeploySCParams): Promise<SmartContract> {
throw new Error('Method not implemented.');
public async deploySC(params: DeploySCParams): Promise<SmartContract> {
try {
const args = params.parameter ?? new Uint8Array();
const parameters = args instanceof Uint8Array ? args : args.serialize();
const totalCost =
StorageCost.smartContract(params.byteCode.length) + params.coins;
const fee = params.fee || (await this.minimalFee());

const body: DeploySCFunctionBody = {
nickname: this.accountName,
smartContract: uint8ArrayToBase64(params.byteCode),
maxCoins: totalCost.toString(),
coins: params.coins.toString(), // SmartContract deployment costs
fee: fee.toString(),
parameters: uint8ArrayToBase64(parameters),
description: `${formatMas(
params.coins,
)} $MAS coins allocated to datastore + ${formatMas(
fee,
)} $MAS fee for operation`,
};

const res = await postRequest<MSSendOperationResp>(
`${MASSA_STATION_URL}cmd/deploySC`,
body,
);

const operationId = res.result?.operationId;

if (!operationId) throw new Error('Operation ID not found');

const operation = new Operation(this, operationId);
const deployedAddress = await operation.getDeployedAddress(
params.waitFinalExecution,
);

return new SmartContract(this, deployedAddress);
} catch (error) {
throw new Error(`Error during smart contract deployment: ${error}`);
}
}

executeSC(): Promise<Operation> {
Expand Down
10 changes: 10 additions & 0 deletions src/massaStation/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,16 @@ export type ExecuteFunctionBody = {
async?: boolean;
};

export type DeploySCFunctionBody = {
nickname: string;
smartContract: string;
maxCoins: string;
coins: string;
parameters: string;
fee: string;
description: string;
};

export type PluginInfo = {
author: string;
description: string;
Expand Down

0 comments on commit f319128

Please sign in to comment.