@massalabs/wallet-provider
Version:
massa's wallet provider
202 lines • 7.52 kB
JavaScript
import { formatNodeStatusObject, MAX_GAS_CALL, Operation, SmartContract, strToBytes, Mas, } from '@massalabs/massa-web3';
import { WalletName } from '../wallet';
import { errorHandler } from '../errors/utils/errorHandler';
import { operationType } from '../utils/constants';
import { networkInfos, getClient } from './utils/network';
import { buyRolls, callSC, deploySC, getBalance, sellRolls, signMessage, transfer, } from './services';
export class MetamaskAccount {
address;
provider;
constructor(address, provider) {
this.address = address;
this.provider = provider;
}
get accountName() {
return this.address;
}
get providerName() {
return WalletName.Metamask;
}
async balance(final = false) {
const { finalBalance, candidateBalance } = await getBalance(this.provider, {
address: this.address,
});
return BigInt(final ? finalBalance : candidateBalance);
}
async balanceOf(addresses, final = false) {
const client = await getClient(this.provider);
const addressesInfo = await client.getMultipleAddressInfo(addresses);
return addressesInfo.map((addressInfo) => ({
address: addressInfo.address,
balance: final
? Mas.fromString(addressInfo.final_balance)
: Mas.fromString(addressInfo.candidate_balance),
}));
}
async networkInfos() {
return networkInfos(this.provider);
}
async sign(inData) {
try {
const data = typeof inData === 'string' ? inData : Array.from(inData);
const { publicKey, signature } = await signMessage(this.provider, {
data,
});
return {
publicKey,
signature,
};
}
catch (error) {
throw errorHandler(operationType.Sign, error);
}
}
async handleRollOperation(operation, amount, opts) {
try {
const params = {
amount: amount.toString(),
};
if (opts?.fee) {
params.fee = opts?.fee.toString();
}
const { operationId } = await (operation === 'buy'
? buyRolls(this.provider, params)
: sellRolls(this.provider, params));
return new Operation(this, operationId);
}
catch (error) {
throw errorHandler(operation === 'buy' ? operationType.BuyRolls : operationType.SellRolls, error);
}
}
async buyRolls(amount, opts) {
return this.handleRollOperation('buy', amount, opts);
}
async sellRolls(amount, opts) {
return this.handleRollOperation('sell', amount, opts);
}
async transfer(to, amount, opts) {
try {
const params = {
amount: amount.toString(),
recipientAddress: to.toString(),
};
if (opts?.fee) {
params.fee = opts?.fee.toString();
}
const { operationId } = await transfer(this.provider, params);
return new Operation(this, operationId);
}
catch (error) {
throw errorHandler(operationType.SendTransaction, error);
}
}
async callSC(params) {
try {
const callSCparams = {
functionName: params.func,
at: params.target,
};
if (params.parameter) {
callSCparams.args =
params.parameter instanceof Uint8Array
? Array.from(params.parameter)
: Array.from(params.parameter.serialize());
}
if (params.coins) {
callSCparams.coins = params.coins.toString();
}
if (params.maxGas) {
callSCparams.maxGas = params.maxGas.toString();
}
if (params.fee) {
callSCparams.fee = params.fee.toString();
}
const { operationId } = await callSC(this.provider, callSCparams);
return new Operation(this, operationId);
}
catch (error) {
throw errorHandler(operationType.CallSC, error);
}
}
async readSC(params) {
if (params.maxGas && params.maxGas > MAX_GAS_CALL) {
throw new Error(`Gas amount ${params.maxGas} exceeds the maximum allowed ${MAX_GAS_CALL}`);
}
try {
const args = params.parameter ?? new Uint8Array();
const parameter = args instanceof Uint8Array ? args : Uint8Array.from(args.serialize());
const client = await getClient(this.provider);
const readOnlyParams = {
...params,
caller: params.caller ?? this.address,
parameter,
};
return client.executeReadOnlyCall(readOnlyParams);
}
catch (error) {
throw new Error(`Smart contract read failed: ${error.message}`);
}
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
async deploySC(params) {
try {
const deployParams = {
bytecode: Array.from(params.byteCode),
};
if (params.parameter) {
deployParams.args = Array.from(params.parameter instanceof Uint8Array
? params.parameter
: params.parameter.serialize());
}
if (params.coins) {
deployParams.coins = params.coins.toString();
}
if (params.maxGas) {
deployParams.maxGas = params.maxGas.toString();
}
if (params.fee) {
deployParams.fee = params.fee.toString();
}
if (params.maxCoins) {
deployParams.maxCoins = params.maxCoins.toString();
}
const { operationId } = await deploySC(this.provider, deployParams);
const op = new Operation(this, operationId);
const deployedAddress = await op.getDeployedAddress(params.waitFinalExecution);
return new SmartContract(this, deployedAddress);
}
catch (error) {
throw errorHandler(operationType.DeploySC, error);
}
}
async getOperationStatus(opId) {
const client = await getClient(this.provider);
return client.getOperationStatus(opId);
}
async getEvents(filter) {
const client = await getClient(this.provider);
return client.getEvents(filter);
}
async getNodeStatus() {
const client = await getClient(this.provider);
const status = await client.status();
return formatNodeStatusObject(status);
}
async getStorageKeys(address, filter = new Uint8Array(), final = true) {
const client = await getClient(this.provider);
const filterBytes = typeof filter === 'string' ? strToBytes(filter) : filter;
return client.getDataStoreKeys(address, filterBytes, final);
}
async readStorage(address, keys, final = true) {
const client = await getClient(this.provider);
const entries = keys.map((key) => ({
key,
address,
}));
return client.getDatastoreEntries(entries, final);
}
executeSC() {
throw new Error('Method not implemented.');
}
}
//# sourceMappingURL=MetamaskAccount.js.map