From 65b548c7d2794b5ddfba8f320ede09cdff0fe2af Mon Sep 17 00:00:00 2001 From: stan Date: Fri, 7 Jul 2023 18:32:16 +0800 Subject: [PATCH] feat: add deployConstantContract api --- src/lib/transactionBuilder.js | 73 +++++++++++++++++++++++++++++ test/lib/transactionBuilder.test.js | 14 +++++- 2 files changed, 86 insertions(+), 1 deletion(-) diff --git a/src/lib/transactionBuilder.js b/src/lib/transactionBuilder.js index 3fc278f6..c7f723d3 100644 --- a/src/lib/transactionBuilder.js +++ b/src/lib/transactionBuilder.js @@ -1224,6 +1224,79 @@ export default class TransactionBuilder { return this._triggerSmartContract(...params); } + async deployConstantContract(options = {}) { + const { + input, + ownerAddress, + tokenId, + tokenValue, + callValue = 0, + } = options; + + this.validator.notValid([ + { + name: 'input', + type: 'not-empty-string', + value: input, + }, + { + name: 'callValue', + type: 'integer', + value: callValue, + gte: 0 + }, + { + name: 'owner', + type: 'address', + value: ownerAddress + }, + { + name: 'tokenValue', + type: 'integer', + value: tokenValue, + gte: 0, + optional: true + }, + { + name: 'tokenId', + type: 'integer', + value: tokenId, + gte: 0, + optional: true + } + ], (str) => { + throw new Error(str); + }); + + const args = { + data: input, + owner_address: toHex(ownerAddress), + call_value: callValue, + } + + if (tokenId) { + args.token_id = tokenId; + } + if (tokenValue) { + args.call_token_value = tokenValue; + } + + const pathInfo = `wallet${options.confirmed ? 'solidity' : ''}/estimateenergy`; + return this.tronWeb[options.confirmed ? 'solidityNode' : 'fullNode'] + .request(pathInfo, args, 'post') + .then(transaction => { + if (transaction.Error) + throw new Error(transaction.Error); + + if (transaction.result && transaction.result.message) { + throw new Error( + this.tronWeb.toUtf8(transaction.result.message) + ); + } + return transaction; + }); + } + _getTriggerSmartContractArgs( contractAddress, functionSelector, diff --git a/test/lib/transactionBuilder.test.js b/test/lib/transactionBuilder.test.js index 782f153b..cc139da2 100644 --- a/test/lib/transactionBuilder.test.js +++ b/test/lib/transactionBuilder.test.js @@ -3370,7 +3370,7 @@ describe('TronWeb.transactionBuilder', function () { }); }); - describe.only('#triggerSmartContractWithData', async function() { + describe('#triggerSmartContractWithData', async function() { let transaction; let contract1Address; let issuerAddress; @@ -3542,4 +3542,16 @@ describe('TronWeb.transactionBuilder', function () { } }); }); + + describe('#deployConstantContract', async function () { + it('should get the estimated energy of deploying a contract', async function () { + const receipt = await tronWeb.transactionBuilder.deployConstantContract({ + input: testSetVal.bytecode, + ownerAddress: accounts.hex[1], + }); + assert.isTrue(receipt.result.result); + assert.isDefined(receipt.energy_required); + assert.isNumber(receipt.energy_required); + }) + }) });