@layerzerolabs/hardhat-deploy
Version:
Hardhat Plugin For Replicable Deployments And Tests
80 lines • 4.15 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.TronContractFactory = exports.Contract = void 0;
const ethers_1 = require("ethers");
const types_1 = require("./types");
const utils_1 = require("./utils");
var ethers_2 = require("ethers");
Object.defineProperty(exports, "Contract", { enumerable: true, get: function () { return ethers_2.Contract; } });
/**
* A contract factory for deploying and interacting with smart contracts on the TRON network.
*
* `TronContractFactory` extends `ContractFactory` from ethers.js to provide functionalities
* specific to the TRON network. It is used for deploying smart contracts and creating contract instances
* with a given ABI and bytecode, signed by a `TronSigner`.
*
* @extends ContractFactory
*
* @constructor
* @param {ContractInterface} abi - The contract's ABI.
* @param {ethers.BytesLike} bytecode - The contract's bytecode.
* @param {TronSigner} signer - The `TronSigner` instance to sign transactions.
* @param {string} [contractName=''] - An optional name for the contract, used in TRON-specific transaction fields.
*/
class TronContractFactory extends ethers_1.ContractFactory {
constructor(abi, bytecode, signer, contractName = '') {
super(abi, bytecode, signer);
this.contractName = contractName;
this.default_originEnergyLimit = 1e7;
this.abi = abi;
}
/**
* Deploys a smart contract to the TRON network.
*
* This method overrides the `deploy` method from the base contract factory. However, it is not implemented
* for the Tron contract factory and will throw an error if called. Deploying contracts on the TRON network
* requires a different approach and should be handled using the `getDeployTransaction` method.
*
* @param {...any[]} args - Constructor arguments for the smart contract.
* @returns {Promise<Contract>} A promise that resolves to the deployed contract instance.
* @throws {Error} Throws an error indicating that the deploy method is not implemented.
*/
async deploy(...args) {
throw new Error('deploy is not implemented on Tron contract factory');
}
/**
* Constructs the deployment transaction for a smart contract on the TRON network.
*
* This method overrides `getDeployTransaction` from the base contract factory to construct
* a transaction specifically tailored for deploying smart contracts on the TRON network.
* It encodes the constructor arguments, sets the necessary TRON-specific fields in the transaction,
* and handles the conversion of values to match TRON's requirements.
*
* Special Considerations:
* - The `name` field of the transaction is derived from the contract's name and truncated to 32 characters.
* - TRON-specific fields like `feeLimit`, `callValue`, `userFeePercentage`, and `originEnergyLimit` are set.
* - The transaction is prepared in a format suitable for TRON, differing from standard Ethereum transactions.
*
* @param {...any[]} args - Constructor arguments for the smart contract.
* @returns {ethers.providers.TransactionRequest} A TRON-specific transaction request object for contract deployment.
*/
getDeployTransaction(...args) {
const { data, value } = super.getDeployTransaction(...args);
const params = this.interface.encodeDeploy(args.slice(0, this.interface.deploy.inputs.length));
const tx = {
feeLimit: undefined,
callValue: value ? Number(value.toString()) : 0,
userFeePercentage: 100,
originEnergyLimit: this.default_originEnergyLimit,
abi: this.abi,
bytecode: (0, utils_1.strip0x)(this.bytecode),
rawParameter: (0, utils_1.strip0x)(params),
name: this.contractName.slice(0, 32), //contractName's length cannot be greater than 32
data: data?.toString() ?? '',
[types_1.MethodSymbol]: types_1.TronTxMethods.CREATE,
};
return tx;
}
}
exports.TronContractFactory = TronContractFactory;
//# sourceMappingURL=contract.js.map