Line data Source code
1 : import 'package:walletkit_dart/src/common/http_repository.dart';
2 : import 'package:walletkit_dart/src/crypto/tron/repositories/rpc/core/contract/smart_contract.pb.dart';
3 : import 'package:walletkit_dart/walletkit_dart.dart';
4 :
5 : const _tronGridBaseUrl = 'https://api.trongrid.io';
6 :
7 : typedef TronBlockInfo = ({String blockId, int blockNumber});
8 :
9 : class TronHTTPRepository extends HTTPRepository {
10 : final List<String> apiKeys;
11 :
12 1 : const TronHTTPRepository({required this.apiKeys})
13 1 : : super(
14 : apiKeys: apiKeys,
15 : baseURL: _tronGridBaseUrl,
16 : apiKeyHeader: "TRON-PRO-API-KEY",
17 : );
18 :
19 1 : Future<JSON> validateAddress(String address) async {
20 1 : final result = await postCall<JSON>(
21 2 : "$baseURL/wallet/validateaddress",
22 1 : data: {
23 : "address": address,
24 : "visible": true,
25 : },
26 : );
27 :
28 : return result;
29 : }
30 :
31 1 : Future<TronBlockInfo> getBlock() async {
32 4 : final result = await postCall<JSON>("$baseURL/wallet/getnowblock", data: {});
33 :
34 1 : final blockId = result["blockID"] as String;
35 3 : final blockNumber = result["block_header"]["raw_data"]["number"] as int;
36 :
37 : return (blockId: blockId, blockNumber: blockNumber);
38 : }
39 :
40 1 : Future<JSON> getAccount({required String address, bool visible = true}) {
41 1 : return postCall<JSON>(
42 2 : "$baseURL/wallet/getaccount",
43 1 : data: {
44 : "address": address,
45 : "visible": visible,
46 : },
47 : );
48 : }
49 :
50 1 : Future<({int bandwidth, int energy})> getAccountResource({
51 : required String address,
52 : bool visible = true,
53 : }) async {
54 1 : final json = await postCall<JSON>(
55 2 : "$baseURL/wallet/getaccountresource",
56 1 : data: {
57 : "address": address,
58 : "visible": visible,
59 : },
60 : );
61 :
62 1 : final freeNetUsed = json["freeNetUsed"] as int? ?? 0;
63 1 : final freeNetLimit = json["freeNetLimit"] as int;
64 :
65 1 : final remainingFreeBandwidth = freeNetLimit - freeNetUsed;
66 :
67 1 : final energyLimit = json["EnergyLimit"] as int?;
68 1 : final energyUsed = json["EnergyUsed"] as int?;
69 : final energyBalance =
70 0 : (energyUsed != null && energyLimit != null) ? energyLimit - energyUsed : 0;
71 :
72 : return (
73 : bandwidth: remainingFreeBandwidth,
74 : energy: energyBalance,
75 : );
76 : }
77 :
78 1 : Future<Amount> getBalance({required String address}) async {
79 1 : final accountInfo = await getAccount(address: address);
80 :
81 1 : final balance = accountInfo["balance"] as int;
82 :
83 2 : return Amount(value: balance.toBigInt, decimals: 6);
84 : }
85 :
86 1 : Future<JSON> getAllTRC10Tokens() {
87 3 : return getCall<JSON>("$baseURL/wallet/getassetissuelist");
88 : }
89 :
90 1 : Future<JSON> listNodes() {
91 3 : return getCall<JSON>("$baseURL/wallet/listnodes");
92 : }
93 :
94 1 : Future<JSON> getTRC10byID(String id) {
95 1 : return postCall<JSON>(
96 2 : "$baseURL/wallet/getassetissuebyid",
97 1 : data: {"value": id},
98 : );
99 : }
100 :
101 1 : Future<JSON> getTransactionById(String id) {
102 1 : return postCall<JSON>(
103 2 : "$baseURL/wallet/gettransactionbyid",
104 1 : data: {"value": id},
105 : );
106 : }
107 :
108 1 : Future<JSON> getAccountInfo({required String address}) {
109 1 : return getCall<JSON>(
110 2 : "$baseURL/v1/accounts/$address",
111 : );
112 : }
113 :
114 1 : Future<JSON> getTRC20TransactionList({
115 : required String address,
116 : required String contractAddress,
117 : int limit = 200,
118 : }) {
119 1 : return getCall<JSON>(
120 2 : "$baseURL/v1/accounts/$address/transactions/trc20?limit=$limit&contract_address=$contractAddress",
121 : );
122 : }
123 :
124 1 : Future<JSON> getTRXTransactionList({
125 : required String address,
126 : int limit = 200,
127 : }) {
128 1 : return getCall<JSON>(
129 2 : "$baseURL/v1/accounts/$address/transactions?limit=$limit",
130 : );
131 : }
132 :
133 : ///
134 : /// Trigger a constant contract (read-only, does not modify the blockchain)
135 : ///
136 1 : Future<JSON> triggerConstantContract({
137 : required String address,
138 : required String contractAddress,
139 : String? functionSelector,
140 : String? parameter,
141 : String? data,
142 : bool visible = true,
143 : }) {
144 : assert(
145 1 : functionSelector != null || data != null,
146 : "FunctionSelector or Data must be provided",
147 : );
148 :
149 1 : return postCall<JSON>(
150 2 : "$baseURL/wallet/triggerconstantcontract",
151 1 : data: {
152 1 : "owner_address": address,
153 1 : "contract_address": contractAddress,
154 1 : if (functionSelector != null) "function_selector": functionSelector,
155 1 : if (parameter != null) "parameter": parameter,
156 0 : if (data != null) "data": data,
157 1 : "visible": visible,
158 : },
159 : );
160 : }
161 :
162 0 : Future<JSON> transferTRC20({
163 : required String address,
164 : required String contractAddress,
165 : required Amount amount,
166 : }) {
167 : const functionSelector = "transfer(address,uint256)";
168 0 : final addressParameter = base58ToEVM(address, false).padLeft(64, '0');
169 0 : final amountParameter = amount.value.toHex.padLeft(64, '0');
170 0 : final parmeter = "$addressParameter$amountParameter";
171 :
172 0 : return triggerSmartContract(
173 : address: address,
174 : contractAddress: contractAddress,
175 : functionSelector: functionSelector,
176 : parameter: parmeter,
177 : );
178 : }
179 :
180 : ///
181 : /// Trigger a smart contract (write, modifies the blockchain)
182 : ///
183 0 : Future<JSON> triggerSmartContract({
184 : required String address,
185 : required String contractAddress,
186 : required String functionSelector,
187 : required String parameter,
188 : bool visible = true,
189 : int feeLimit = 1000000, // 1 TRX
190 : int? call_value, // Amount of TRX within the transaction
191 : int? call_token_value, // Amount of TRC10 token within the transaction
192 : String? token_id, // Token ID for TRC10
193 : }) {
194 0 : return postCall<JSON>(
195 0 : "$baseURL/wallet/triggercontract",
196 0 : data: {
197 0 : "owner_address": address,
198 0 : "contract_address": contractAddress,
199 0 : "function_selector": functionSelector,
200 0 : "parameter": parameter,
201 0 : "visible": visible,
202 0 : "fee_limit": feeLimit,
203 0 : if (call_value != null) "call_value": call_value,
204 0 : if (call_token_value != null) "call_token_value": call_token_value,
205 0 : if (token_id != null) "token_id": token_id,
206 : },
207 : );
208 : }
209 :
210 1 : Future<Amount> getTRC20Balance({
211 : required String address,
212 : required ERC20Entity trc20,
213 : }) async {
214 2 : final addressParameter = base58ToEVM(address, false).padLeft(64, '0');
215 1 : final result = await triggerConstantContract(
216 : address: address,
217 1 : contractAddress: trc20.contractAddress,
218 : functionSelector: "balanceOf(address)",
219 : parameter: addressParameter,
220 : );
221 :
222 2 : final balance_s = result["constant_result"][0] as String;
223 :
224 1 : final balance_bi = balance_s.toBigIntFromHex;
225 :
226 1 : return Amount(
227 : value: balance_bi,
228 1 : decimals: trc20.decimals,
229 : );
230 : }
231 :
232 0 : Future<int> estimateEnergy(TriggerSmartContract contract) async {
233 0 : final json = await triggerConstantContract(
234 0 : address: contract.ownerAddress.toUint8List.toHex,
235 0 : contractAddress: contract.contractAddress.toUint8List.toHex,
236 : visible: false,
237 0 : data: contract.data.toUint8List.toHex,
238 : );
239 :
240 0 : final energy_used = json["energy_used"] as int;
241 : return energy_used;
242 : }
243 :
244 0 : Future<JSON> broadcastCastTransactionHex(String hex) {
245 0 : return postCall<JSON>(
246 0 : "$baseURL/wallet/broadcasthex",
247 0 : data: {"transaction": hex},
248 : );
249 : }
250 :
251 0 : Future<JSON> createTransaction({
252 : required String ownerAddress,
253 : required String toAddress,
254 : required Amount amount,
255 : bool visible = true,
256 : }) {
257 0 : return postCall<JSON>(
258 0 : "$baseURL/wallet/createtransaction",
259 0 : data: {
260 : "owner_address": ownerAddress,
261 : "to_address": toAddress,
262 0 : "amount": amount.value.toInt(),
263 : "visible": visible,
264 : },
265 : );
266 : }
267 :
268 0 : Future<JSON> broadcastTransaction({required JSON json}) {
269 0 : return postCall<JSON>(
270 0 : "$baseURL/wallet/broadcasttransaction",
271 : data: json,
272 : );
273 : }
274 :
275 0 : Future<JSON> getBlockByNumber(int number) {
276 0 : return postCall<JSON>(
277 0 : "$baseURL/wallet/getblockbynum",
278 0 : data: {
279 : "num": number,
280 : },
281 : );
282 : }
283 :
284 : // Future<JSON> transferTRC20({
285 : // required String address,
286 : // required EthBasedTokenEntity trc20,
287 : // }){
288 :
289 : // }
290 : }
|