@massalabs/massa-web3
Version:
massa's web3 sdk client
64 lines • 2.43 kB
JavaScript
import { isProvider } from '../provider/helpers';
/**
* A class to interact with a smart contract.
*/
export class SmartContract {
provider;
address;
constructor(provider, address) {
this.provider = provider;
this.address = address;
}
/**
* Executes a smart contract call operation
* @param func - The smart contract function to be called.
* @param parameter - Parameters for the function call in Uint8Array or number[] format.
* @param options - Includes optional and required parameters like fee, maxGas, coins, and periodToLive.
* @returns A promise that resolves to an Operation object representing the transaction.
*/
async call(func, args = new Uint8Array(), options = {}) {
const callParams = {
func,
parameter: args,
target: this.address,
...options,
};
if (isProvider(this.provider)) {
return this.provider.callSC(callParams);
}
throw new Error('Provider does not support callSC');
}
/**
* Executes a smart contract read operation
* @param func - The smart contract function to be called.
* @param args - Parameter for the function call in Uint8Array format.
* @param options - Includes optional parameters like fee, maxGas, coins, and periodToLive.
* @returns A promise that resolves to the result of the read operation.
*/
async read(func, args = new Uint8Array(), options = {}) {
const readParams = {
func,
parameter: args,
target: this.address,
...options,
};
return this.provider.readSC(readParams);
}
/**
* Deploy a SmartContract byteCode
* @param provider - Web3 provider.
* @param byteCode - Compiled SmartContract bytecode.
* @param constructorArgs - Parameter for call of constructor function.
* @param options - Includes optional parameters like fee, maxGas, coins, and periodToLive.
* @returns A promise that resolves to the result of the read operation.
*/
static async deploy(provider, byteCode, constructorArgs = new Uint8Array(), options = {}) {
const deployParams = {
byteCode,
parameter: constructorArgs,
...options,
};
return provider.deploySC(deployParams);
}
}
//# sourceMappingURL=smartContract.js.map