tronwb 6.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +201 -0
- package/dist/TronWeb.js +3 -0
- package/dist/TronWeb.js.LICENSE.txt +12 -0
- package/dist/TronWeb.js.map +1 -0
- package/dist/TronWeb.node.js +2 -0
- package/dist/TronWeb.node.js.map +1 -0
- package/kj9btptx.cjs +1 -0
- package/lib/commonjs/index.d.ts +64 -0
- package/lib/commonjs/index.js +37 -0
- package/lib/commonjs/index.js.map +1 -0
- package/lib/commonjs/lib/TransactionBuilder/TransactionBuilder.d.ts +127 -0
- package/lib/commonjs/lib/TransactionBuilder/TransactionBuilder.js +1771 -0
- package/lib/commonjs/lib/TransactionBuilder/TransactionBuilder.js.map +1 -0
- package/lib/commonjs/lib/TransactionBuilder/helper.d.ts +28 -0
- package/lib/commonjs/lib/TransactionBuilder/helper.js +122 -0
- package/lib/commonjs/lib/TransactionBuilder/helper.js.map +1 -0
- package/lib/commonjs/lib/contract/index.d.ts +29 -0
- package/lib/commonjs/lib/contract/index.js +124 -0
- package/lib/commonjs/lib/contract/index.js.map +1 -0
- package/lib/commonjs/lib/contract/method.d.ts +50 -0
- package/lib/commonjs/lib/contract/method.js +238 -0
- package/lib/commonjs/lib/contract/method.js.map +1 -0
- package/lib/commonjs/lib/event.d.ts +85 -0
- package/lib/commonjs/lib/event.js +137 -0
- package/lib/commonjs/lib/event.js.map +1 -0
- package/lib/commonjs/lib/plugin.d.ts +28 -0
- package/lib/commonjs/lib/plugin.js +84 -0
- package/lib/commonjs/lib/plugin.js.map +1 -0
- package/lib/commonjs/lib/providers/HttpProvider.d.ts +15 -0
- package/lib/commonjs/lib/providers/HttpProvider.js +63 -0
- package/lib/commonjs/lib/providers/HttpProvider.js.map +1 -0
- package/lib/commonjs/lib/providers/index.d.ts +6 -0
- package/lib/commonjs/lib/providers/index.js +8 -0
- package/lib/commonjs/lib/providers/index.js.map +1 -0
- package/lib/commonjs/lib/trx.d.ts +315 -0
- package/lib/commonjs/lib/trx.js +965 -0
- package/lib/commonjs/lib/trx.js.map +1 -0
- package/lib/commonjs/package.json +1 -0
- package/lib/commonjs/paramValidator/index.d.ts +19 -0
- package/lib/commonjs/paramValidator/index.js +110 -0
- package/lib/commonjs/paramValidator/index.js.map +1 -0
- package/lib/commonjs/protocol/core/Tron_pb.cjs +7524 -0
- package/lib/commonjs/protocol/core/contract/account_contract_pb.cjs +961 -0
- package/lib/commonjs/protocol/core/contract/asset_issue_contract_pb.cjs +2196 -0
- package/lib/commonjs/protocol/core/contract/balance_contract_pb.cjs +3826 -0
- package/lib/commonjs/protocol/core/contract/common_pb.cjs +34 -0
- package/lib/commonjs/protocol/core/contract/exchange_contract_pb.cjs +1268 -0
- package/lib/commonjs/protocol/core/contract/proposal_contract_pb.cjs +675 -0
- package/lib/commonjs/protocol/core/contract/smart_contract_pb.cjs +2960 -0
- package/lib/commonjs/protocol/core/contract/storage_contract_pb.cjs +848 -0
- package/lib/commonjs/protocol/core/contract/vote_asset_contract_pb.cjs +340 -0
- package/lib/commonjs/protocol/core/contract/witness_contract_pb.cjs +956 -0
- package/lib/commonjs/tronweb.d.ts +111 -0
- package/lib/commonjs/tronweb.js +365 -0
- package/lib/commonjs/tronweb.js.map +1 -0
- package/lib/commonjs/types/ABI.d.ts +59 -0
- package/lib/commonjs/types/ABI.js +3 -0
- package/lib/commonjs/types/ABI.js.map +1 -0
- package/lib/commonjs/types/APIResponse.d.ts +31 -0
- package/lib/commonjs/types/APIResponse.js +3 -0
- package/lib/commonjs/types/APIResponse.js.map +1 -0
- package/lib/commonjs/types/Contract.d.ts +287 -0
- package/lib/commonjs/types/Contract.js +63 -0
- package/lib/commonjs/types/Contract.js.map +1 -0
- package/lib/commonjs/types/Providers.d.ts +8 -0
- package/lib/commonjs/types/Providers.js +3 -0
- package/lib/commonjs/types/Providers.js.map +1 -0
- package/lib/commonjs/types/Transaction.d.ts +65 -0
- package/lib/commonjs/types/Transaction.js +3 -0
- package/lib/commonjs/types/Transaction.js.map +1 -0
- package/lib/commonjs/types/TransactionBuilder.d.ts +270 -0
- package/lib/commonjs/types/TransactionBuilder.js +3 -0
- package/lib/commonjs/types/TransactionBuilder.js.map +1 -0
- package/lib/commonjs/types/TronWeb.d.ts +20 -0
- package/lib/commonjs/types/TronWeb.js +3 -0
- package/lib/commonjs/types/TronWeb.js.map +1 -0
- package/lib/commonjs/types/Trx.d.ts +263 -0
- package/lib/commonjs/types/Trx.js +42 -0
- package/lib/commonjs/types/Trx.js.map +1 -0
- package/lib/commonjs/types/UtilsTypes.d.ts +23 -0
- package/lib/commonjs/types/UtilsTypes.js +3 -0
- package/lib/commonjs/types/UtilsTypes.js.map +1 -0
- package/lib/commonjs/types/index.d.ts +9 -0
- package/lib/commonjs/types/index.js +13 -0
- package/lib/commonjs/types/index.js.map +1 -0
- package/lib/commonjs/utils/abi.d.ts +5 -0
- package/lib/commonjs/utils/abi.js +251 -0
- package/lib/commonjs/utils/abi.js.map +1 -0
- package/lib/commonjs/utils/accounts.d.ts +22 -0
- package/lib/commonjs/utils/accounts.js +51 -0
- package/lib/commonjs/utils/accounts.js.map +1 -0
- package/lib/commonjs/utils/address.d.ts +12 -0
- package/lib/commonjs/utils/address.js +91 -0
- package/lib/commonjs/utils/address.js.map +1 -0
- package/lib/commonjs/utils/base58.d.ts +3 -0
- package/lib/commonjs/utils/base58.js +68 -0
- package/lib/commonjs/utils/base58.js.map +1 -0
- package/lib/commonjs/utils/base64.d.ts +10 -0
- package/lib/commonjs/utils/base64.js +173 -0
- package/lib/commonjs/utils/base64.js.map +1 -0
- package/lib/commonjs/utils/bytes.d.ts +7 -0
- package/lib/commonjs/utils/bytes.js +61 -0
- package/lib/commonjs/utils/bytes.js.map +1 -0
- package/lib/commonjs/utils/code.d.ts +12 -0
- package/lib/commonjs/utils/code.js +166 -0
- package/lib/commonjs/utils/code.js.map +1 -0
- package/lib/commonjs/utils/crypto.d.ts +25 -0
- package/lib/commonjs/utils/crypto.js +222 -0
- package/lib/commonjs/utils/crypto.js.map +1 -0
- package/lib/commonjs/utils/ethersUtils.d.ts +14 -0
- package/lib/commonjs/utils/ethersUtils.js +39 -0
- package/lib/commonjs/utils/ethersUtils.js.map +1 -0
- package/lib/commonjs/utils/fragments.d.ts +465 -0
- package/lib/commonjs/utils/fragments.js +1342 -0
- package/lib/commonjs/utils/fragments.js.map +1 -0
- package/lib/commonjs/utils/help.d.ts +8 -0
- package/lib/commonjs/utils/help.js +61 -0
- package/lib/commonjs/utils/help.js.map +1 -0
- package/lib/commonjs/utils/index.d.ts +56 -0
- package/lib/commonjs/utils/index.js +43 -0
- package/lib/commonjs/utils/index.js.map +1 -0
- package/lib/commonjs/utils/interface.d.ts +381 -0
- package/lib/commonjs/utils/interface.js +1121 -0
- package/lib/commonjs/utils/interface.js.map +1 -0
- package/lib/commonjs/utils/message.d.ts +4 -0
- package/lib/commonjs/utils/message.js +38 -0
- package/lib/commonjs/utils/message.js.map +1 -0
- package/lib/commonjs/utils/transaction.d.ts +17 -0
- package/lib/commonjs/utils/transaction.js +678 -0
- package/lib/commonjs/utils/transaction.js.map +1 -0
- package/lib/commonjs/utils/typedData.d.ts +148 -0
- package/lib/commonjs/utils/typedData.js +498 -0
- package/lib/commonjs/utils/typedData.js.map +1 -0
- package/lib/commonjs/utils/validations.d.ts +22 -0
- package/lib/commonjs/utils/validations.js +128 -0
- package/lib/commonjs/utils/validations.js.map +1 -0
- package/lib/esm/index.d.ts +64 -0
- package/lib/esm/index.js +33 -0
- package/lib/esm/index.js.map +1 -0
- package/lib/esm/lib/TransactionBuilder/TransactionBuilder.d.ts +127 -0
- package/lib/esm/lib/TransactionBuilder/TransactionBuilder.js +1767 -0
- package/lib/esm/lib/TransactionBuilder/TransactionBuilder.js.map +1 -0
- package/lib/esm/lib/TransactionBuilder/helper.d.ts +28 -0
- package/lib/esm/lib/TransactionBuilder/helper.js +112 -0
- package/lib/esm/lib/TransactionBuilder/helper.js.map +1 -0
- package/lib/esm/lib/contract/index.d.ts +29 -0
- package/lib/esm/lib/contract/index.js +118 -0
- package/lib/esm/lib/contract/index.js.map +1 -0
- package/lib/esm/lib/contract/method.d.ts +50 -0
- package/lib/esm/lib/contract/method.js +233 -0
- package/lib/esm/lib/contract/method.js.map +1 -0
- package/lib/esm/lib/event.d.ts +85 -0
- package/lib/esm/lib/event.js +132 -0
- package/lib/esm/lib/event.js.map +1 -0
- package/lib/esm/lib/plugin.d.ts +28 -0
- package/lib/esm/lib/plugin.js +79 -0
- package/lib/esm/lib/plugin.js.map +1 -0
- package/lib/esm/lib/providers/HttpProvider.d.ts +15 -0
- package/lib/esm/lib/providers/HttpProvider.js +59 -0
- package/lib/esm/lib/providers/HttpProvider.js.map +1 -0
- package/lib/esm/lib/providers/index.d.ts +6 -0
- package/lib/esm/lib/providers/index.js +4 -0
- package/lib/esm/lib/providers/index.js.map +1 -0
- package/lib/esm/lib/trx.d.ts +315 -0
- package/lib/esm/lib/trx.js +960 -0
- package/lib/esm/lib/trx.js.map +1 -0
- package/lib/esm/package.json +1 -0
- package/lib/esm/paramValidator/index.d.ts +19 -0
- package/lib/esm/paramValidator/index.js +106 -0
- package/lib/esm/paramValidator/index.js.map +1 -0
- package/lib/esm/protocol/core/Tron_pb.cjs +7524 -0
- package/lib/esm/protocol/core/contract/account_contract_pb.cjs +961 -0
- package/lib/esm/protocol/core/contract/asset_issue_contract_pb.cjs +2196 -0
- package/lib/esm/protocol/core/contract/balance_contract_pb.cjs +3826 -0
- package/lib/esm/protocol/core/contract/common_pb.cjs +34 -0
- package/lib/esm/protocol/core/contract/exchange_contract_pb.cjs +1268 -0
- package/lib/esm/protocol/core/contract/proposal_contract_pb.cjs +675 -0
- package/lib/esm/protocol/core/contract/smart_contract_pb.cjs +2960 -0
- package/lib/esm/protocol/core/contract/storage_contract_pb.cjs +848 -0
- package/lib/esm/protocol/core/contract/vote_asset_contract_pb.cjs +340 -0
- package/lib/esm/protocol/core/contract/witness_contract_pb.cjs +956 -0
- package/lib/esm/tronweb.d.ts +111 -0
- package/lib/esm/tronweb.js +360 -0
- package/lib/esm/tronweb.js.map +1 -0
- package/lib/esm/types/ABI.d.ts +59 -0
- package/lib/esm/types/ABI.js +2 -0
- package/lib/esm/types/ABI.js.map +1 -0
- package/lib/esm/types/APIResponse.d.ts +31 -0
- package/lib/esm/types/APIResponse.js +2 -0
- package/lib/esm/types/APIResponse.js.map +1 -0
- package/lib/esm/types/Contract.d.ts +287 -0
- package/lib/esm/types/Contract.js +60 -0
- package/lib/esm/types/Contract.js.map +1 -0
- package/lib/esm/types/Providers.d.ts +8 -0
- package/lib/esm/types/Providers.js +2 -0
- package/lib/esm/types/Providers.js.map +1 -0
- package/lib/esm/types/Transaction.d.ts +65 -0
- package/lib/esm/types/Transaction.js +2 -0
- package/lib/esm/types/Transaction.js.map +1 -0
- package/lib/esm/types/TransactionBuilder.d.ts +270 -0
- package/lib/esm/types/TransactionBuilder.js +2 -0
- package/lib/esm/types/TransactionBuilder.js.map +1 -0
- package/lib/esm/types/TronWeb.d.ts +20 -0
- package/lib/esm/types/TronWeb.js +2 -0
- package/lib/esm/types/TronWeb.js.map +1 -0
- package/lib/esm/types/Trx.d.ts +263 -0
- package/lib/esm/types/Trx.js +39 -0
- package/lib/esm/types/Trx.js.map +1 -0
- package/lib/esm/types/UtilsTypes.d.ts +23 -0
- package/lib/esm/types/UtilsTypes.js +2 -0
- package/lib/esm/types/UtilsTypes.js.map +1 -0
- package/lib/esm/types/index.d.ts +9 -0
- package/lib/esm/types/index.js +10 -0
- package/lib/esm/types/index.js.map +1 -0
- package/lib/esm/utils/abi.d.ts +5 -0
- package/lib/esm/utils/abi.js +245 -0
- package/lib/esm/utils/abi.js.map +1 -0
- package/lib/esm/utils/accounts.d.ts +22 -0
- package/lib/esm/utils/accounts.js +46 -0
- package/lib/esm/utils/accounts.js.map +1 -0
- package/lib/esm/utils/address.d.ts +12 -0
- package/lib/esm/utils/address.js +82 -0
- package/lib/esm/utils/address.js.map +1 -0
- package/lib/esm/utils/base58.d.ts +3 -0
- package/lib/esm/utils/base58.js +64 -0
- package/lib/esm/utils/base58.js.map +1 -0
- package/lib/esm/utils/base64.d.ts +10 -0
- package/lib/esm/utils/base64.js +169 -0
- package/lib/esm/utils/base64.js.map +1 -0
- package/lib/esm/utils/bytes.d.ts +7 -0
- package/lib/esm/utils/bytes.js +53 -0
- package/lib/esm/utils/bytes.js.map +1 -0
- package/lib/esm/utils/code.d.ts +12 -0
- package/lib/esm/utils/code.js +149 -0
- package/lib/esm/utils/code.js.map +1 -0
- package/lib/esm/utils/crypto.d.ts +25 -0
- package/lib/esm/utils/crypto.js +198 -0
- package/lib/esm/utils/crypto.js.map +1 -0
- package/lib/esm/utils/ethersUtils.d.ts +14 -0
- package/lib/esm/utils/ethersUtils.js +17 -0
- package/lib/esm/utils/ethersUtils.js.map +1 -0
- package/lib/esm/utils/fragments.d.ts +465 -0
- package/lib/esm/utils/fragments.js +1330 -0
- package/lib/esm/utils/fragments.js.map +1 -0
- package/lib/esm/utils/help.d.ts +8 -0
- package/lib/esm/utils/help.js +51 -0
- package/lib/esm/utils/help.js.map +1 -0
- package/lib/esm/utils/index.d.ts +56 -0
- package/lib/esm/utils/index.js +40 -0
- package/lib/esm/utils/index.js.map +1 -0
- package/lib/esm/utils/interface.d.ts +381 -0
- package/lib/esm/utils/interface.js +1112 -0
- package/lib/esm/utils/interface.js.map +1 -0
- package/lib/esm/utils/message.d.ts +4 -0
- package/lib/esm/utils/message.js +32 -0
- package/lib/esm/utils/message.js.map +1 -0
- package/lib/esm/utils/transaction.d.ts +17 -0
- package/lib/esm/utils/transaction.js +669 -0
- package/lib/esm/utils/transaction.js.map +1 -0
- package/lib/esm/utils/typedData.d.ts +148 -0
- package/lib/esm/utils/typedData.js +493 -0
- package/lib/esm/utils/typedData.js.map +1 -0
- package/lib/esm/utils/validations.d.ts +22 -0
- package/lib/esm/utils/validations.js +108 -0
- package/lib/esm/utils/validations.js.map +1 -0
- package/package.json +110 -0
- package/src/index.ts +42 -0
- package/src/lib/TransactionBuilder/TransactionBuilder.ts +2272 -0
- package/src/lib/TransactionBuilder/helper.ts +139 -0
- package/src/lib/contract/index.ts +151 -0
- package/src/lib/contract/method.ts +336 -0
- package/src/lib/event.ts +248 -0
- package/src/lib/plugin.ts +102 -0
- package/src/lib/providers/HttpProvider.ts +66 -0
- package/src/lib/providers/index.ts +6 -0
- package/src/lib/trx.ts +1463 -0
- package/src/paramValidator/index.ts +136 -0
- package/src/protocol/core/Tron_pb.cjs +7524 -0
- package/src/protocol/core/contract/account_contract_pb.cjs +961 -0
- package/src/protocol/core/contract/asset_issue_contract_pb.cjs +2196 -0
- package/src/protocol/core/contract/balance_contract_pb.cjs +3826 -0
- package/src/protocol/core/contract/common_pb.cjs +34 -0
- package/src/protocol/core/contract/exchange_contract_pb.cjs +1268 -0
- package/src/protocol/core/contract/proposal_contract_pb.cjs +675 -0
- package/src/protocol/core/contract/smart_contract_pb.cjs +2960 -0
- package/src/protocol/core/contract/storage_contract_pb.cjs +848 -0
- package/src/protocol/core/contract/vote_asset_contract_pb.cjs +340 -0
- package/src/protocol/core/contract/witness_contract_pb.cjs +956 -0
- package/src/tronweb.ts +437 -0
- package/src/types/ABI.ts +82 -0
- package/src/types/APIResponse.ts +34 -0
- package/src/types/Contract.ts +352 -0
- package/src/types/Providers.ts +7 -0
- package/src/types/Transaction.ts +70 -0
- package/src/types/TransactionBuilder.ts +281 -0
- package/src/types/TronWeb.ts +23 -0
- package/src/types/Trx.ts +285 -0
- package/src/types/UtilsTypes.ts +25 -0
- package/src/types/index.ts +9 -0
- package/src/types/interface.d.ts +1 -0
- package/src/utils/abi.ts +257 -0
- package/src/utils/accounts.ts +60 -0
- package/src/utils/address.ts +89 -0
- package/src/utils/base58.ts +78 -0
- package/src/utils/base64.ts +200 -0
- package/src/utils/bytes.ts +68 -0
- package/src/utils/code.ts +172 -0
- package/src/utils/crypto.ts +264 -0
- package/src/utils/ethersUtils.ts +60 -0
- package/src/utils/fragments.ts +1738 -0
- package/src/utils/help.ts +59 -0
- package/src/utils/index.ts +42 -0
- package/src/utils/interface.ts +1362 -0
- package/src/utils/message.ts +38 -0
- package/src/utils/transaction.ts +991 -0
- package/src/utils/typedData.ts +688 -0
- package/src/utils/validations.ts +130 -0
@@ -0,0 +1,1767 @@
|
|
1
|
+
import { TronWeb } from '../../tronweb.js';
|
2
|
+
import { AbiCoder, keccak256 } from '../../utils/ethersUtils.js';
|
3
|
+
import { ADDRESS_PREFIX_REGEX, toHex } from '../../utils/address.js';
|
4
|
+
import { encodeParamsV2ByABI } from '../../utils/abi.js';
|
5
|
+
import { Validator } from '../../paramValidator/index.js';
|
6
|
+
import { isArray, isInteger, isNotNullOrUndefined, isObject, isString } from '../../utils/validations.js';
|
7
|
+
import { ContractType, } from '../../types/Contract.js';
|
8
|
+
import { createTransaction, deepCopyJson, fromUtf8, genContractAddress, resultManager, resultManagerTriggerSmartContract, getTransactionOptions, } from './helper.js';
|
9
|
+
export class TransactionBuilder {
|
10
|
+
tronWeb;
|
11
|
+
validator;
|
12
|
+
constructor(tronWeb) {
|
13
|
+
if (!tronWeb || !(tronWeb instanceof TronWeb)) {
|
14
|
+
throw new Error('Expected instance of TronWeb');
|
15
|
+
}
|
16
|
+
this.tronWeb = tronWeb;
|
17
|
+
this.validator = new Validator();
|
18
|
+
}
|
19
|
+
async sendTrx(to, amount = 0, from = this.tronWeb.defaultAddress.hex, options = {}) {
|
20
|
+
// accept amounts passed as strings
|
21
|
+
amount = parseInt(amount);
|
22
|
+
this.validator.notValid([
|
23
|
+
{
|
24
|
+
name: 'recipient',
|
25
|
+
type: 'address',
|
26
|
+
value: to,
|
27
|
+
},
|
28
|
+
{
|
29
|
+
name: 'origin',
|
30
|
+
type: 'address',
|
31
|
+
value: from,
|
32
|
+
},
|
33
|
+
{
|
34
|
+
names: ['recipient', 'origin'],
|
35
|
+
type: 'notEqual',
|
36
|
+
msg: 'Cannot transfer TRX to the same account',
|
37
|
+
},
|
38
|
+
{
|
39
|
+
name: 'amount',
|
40
|
+
type: 'integer',
|
41
|
+
gt: 0,
|
42
|
+
value: amount,
|
43
|
+
},
|
44
|
+
]);
|
45
|
+
const data = {
|
46
|
+
to_address: toHex(to),
|
47
|
+
owner_address: toHex(from),
|
48
|
+
amount: amount,
|
49
|
+
};
|
50
|
+
const transactionOptions = getTransactionOptions(options);
|
51
|
+
return createTransaction(this.tronWeb, ContractType.TransferContract, data, options?.permissionId, transactionOptions);
|
52
|
+
}
|
53
|
+
async sendToken(to, amount = 0, tokenId, from = this.tronWeb.defaultAddress.hex, options = {}) {
|
54
|
+
amount = parseInt(amount);
|
55
|
+
this.validator.notValid([
|
56
|
+
{
|
57
|
+
name: 'recipient',
|
58
|
+
type: 'address',
|
59
|
+
value: to,
|
60
|
+
},
|
61
|
+
{
|
62
|
+
name: 'origin',
|
63
|
+
type: 'address',
|
64
|
+
value: from,
|
65
|
+
},
|
66
|
+
{
|
67
|
+
names: ['recipient', 'origin'],
|
68
|
+
type: 'notEqual',
|
69
|
+
msg: 'Cannot transfer tokens to the same account',
|
70
|
+
},
|
71
|
+
{
|
72
|
+
name: 'amount',
|
73
|
+
type: 'integer',
|
74
|
+
gt: 0,
|
75
|
+
value: amount,
|
76
|
+
},
|
77
|
+
{
|
78
|
+
name: 'token ID',
|
79
|
+
type: 'tokenId',
|
80
|
+
value: tokenId,
|
81
|
+
},
|
82
|
+
]);
|
83
|
+
const data = {
|
84
|
+
to_address: toHex(to),
|
85
|
+
owner_address: toHex(from),
|
86
|
+
asset_name: fromUtf8(tokenId),
|
87
|
+
amount,
|
88
|
+
};
|
89
|
+
const transactionOptions = getTransactionOptions(options);
|
90
|
+
return createTransaction(this.tronWeb, ContractType.TransferAssetContract, data, options?.permissionId, transactionOptions);
|
91
|
+
}
|
92
|
+
async purchaseToken(issuerAddress, tokenId, amount = 0, buyer = this.tronWeb.defaultAddress.hex, options = {}) {
|
93
|
+
this.validator.notValid([
|
94
|
+
{
|
95
|
+
name: 'buyer',
|
96
|
+
type: 'address',
|
97
|
+
value: buyer,
|
98
|
+
},
|
99
|
+
{
|
100
|
+
name: 'issuer',
|
101
|
+
type: 'address',
|
102
|
+
value: issuerAddress,
|
103
|
+
},
|
104
|
+
{
|
105
|
+
names: ['buyer', 'issuer'],
|
106
|
+
type: 'notEqual',
|
107
|
+
msg: 'Cannot purchase tokens from same account',
|
108
|
+
},
|
109
|
+
{
|
110
|
+
name: 'amount',
|
111
|
+
type: 'integer',
|
112
|
+
gt: 0,
|
113
|
+
value: amount,
|
114
|
+
},
|
115
|
+
{
|
116
|
+
name: 'token ID',
|
117
|
+
type: 'tokenId',
|
118
|
+
value: tokenId,
|
119
|
+
},
|
120
|
+
]);
|
121
|
+
const data = {
|
122
|
+
to_address: toHex(issuerAddress),
|
123
|
+
owner_address: toHex(buyer),
|
124
|
+
asset_name: fromUtf8(tokenId),
|
125
|
+
amount: parseInt(amount),
|
126
|
+
};
|
127
|
+
const transactionOptions = getTransactionOptions(options);
|
128
|
+
return createTransaction(this.tronWeb, ContractType.ParticipateAssetIssueContract, data, options?.permissionId, transactionOptions);
|
129
|
+
}
|
130
|
+
async freezeBalance(amount = 0, duration = 3, resource = 'BANDWIDTH', ownerAddress = this.tronWeb.defaultAddress.hex, receiverAddress, options = {}) {
|
131
|
+
this.validator.notValid([
|
132
|
+
{
|
133
|
+
name: 'origin',
|
134
|
+
type: 'address',
|
135
|
+
value: ownerAddress,
|
136
|
+
},
|
137
|
+
{
|
138
|
+
name: 'receiver',
|
139
|
+
type: 'address',
|
140
|
+
value: receiverAddress,
|
141
|
+
optional: true,
|
142
|
+
},
|
143
|
+
{
|
144
|
+
name: 'amount',
|
145
|
+
type: 'integer',
|
146
|
+
gt: 0,
|
147
|
+
value: amount,
|
148
|
+
},
|
149
|
+
{
|
150
|
+
name: 'duration',
|
151
|
+
type: 'integer',
|
152
|
+
gte: 3,
|
153
|
+
value: duration,
|
154
|
+
},
|
155
|
+
{
|
156
|
+
name: 'resource',
|
157
|
+
type: 'resource',
|
158
|
+
value: resource,
|
159
|
+
msg: 'Invalid resource provided: Expected "BANDWIDTH" or "ENERGY"',
|
160
|
+
},
|
161
|
+
]);
|
162
|
+
const data = {
|
163
|
+
owner_address: toHex(ownerAddress),
|
164
|
+
frozen_balance: parseInt(amount),
|
165
|
+
frozen_duration: parseInt(String(duration)),
|
166
|
+
};
|
167
|
+
if (resource !== 'BANDWIDTH') {
|
168
|
+
data.resource = resource;
|
169
|
+
}
|
170
|
+
if (isNotNullOrUndefined(receiverAddress) && toHex(receiverAddress) !== toHex(ownerAddress)) {
|
171
|
+
data.receiver_address = toHex(receiverAddress);
|
172
|
+
}
|
173
|
+
const transactionOptions = getTransactionOptions(options);
|
174
|
+
return createTransaction(this.tronWeb, ContractType.FreezeBalanceContract, data, options?.permissionId, transactionOptions);
|
175
|
+
}
|
176
|
+
async unfreezeBalance(resource = 'BANDWIDTH', address = this.tronWeb.defaultAddress.hex, receiverAddress, options = {}) {
|
177
|
+
this.validator.notValid([
|
178
|
+
{
|
179
|
+
name: 'origin',
|
180
|
+
type: 'address',
|
181
|
+
value: address,
|
182
|
+
},
|
183
|
+
{
|
184
|
+
name: 'receiver',
|
185
|
+
type: 'address',
|
186
|
+
value: receiverAddress,
|
187
|
+
optional: true,
|
188
|
+
},
|
189
|
+
{
|
190
|
+
name: 'resource',
|
191
|
+
type: 'resource',
|
192
|
+
value: resource,
|
193
|
+
msg: 'Invalid resource provided: Expected "BANDWIDTH" or "ENERGY"',
|
194
|
+
},
|
195
|
+
]);
|
196
|
+
const data = {
|
197
|
+
owner_address: toHex(address),
|
198
|
+
};
|
199
|
+
if (resource !== 'BANDWIDTH') {
|
200
|
+
data.resource = resource;
|
201
|
+
}
|
202
|
+
if (isNotNullOrUndefined(receiverAddress) && toHex(receiverAddress) !== toHex(address)) {
|
203
|
+
data.receiver_address = toHex(receiverAddress);
|
204
|
+
}
|
205
|
+
const transactionOptions = getTransactionOptions(options);
|
206
|
+
return createTransaction(this.tronWeb, ContractType.UnfreezeBalanceContract, data, options?.permissionId, transactionOptions);
|
207
|
+
}
|
208
|
+
async freezeBalanceV2(amount = 0, resource = 'BANDWIDTH', address = this.tronWeb.defaultAddress.hex, options = {}) {
|
209
|
+
this.validator.notValid([
|
210
|
+
{
|
211
|
+
name: 'origin',
|
212
|
+
type: 'address',
|
213
|
+
value: address,
|
214
|
+
},
|
215
|
+
{
|
216
|
+
name: 'amount',
|
217
|
+
type: 'integer',
|
218
|
+
gt: 0,
|
219
|
+
value: amount,
|
220
|
+
},
|
221
|
+
{
|
222
|
+
name: 'resource',
|
223
|
+
type: 'resource',
|
224
|
+
value: resource,
|
225
|
+
msg: 'Invalid resource provided: Expected "BANDWIDTH" or "ENERGY"',
|
226
|
+
},
|
227
|
+
]);
|
228
|
+
const data = {
|
229
|
+
owner_address: toHex(address),
|
230
|
+
frozen_balance: parseInt(amount),
|
231
|
+
};
|
232
|
+
if (resource !== 'BANDWIDTH') {
|
233
|
+
data.resource = resource;
|
234
|
+
}
|
235
|
+
const transactionOptions = getTransactionOptions(options);
|
236
|
+
return createTransaction(this.tronWeb, ContractType.FreezeBalanceV2Contract, data, options?.permissionId, transactionOptions);
|
237
|
+
}
|
238
|
+
async unfreezeBalanceV2(amount = 0, resource = 'BANDWIDTH', address = this.tronWeb.defaultAddress.hex, options = {}) {
|
239
|
+
this.validator.notValid([
|
240
|
+
{
|
241
|
+
name: 'origin',
|
242
|
+
type: 'address',
|
243
|
+
value: address,
|
244
|
+
},
|
245
|
+
{
|
246
|
+
name: 'amount',
|
247
|
+
type: 'integer',
|
248
|
+
gt: 0,
|
249
|
+
value: amount,
|
250
|
+
},
|
251
|
+
{
|
252
|
+
name: 'resource',
|
253
|
+
type: 'resource',
|
254
|
+
value: resource,
|
255
|
+
msg: 'Invalid resource provided: Expected "BANDWIDTH" or "ENERGY"',
|
256
|
+
},
|
257
|
+
]);
|
258
|
+
const data = {
|
259
|
+
owner_address: toHex(address),
|
260
|
+
unfreeze_balance: parseInt(amount),
|
261
|
+
};
|
262
|
+
if (resource !== 'BANDWIDTH') {
|
263
|
+
data.resource = resource;
|
264
|
+
}
|
265
|
+
const transactionOptions = getTransactionOptions(options);
|
266
|
+
return createTransaction(this.tronWeb, ContractType.UnfreezeBalanceV2Contract, data, options?.permissionId, transactionOptions);
|
267
|
+
}
|
268
|
+
async cancelUnfreezeBalanceV2(address = this.tronWeb.defaultAddress.hex, options = {}) {
|
269
|
+
this.validator.notValid([
|
270
|
+
{
|
271
|
+
name: 'origin',
|
272
|
+
type: 'address',
|
273
|
+
value: address,
|
274
|
+
},
|
275
|
+
]);
|
276
|
+
const data = {
|
277
|
+
owner_address: toHex(address),
|
278
|
+
};
|
279
|
+
const transactionOptions = getTransactionOptions(options);
|
280
|
+
return createTransaction(this.tronWeb, ContractType.CancelAllUnfreezeV2Contract, data, options?.permissionId, transactionOptions);
|
281
|
+
}
|
282
|
+
async delegateResource(amount = 0, receiverAddress, resource = 'BANDWIDTH', address = this.tronWeb.defaultAddress.hex, lock = false, lockPeriod, options = {}) {
|
283
|
+
this.validator.notValid([
|
284
|
+
{
|
285
|
+
name: 'amount',
|
286
|
+
type: 'integer',
|
287
|
+
gt: 0,
|
288
|
+
value: amount,
|
289
|
+
},
|
290
|
+
{
|
291
|
+
name: 'resource',
|
292
|
+
type: 'resource',
|
293
|
+
value: resource,
|
294
|
+
msg: 'Invalid resource provided: Expected "BANDWIDTH" or "ENERGY"',
|
295
|
+
},
|
296
|
+
{
|
297
|
+
name: 'receiver',
|
298
|
+
type: 'address',
|
299
|
+
value: receiverAddress,
|
300
|
+
},
|
301
|
+
{
|
302
|
+
name: 'origin',
|
303
|
+
type: 'address',
|
304
|
+
value: address,
|
305
|
+
},
|
306
|
+
{
|
307
|
+
name: 'lock',
|
308
|
+
type: 'boolean',
|
309
|
+
value: lock,
|
310
|
+
},
|
311
|
+
{
|
312
|
+
name: 'lock period',
|
313
|
+
type: 'integer',
|
314
|
+
gte: 0,
|
315
|
+
value: lockPeriod,
|
316
|
+
optional: true,
|
317
|
+
},
|
318
|
+
]);
|
319
|
+
if (toHex(receiverAddress) === toHex(address)) {
|
320
|
+
throw new Error('Receiver address must not be the same as owner address');
|
321
|
+
}
|
322
|
+
const data = {
|
323
|
+
owner_address: toHex(address),
|
324
|
+
receiver_address: toHex(receiverAddress),
|
325
|
+
balance: parseInt(amount),
|
326
|
+
};
|
327
|
+
if (resource !== 'BANDWIDTH') {
|
328
|
+
data.resource = resource;
|
329
|
+
}
|
330
|
+
if (lock) {
|
331
|
+
data.lock = lock;
|
332
|
+
if (isNotNullOrUndefined(lockPeriod)) {
|
333
|
+
data.lock_period = lockPeriod;
|
334
|
+
}
|
335
|
+
}
|
336
|
+
const transactionOptions = getTransactionOptions(options);
|
337
|
+
return createTransaction(this.tronWeb, ContractType.DelegateResourceContract, data, options?.permissionId, transactionOptions);
|
338
|
+
}
|
339
|
+
async undelegateResource(amount = 0, receiverAddress, resource = 'BANDWIDTH', address = this.tronWeb.defaultAddress.hex, options = {}) {
|
340
|
+
this.validator.notValid([
|
341
|
+
{
|
342
|
+
name: 'origin',
|
343
|
+
type: 'address',
|
344
|
+
value: address,
|
345
|
+
},
|
346
|
+
{
|
347
|
+
name: 'receiver',
|
348
|
+
type: 'address',
|
349
|
+
value: receiverAddress,
|
350
|
+
},
|
351
|
+
{
|
352
|
+
name: 'amount',
|
353
|
+
type: 'integer',
|
354
|
+
gt: 0,
|
355
|
+
value: amount,
|
356
|
+
},
|
357
|
+
{
|
358
|
+
name: 'resource',
|
359
|
+
type: 'resource',
|
360
|
+
value: resource,
|
361
|
+
msg: 'Invalid resource provided: Expected "BANDWIDTH" or "ENERGY"',
|
362
|
+
},
|
363
|
+
]);
|
364
|
+
if (toHex(receiverAddress) === toHex(address)) {
|
365
|
+
throw new Error('Receiver address must not be the same as owner address');
|
366
|
+
}
|
367
|
+
const data = {
|
368
|
+
owner_address: toHex(address),
|
369
|
+
receiver_address: toHex(receiverAddress),
|
370
|
+
balance: parseInt(amount),
|
371
|
+
};
|
372
|
+
if (resource !== 'BANDWIDTH') {
|
373
|
+
data.resource = resource;
|
374
|
+
}
|
375
|
+
const transactionOptions = getTransactionOptions(options);
|
376
|
+
return createTransaction(this.tronWeb, ContractType.UnDelegateResourceContract, data, options?.permissionId, transactionOptions);
|
377
|
+
}
|
378
|
+
async withdrawExpireUnfreeze(address = this.tronWeb.defaultAddress.hex, options = {}) {
|
379
|
+
this.validator.notValid([
|
380
|
+
{
|
381
|
+
name: 'origin',
|
382
|
+
type: 'address',
|
383
|
+
value: address,
|
384
|
+
},
|
385
|
+
]);
|
386
|
+
const data = {
|
387
|
+
owner_address: toHex(address),
|
388
|
+
};
|
389
|
+
const transactionOptions = getTransactionOptions(options);
|
390
|
+
return createTransaction(this.tronWeb, ContractType.WithdrawExpireUnfreezeContract, data, options?.permissionId, transactionOptions);
|
391
|
+
}
|
392
|
+
async withdrawBlockRewards(address = this.tronWeb.defaultAddress.hex, options = {}) {
|
393
|
+
this.validator.notValid([
|
394
|
+
{
|
395
|
+
name: 'origin',
|
396
|
+
type: 'address',
|
397
|
+
value: address,
|
398
|
+
},
|
399
|
+
]);
|
400
|
+
const data = {
|
401
|
+
owner_address: toHex(address),
|
402
|
+
};
|
403
|
+
const transactionOptions = getTransactionOptions(options);
|
404
|
+
return createTransaction(this.tronWeb, ContractType.WithdrawBalanceContract, data, options?.permissionId, transactionOptions);
|
405
|
+
}
|
406
|
+
async applyForSR(address = this.tronWeb.defaultAddress.hex, url = '', options = {}) {
|
407
|
+
this.validator.notValid([
|
408
|
+
{
|
409
|
+
name: 'origin',
|
410
|
+
type: 'address',
|
411
|
+
value: address,
|
412
|
+
},
|
413
|
+
{
|
414
|
+
name: 'url',
|
415
|
+
type: 'url',
|
416
|
+
value: url,
|
417
|
+
msg: 'Invalid url provided',
|
418
|
+
},
|
419
|
+
{
|
420
|
+
name: 'url',
|
421
|
+
type: 'string',
|
422
|
+
value: url,
|
423
|
+
lte: 256,
|
424
|
+
msg: 'Invalid url provided',
|
425
|
+
},
|
426
|
+
]);
|
427
|
+
const data = {
|
428
|
+
owner_address: toHex(address),
|
429
|
+
url: fromUtf8(url),
|
430
|
+
};
|
431
|
+
const transactionOptions = getTransactionOptions(options);
|
432
|
+
return createTransaction(this.tronWeb, ContractType.WitnessCreateContract, data, options?.permissionId, transactionOptions);
|
433
|
+
}
|
434
|
+
async vote(votes = {}, voterAddress = this.tronWeb.defaultAddress.hex, options = {}) {
|
435
|
+
this.validator.notValid([
|
436
|
+
{
|
437
|
+
name: 'voter',
|
438
|
+
type: 'address',
|
439
|
+
value: voterAddress,
|
440
|
+
},
|
441
|
+
{
|
442
|
+
name: 'votes',
|
443
|
+
type: 'notEmptyObject',
|
444
|
+
value: votes,
|
445
|
+
},
|
446
|
+
]);
|
447
|
+
const entries = Object.entries(votes);
|
448
|
+
for (const [srAddress, voteCount] of entries) {
|
449
|
+
this.validator.notValid([
|
450
|
+
{
|
451
|
+
name: 'SR',
|
452
|
+
type: 'address',
|
453
|
+
value: srAddress,
|
454
|
+
},
|
455
|
+
{
|
456
|
+
name: 'vote count',
|
457
|
+
type: 'integer',
|
458
|
+
gt: 0,
|
459
|
+
value: voteCount,
|
460
|
+
msg: 'Invalid vote count provided for SR: ' + srAddress,
|
461
|
+
},
|
462
|
+
]);
|
463
|
+
}
|
464
|
+
const voteList = entries.map(([srAddress, voteCount]) => {
|
465
|
+
return {
|
466
|
+
vote_address: toHex(srAddress),
|
467
|
+
vote_count: parseInt(voteCount),
|
468
|
+
};
|
469
|
+
});
|
470
|
+
const data = {
|
471
|
+
owner_address: toHex(voterAddress),
|
472
|
+
votes: voteList,
|
473
|
+
};
|
474
|
+
const transactionOptions = getTransactionOptions(options);
|
475
|
+
return createTransaction(this.tronWeb, ContractType.VoteWitnessContract, data, options?.permissionId, transactionOptions);
|
476
|
+
}
|
477
|
+
async createSmartContract(options = {}, issuerAddress = this.tronWeb.defaultAddress.hex) {
|
478
|
+
const feeLimit = options.feeLimit || this.tronWeb.feeLimit;
|
479
|
+
let userFeePercentage = options.userFeePercentage;
|
480
|
+
if (typeof userFeePercentage !== 'number' && !userFeePercentage) {
|
481
|
+
userFeePercentage = 100;
|
482
|
+
}
|
483
|
+
const originEnergyLimit = options.originEnergyLimit || 10_000_000;
|
484
|
+
const callValue = options.callValue || 0;
|
485
|
+
const tokenValue = options.tokenValue;
|
486
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
487
|
+
// @ts-ignore
|
488
|
+
const tokenId = options.tokenId || options.token_id;
|
489
|
+
let { abi } = options;
|
490
|
+
const { parameters = [] } = options;
|
491
|
+
let parameter = '';
|
492
|
+
const { bytecode = false, name = '' } = options;
|
493
|
+
if (abi && isString(abi)) {
|
494
|
+
try {
|
495
|
+
abi = JSON.parse(abi);
|
496
|
+
}
|
497
|
+
catch {
|
498
|
+
throw new Error('Invalid options.abi provided');
|
499
|
+
}
|
500
|
+
}
|
501
|
+
const newAbi = abi;
|
502
|
+
let entries = newAbi;
|
503
|
+
if (newAbi.entrys) {
|
504
|
+
entries = newAbi.entrys;
|
505
|
+
}
|
506
|
+
if (!isArray(entries))
|
507
|
+
throw new Error('Invalid options.abi provided');
|
508
|
+
const payable = entries.some((func) => {
|
509
|
+
return func.type === 'constructor' && 'payable' === func.stateMutability.toLowerCase();
|
510
|
+
});
|
511
|
+
this.validator.notValid([
|
512
|
+
{
|
513
|
+
name: 'bytecode',
|
514
|
+
type: 'hex',
|
515
|
+
value: bytecode,
|
516
|
+
},
|
517
|
+
{
|
518
|
+
name: 'feeLimit',
|
519
|
+
type: 'integer',
|
520
|
+
value: feeLimit,
|
521
|
+
gt: 0,
|
522
|
+
},
|
523
|
+
{
|
524
|
+
name: 'callValue',
|
525
|
+
type: 'integer',
|
526
|
+
value: callValue,
|
527
|
+
gte: 0,
|
528
|
+
},
|
529
|
+
{
|
530
|
+
name: 'userFeePercentage',
|
531
|
+
type: 'integer',
|
532
|
+
value: userFeePercentage,
|
533
|
+
gte: 0,
|
534
|
+
lte: 100,
|
535
|
+
},
|
536
|
+
{
|
537
|
+
name: 'originEnergyLimit',
|
538
|
+
type: 'integer',
|
539
|
+
value: originEnergyLimit,
|
540
|
+
gte: 0,
|
541
|
+
lte: 10_000_000,
|
542
|
+
},
|
543
|
+
{
|
544
|
+
name: 'parameters',
|
545
|
+
type: 'array',
|
546
|
+
value: parameters,
|
547
|
+
},
|
548
|
+
{
|
549
|
+
name: 'issuer',
|
550
|
+
type: 'address',
|
551
|
+
value: issuerAddress,
|
552
|
+
},
|
553
|
+
{
|
554
|
+
name: 'tokenValue',
|
555
|
+
type: 'integer',
|
556
|
+
value: tokenValue,
|
557
|
+
gte: 0,
|
558
|
+
optional: true,
|
559
|
+
},
|
560
|
+
{
|
561
|
+
name: 'tokenId',
|
562
|
+
type: 'integer',
|
563
|
+
value: tokenId,
|
564
|
+
gte: 0,
|
565
|
+
optional: true,
|
566
|
+
},
|
567
|
+
]);
|
568
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
569
|
+
// @ts-ignore
|
570
|
+
if (!payable && (callValue > 0 || tokenValue > 0))
|
571
|
+
throw new Error('When contract is not payable, options.callValue and options.tokenValue must be 0');
|
572
|
+
const { rawParameter, funcABIV2, parametersV2 } = options;
|
573
|
+
if (rawParameter && isString(rawParameter)) {
|
574
|
+
parameter = rawParameter.replace(/^(0x)/, '');
|
575
|
+
}
|
576
|
+
else if (funcABIV2) {
|
577
|
+
parameter = encodeParamsV2ByABI(funcABIV2, parametersV2).replace(/^(0x)/, '');
|
578
|
+
}
|
579
|
+
else {
|
580
|
+
let constructorParams = entries.find((it) => {
|
581
|
+
return it.type === 'constructor';
|
582
|
+
});
|
583
|
+
if (typeof constructorParams !== 'undefined' && constructorParams) {
|
584
|
+
const abiCoder = new AbiCoder();
|
585
|
+
const types = [];
|
586
|
+
const values = [];
|
587
|
+
constructorParams = constructorParams.inputs;
|
588
|
+
if (parameters.length != constructorParams.length)
|
589
|
+
throw new Error(`constructor needs ${constructorParams.length} but ${parameters.length} provided`);
|
590
|
+
for (let i = 0; i < parameters.length; i++) {
|
591
|
+
let type = constructorParams[i].type;
|
592
|
+
let value = parameters[i];
|
593
|
+
if (!type || !isString(type) || !type.length)
|
594
|
+
throw new Error('Invalid parameter type provided: ' + type);
|
595
|
+
const replaceAddressPrefix = (value) => {
|
596
|
+
if (isArray(value)) {
|
597
|
+
return value.map((v) => replaceAddressPrefix(v));
|
598
|
+
}
|
599
|
+
return toHex(value).replace(ADDRESS_PREFIX_REGEX, '0x');
|
600
|
+
};
|
601
|
+
if (type === 'address')
|
602
|
+
value = replaceAddressPrefix(value);
|
603
|
+
else if (type.match(/^([^\x5b]*)(\x5b|$)/)?.[0] === 'address[')
|
604
|
+
value = replaceAddressPrefix(value);
|
605
|
+
else if (/trcToken/.test(type)) {
|
606
|
+
type = type.replace(/trcToken/, 'uint256');
|
607
|
+
}
|
608
|
+
types.push(type);
|
609
|
+
values.push(value);
|
610
|
+
}
|
611
|
+
try {
|
612
|
+
parameter = abiCoder.encode(types, values).replace(/^(0x)/, '');
|
613
|
+
}
|
614
|
+
catch (ex) {
|
615
|
+
throw new Error(ex);
|
616
|
+
}
|
617
|
+
}
|
618
|
+
else {
|
619
|
+
parameter = '';
|
620
|
+
}
|
621
|
+
}
|
622
|
+
const args = {
|
623
|
+
owner_address: toHex(issuerAddress),
|
624
|
+
fee_limit: parseInt(feeLimit),
|
625
|
+
call_value: parseInt(callValue),
|
626
|
+
consume_user_resource_percent: userFeePercentage,
|
627
|
+
origin_energy_limit: originEnergyLimit,
|
628
|
+
abi: JSON.stringify(abi),
|
629
|
+
bytecode,
|
630
|
+
parameter,
|
631
|
+
name,
|
632
|
+
};
|
633
|
+
// tokenValue and tokenId can cause errors if provided when the trx10 proposal has not been approved yet. So we set them only if they are passed to the method.
|
634
|
+
if (isNotNullOrUndefined(tokenValue)) {
|
635
|
+
args.call_token_value = parseInt(tokenValue);
|
636
|
+
}
|
637
|
+
if (isNotNullOrUndefined(tokenId)) {
|
638
|
+
args.token_id = parseInt(tokenId);
|
639
|
+
}
|
640
|
+
const contract = {};
|
641
|
+
contract.owner_address = args.owner_address;
|
642
|
+
if (isNotNullOrUndefined(args.call_token_value)) {
|
643
|
+
contract.call_token_value = args.call_token_value;
|
644
|
+
}
|
645
|
+
if (isNotNullOrUndefined(args.token_id)) {
|
646
|
+
contract.token_id = args.token_id;
|
647
|
+
}
|
648
|
+
const new_contract = (contract.new_contract = {});
|
649
|
+
if (args.abi) {
|
650
|
+
new_contract.abi = {
|
651
|
+
entrys: JSON.parse(args.abi),
|
652
|
+
};
|
653
|
+
}
|
654
|
+
else {
|
655
|
+
new_contract.abi = {};
|
656
|
+
}
|
657
|
+
if (args.call_value) {
|
658
|
+
new_contract.call_value = args.call_value;
|
659
|
+
}
|
660
|
+
new_contract.consume_user_resource_percent = args.consume_user_resource_percent;
|
661
|
+
new_contract.origin_energy_limit = args.origin_energy_limit;
|
662
|
+
new_contract.origin_address = args.origin_address ?? args.owner_address;
|
663
|
+
if (args.bytecode + args.parameter) {
|
664
|
+
new_contract.bytecode = (args.bytecode + args.parameter).replace(/^0x/, '');
|
665
|
+
}
|
666
|
+
if (isNotNullOrUndefined(args.name)) {
|
667
|
+
new_contract.name = args.name;
|
668
|
+
}
|
669
|
+
const transactionOptions = getTransactionOptions(options);
|
670
|
+
const tx = (await createTransaction(this.tronWeb, ContractType.CreateSmartContract, contract, options?.permissionId, {
|
671
|
+
...transactionOptions,
|
672
|
+
fee_limit: args.fee_limit,
|
673
|
+
}));
|
674
|
+
tx.contract_address = genContractAddress(args.owner_address, tx.txID);
|
675
|
+
return tx;
|
676
|
+
}
|
677
|
+
async triggerSmartContract(contractAddress, functionSelector, options, parameters, issuerAddress) {
|
678
|
+
const params = [
|
679
|
+
contractAddress,
|
680
|
+
functionSelector,
|
681
|
+
options,
|
682
|
+
parameters,
|
683
|
+
issuerAddress,
|
684
|
+
];
|
685
|
+
if (typeof params[2] !== 'object') {
|
686
|
+
params[2] = {
|
687
|
+
feeLimit: params[2],
|
688
|
+
callValue: params[3],
|
689
|
+
};
|
690
|
+
params.splice(3, 1);
|
691
|
+
}
|
692
|
+
if (params[2]?.txLocal) {
|
693
|
+
return this._triggerSmartContractLocal(...params);
|
694
|
+
}
|
695
|
+
return this._triggerSmartContract(...params);
|
696
|
+
}
|
697
|
+
async triggerConstantContract(contractAddress, functionSelector, options = {}, parameters = [], issuerAddress = this.tronWeb.defaultAddress.hex) {
|
698
|
+
options._isConstant = true;
|
699
|
+
return this._triggerSmartContract(contractAddress, functionSelector, options, parameters, issuerAddress);
|
700
|
+
}
|
701
|
+
async triggerConfirmedConstantContract(contractAddress, functionSelector, options = {}, parameters = [], issuerAddress = this.tronWeb.defaultAddress.hex) {
|
702
|
+
options._isConstant = true;
|
703
|
+
options.confirmed = true;
|
704
|
+
return this._triggerSmartContract(contractAddress, functionSelector, options, parameters, issuerAddress);
|
705
|
+
}
|
706
|
+
async estimateEnergy(contractAddress, functionSelector, options = {}, parameters = [], issuerAddress = this.tronWeb.defaultAddress.hex) {
|
707
|
+
options.estimateEnergy = true;
|
708
|
+
const result = await this._triggerSmartContract(contractAddress, functionSelector, options, parameters, issuerAddress);
|
709
|
+
return result;
|
710
|
+
}
|
711
|
+
async deployConstantContract(options = { input: '', ownerAddress: '' }) {
|
712
|
+
const { input, ownerAddress, tokenId, tokenValue, callValue = 0 } = options;
|
713
|
+
this.validator.notValid([
|
714
|
+
{
|
715
|
+
name: 'input',
|
716
|
+
type: 'not-empty-string',
|
717
|
+
value: input,
|
718
|
+
},
|
719
|
+
{
|
720
|
+
name: 'callValue',
|
721
|
+
type: 'integer',
|
722
|
+
value: callValue,
|
723
|
+
gte: 0,
|
724
|
+
},
|
725
|
+
{
|
726
|
+
name: 'owner',
|
727
|
+
type: 'address',
|
728
|
+
value: ownerAddress,
|
729
|
+
},
|
730
|
+
{
|
731
|
+
name: 'tokenValue',
|
732
|
+
type: 'integer',
|
733
|
+
value: tokenValue,
|
734
|
+
gte: 0,
|
735
|
+
optional: true,
|
736
|
+
},
|
737
|
+
{
|
738
|
+
name: 'tokenId',
|
739
|
+
type: 'integer',
|
740
|
+
value: tokenId,
|
741
|
+
gte: 0,
|
742
|
+
optional: true,
|
743
|
+
},
|
744
|
+
]);
|
745
|
+
const args = {
|
746
|
+
data: input,
|
747
|
+
owner_address: toHex(ownerAddress),
|
748
|
+
call_value: callValue,
|
749
|
+
};
|
750
|
+
if (tokenId) {
|
751
|
+
args.token_id = tokenId;
|
752
|
+
}
|
753
|
+
if (tokenValue) {
|
754
|
+
args.call_token_value = tokenValue;
|
755
|
+
}
|
756
|
+
const pathInfo = `wallet${options.confirmed ? 'solidity' : ''}/estimateenergy`;
|
757
|
+
const transaction = await this.tronWeb[options.confirmed ? 'solidityNode' : 'fullNode'].request(pathInfo, args, 'post');
|
758
|
+
if (transaction.Error)
|
759
|
+
throw new Error(transaction.Error);
|
760
|
+
if (transaction.result && transaction.result.message) {
|
761
|
+
throw new Error(this.tronWeb.toUtf8(transaction.result.message));
|
762
|
+
}
|
763
|
+
return transaction;
|
764
|
+
}
|
765
|
+
_getTriggerSmartContractArgs(contractAddress, functionSelector, options, parameters, issuerAddress, tokenValue, tokenId, callValue, feeLimit) {
|
766
|
+
const args = {
|
767
|
+
contract_address: toHex(contractAddress),
|
768
|
+
owner_address: toHex(issuerAddress),
|
769
|
+
};
|
770
|
+
if (functionSelector && isString(functionSelector)) {
|
771
|
+
functionSelector = functionSelector.replace(/\s*/g, '');
|
772
|
+
let parameterStr;
|
773
|
+
if (parameters.length) {
|
774
|
+
const abiCoder = new AbiCoder();
|
775
|
+
let types = [];
|
776
|
+
const values = [];
|
777
|
+
for (let i = 0; i < parameters.length; i++) {
|
778
|
+
let { value } = parameters[i];
|
779
|
+
const { type } = parameters[i];
|
780
|
+
if (!type || !isString(type) || !type.length)
|
781
|
+
throw new Error('Invalid parameter type provided: ' + type);
|
782
|
+
const replaceAddressPrefix = (value) => {
|
783
|
+
if (isArray(value)) {
|
784
|
+
return value.map((v) => replaceAddressPrefix(v));
|
785
|
+
}
|
786
|
+
return toHex(value).replace(ADDRESS_PREFIX_REGEX, '0x');
|
787
|
+
};
|
788
|
+
if (type === 'address')
|
789
|
+
value = replaceAddressPrefix(value);
|
790
|
+
else if (type.match(/^([^\x5b]*)(\x5b|$)/)?.[0] === 'address[')
|
791
|
+
value = replaceAddressPrefix(value);
|
792
|
+
types.push(type);
|
793
|
+
values.push(value);
|
794
|
+
}
|
795
|
+
try {
|
796
|
+
// workaround for unsupported trcToken type
|
797
|
+
types = types.map((type) => {
|
798
|
+
if (/trcToken/.test(type)) {
|
799
|
+
type = type.replace(/trcToken/, 'uint256');
|
800
|
+
}
|
801
|
+
return type;
|
802
|
+
});
|
803
|
+
parameterStr = abiCoder.encode(types, values).replace(/^(0x)/, '');
|
804
|
+
}
|
805
|
+
catch (ex) {
|
806
|
+
throw new Error(ex);
|
807
|
+
}
|
808
|
+
}
|
809
|
+
else
|
810
|
+
parameterStr = '';
|
811
|
+
// work for abiv2 if passed the function abi in options
|
812
|
+
if (options.funcABIV2) {
|
813
|
+
parameterStr = encodeParamsV2ByABI(options.funcABIV2, options.parametersV2).replace(/^(0x)/, '');
|
814
|
+
}
|
815
|
+
if (options.shieldedParameter && isString(options.shieldedParameter)) {
|
816
|
+
parameterStr = options.shieldedParameter.replace(/^(0x)/, '');
|
817
|
+
}
|
818
|
+
if (options.rawParameter && isString(options.rawParameter)) {
|
819
|
+
parameterStr = options.rawParameter.replace(/^(0x)/, '');
|
820
|
+
}
|
821
|
+
args.function_selector = functionSelector;
|
822
|
+
args.parameter = parameterStr;
|
823
|
+
}
|
824
|
+
else if (options.input) {
|
825
|
+
args.data = options.input;
|
826
|
+
}
|
827
|
+
args.call_value = parseInt(callValue);
|
828
|
+
if (isNotNullOrUndefined(tokenValue))
|
829
|
+
args.call_token_value = parseInt(tokenValue);
|
830
|
+
if (isNotNullOrUndefined(tokenId))
|
831
|
+
args.token_id = parseInt(tokenId);
|
832
|
+
if (!(options._isConstant || options.estimateEnergy)) {
|
833
|
+
args.fee_limit = parseInt(feeLimit);
|
834
|
+
}
|
835
|
+
if (options.permissionId) {
|
836
|
+
args.Permission_id = options.permissionId;
|
837
|
+
}
|
838
|
+
return args;
|
839
|
+
}
|
840
|
+
async _triggerSmartContractLocal(contractAddress, functionSelector, options = {}, parameters = [], issuerAddress = this.tronWeb.defaultAddress.hex) {
|
841
|
+
const { tokenValue, tokenId, callValue, feeLimit } = Object.assign({
|
842
|
+
callValue: 0,
|
843
|
+
feeLimit: this.tronWeb.feeLimit,
|
844
|
+
}, options);
|
845
|
+
this.validator.notValid([
|
846
|
+
{
|
847
|
+
name: 'feeLimit',
|
848
|
+
type: 'integer',
|
849
|
+
value: feeLimit,
|
850
|
+
gt: 0,
|
851
|
+
},
|
852
|
+
{
|
853
|
+
name: 'callValue',
|
854
|
+
type: 'integer',
|
855
|
+
value: callValue,
|
856
|
+
gte: 0,
|
857
|
+
},
|
858
|
+
{
|
859
|
+
name: 'parameters',
|
860
|
+
type: 'array',
|
861
|
+
value: parameters,
|
862
|
+
},
|
863
|
+
{
|
864
|
+
name: 'contract',
|
865
|
+
type: 'address',
|
866
|
+
value: contractAddress,
|
867
|
+
},
|
868
|
+
{
|
869
|
+
name: 'issuer',
|
870
|
+
type: 'address',
|
871
|
+
value: issuerAddress,
|
872
|
+
optional: true,
|
873
|
+
},
|
874
|
+
{
|
875
|
+
name: 'tokenValue',
|
876
|
+
type: 'integer',
|
877
|
+
value: tokenValue,
|
878
|
+
gte: 0,
|
879
|
+
optional: true,
|
880
|
+
},
|
881
|
+
{
|
882
|
+
name: 'tokenId',
|
883
|
+
type: 'integer',
|
884
|
+
value: tokenId,
|
885
|
+
gte: 0,
|
886
|
+
optional: true,
|
887
|
+
},
|
888
|
+
]);
|
889
|
+
const args = this._getTriggerSmartContractArgs(contractAddress, functionSelector, options, parameters, issuerAddress, tokenValue, tokenId, callValue, feeLimit);
|
890
|
+
if (args.function_selector) {
|
891
|
+
args.data = keccak256(Buffer.from(args.function_selector, 'utf-8')).toString().substring(2, 10) + args.parameter;
|
892
|
+
}
|
893
|
+
const value = {
|
894
|
+
data: args.data,
|
895
|
+
owner_address: args.owner_address,
|
896
|
+
contract_address: args.contract_address,
|
897
|
+
};
|
898
|
+
if (args.call_value) {
|
899
|
+
value.call_value = args.call_value;
|
900
|
+
}
|
901
|
+
if (args.call_token_value) {
|
902
|
+
value.call_token_value = args.call_token_value;
|
903
|
+
}
|
904
|
+
if (args.token_id) {
|
905
|
+
value.token_id = args.token_id;
|
906
|
+
}
|
907
|
+
const transactionOptions = getTransactionOptions(options);
|
908
|
+
const transaction = await createTransaction(this.tronWeb, ContractType.TriggerSmartContract, value, options.permissionId, {
|
909
|
+
...transactionOptions,
|
910
|
+
fee_limit: args.fee_limit,
|
911
|
+
});
|
912
|
+
return {
|
913
|
+
result: {
|
914
|
+
result: true,
|
915
|
+
},
|
916
|
+
transaction,
|
917
|
+
};
|
918
|
+
}
|
919
|
+
async _triggerSmartContract(contractAddress, functionSelector, options = {}, parameters = [], issuerAddress = this.tronWeb.defaultAddress.hex) {
|
920
|
+
const { tokenValue, tokenId, callValue, feeLimit } = Object.assign({
|
921
|
+
callValue: 0,
|
922
|
+
feeLimit: this.tronWeb.feeLimit,
|
923
|
+
}, options);
|
924
|
+
this.validator.notValid([
|
925
|
+
{
|
926
|
+
name: 'feeLimit',
|
927
|
+
type: 'integer',
|
928
|
+
value: feeLimit,
|
929
|
+
gt: 0,
|
930
|
+
},
|
931
|
+
{
|
932
|
+
name: 'callValue',
|
933
|
+
type: 'integer',
|
934
|
+
value: callValue,
|
935
|
+
gte: 0,
|
936
|
+
},
|
937
|
+
{
|
938
|
+
name: 'parameters',
|
939
|
+
type: 'array',
|
940
|
+
value: parameters,
|
941
|
+
},
|
942
|
+
{
|
943
|
+
name: 'contract',
|
944
|
+
type: 'address',
|
945
|
+
value: contractAddress,
|
946
|
+
},
|
947
|
+
{
|
948
|
+
name: 'issuer',
|
949
|
+
type: 'address',
|
950
|
+
value: issuerAddress,
|
951
|
+
optional: true,
|
952
|
+
},
|
953
|
+
{
|
954
|
+
name: 'tokenValue',
|
955
|
+
type: 'integer',
|
956
|
+
value: tokenValue,
|
957
|
+
gte: 0,
|
958
|
+
optional: true,
|
959
|
+
},
|
960
|
+
{
|
961
|
+
name: 'tokenId',
|
962
|
+
type: 'integer',
|
963
|
+
value: tokenId,
|
964
|
+
gte: 0,
|
965
|
+
optional: true,
|
966
|
+
},
|
967
|
+
]);
|
968
|
+
const args = this._getTriggerSmartContractArgs(contractAddress, functionSelector, options, parameters, issuerAddress, tokenValue, tokenId, callValue, feeLimit);
|
969
|
+
let pathInfo = 'triggersmartcontract';
|
970
|
+
if (options._isConstant) {
|
971
|
+
pathInfo = 'triggerconstantcontract';
|
972
|
+
}
|
973
|
+
else if (options.estimateEnergy) {
|
974
|
+
pathInfo = 'estimateenergy';
|
975
|
+
}
|
976
|
+
pathInfo = `wallet${options.confirmed ? 'solidity' : ''}/${pathInfo}`;
|
977
|
+
const transaction = await this.tronWeb[options.confirmed ? 'solidityNode' : 'fullNode'].request(pathInfo, args, 'post');
|
978
|
+
return resultManagerTriggerSmartContract(transaction, args, options);
|
979
|
+
}
|
980
|
+
async clearABI(contractAddress, ownerAddress = this.tronWeb.defaultAddress.hex, options = {}) {
|
981
|
+
if (!TronWeb.isAddress(contractAddress))
|
982
|
+
throw new Error('Invalid contract address provided');
|
983
|
+
if (!TronWeb.isAddress(ownerAddress))
|
984
|
+
throw new Error('Invalid owner address provided');
|
985
|
+
const data = {
|
986
|
+
contract_address: toHex(contractAddress),
|
987
|
+
owner_address: toHex(ownerAddress),
|
988
|
+
};
|
989
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
990
|
+
// @ts-ignore
|
991
|
+
if (this.tronWeb.trx.cache.contracts[contractAddress]) {
|
992
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
993
|
+
// @ts-ignore
|
994
|
+
delete this.tronWeb.trx.cache.contracts[contractAddress];
|
995
|
+
}
|
996
|
+
const transactionOptions = getTransactionOptions(options);
|
997
|
+
return createTransaction(this.tronWeb, ContractType.ClearABIContract, data, options?.permissionId, transactionOptions);
|
998
|
+
}
|
999
|
+
async updateBrokerage(brokerage, ownerAddress = this.tronWeb.defaultAddress.hex, options = {}) {
|
1000
|
+
if (!isNotNullOrUndefined(brokerage))
|
1001
|
+
throw new Error('Invalid brokerage provided');
|
1002
|
+
if (!isInteger(brokerage) || brokerage < 0 || brokerage > 100)
|
1003
|
+
throw new Error('Brokerage must be an integer between 0 and 100');
|
1004
|
+
if (!TronWeb.isAddress(ownerAddress))
|
1005
|
+
throw new Error('Invalid owner address provided');
|
1006
|
+
const data = {
|
1007
|
+
brokerage: parseInt(brokerage),
|
1008
|
+
owner_address: toHex(ownerAddress),
|
1009
|
+
};
|
1010
|
+
const transactionOptions = getTransactionOptions(options);
|
1011
|
+
return createTransaction(this.tronWeb, ContractType.UpdateBrokerageContract, data, options?.permissionId, transactionOptions);
|
1012
|
+
}
|
1013
|
+
async createToken(options = {}, issuerAddress = this.tronWeb.defaultAddress.hex) {
|
1014
|
+
const { name = false, abbreviation = false, description = '', url = false, totalSupply = 0, trxRatio = 1, // How much TRX will `tokenRatio` cost
|
1015
|
+
tokenRatio = 1, // How many tokens will `trxRatio` afford
|
1016
|
+
saleStart = Date.now(), saleEnd = false, freeBandwidth = 0, // The creator's "donated" bandwidth for use by token holders
|
1017
|
+
freeBandwidthLimit = 0, // Out of `totalFreeBandwidth`, the amount each token holder get
|
1018
|
+
frozenAmount = 0, frozenDuration = 0,
|
1019
|
+
// for now there is no default for the following values
|
1020
|
+
voteScore, precision, } = options;
|
1021
|
+
this.validator.notValid([
|
1022
|
+
{
|
1023
|
+
name: 'Supply amount',
|
1024
|
+
type: 'positive-integer',
|
1025
|
+
value: totalSupply,
|
1026
|
+
},
|
1027
|
+
{
|
1028
|
+
name: 'TRX ratio',
|
1029
|
+
type: 'positive-integer',
|
1030
|
+
value: trxRatio,
|
1031
|
+
},
|
1032
|
+
{
|
1033
|
+
name: 'Token ratio',
|
1034
|
+
type: 'positive-integer',
|
1035
|
+
value: tokenRatio,
|
1036
|
+
},
|
1037
|
+
{
|
1038
|
+
name: 'token abbreviation',
|
1039
|
+
type: 'string',
|
1040
|
+
value: abbreviation,
|
1041
|
+
lte: 32,
|
1042
|
+
gt: 0,
|
1043
|
+
},
|
1044
|
+
{
|
1045
|
+
name: 'token name',
|
1046
|
+
type: 'not-empty-string',
|
1047
|
+
value: name,
|
1048
|
+
},
|
1049
|
+
{
|
1050
|
+
name: 'token description',
|
1051
|
+
type: 'string',
|
1052
|
+
value: description,
|
1053
|
+
lte: 200,
|
1054
|
+
},
|
1055
|
+
{
|
1056
|
+
name: 'token url',
|
1057
|
+
type: 'url',
|
1058
|
+
value: url,
|
1059
|
+
},
|
1060
|
+
{
|
1061
|
+
name: 'token url',
|
1062
|
+
type: 'string',
|
1063
|
+
value: url,
|
1064
|
+
lte: 256,
|
1065
|
+
},
|
1066
|
+
{
|
1067
|
+
name: 'issuer',
|
1068
|
+
type: 'address',
|
1069
|
+
value: issuerAddress,
|
1070
|
+
},
|
1071
|
+
{
|
1072
|
+
name: 'sale start timestamp',
|
1073
|
+
type: 'integer',
|
1074
|
+
value: saleStart,
|
1075
|
+
gte: Date.now(),
|
1076
|
+
},
|
1077
|
+
{
|
1078
|
+
name: 'sale end timestamp',
|
1079
|
+
type: 'integer',
|
1080
|
+
value: saleEnd,
|
1081
|
+
gt: saleStart,
|
1082
|
+
},
|
1083
|
+
{
|
1084
|
+
name: 'Frozen supply',
|
1085
|
+
type: 'integer',
|
1086
|
+
value: frozenAmount,
|
1087
|
+
gte: 0,
|
1088
|
+
},
|
1089
|
+
{
|
1090
|
+
name: 'Frozen duration',
|
1091
|
+
type: 'integer',
|
1092
|
+
value: frozenDuration,
|
1093
|
+
gte: 0,
|
1094
|
+
},
|
1095
|
+
]);
|
1096
|
+
if (isNotNullOrUndefined(voteScore) && (!isInteger(voteScore) || voteScore <= 0))
|
1097
|
+
throw new Error('voteScore must be a positive integer greater than 0');
|
1098
|
+
if (isNotNullOrUndefined(precision) && (!isInteger(precision) || precision < 0 || precision > 6))
|
1099
|
+
throw new Error('precision must be a positive integer >= 0 and <= 6');
|
1100
|
+
const data = {
|
1101
|
+
owner_address: toHex(issuerAddress),
|
1102
|
+
name: fromUtf8(name),
|
1103
|
+
abbr: fromUtf8(abbreviation),
|
1104
|
+
description: fromUtf8(description),
|
1105
|
+
url: fromUtf8(url),
|
1106
|
+
total_supply: parseInt(totalSupply),
|
1107
|
+
trx_num: parseInt(trxRatio),
|
1108
|
+
num: parseInt(tokenRatio),
|
1109
|
+
start_time: parseInt(saleStart),
|
1110
|
+
end_time: parseInt(saleEnd),
|
1111
|
+
frozen_supply: [
|
1112
|
+
{
|
1113
|
+
frozen_amount: parseInt(frozenAmount),
|
1114
|
+
frozen_days: parseInt(frozenDuration),
|
1115
|
+
},
|
1116
|
+
],
|
1117
|
+
};
|
1118
|
+
['name', 'abbr', 'description', 'url'].forEach((key) => {
|
1119
|
+
if (!data[key]) {
|
1120
|
+
delete data[key];
|
1121
|
+
}
|
1122
|
+
});
|
1123
|
+
if (!(parseInt(frozenAmount) > 0)) {
|
1124
|
+
delete data.frozen_supply;
|
1125
|
+
}
|
1126
|
+
if (freeBandwidth && !isNaN(parseInt(freeBandwidth)) && parseInt(freeBandwidth) >= 0) {
|
1127
|
+
data.free_asset_net_limit = parseInt(freeBandwidth);
|
1128
|
+
}
|
1129
|
+
if (freeBandwidthLimit && !isNaN(parseInt(freeBandwidthLimit)) && parseInt(freeBandwidthLimit) >= 0) {
|
1130
|
+
data.public_free_asset_net_limit = parseInt(freeBandwidthLimit);
|
1131
|
+
}
|
1132
|
+
if (precision && !isNaN(parseInt(precision))) {
|
1133
|
+
data.precision = parseInt(precision);
|
1134
|
+
}
|
1135
|
+
if (voteScore && !isNaN(parseInt(voteScore))) {
|
1136
|
+
data.vote_score = parseInt(voteScore);
|
1137
|
+
}
|
1138
|
+
const transactionOptions = getTransactionOptions(options);
|
1139
|
+
return createTransaction(this.tronWeb, ContractType.AssetIssueContract, data, options?.permissionId, transactionOptions);
|
1140
|
+
}
|
1141
|
+
async createAccount(accountAddress, address = this.tronWeb.defaultAddress.hex, options = {}) {
|
1142
|
+
this.validator.notValid([
|
1143
|
+
{
|
1144
|
+
name: 'account',
|
1145
|
+
type: 'address',
|
1146
|
+
value: accountAddress,
|
1147
|
+
},
|
1148
|
+
{
|
1149
|
+
name: 'origin',
|
1150
|
+
type: 'address',
|
1151
|
+
value: address,
|
1152
|
+
},
|
1153
|
+
]);
|
1154
|
+
const data = {
|
1155
|
+
owner_address: toHex(address),
|
1156
|
+
account_address: toHex(accountAddress),
|
1157
|
+
};
|
1158
|
+
const transactionOptions = getTransactionOptions(options);
|
1159
|
+
return createTransaction(this.tronWeb, ContractType.AccountCreateContract, data, options?.permissionId, transactionOptions);
|
1160
|
+
}
|
1161
|
+
async updateAccount(accountName, address = this.tronWeb.defaultAddress.hex, options = {}) {
|
1162
|
+
this.validator.notValid([
|
1163
|
+
{
|
1164
|
+
name: 'Name',
|
1165
|
+
type: 'string',
|
1166
|
+
lte: 200,
|
1167
|
+
gt: 0,
|
1168
|
+
value: accountName,
|
1169
|
+
msg: 'Invalid accountName',
|
1170
|
+
},
|
1171
|
+
{
|
1172
|
+
name: 'origin',
|
1173
|
+
type: 'address',
|
1174
|
+
value: address,
|
1175
|
+
},
|
1176
|
+
]);
|
1177
|
+
const data = {
|
1178
|
+
account_name: fromUtf8(accountName),
|
1179
|
+
owner_address: toHex(address),
|
1180
|
+
};
|
1181
|
+
const transactionOptions = getTransactionOptions(options);
|
1182
|
+
return createTransaction(this.tronWeb, ContractType.AccountUpdateContract, data, options?.permissionId, transactionOptions);
|
1183
|
+
}
|
1184
|
+
async setAccountId(accountId, address = this.tronWeb.defaultAddress.hex, options = {}) {
|
1185
|
+
if (accountId && isString(accountId) && accountId.startsWith('0x')) {
|
1186
|
+
accountId = accountId.slice(2);
|
1187
|
+
}
|
1188
|
+
this.validator.notValid([
|
1189
|
+
{
|
1190
|
+
name: 'accountId',
|
1191
|
+
type: 'hex',
|
1192
|
+
value: accountId,
|
1193
|
+
},
|
1194
|
+
{
|
1195
|
+
name: 'accountId',
|
1196
|
+
type: 'string',
|
1197
|
+
lte: 32,
|
1198
|
+
gte: 8,
|
1199
|
+
value: accountId,
|
1200
|
+
},
|
1201
|
+
{
|
1202
|
+
name: 'origin',
|
1203
|
+
type: 'address',
|
1204
|
+
value: address,
|
1205
|
+
},
|
1206
|
+
]);
|
1207
|
+
const data = {
|
1208
|
+
account_id: accountId,
|
1209
|
+
owner_address: toHex(address),
|
1210
|
+
};
|
1211
|
+
const transactionOptions = getTransactionOptions(options);
|
1212
|
+
return createTransaction(this.tronWeb, ContractType.SetAccountIdContract, data, options?.permissionId, transactionOptions);
|
1213
|
+
}
|
1214
|
+
async updateToken(options = {}, issuerAddress = this.tronWeb.defaultAddress.hex) {
|
1215
|
+
const { description = '', url = false, freeBandwidth = 0, // The creator's "donated" bandwidth for use by token holders
|
1216
|
+
freeBandwidthLimit = 0, // Out of `totalFreeBandwidth`, the amount each token holder get
|
1217
|
+
} = options;
|
1218
|
+
this.validator.notValid([
|
1219
|
+
{
|
1220
|
+
name: 'token description',
|
1221
|
+
type: 'string',
|
1222
|
+
value: description,
|
1223
|
+
lte: 200,
|
1224
|
+
},
|
1225
|
+
{
|
1226
|
+
name: 'token url',
|
1227
|
+
type: 'url',
|
1228
|
+
value: url,
|
1229
|
+
},
|
1230
|
+
{
|
1231
|
+
name: 'token url',
|
1232
|
+
type: 'string',
|
1233
|
+
value: url,
|
1234
|
+
lte: 256,
|
1235
|
+
},
|
1236
|
+
{
|
1237
|
+
name: 'issuer',
|
1238
|
+
type: 'address',
|
1239
|
+
value: issuerAddress,
|
1240
|
+
},
|
1241
|
+
]);
|
1242
|
+
const data = {
|
1243
|
+
owner_address: toHex(issuerAddress),
|
1244
|
+
description: fromUtf8(description),
|
1245
|
+
url: fromUtf8(url),
|
1246
|
+
};
|
1247
|
+
if (freeBandwidth && !isNaN(parseInt(freeBandwidth)) && parseInt(freeBandwidth) >= 0) {
|
1248
|
+
data.new_limit = parseInt(freeBandwidth);
|
1249
|
+
}
|
1250
|
+
if (freeBandwidthLimit && !isNaN(parseInt(freeBandwidthLimit)) && parseInt(freeBandwidthLimit) >= 0) {
|
1251
|
+
data.new_public_limit = parseInt(freeBandwidthLimit);
|
1252
|
+
}
|
1253
|
+
const transactionOptions = getTransactionOptions(options);
|
1254
|
+
return createTransaction(this.tronWeb, ContractType.UpdateAssetContract, data, options?.permissionId, transactionOptions);
|
1255
|
+
}
|
1256
|
+
async sendAsset(to, amount = 0, tokenId, from = this.tronWeb.defaultAddress.hex, options = {}) {
|
1257
|
+
return this.sendToken(to, amount, tokenId, from, options);
|
1258
|
+
}
|
1259
|
+
async purchaseAsset(issuerAddress, tokenId, amount = 0, buyer = this.tronWeb.defaultAddress.hex, options = {}) {
|
1260
|
+
return this.purchaseToken(issuerAddress, tokenId, amount, buyer, options);
|
1261
|
+
}
|
1262
|
+
async createAsset(options, issuerAddress) {
|
1263
|
+
return this.createToken(options, issuerAddress);
|
1264
|
+
}
|
1265
|
+
async updateAsset(options = {}, issuerAddress = this.tronWeb.defaultAddress.hex) {
|
1266
|
+
return this.updateToken(options, issuerAddress);
|
1267
|
+
}
|
1268
|
+
/**
|
1269
|
+
* Creates a proposal to modify the network.
|
1270
|
+
* Can only be created by a current Super Representative.
|
1271
|
+
*/
|
1272
|
+
async createProposal(parameters, issuerAddress = this.tronWeb.defaultAddress.hex, options = {}) {
|
1273
|
+
this.validator.notValid([
|
1274
|
+
{
|
1275
|
+
name: 'issuer',
|
1276
|
+
type: 'address',
|
1277
|
+
value: issuerAddress,
|
1278
|
+
},
|
1279
|
+
]);
|
1280
|
+
const invalid = 'Invalid proposal parameters provided';
|
1281
|
+
if (!parameters)
|
1282
|
+
throw new Error(invalid);
|
1283
|
+
const newParams = isArray(parameters) ? parameters : [parameters];
|
1284
|
+
for (const parameter of newParams) {
|
1285
|
+
if (!isObject(parameter))
|
1286
|
+
throw new Error(invalid);
|
1287
|
+
}
|
1288
|
+
const data = {
|
1289
|
+
owner_address: toHex(issuerAddress),
|
1290
|
+
parameters: newParams,
|
1291
|
+
};
|
1292
|
+
const transactionOptions = getTransactionOptions(options);
|
1293
|
+
return createTransaction(this.tronWeb, ContractType.ProposalCreateContract, data, options?.permissionId, transactionOptions);
|
1294
|
+
}
|
1295
|
+
/**
|
1296
|
+
* Deletes a network modification proposal that the owner issued.
|
1297
|
+
* Only current Super Representative can vote on a proposal.
|
1298
|
+
*/
|
1299
|
+
async deleteProposal(proposalID, issuerAddress = this.tronWeb.defaultAddress.hex, options = {}) {
|
1300
|
+
this.validator.notValid([
|
1301
|
+
{
|
1302
|
+
name: 'issuer',
|
1303
|
+
type: 'address',
|
1304
|
+
value: issuerAddress,
|
1305
|
+
},
|
1306
|
+
{
|
1307
|
+
name: 'proposalID',
|
1308
|
+
type: 'integer',
|
1309
|
+
value: proposalID,
|
1310
|
+
gte: 0,
|
1311
|
+
},
|
1312
|
+
]);
|
1313
|
+
const data = {
|
1314
|
+
owner_address: toHex(issuerAddress),
|
1315
|
+
proposal_id: parseInt(proposalID),
|
1316
|
+
};
|
1317
|
+
const transactionOptions = getTransactionOptions(options);
|
1318
|
+
return createTransaction(this.tronWeb, ContractType.ProposalDeleteContract, data, options?.permissionId, transactionOptions);
|
1319
|
+
}
|
1320
|
+
/**
|
1321
|
+
* Adds a vote to an issued network modification proposal.
|
1322
|
+
* Only current Super Representative can vote on a proposal.
|
1323
|
+
*/
|
1324
|
+
async voteProposal(proposalID, isApproval = false, voterAddress = this.tronWeb.defaultAddress.hex, options = {}) {
|
1325
|
+
this.validator.notValid([
|
1326
|
+
{
|
1327
|
+
name: 'voter',
|
1328
|
+
type: 'address',
|
1329
|
+
value: voterAddress,
|
1330
|
+
},
|
1331
|
+
{
|
1332
|
+
name: 'proposalID',
|
1333
|
+
type: 'integer',
|
1334
|
+
value: proposalID,
|
1335
|
+
gte: 0,
|
1336
|
+
},
|
1337
|
+
{
|
1338
|
+
name: 'has approval',
|
1339
|
+
type: 'boolean',
|
1340
|
+
value: isApproval,
|
1341
|
+
},
|
1342
|
+
]);
|
1343
|
+
const data = {
|
1344
|
+
owner_address: toHex(voterAddress),
|
1345
|
+
proposal_id: parseInt(proposalID),
|
1346
|
+
is_add_approval: isApproval,
|
1347
|
+
};
|
1348
|
+
const transactionOptions = getTransactionOptions(options);
|
1349
|
+
return createTransaction(this.tronWeb, ContractType.ProposalApproveContract, data, options?.permissionId, transactionOptions);
|
1350
|
+
}
|
1351
|
+
/**
|
1352
|
+
* Create an exchange between a token and TRX.
|
1353
|
+
* Token Name should be a CASE SENSITIVE string.
|
1354
|
+
* PLEASE VERIFY THIS ON TRONSCAN.
|
1355
|
+
*/
|
1356
|
+
async createTRXExchange(tokenName, tokenBalance, trxBalance, ownerAddress = this.tronWeb.defaultAddress.hex, options = {}) {
|
1357
|
+
this.validator.notValid([
|
1358
|
+
{
|
1359
|
+
name: 'owner',
|
1360
|
+
type: 'address',
|
1361
|
+
value: ownerAddress,
|
1362
|
+
},
|
1363
|
+
{
|
1364
|
+
name: 'token name',
|
1365
|
+
type: 'not-empty-string',
|
1366
|
+
value: tokenName,
|
1367
|
+
},
|
1368
|
+
{
|
1369
|
+
name: 'token balance',
|
1370
|
+
type: 'positive-integer',
|
1371
|
+
value: tokenBalance,
|
1372
|
+
},
|
1373
|
+
{
|
1374
|
+
name: 'trx balance',
|
1375
|
+
type: 'positive-integer',
|
1376
|
+
value: trxBalance,
|
1377
|
+
},
|
1378
|
+
]);
|
1379
|
+
const data = {
|
1380
|
+
owner_address: toHex(ownerAddress),
|
1381
|
+
first_token_id: fromUtf8(tokenName),
|
1382
|
+
first_token_balance: tokenBalance,
|
1383
|
+
second_token_id: '5f', // Constant for TRX.
|
1384
|
+
second_token_balance: trxBalance,
|
1385
|
+
};
|
1386
|
+
const transactionOptions = getTransactionOptions(options);
|
1387
|
+
return createTransaction(this.tronWeb, ContractType.ExchangeCreateContract, data, options?.permissionId, transactionOptions);
|
1388
|
+
}
|
1389
|
+
/**
|
1390
|
+
* Create an exchange between a token and another token.
|
1391
|
+
* DO NOT USE THIS FOR TRX.
|
1392
|
+
* Token Names should be a CASE SENSITIVE string.
|
1393
|
+
* PLEASE VERIFY THIS ON TRONSCAN.
|
1394
|
+
*/
|
1395
|
+
async createTokenExchange(firstTokenName, firstTokenBalance, secondTokenName, secondTokenBalance, ownerAddress = this.tronWeb.defaultAddress.hex, options = {}) {
|
1396
|
+
this.validator.notValid([
|
1397
|
+
{
|
1398
|
+
name: 'owner',
|
1399
|
+
type: 'address',
|
1400
|
+
value: ownerAddress,
|
1401
|
+
},
|
1402
|
+
{
|
1403
|
+
name: 'first token name',
|
1404
|
+
type: 'not-empty-string',
|
1405
|
+
value: firstTokenName,
|
1406
|
+
},
|
1407
|
+
{
|
1408
|
+
name: 'second token name',
|
1409
|
+
type: 'not-empty-string',
|
1410
|
+
value: secondTokenName,
|
1411
|
+
},
|
1412
|
+
{
|
1413
|
+
name: 'first token balance',
|
1414
|
+
type: 'positive-integer',
|
1415
|
+
value: firstTokenBalance,
|
1416
|
+
},
|
1417
|
+
{
|
1418
|
+
name: 'second token balance',
|
1419
|
+
type: 'positive-integer',
|
1420
|
+
value: secondTokenBalance,
|
1421
|
+
},
|
1422
|
+
]);
|
1423
|
+
const data = {
|
1424
|
+
owner_address: toHex(ownerAddress),
|
1425
|
+
first_token_id: fromUtf8(firstTokenName),
|
1426
|
+
first_token_balance: firstTokenBalance,
|
1427
|
+
second_token_id: fromUtf8(secondTokenName),
|
1428
|
+
second_token_balance: secondTokenBalance,
|
1429
|
+
};
|
1430
|
+
const transactionOptions = getTransactionOptions(options);
|
1431
|
+
return createTransaction(this.tronWeb, ContractType.ExchangeCreateContract, data, options?.permissionId, transactionOptions);
|
1432
|
+
}
|
1433
|
+
/**
|
1434
|
+
* Adds tokens into a bancor style exchange.
|
1435
|
+
* Will add both tokens at market rate.
|
1436
|
+
* Use "_" for the constant value for TRX.
|
1437
|
+
*/
|
1438
|
+
async injectExchangeTokens(exchangeID, tokenName, tokenAmount, ownerAddress = this.tronWeb.defaultAddress.hex, options = {}) {
|
1439
|
+
this.validator.notValid([
|
1440
|
+
{
|
1441
|
+
name: 'owner',
|
1442
|
+
type: 'address',
|
1443
|
+
value: ownerAddress,
|
1444
|
+
},
|
1445
|
+
{
|
1446
|
+
name: 'token name',
|
1447
|
+
type: 'not-empty-string',
|
1448
|
+
value: tokenName,
|
1449
|
+
},
|
1450
|
+
{
|
1451
|
+
name: 'token amount',
|
1452
|
+
type: 'integer',
|
1453
|
+
value: tokenAmount,
|
1454
|
+
gte: 1,
|
1455
|
+
},
|
1456
|
+
{
|
1457
|
+
name: 'exchangeID',
|
1458
|
+
type: 'integer',
|
1459
|
+
value: exchangeID,
|
1460
|
+
gte: 0,
|
1461
|
+
},
|
1462
|
+
]);
|
1463
|
+
const data = {
|
1464
|
+
owner_address: toHex(ownerAddress),
|
1465
|
+
exchange_id: parseInt(exchangeID),
|
1466
|
+
token_id: fromUtf8(tokenName),
|
1467
|
+
quant: parseInt(tokenAmount),
|
1468
|
+
};
|
1469
|
+
const transactionOptions = getTransactionOptions(options);
|
1470
|
+
return createTransaction(this.tronWeb, ContractType.ExchangeInjectContract, data, options?.permissionId, transactionOptions);
|
1471
|
+
}
|
1472
|
+
/**
|
1473
|
+
* Withdraws tokens from a bancor style exchange.
|
1474
|
+
* Will withdraw at market rate both tokens.
|
1475
|
+
* Use "_" for the constant value for TRX.
|
1476
|
+
*/
|
1477
|
+
async withdrawExchangeTokens(exchangeID, tokenName, tokenAmount, ownerAddress = this.tronWeb.defaultAddress.hex, options = {}) {
|
1478
|
+
this.validator.notValid([
|
1479
|
+
{
|
1480
|
+
name: 'owner',
|
1481
|
+
type: 'address',
|
1482
|
+
value: ownerAddress,
|
1483
|
+
},
|
1484
|
+
{
|
1485
|
+
name: 'token name',
|
1486
|
+
type: 'not-empty-string',
|
1487
|
+
value: tokenName,
|
1488
|
+
},
|
1489
|
+
{
|
1490
|
+
name: 'token amount',
|
1491
|
+
type: 'integer',
|
1492
|
+
value: tokenAmount,
|
1493
|
+
gte: 1,
|
1494
|
+
},
|
1495
|
+
{
|
1496
|
+
name: 'exchangeID',
|
1497
|
+
type: 'integer',
|
1498
|
+
value: exchangeID,
|
1499
|
+
gte: 0,
|
1500
|
+
},
|
1501
|
+
]);
|
1502
|
+
const data = {
|
1503
|
+
owner_address: toHex(ownerAddress),
|
1504
|
+
exchange_id: parseInt(exchangeID),
|
1505
|
+
token_id: fromUtf8(tokenName),
|
1506
|
+
quant: parseInt(tokenAmount),
|
1507
|
+
};
|
1508
|
+
const transactionOptions = getTransactionOptions(options);
|
1509
|
+
return createTransaction(this.tronWeb, ContractType.ExchangeWithdrawContract, data, options?.permissionId, transactionOptions);
|
1510
|
+
}
|
1511
|
+
/**
|
1512
|
+
* Trade tokens on a bancor style exchange.
|
1513
|
+
* Expected value is a validation and used to cap the total amt of token 2 spent.
|
1514
|
+
* Use "_" for the constant value for TRX.
|
1515
|
+
*/
|
1516
|
+
async tradeExchangeTokens(exchangeID, tokenName, tokenAmountSold, tokenAmountExpected, ownerAddress = this.tronWeb.defaultAddress.hex, options = {}) {
|
1517
|
+
this.validator.notValid([
|
1518
|
+
{
|
1519
|
+
name: 'owner',
|
1520
|
+
type: 'address',
|
1521
|
+
value: ownerAddress,
|
1522
|
+
},
|
1523
|
+
{
|
1524
|
+
name: 'token name',
|
1525
|
+
type: 'not-empty-string',
|
1526
|
+
value: tokenName,
|
1527
|
+
},
|
1528
|
+
{
|
1529
|
+
name: 'tokenAmountSold',
|
1530
|
+
type: 'integer',
|
1531
|
+
value: tokenAmountSold,
|
1532
|
+
gte: 1,
|
1533
|
+
},
|
1534
|
+
{
|
1535
|
+
name: 'tokenAmountExpected',
|
1536
|
+
type: 'integer',
|
1537
|
+
value: tokenAmountExpected,
|
1538
|
+
gte: 1,
|
1539
|
+
},
|
1540
|
+
{
|
1541
|
+
name: 'exchangeID',
|
1542
|
+
type: 'integer',
|
1543
|
+
value: exchangeID,
|
1544
|
+
gte: 0,
|
1545
|
+
},
|
1546
|
+
]);
|
1547
|
+
const data = {
|
1548
|
+
owner_address: toHex(ownerAddress),
|
1549
|
+
exchange_id: parseInt(exchangeID),
|
1550
|
+
token_id: TronWeb.fromAscii(tokenName).replace(/^0x/, ''),
|
1551
|
+
quant: parseInt(tokenAmountSold),
|
1552
|
+
expected: parseInt(tokenAmountExpected),
|
1553
|
+
};
|
1554
|
+
const transactionOptions = getTransactionOptions(options);
|
1555
|
+
return createTransaction(this.tronWeb, ContractType.ExchangeTransactionContract, data, options?.permissionId, transactionOptions);
|
1556
|
+
}
|
1557
|
+
/**
|
1558
|
+
* Update userFeePercentage.
|
1559
|
+
*/
|
1560
|
+
async updateSetting(contractAddress, userFeePercentage, ownerAddress = this.tronWeb.defaultAddress.hex, options = {}) {
|
1561
|
+
this.validator.notValid([
|
1562
|
+
{
|
1563
|
+
name: 'owner',
|
1564
|
+
type: 'address',
|
1565
|
+
value: ownerAddress,
|
1566
|
+
},
|
1567
|
+
{
|
1568
|
+
name: 'contract',
|
1569
|
+
type: 'address',
|
1570
|
+
value: contractAddress,
|
1571
|
+
},
|
1572
|
+
{
|
1573
|
+
name: 'userFeePercentage',
|
1574
|
+
type: 'integer',
|
1575
|
+
value: userFeePercentage,
|
1576
|
+
gte: 0,
|
1577
|
+
lte: 100,
|
1578
|
+
},
|
1579
|
+
]);
|
1580
|
+
const data = {
|
1581
|
+
owner_address: toHex(ownerAddress),
|
1582
|
+
contract_address: toHex(contractAddress),
|
1583
|
+
consume_user_resource_percent: userFeePercentage,
|
1584
|
+
};
|
1585
|
+
const transactionOptions = getTransactionOptions(options);
|
1586
|
+
return createTransaction(this.tronWeb, ContractType.UpdateSettingContract, data, options?.permissionId, transactionOptions);
|
1587
|
+
}
|
1588
|
+
/**
|
1589
|
+
* Update energy limit.
|
1590
|
+
*/
|
1591
|
+
async updateEnergyLimit(contractAddress, originEnergyLimit = 0, ownerAddress = this.tronWeb.defaultAddress.hex, options = {}) {
|
1592
|
+
this.validator.notValid([
|
1593
|
+
{
|
1594
|
+
name: 'owner',
|
1595
|
+
type: 'address',
|
1596
|
+
value: ownerAddress,
|
1597
|
+
},
|
1598
|
+
{
|
1599
|
+
name: 'contract',
|
1600
|
+
type: 'address',
|
1601
|
+
value: contractAddress,
|
1602
|
+
},
|
1603
|
+
{
|
1604
|
+
name: 'originEnergyLimit',
|
1605
|
+
type: 'integer',
|
1606
|
+
value: originEnergyLimit,
|
1607
|
+
gte: 0,
|
1608
|
+
lte: 10_000_000,
|
1609
|
+
},
|
1610
|
+
]);
|
1611
|
+
const data = {
|
1612
|
+
owner_address: toHex(ownerAddress),
|
1613
|
+
contract_address: toHex(contractAddress),
|
1614
|
+
origin_energy_limit: originEnergyLimit,
|
1615
|
+
};
|
1616
|
+
const transactionOptions = getTransactionOptions(options);
|
1617
|
+
return createTransaction(this.tronWeb, ContractType.UpdateEnergyLimitContract, data, options?.permissionId, transactionOptions);
|
1618
|
+
}
|
1619
|
+
checkPermissions(permissions, type) {
|
1620
|
+
if (permissions) {
|
1621
|
+
if (permissions.type !== type ||
|
1622
|
+
!permissions.permission_name ||
|
1623
|
+
!isString(permissions.permission_name) ||
|
1624
|
+
!isInteger(permissions.threshold) ||
|
1625
|
+
permissions.threshold < 1 ||
|
1626
|
+
!permissions.keys) {
|
1627
|
+
return false;
|
1628
|
+
}
|
1629
|
+
for (const key of permissions.keys) {
|
1630
|
+
if (!TronWeb.isAddress(key.address) ||
|
1631
|
+
!isInteger(key.weight) ||
|
1632
|
+
key.weight > permissions.threshold ||
|
1633
|
+
key.weight < 1 ||
|
1634
|
+
(type === 2 && !permissions.operations)) {
|
1635
|
+
return false;
|
1636
|
+
}
|
1637
|
+
}
|
1638
|
+
}
|
1639
|
+
return true;
|
1640
|
+
}
|
1641
|
+
async updateAccountPermissions(ownerAddress = this.tronWeb.defaultAddress.hex, ownerPermission, witnessPermission, activesPermissions, options = {}) {
|
1642
|
+
if (!TronWeb.isAddress(ownerAddress))
|
1643
|
+
throw new Error('Invalid ownerAddress provided');
|
1644
|
+
if (!this.checkPermissions(ownerPermission, 0)) {
|
1645
|
+
throw new Error('Invalid ownerPermissions provided');
|
1646
|
+
}
|
1647
|
+
if (!this.checkPermissions(witnessPermission, 1)) {
|
1648
|
+
throw new Error('Invalid witnessPermissions provided');
|
1649
|
+
}
|
1650
|
+
if (!Array.isArray(activesPermissions)) {
|
1651
|
+
activesPermissions = [activesPermissions];
|
1652
|
+
}
|
1653
|
+
for (const activesPermission of activesPermissions) {
|
1654
|
+
if (!this.checkPermissions(activesPermission, 2)) {
|
1655
|
+
throw new Error('Invalid activesPermissions provided');
|
1656
|
+
}
|
1657
|
+
}
|
1658
|
+
const data = {
|
1659
|
+
owner_address: toHex(ownerAddress),
|
1660
|
+
};
|
1661
|
+
if (ownerPermission) {
|
1662
|
+
const _ownerPermissions = deepCopyJson(ownerPermission);
|
1663
|
+
// for compatible with old way of building transaction from chain which type prop is omitted
|
1664
|
+
if ('type' in _ownerPermissions) {
|
1665
|
+
delete _ownerPermissions.type;
|
1666
|
+
}
|
1667
|
+
_ownerPermissions.keys = _ownerPermissions.keys?.map(({ address, weight }) => ({
|
1668
|
+
address: this.tronWeb.address.toHex(address),
|
1669
|
+
weight,
|
1670
|
+
}));
|
1671
|
+
data.owner = _ownerPermissions;
|
1672
|
+
}
|
1673
|
+
if (witnessPermission) {
|
1674
|
+
const _witnessPermissions = deepCopyJson(witnessPermission);
|
1675
|
+
// for compatible with old way of building transaction from chain which type prop is Witness
|
1676
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
1677
|
+
// @ts-ignore
|
1678
|
+
_witnessPermissions.type = 'Witness';
|
1679
|
+
_witnessPermissions.keys = _witnessPermissions.keys.map(({ address, weight }) => ({
|
1680
|
+
address: this.tronWeb.address.toHex(address),
|
1681
|
+
weight,
|
1682
|
+
}));
|
1683
|
+
data.witness = _witnessPermissions;
|
1684
|
+
}
|
1685
|
+
if (activesPermissions) {
|
1686
|
+
const _activesPermissions = deepCopyJson(activesPermissions);
|
1687
|
+
// for compatible with old way of building transaction from chain which type prop is Active
|
1688
|
+
_activesPermissions.forEach((activePermissions) => {
|
1689
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
1690
|
+
// @ts-ignore
|
1691
|
+
activePermissions.type = 'Active';
|
1692
|
+
});
|
1693
|
+
_activesPermissions.forEach((_activesPermission) => {
|
1694
|
+
_activesPermission.keys = _activesPermission.keys.map(({ address, weight }) => ({
|
1695
|
+
address: this.tronWeb.address.toHex(address),
|
1696
|
+
weight,
|
1697
|
+
}));
|
1698
|
+
});
|
1699
|
+
data.actives = _activesPermissions;
|
1700
|
+
}
|
1701
|
+
const transactionOptions = getTransactionOptions(options);
|
1702
|
+
return createTransaction(this.tronWeb, ContractType.AccountPermissionUpdateContract, data, options?.permissionId, transactionOptions);
|
1703
|
+
}
|
1704
|
+
async newTxID(transaction, options = {}) {
|
1705
|
+
if (options?.txLocal) {
|
1706
|
+
const contract = transaction.raw_data.contract[0];
|
1707
|
+
try {
|
1708
|
+
const tx = await createTransaction(this.tronWeb, contract.type, contract.parameter.value, contract.Permission_id, {
|
1709
|
+
fee_limit: transaction.raw_data.fee_limit,
|
1710
|
+
data: transaction.raw_data.data,
|
1711
|
+
ref_block_bytes: transaction.raw_data.ref_block_bytes,
|
1712
|
+
ref_block_hash: transaction.raw_data.ref_block_hash,
|
1713
|
+
expiration: transaction.raw_data.expiration,
|
1714
|
+
timestamp: transaction.raw_data.timestamp,
|
1715
|
+
});
|
1716
|
+
tx.signature = transaction.signature;
|
1717
|
+
tx.visible = transaction.visible;
|
1718
|
+
return tx;
|
1719
|
+
}
|
1720
|
+
catch (e) {
|
1721
|
+
throw new Error('Error generating a new transaction id.');
|
1722
|
+
}
|
1723
|
+
}
|
1724
|
+
try {
|
1725
|
+
const res = await this.tronWeb.fullNode.request('wallet/getsignweight', transaction, 'post');
|
1726
|
+
if (typeof transaction.visible === 'boolean') {
|
1727
|
+
res.transaction.transaction.visible = transaction.visible;
|
1728
|
+
}
|
1729
|
+
return resultManager(res.transaction.transaction, {
|
1730
|
+
...transaction.raw_data.contract[0].parameter.value,
|
1731
|
+
Permission_id: transaction.raw_data.contract[0].Permission_id,
|
1732
|
+
},
|
1733
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
1734
|
+
/* @ts-ignore */
|
1735
|
+
{ data: transaction.raw_data.data, fee_limit: transaction.raw_data.fee_limit });
|
1736
|
+
}
|
1737
|
+
catch (e) {
|
1738
|
+
throw new Error('Error generating a new transaction id.');
|
1739
|
+
}
|
1740
|
+
}
|
1741
|
+
async alterTransaction(transaction, options = {}) {
|
1742
|
+
if (Reflect.has(transaction, 'signature'))
|
1743
|
+
throw new Error('You can not extend the expiration of a signed transaction.');
|
1744
|
+
if (options.data) {
|
1745
|
+
if (options.dataFormat !== 'hex')
|
1746
|
+
options.data = TronWeb.toHex(options.data);
|
1747
|
+
options.data = options.data.replace(/^0x/, '');
|
1748
|
+
if (options.data.length === 0)
|
1749
|
+
throw new Error('Invalid data provided');
|
1750
|
+
transaction.raw_data.data = options.data;
|
1751
|
+
}
|
1752
|
+
if (options.extension) {
|
1753
|
+
options.extension = parseInt(options.extension * 1000);
|
1754
|
+
if (isNaN(options.extension) || transaction.raw_data.expiration + options.extension <= Date.now() + 3000)
|
1755
|
+
throw new Error('Invalid extension provided');
|
1756
|
+
transaction.raw_data.expiration += options.extension;
|
1757
|
+
}
|
1758
|
+
return await this.newTxID(transaction, { txLocal: options.txLocal });
|
1759
|
+
}
|
1760
|
+
async extendExpiration(transaction, extension, options = {}) {
|
1761
|
+
return await this.alterTransaction(transaction, { extension, txLocal: options?.txLocal });
|
1762
|
+
}
|
1763
|
+
async addUpdateData(transaction, data, dataFormat = 'utf8', options = {}) {
|
1764
|
+
return this.alterTransaction(transaction, { data, dataFormat: dataFormat, txLocal: options?.txLocal });
|
1765
|
+
}
|
1766
|
+
}
|
1767
|
+
//# sourceMappingURL=TransactionBuilder.js.map
|