|
| 1 | +import {SafeheronClient} from '../safeheron'; |
| 2 | +import {SafeheronConfig} from "../config"; |
| 3 | + |
| 4 | +export interface GasStatusResponse { |
| 5 | + /** |
| 6 | + * Gas Balance |
| 7 | + */ |
| 8 | + gasBalance: Array<GasBalance>; |
| 9 | +} |
| 10 | + |
| 11 | +export interface GasBalance { |
| 12 | + /** |
| 13 | + * Coin |
| 14 | + */ |
| 15 | + symbol: string; |
| 16 | + |
| 17 | + /** |
| 18 | + * Balance |
| 19 | + */ |
| 20 | + amount: string; |
| 21 | +} |
| 22 | + |
| 23 | + |
| 24 | +export interface GasTransactionsGetByTxKeyRequest { |
| 25 | + /** |
| 26 | + * Transaction key, obtained from transactions created via the Create a Transaction V3 API, App, or Web Console. |
| 27 | + */ |
| 28 | + txKey: string; |
| 29 | +} |
| 30 | + |
| 31 | +export interface GasTransactionsGetByTxKeyResponse { |
| 32 | + /** |
| 33 | + * Transaction key |
| 34 | + */ |
| 35 | + txKey: string; |
| 36 | + |
| 37 | + /** |
| 38 | + * Transaction fee coin |
| 39 | + */ |
| 40 | + symbol: string; |
| 41 | + |
| 42 | + /** |
| 43 | + * Total fee amount. A single transaction may have multiple Gas records; the total fee paid is the sum of all records with SUCCESS and FAILURE_GAS_CONSUMED statuses. |
| 44 | + */ |
| 45 | + totalAmount: string; |
| 46 | + |
| 47 | + /** |
| 48 | + * Gas records |
| 49 | + */ |
| 50 | + detailList: Array<Detail>; |
| 51 | +} |
| 52 | + |
| 53 | + |
| 54 | + |
| 55 | + |
| 56 | +export interface Detail { |
| 57 | + /** |
| 58 | + * Energy rental transaction Key |
| 59 | + */ |
| 60 | + gasServiceTxKey: string; |
| 61 | + |
| 62 | + /** |
| 63 | + * Transaction fee coin |
| 64 | + */ |
| 65 | + symbol: string; |
| 66 | + |
| 67 | + /** |
| 68 | + * Amount |
| 69 | + */ |
| 70 | + amount: string; |
| 71 | + |
| 72 | + /** |
| 73 | + * Balance after paying the fee |
| 74 | + */ |
| 75 | + balance: string; |
| 76 | + |
| 77 | + /** |
| 78 | + * SUCCESS: Gas successful |
| 79 | + * FAILURE_GAS_REFUNDED: Gas failed, refunded |
| 80 | + * FAILURE_GAS_CONSUMED: Gas failed, but fees were incurred |
| 81 | + */ |
| 82 | + status: string; |
| 83 | + |
| 84 | + /** |
| 85 | + * Gas resource type, only valid for TRON network: |
| 86 | + * ENERGY |
| 87 | + * BANDWIDTH |
| 88 | + */ |
| 89 | + resourceType: string; |
| 90 | + |
| 91 | + /** |
| 92 | + * Gas deduction time, UNIX timestamp in milliseconds |
| 93 | + */ |
| 94 | + timestamp: string; |
| 95 | +} |
| 96 | + |
| 97 | +export class GasApi { |
| 98 | + |
| 99 | + private client: SafeheronClient; |
| 100 | + |
| 101 | + constructor(config: SafeheronConfig) { |
| 102 | + this.client = new SafeheronClient({ |
| 103 | + baseUrl: config.baseUrl, |
| 104 | + apiKey: config.apiKey, |
| 105 | + rsaPrivateKey: config.rsaPrivateKey, |
| 106 | + safeheronRsaPublicKey: config.safeheronRsaPublicKey, |
| 107 | + requestTimeout: config.requestTimeout |
| 108 | + }); |
| 109 | + } |
| 110 | + |
| 111 | + |
| 112 | + /** |
| 113 | + * Retrieve Gas Balance |
| 114 | + * Retrieve your Gas balance for the TRON energy rental service. |
| 115 | + */ |
| 116 | + async gasStatus(): Promise<GasStatusResponse> { |
| 117 | + return await this.client.doRequest<null, GasStatusResponse>('/v1/gas/status', null); |
| 118 | + } |
| 119 | + |
| 120 | + |
| 121 | + /** |
| 122 | + * Retrieve Automatic Gas Records for Transactions |
| 123 | + * When the TRON energy rental service is enabled, Safeheron automatically tops up the required Gas fees for TRON network transactions. This API allows you to query the energy rental records used by a transaction. A single transaction may have multiple records. The actual Gas fee consumed by the transaction is the sum of all records with SUCCESS and FAILURE_GAS_CONSUMED statuses. |
| 124 | + */ |
| 125 | + async gasTransactionsGetByTxKey(request: GasTransactionsGetByTxKeyRequest): Promise<GasTransactionsGetByTxKeyResponse> { |
| 126 | + return await this.client.doRequest<GasTransactionsGetByTxKeyRequest, GasTransactionsGetByTxKeyResponse>('/v1/gas/transactions/getByTxKey', request); |
| 127 | + } |
| 128 | +} |
0 commit comments