Skip to content

Commit 149828b

Browse files
authored
Merge pull request #652 from tronprotocol/release/v6.0.4
Release/v6.0.4
2 parents 71bac09 + 51e683c commit 149828b

File tree

17 files changed

+2815
-3206
lines changed

17 files changed

+2815
-3206
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,4 @@ test/lib/dist/
1313
/lib/
1414
!src/lib
1515
!test/lib
16-
16+
.nyc_output/

CHANGELOG.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,25 @@
11
Change Log
22
=========
33

4+
__6.0.4__
5+
6+
## New Features
7+
- Improve type inference in `Contract` module when using typescript.
8+
- The `Contract` now infers method signatures based on the provided ABI.
9+
- To enable accurate inference, the ABI should be defined using the `as const` assertion or passed directly into tronWeb.contract().
10+
11+
## Change
12+
- Change the return behavior of the `contract.new()` method.
13+
- Previously, this method mutated the current instance and used the ABI stored on the Tron blockchain, which proved to be unreliable. It now returns a new instance that uses the ABI provided in the options parameter.
14+
- Export `GetEventResultOptions` and `EventResponse`.
15+
- Allow using length as the value of the name field in the ABI, but you cannot use `result['length']` to read its value.
16+
- Bump `axios` from 1.8.3 to 1.11.0, bump `eslint` from 9.22.0 to 9.31.0.
17+
18+
## Bug Fixes
19+
- Fix the issue where `addUpdateData` treats numeric strings as numbers.[#629](https://github.com/tronprotocol/tronweb/issues/629)
20+
- Starting from TronWeb v6.0.4, `addUpdateData` will use `TronWeb.fromUtf8` to convert the provided data string—unless it starts with '0x'.
21+
- If the resulting data string has an odd length, a '0' will be prepended to ensure even length.
22+
423
__6.0.3__
524
- Add support for deserializing 6 more transactions.
625
- Fix npm audit issues.

package-lock.json

Lines changed: 2355 additions & 3024 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "tronweb",
3-
"version": "6.0.3",
3+
"version": "6.0.4",
44
"description": "JavaScript SDK that encapsulates the TRON HTTP API",
55
"main": "./lib/commonjs/index.js",
66
"module": "./lib/esm/index.js",
@@ -45,7 +45,7 @@
4545
"testTx": "node scripts/test-node.js && node test/helpers/newAccounts 100 && npx mocha 'test/**/transaction.test.js' --timeout 120000",
4646
"test-no-accounts": "node scripts/test-node.js && npx mocha 'test/**/*.test.js'",
4747
"test:browser": "npm run-script newaccount && node scripts/test-browser.js && npx karma start --single-run --browsers ChromeHeadless",
48-
"coverage": "npm run-script test:browser && npm run-script test",
48+
"coverage": "npm run build:test && npm run-script newaccount && nyc mocha 'lib/commonjs/test/**/*.test.js' --timeout 120000",
4949
"btest": "npm run build:dev && npm run test",
5050
"format-all": "prettier --write ./src",
5151
"lint": "eslint src",
@@ -58,7 +58,7 @@
5858
},
5959
"dependencies": {
6060
"@babel/runtime": "7.26.10",
61-
"axios": "1.8.3",
61+
"axios": "1.11.0",
6262
"bignumber.js": "9.1.2",
6363
"ethereum-cryptography": "2.2.1",
6464
"ethers": "6.13.5",
@@ -95,14 +95,13 @@
9595
"buffer": "6.0.3",
9696
"chai": "4.5.0",
9797
"chalk": "2.4.2",
98-
"eslint": "9.22.0",
98+
"eslint": "9.31.0",
9999
"eslint-config-prettier": "10.1.1",
100100
"eslint-import-resolver-typescript": "4.2.0",
101101
"events": "3.3.0",
102102
"globals": "16.0.0",
103103
"globby": "14.1.0",
104104
"husky": "9.1.7",
105-
"istanbul": "0.4.5",
106105
"json-schema": "0.4.0",
107106
"jsonwebtoken": "9.0.2",
108107
"karma": "6.4.4",
@@ -116,6 +115,7 @@
116115
"karma-spec-reporter": "0.0.36",
117116
"karma-webpack": "5.0.1",
118117
"mocha": "11.1.0",
118+
"nyc": "17.1.0",
119119
"prettier": "3.5.3",
120120
"puppeteer": "24.4.0",
121121
"querystring-es3": "0.2.1",

src/lib/TransactionBuilder/TransactionBuilder.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { encodeParamsV2ByABI } from '../../utils/abi.js';
55
import { CreateSmartContractTransaction, SignedTransaction, Transaction, TransactionWrapper } from '../../types/Transaction.js';
66
import { Validator } from '../../paramValidator/index.js';
77
import { GetSignWeightResponse } from '../../types/APIResponse.js';
8-
import { isArray, isInteger, isNotNullOrUndefined, isObject, isString } from '../../utils/validations.js';
8+
import { isArray, isHex, isInteger, isNotNullOrUndefined, isObject, isString } from '../../utils/validations.js';
99
import {
1010
AccountCreateContract,
1111
AccountPermissionUpdateContract,
@@ -2159,7 +2159,7 @@ export class TransactionBuilder {
21592159
delete _ownerPermissions.type;
21602160
}
21612161
_ownerPermissions.keys = _ownerPermissions.keys?.map(({ address, weight }) => ({
2162-
address: this.tronWeb.address.toHex(address),
2162+
address: toHex(address),
21632163
weight,
21642164
}));
21652165
data.owner = _ownerPermissions as Permission;
@@ -2171,7 +2171,7 @@ export class TransactionBuilder {
21712171
// @ts-ignore
21722172
_witnessPermissions.type = 'Witness';
21732173
_witnessPermissions.keys = _witnessPermissions.keys.map(({ address, weight }) => ({
2174-
address: this.tronWeb.address.toHex(address),
2174+
address: toHex(address),
21752175
weight,
21762176
}));
21772177
data.witness = _witnessPermissions;
@@ -2186,7 +2186,7 @@ export class TransactionBuilder {
21862186
});
21872187
_activesPermissions.forEach((_activesPermission) => {
21882188
_activesPermission.keys = _activesPermission.keys.map(({ address, weight }) => ({
2189-
address: this.tronWeb.address.toHex(address),
2189+
address: toHex(address),
21902190
weight,
21912191
}));
21922192
});
@@ -2246,8 +2246,10 @@ export class TransactionBuilder {
22462246
if (Reflect.has(transaction, 'signature')) throw new Error('You can not extend the expiration of a signed transaction.');
22472247

22482248
if (options.data) {
2249-
if (options.dataFormat !== 'hex') options.data = TronWeb.toHex(options.data);
2250-
options.data = options.data!.replace(/^0x/, '');
2249+
if (options.dataFormat !== 'hex' && !/^0x/.test(options.data)) options.data = TronWeb.fromUtf8(options.data);
2250+
if (!isHex(options.data)) throw new Error('Invalid data provided');
2251+
options.data = options.data.replace(/^0x/, '');
2252+
options.data = options.data.padStart(Math.ceil(options.data.length/2)*2, '0');
22512253
if (options.data.length === 0) throw new Error('Invalid data provided');
22522254
transaction.raw_data.data = options.data;
22532255
}

src/lib/contract/index.ts

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,24 @@
11
import { TronWeb } from '../../tronweb.js';
22
import utils from '../../utils/index.js';
33
import { Method, AbiFragmentNoErrConstructor } from './method.js';
4-
import type { ContractAbiInterface } from '../../types/ABI.js';
5-
import { Address } from '../../types/Trx.js';
6-
import { CreateSmartContractOptions } from '../../types/TransactionBuilder.js';
4+
import type { ContractAbiInterface, GetMethodsTypeFromAbi, GetOnMethodTypeFromAbi, AnyOnMethodType } from '../../types/ABI.js';
5+
import type { Address } from '../../types/Trx.js';
6+
import type { CreateSmartContractOptions } from '../../types/TransactionBuilder.js';
77

8-
9-
export class Contract {
8+
export class Contract<Abi extends ContractAbiInterface = ContractAbiInterface> {
109
tronWeb: TronWeb;
11-
abi: ContractAbiInterface;
10+
abi: Abi;
1211
address: false | string;
1312
eventListener: any;
1413
bytecode?: false | string;
1514
deployed?: boolean;
1615
lastBlock?: false | number;
17-
methods: Record<string | number | symbol, (...args: any) => ReturnType<Method['onMethod']>>;
18-
methodInstances: Record<string | number | symbol, Method>;
16+
methods: GetOnMethodTypeFromAbi<Abi> & AnyOnMethodType;
17+
methodInstances: GetMethodsTypeFromAbi<Abi>;
1918
props: any[];
2019
[key: string | number | symbol]: any;
2120

22-
constructor(tronWeb: TronWeb, abi: ContractAbiInterface = [], address: Address) {
21+
constructor(tronWeb: TronWeb, abi: Abi = [] as any, address: Address) {
2322
if (!tronWeb || !(tronWeb instanceof TronWeb)) throw new Error('Expected instance of TronWeb');
2423

2524
this.tronWeb = tronWeb;
@@ -32,8 +31,8 @@ export class Contract {
3231
this.deployed = false;
3332
this.lastBlock = false;
3433

35-
this.methods = {};
36-
this.methodInstances = {};
34+
this.methods = {} as GetOnMethodTypeFromAbi<Abi> & AnyOnMethodType;
35+
this.methodInstances = {} as GetMethodsTypeFromAbi<Abi>;
3736
this.props = [];
3837

3938
if (utils.address.isAddress(address)) {
@@ -50,29 +49,31 @@ export class Contract {
5049
return this.hasOwnProperty(property) || (this as any).__proto__.hasOwnProperty(property);
5150
}
5251

53-
loadAbi(abi: ContractAbiInterface) {
52+
loadAbi(abi: Abi) {
5453
this.abi = abi;
55-
this.methods = {};
54+
this.methods = {} as GetOnMethodTypeFromAbi<Abi> & AnyOnMethodType;
5655

5756
this.props.forEach((prop: string) => delete (this as any)[prop]);
5857

5958
abi.forEach((func) => {
6059
// Don't build a method for constructor function. That's handled through contract create.
6160
// Don't build a method for error function.
62-
if (!func.type || /constructor|error/i.test(func.type)) return;
63-
64-
const method = new Method(this, func as AbiFragmentNoErrConstructor);
61+
if (!func.type || ['constructor', 'error'].includes(func.type.toLowerCase())) return;
62+
const method = new Method<any>(this, func as AbiFragmentNoErrConstructor) as GetMethodsTypeFromAbi<Abi>[keyof GetMethodsTypeFromAbi<Abi>];
6563
const methodCall = method.onMethod.bind(method);
6664

6765
const { name, functionSelector, signature } = method;
66+
const internalName = name as keyof GetMethodsTypeFromAbi<Abi>;
67+
const internalFunctionSelector = functionSelector as keyof GetMethodsTypeFromAbi<Abi>;
68+
const internalSignature = signature as keyof GetMethodsTypeFromAbi<Abi>;
6869

69-
this.methods[name] = methodCall;
70-
this.methods[functionSelector!] = methodCall;
71-
this.methods[signature] = methodCall;
70+
(this.methods as AnyOnMethodType)[name] = methodCall;
71+
(this.methods as AnyOnMethodType)[functionSelector!] = methodCall;
72+
(this.methods as AnyOnMethodType)[signature] = methodCall;
7273

73-
this.methodInstances[name] = method;
74-
this.methodInstances[functionSelector!] = method;
75-
this.methodInstances[signature] = method;
74+
this.methodInstances[internalName] = method;
75+
this.methodInstances[internalFunctionSelector!] = method;
76+
this.methodInstances[internalSignature] = method;
7677

7778
if (!this.hasProperty(name)) {
7879
(this as any)[name] = methodCall;
@@ -119,7 +120,8 @@ export class Contract {
119120
}
120121

121122
await utils.sleep(3000);
122-
return this.at(signedTransaction.contract_address);
123+
const abi = signedTransaction.raw_data.contract[0].parameter.value.new_contract.abi.entrys || [];
124+
return this.tronWeb.contract(abi, signedTransaction.contract_address);
123125
}
124126

125127
async at(contractAddress: Address) {

0 commit comments

Comments
 (0)