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