Skip to content

Commit

Permalink
Merge pull request #224 from tronprotocol/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
llwslc authored Jan 12, 2022
2 parents 11d508a + 69db1ba commit 5fe79d5
Show file tree
Hide file tree
Showing 18 changed files with 6,338 additions and 5,175 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ yarn-error.log
.vscode/
.idea/
example/
test/lib/dist/
15 changes: 11 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@
</h1>

<p align="center">
<a href="https://discord.gg/GsRgsTD">
<a href="https://discord.gg/FgvVFQgdCW">
<img src="https://img.shields.io/badge/chat-on%20discord-brightgreen.svg">
</a>

<a href="https://github.com/tron-us/tronweb/issues">
<a href="https://github.com/tronprotocol/tronweb/issues">
<img src="https://img.shields.io/github/issues/tron-us/tronweb.svg">
</a>

<a href="https://github.com/tron-us/tronweb/pulls">
<a href="https://github.com/tronprotocol/tronweb/pulls">
<img src="https://img.shields.io/github/issues-pr/tron-us/tronweb.svg">
</a>

<a href="https://github.com/tron-us/tronweb/graphs/contributors">
<a href="https://github.com/tronprotocol/tronweb/graphs/contributors">
<img src="https://img.shields.io/github/contributors/tron-us/tronweb.svg">
</a>

Expand Down Expand Up @@ -172,6 +172,13 @@ In order to contribute you can

## Recent History

__4.1.0__
- add `encodeParamsV2ByABI` and `decodeParamsV2ByABI` functions in `tronWeb.utils.abi` lib
- support abi v2 for `triggerSmartContract`, `createSmartContract`, `call` and `send` method
- update `validator` to version 13.7.0
- update `axios` t0 version 0.24.0
- update discord group link

__4.0.1__
- set _isConstant as true for call method
- ignore max feeLimit check
Expand Down
51 changes: 27 additions & 24 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 5 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "tronweb",
"version": "4.0.1",
"version": "4.1.0",
"description": "JavaScript SDK that encapsulates the TRON HTTP API",
"main": "dist/TronWeb.node.js",
"scripts": {
Expand All @@ -10,6 +10,8 @@
"clean": "rimraf dist",
"newaccount": "node scripts/test-node.js && node test/helpers/newAccounts 50",
"test": "node scripts/test-node.js && node test/helpers/newAccounts 50 npm run-script newaccount && npx mocha 'test/**/*.test.js' --timeout 120000",
"testAbi": "node scripts/test-node.js && node test/helpers/newAccounts 1 npm run-script newaccount && npx mocha 'test/**/abi.test.js' --timeout 120000",
"testContract": "node scripts/test-node.js && node test/helpers/newAccounts 1 npm run-script newaccount && npx mocha 'test/**/transactionBuilder.test.js' --timeout 120000",
"test-no-accounts": "node scripts/test-node.js && npx mocha 'test/**/*.test.js'",
"test:browser": "npm run-script newaccount && node scripts/test-browser.js && npx karma start --single-run --browsers ChromeHeadless",
"coverage": "npm run-script test:browser && npm run-script test",
Expand All @@ -22,15 +24,15 @@
},
"dependencies": {
"@babel/runtime": "^7.0.0",
"axios": "^0.21.1",
"axios": "^0.24.0",
"bignumber.js": "^9.0.1",
"elliptic": "^6.5.4",
"ethers": "^5.4.4",
"eventemitter3": "^3.1.0",
"injectpromise": "^1.0.0",
"lodash": "^4.17.21",
"semver": "^5.6.0",
"validator": "^13.6.0"
"validator": "^13.7.0"
},
"devDependencies": {
"@babel/core": "^7.0.0",
Expand Down
76 changes: 47 additions & 29 deletions src/lib/contract/method.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
import utils from 'utils';
import {ADDRESS_PREFIX_REGEX} from 'utils/address';
import {encodeParamsV2ByABI, decodeParamsV2ByABI} from 'utils/abi';
import injectpromise from 'injectpromise';

const getFunctionSelector = abi => {
return abi.name + '(' + getParamTypes(abi.inputs || []).join(',') + ')';
}

const getParamTypes = params => {
return params.map(({type}) => type);
abi.stateMutability = abi.stateMutability ? abi.stateMutability.toLowerCase() : 'nonpayable';
abi.type = abi.type ? abi.type.toLowerCase() : '';
if(abi.type === 'fallback' || abi.type === 'receive') return '0x';
let iface = new utils.ethersUtils.Interface([abi]);
if(abi.type === 'event') {
return iface.getEvent(abi.name).format(utils.ethersUtils.FormatTypes.sighash);
}
return iface.getFunction(abi.name).format(utils.ethersUtils.FormatTypes.sighash)
}

const decodeOutput = (abi, output) => {
const names = abi.map(({name}) => name).filter(name => !!name);
const types = abi.map(({type}) => type);

return utils.abi.decodeParams(names, types, output);
return decodeParamsV2ByABI(abi, output)
};

export default class Method {
Expand Down Expand Up @@ -45,24 +46,41 @@ export default class Method {
}

onMethod(...args) {
const types = getParamTypes(this.inputs);

args.forEach((arg, index) => {
if (types[index] === 'address')
args[index] = this.tronWeb.address.toHex(arg).replace(ADDRESS_PREFIX_REGEX, '0x')

if (types[index].match(/^([^\x5b]*)(\x5b|$)/)[0] === 'address[') {
args[index] = args[index].map(address => {
return this.tronWeb.address.toHex(address).replace(ADDRESS_PREFIX_REGEX, '0x')
})
}
});

return {
call: (...methodArgs) => this._call(types, args, ...methodArgs),
send: (...methodArgs) => this._send(types, args, ...methodArgs),
watch: (...methodArgs) => this._watch(...methodArgs)
}
let rawParameter = '';
if(this.abi && !/event/i.test(this.abi.type)) {
rawParameter = encodeParamsV2ByABI(this.abi, args);
}
return {
call: (options = {}, callback = false) => {
if (utils.isFunction(options)) {
callback = options;
options = {};
}
options = {
...options,
rawParameter
};

return this._call([], [], options, callback);
},
send: (options = {}, privateKey = this.tronWeb.defaultPrivateKey, callback = false) => {
if (utils.isFunction(privateKey)) {
callback = privateKey;
privateKey = this.tronWeb.defaultPrivateKey;
}
if (utils.isFunction(options)) {
callback = options;
options = {};
}
options = {
...options,
rawParameter
};

return this._send([], [], options, privateKey, callback);
},
watch: (...methodArgs) => this._watch(...methodArgs)
}
}

async _call(types, args, options = {}, callback = false) {
Expand Down Expand Up @@ -130,7 +148,7 @@ export default class Method {
return callback(msg)
}

let output = decodeOutput(this.outputs, '0x' + transaction.constant_result[0]);
let output = decodeOutput(this.abi, '0x' + transaction.constant_result[0]);

if (output.length === 1)
output = output[0];
Expand Down Expand Up @@ -258,7 +276,7 @@ export default class Method {
if (options.rawResponse)
return callback(null, output);

let decoded = decodeOutput(this.outputs, '0x' + output.contractResult[0]);
let decoded = decodeOutput(this.abi, '0x' + output.contractResult[0]);

if (decoded.length === 1)
decoded = decoded[0];
Expand Down
1 change: 1 addition & 0 deletions src/lib/sidechain.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import injectpromise from 'injectpromise';
import Validator from 'paramValidator';

export default class SideChain {
constructor(sideOptions, TronWeb = false, mainchain = false, privateKey = false) {
this.mainchain = mainchain;
Expand Down
10 changes: 10 additions & 0 deletions src/lib/transactionBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {AbiCoder} from 'utils/ethersUtils';
import Validator from 'paramValidator';
import {ADDRESS_PREFIX_REGEX} from 'utils/address';
import injectpromise from 'injectpromise';
import {encodeParamsV2ByABI} from 'utils/abi';

let self;

Expand Down Expand Up @@ -646,6 +647,8 @@ export default class TransactionBuilder {

if (options.rawParameter && utils.isString(options.rawParameter)) {
parameters = options.rawParameter.replace(/^(0x)/, '');
} else if (options.funcABIV2) {
parameters = encodeParamsV2ByABI(options.funcABIV2, options.parametersV2).replace(/^(0x)/, '');
} else {
var constructorParams = abi.find(
(it) => {
Expand Down Expand Up @@ -825,6 +828,7 @@ export default class TransactionBuilder {
owner_address: toHex(issuerAddress)
};


if (functionSelector && utils.isString(functionSelector)) {
functionSelector = functionSelector.replace('/\s*/g', '');
if (parameters.length) {
Expand Down Expand Up @@ -857,11 +861,17 @@ export default class TransactionBuilder {
})

parameters = abiCoder.encode(types, values).replace(/^(0x)/, '');

} catch (ex) {
return callback(ex);
}
} else parameters = '';

// work for abiv2 if passed the function abi in options
if (options.funcABIV2) {
parameters = encodeParamsV2ByABI(options.funcABIV2, options.parametersV2).replace(/^(0x)/, '');
}

if (options.shieldedParameter && utils.isString(options.shieldedParameter)) {
parameters = options.shieldedParameter.replace(/^(0x)/, '');
}
Expand Down
Loading

0 comments on commit 5fe79d5

Please sign in to comment.