Skip to content

Commit

Permalink
Merge pull request #396 from tronprotocol/feature/v5.3.0
Browse files Browse the repository at this point in the history
feat: add deployConstantContract api
  • Loading branch information
start940315 authored Jul 7, 2023
2 parents a71627f + 65b548c commit 66c16c7
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 1 deletion.
73 changes: 73 additions & 0 deletions src/lib/transactionBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
14 changes: 13 additions & 1 deletion test/lib/transactionBuilder.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3370,7 +3370,7 @@ describe('TronWeb.transactionBuilder', function () {
});
});

describe.only('#triggerSmartContractWithData', async function() {
describe('#triggerSmartContractWithData', async function() {
let transaction;
let contract1Address;
let issuerAddress;
Expand Down Expand Up @@ -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);
})
})
});

0 comments on commit 66c16c7

Please sign in to comment.