@shapeshiftoss/unchained-client 10.14.7-alpha.1 → 10.14.8
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/dist/cjs/.tsbuildinfo +1 -1
- package/dist/cjs/generated/thorchain/apis/ProxyApi.d.ts +12 -4
- package/dist/cjs/generated/thorchain/apis/ProxyApi.js +26 -6
- package/dist/cjs/index.d.ts +1 -0
- package/dist/cjs/index.js +2 -1
- package/dist/cjs/tron/api.d.ts +66 -0
- package/dist/cjs/tron/api.js +210 -0
- package/dist/cjs/tron/index.d.ts +3 -0
- package/dist/cjs/tron/index.js +19 -0
- package/dist/cjs/tron/parser/index.d.ts +16 -0
- package/dist/cjs/tron/parser/index.js +75 -0
- package/dist/cjs/tron/types.d.ts +66 -0
- package/dist/cjs/tron/types.js +2 -0
- package/dist/esm/.tsbuildinfo +1 -1
- package/dist/esm/generated/thorchain/apis/ProxyApi.d.ts +12 -4
- package/dist/esm/generated/thorchain/apis/ProxyApi.js +26 -6
- package/dist/esm/index.d.ts +1 -0
- package/dist/esm/index.js +1 -0
- package/dist/esm/tron/api.d.ts +66 -0
- package/dist/esm/tron/api.js +206 -0
- package/dist/esm/tron/index.d.ts +3 -0
- package/dist/esm/tron/index.js +3 -0
- package/dist/esm/tron/parser/index.d.ts +16 -0
- package/dist/esm/tron/parser/index.js +71 -0
- package/dist/esm/tron/types.d.ts +66 -0
- package/dist/esm/tron/types.js +1 -0
- package/package.json +5 -5
|
@@ -14,6 +14,14 @@ import * as runtime from '../runtime';
|
|
|
14
14
|
*
|
|
15
15
|
*/
|
|
16
16
|
export declare class ProxyApi extends runtime.BaseAPI {
|
|
17
|
+
/**
|
|
18
|
+
* Thorchain rpc rest api endpoints.
|
|
19
|
+
*/
|
|
20
|
+
getRPCRaw(initOverrides?: RequestInit | runtime.InitOverrideFunction): Promise<runtime.ApiResponse<void>>;
|
|
21
|
+
/**
|
|
22
|
+
* Thorchain rpc rest api endpoints.
|
|
23
|
+
*/
|
|
24
|
+
getRPC(initOverrides?: RequestInit | runtime.InitOverrideFunction): Promise<void>;
|
|
17
25
|
/**
|
|
18
26
|
* Thorchain lcd rest api endpoints.
|
|
19
27
|
*/
|
|
@@ -31,11 +39,11 @@ export declare class ProxyApi extends runtime.BaseAPI {
|
|
|
31
39
|
*/
|
|
32
40
|
midgard(initOverrides?: RequestInit | runtime.InitOverrideFunction): Promise<void>;
|
|
33
41
|
/**
|
|
34
|
-
* Thorchain rpc
|
|
42
|
+
* Thorchain rpc jsonrpc endpoints.
|
|
35
43
|
*/
|
|
36
|
-
|
|
44
|
+
postRPCRaw(initOverrides?: RequestInit | runtime.InitOverrideFunction): Promise<runtime.ApiResponse<void>>;
|
|
37
45
|
/**
|
|
38
|
-
* Thorchain rpc
|
|
46
|
+
* Thorchain rpc jsonrpc endpoints.
|
|
39
47
|
*/
|
|
40
|
-
|
|
48
|
+
postRPC(initOverrides?: RequestInit | runtime.InitOverrideFunction): Promise<void>;
|
|
41
49
|
}
|
|
@@ -43,6 +43,26 @@ const runtime = __importStar(require("../runtime"));
|
|
|
43
43
|
*
|
|
44
44
|
*/
|
|
45
45
|
class ProxyApi extends runtime.BaseAPI {
|
|
46
|
+
/**
|
|
47
|
+
* Thorchain rpc rest api endpoints.
|
|
48
|
+
*/
|
|
49
|
+
async getRPCRaw(initOverrides) {
|
|
50
|
+
const queryParameters = {};
|
|
51
|
+
const headerParameters = {};
|
|
52
|
+
const response = await this.request({
|
|
53
|
+
path: `/rpc`,
|
|
54
|
+
method: 'GET',
|
|
55
|
+
headers: headerParameters,
|
|
56
|
+
query: queryParameters,
|
|
57
|
+
}, initOverrides);
|
|
58
|
+
return new runtime.VoidApiResponse(response);
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Thorchain rpc rest api endpoints.
|
|
62
|
+
*/
|
|
63
|
+
async getRPC(initOverrides) {
|
|
64
|
+
await this.getRPCRaw(initOverrides);
|
|
65
|
+
}
|
|
46
66
|
/**
|
|
47
67
|
* Thorchain lcd rest api endpoints.
|
|
48
68
|
*/
|
|
@@ -84,24 +104,24 @@ class ProxyApi extends runtime.BaseAPI {
|
|
|
84
104
|
await this.midgardRaw(initOverrides);
|
|
85
105
|
}
|
|
86
106
|
/**
|
|
87
|
-
* Thorchain rpc
|
|
107
|
+
* Thorchain rpc jsonrpc endpoints.
|
|
88
108
|
*/
|
|
89
|
-
async
|
|
109
|
+
async postRPCRaw(initOverrides) {
|
|
90
110
|
const queryParameters = {};
|
|
91
111
|
const headerParameters = {};
|
|
92
112
|
const response = await this.request({
|
|
93
113
|
path: `/rpc`,
|
|
94
|
-
method: '
|
|
114
|
+
method: 'POST',
|
|
95
115
|
headers: headerParameters,
|
|
96
116
|
query: queryParameters,
|
|
97
117
|
}, initOverrides);
|
|
98
118
|
return new runtime.VoidApiResponse(response);
|
|
99
119
|
}
|
|
100
120
|
/**
|
|
101
|
-
* Thorchain rpc
|
|
121
|
+
* Thorchain rpc jsonrpc endpoints.
|
|
102
122
|
*/
|
|
103
|
-
async
|
|
104
|
-
await this.
|
|
123
|
+
async postRPC(initOverrides) {
|
|
124
|
+
await this.postRPCRaw(initOverrides);
|
|
105
125
|
}
|
|
106
126
|
}
|
|
107
127
|
exports.ProxyApi = ProxyApi;
|
package/dist/cjs/index.d.ts
CHANGED
|
@@ -4,6 +4,7 @@ export * as evm from './evm';
|
|
|
4
4
|
export * as utxo from './utxo';
|
|
5
5
|
export * as cosmossdk from './cosmossdk';
|
|
6
6
|
export * as solana from './solana';
|
|
7
|
+
export * as tron from './tron';
|
|
7
8
|
export * as common from './parser';
|
|
8
9
|
export * as ethereum from './evm/ethereum';
|
|
9
10
|
export * as avalanche from './evm/avalanche';
|
package/dist/cjs/index.js
CHANGED
|
@@ -26,13 +26,14 @@ var __importStar = (this && this.__importStar) || function (mod) {
|
|
|
26
26
|
return result;
|
|
27
27
|
};
|
|
28
28
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
29
|
-
exports.mayachain = exports.thorchainV1 = exports.thorchain = exports.cosmos = exports.litecoin = exports.dogecoin = exports.bitcoincash = exports.bitcoin = exports.base = exports.arbitrumNova = exports.arbitrum = exports.gnosis = exports.polygon = exports.bnbsmartchain = exports.optimism = exports.avalanche = exports.ethereum = exports.common = exports.solana = exports.cosmossdk = exports.utxo = exports.evm = exports.ws = void 0;
|
|
29
|
+
exports.mayachain = exports.thorchainV1 = exports.thorchain = exports.cosmos = exports.litecoin = exports.dogecoin = exports.bitcoincash = exports.bitcoin = exports.base = exports.arbitrumNova = exports.arbitrum = exports.gnosis = exports.polygon = exports.bnbsmartchain = exports.optimism = exports.avalanche = exports.ethereum = exports.common = exports.tron = exports.solana = exports.cosmossdk = exports.utxo = exports.evm = exports.ws = void 0;
|
|
30
30
|
__exportStar(require("./types"), exports);
|
|
31
31
|
exports.ws = __importStar(require("./websocket"));
|
|
32
32
|
exports.evm = __importStar(require("./evm"));
|
|
33
33
|
exports.utxo = __importStar(require("./utxo"));
|
|
34
34
|
exports.cosmossdk = __importStar(require("./cosmossdk"));
|
|
35
35
|
exports.solana = __importStar(require("./solana"));
|
|
36
|
+
exports.tron = __importStar(require("./tron"));
|
|
36
37
|
exports.common = __importStar(require("./parser"));
|
|
37
38
|
exports.ethereum = __importStar(require("./evm/ethereum"));
|
|
38
39
|
exports.avalanche = __importStar(require("./evm/avalanche"));
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import type { TronBlock, TronTx } from './types';
|
|
2
|
+
export interface TronApiConfig {
|
|
3
|
+
rpcUrl: string;
|
|
4
|
+
}
|
|
5
|
+
export declare class TronApi {
|
|
6
|
+
private readonly rpcUrl;
|
|
7
|
+
private tronWeb;
|
|
8
|
+
private requestQueue;
|
|
9
|
+
private readonly minRequestInterval;
|
|
10
|
+
constructor(config: TronApiConfig);
|
|
11
|
+
getRpcUrl(): string;
|
|
12
|
+
private throttle;
|
|
13
|
+
private getTronWeb;
|
|
14
|
+
getAccount(params: {
|
|
15
|
+
pubkey: string;
|
|
16
|
+
}): Promise<{
|
|
17
|
+
balance: string;
|
|
18
|
+
unconfirmedBalance: string;
|
|
19
|
+
tokens?: {
|
|
20
|
+
contractAddress: string;
|
|
21
|
+
balance: string;
|
|
22
|
+
}[];
|
|
23
|
+
}>;
|
|
24
|
+
getTRC20Balance(params: {
|
|
25
|
+
address: string;
|
|
26
|
+
contractAddress: string;
|
|
27
|
+
}): Promise<string>;
|
|
28
|
+
getTxHistory(_params: {
|
|
29
|
+
pubkey: string;
|
|
30
|
+
pageSize?: number;
|
|
31
|
+
cursor?: string;
|
|
32
|
+
}): Promise<{
|
|
33
|
+
txs: TronTx[];
|
|
34
|
+
cursor?: string;
|
|
35
|
+
}>;
|
|
36
|
+
getTransaction(params: {
|
|
37
|
+
txid: string;
|
|
38
|
+
}): Promise<TronTx | null>;
|
|
39
|
+
getBlock(params: {
|
|
40
|
+
height: number;
|
|
41
|
+
}): Promise<TronBlock | null>;
|
|
42
|
+
sendTx(params: {
|
|
43
|
+
sendTxBody: {
|
|
44
|
+
hex: string;
|
|
45
|
+
};
|
|
46
|
+
}): Promise<string>;
|
|
47
|
+
private getChainPrices;
|
|
48
|
+
estimateFees(params: {
|
|
49
|
+
estimateFeesBody: {
|
|
50
|
+
serializedTx: string;
|
|
51
|
+
};
|
|
52
|
+
}): Promise<string>;
|
|
53
|
+
estimateTRC20TransferFee(params: {
|
|
54
|
+
contractAddress: string;
|
|
55
|
+
from: string;
|
|
56
|
+
to: string;
|
|
57
|
+
amount: string;
|
|
58
|
+
}): Promise<string>;
|
|
59
|
+
getPriorityFees(): Promise<{
|
|
60
|
+
baseFee: string;
|
|
61
|
+
fast: string;
|
|
62
|
+
average: string;
|
|
63
|
+
slow: string;
|
|
64
|
+
estimatedBandwidth: string;
|
|
65
|
+
}>;
|
|
66
|
+
}
|
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.TronApi = void 0;
|
|
4
|
+
const tronweb_1 = require("tronweb");
|
|
5
|
+
class TronApi {
|
|
6
|
+
rpcUrl;
|
|
7
|
+
tronWeb = null;
|
|
8
|
+
requestQueue = Promise.resolve();
|
|
9
|
+
minRequestInterval = 1_500;
|
|
10
|
+
constructor(config) {
|
|
11
|
+
this.rpcUrl = config.rpcUrl;
|
|
12
|
+
}
|
|
13
|
+
getRpcUrl() {
|
|
14
|
+
return this.rpcUrl;
|
|
15
|
+
}
|
|
16
|
+
async throttle() {
|
|
17
|
+
// Queue the request and wait for all previous requests to complete
|
|
18
|
+
const currentRequest = this.requestQueue.then(async () => {
|
|
19
|
+
await new Promise(resolve => setTimeout(resolve, this.minRequestInterval));
|
|
20
|
+
});
|
|
21
|
+
this.requestQueue = currentRequest;
|
|
22
|
+
await currentRequest;
|
|
23
|
+
}
|
|
24
|
+
getTronWeb() {
|
|
25
|
+
if (!this.tronWeb) {
|
|
26
|
+
this.tronWeb = new tronweb_1.TronWeb({ fullHost: this.rpcUrl });
|
|
27
|
+
}
|
|
28
|
+
return this.tronWeb;
|
|
29
|
+
}
|
|
30
|
+
async getAccount(params) {
|
|
31
|
+
await this.throttle();
|
|
32
|
+
const response = await fetch(`${this.rpcUrl}/wallet/getaccount`, {
|
|
33
|
+
method: 'POST',
|
|
34
|
+
headers: { 'Content-Type': 'application/json' },
|
|
35
|
+
body: JSON.stringify({ address: params.pubkey, visible: true }),
|
|
36
|
+
});
|
|
37
|
+
const data = await response.json();
|
|
38
|
+
const tokens = [];
|
|
39
|
+
// Get TRC10 tokens from assetV2
|
|
40
|
+
if (data.assetV2) {
|
|
41
|
+
data.assetV2.forEach(token => {
|
|
42
|
+
tokens.push({
|
|
43
|
+
contractAddress: token.key,
|
|
44
|
+
balance: String(token.value),
|
|
45
|
+
});
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
// Get TRC20 tokens from TronGrid API
|
|
49
|
+
try {
|
|
50
|
+
await this.throttle();
|
|
51
|
+
const trc20Response = await fetch(`${this.rpcUrl}/v1/accounts/${params.pubkey}`);
|
|
52
|
+
const trc20Data = await trc20Response.json();
|
|
53
|
+
if (trc20Data.data?.[0]?.trc20 && Array.isArray(trc20Data.data[0].trc20)) {
|
|
54
|
+
// trc20 is an array of objects like [{contractAddress: balance}, ...]
|
|
55
|
+
trc20Data.data[0].trc20.forEach((tokenObj) => {
|
|
56
|
+
for (const [contractAddress, balance] of Object.entries(tokenObj)) {
|
|
57
|
+
tokens.push({
|
|
58
|
+
contractAddress,
|
|
59
|
+
balance: String(balance),
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
catch (err) {
|
|
66
|
+
// TRC20 fetch failed, continue with just TRC10 tokens
|
|
67
|
+
console.error('Failed to fetch TRC20 tokens:', err);
|
|
68
|
+
}
|
|
69
|
+
return {
|
|
70
|
+
balance: data.balance ? String(data.balance) : '0',
|
|
71
|
+
unconfirmedBalance: '0',
|
|
72
|
+
tokens,
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
async getTRC20Balance(params) {
|
|
76
|
+
try {
|
|
77
|
+
const tronWeb = await this.getTronWeb();
|
|
78
|
+
const contract = await tronWeb.contract().at(params.contractAddress);
|
|
79
|
+
const balance = await contract.balanceOf(params.address).call();
|
|
80
|
+
return balance.toString();
|
|
81
|
+
}
|
|
82
|
+
catch (_err) {
|
|
83
|
+
return '0';
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
getTxHistory(_params) {
|
|
87
|
+
throw new Error('Transaction history is not supported for TRON');
|
|
88
|
+
}
|
|
89
|
+
async getTransaction(params) {
|
|
90
|
+
await this.throttle();
|
|
91
|
+
const response = await fetch(`${this.rpcUrl}/wallet/gettransactionbyid`, {
|
|
92
|
+
method: 'POST',
|
|
93
|
+
headers: { 'Content-Type': 'application/json' },
|
|
94
|
+
body: JSON.stringify({ value: params.txid, visible: true }),
|
|
95
|
+
});
|
|
96
|
+
if (!response.ok) {
|
|
97
|
+
return null;
|
|
98
|
+
}
|
|
99
|
+
const tx = await response.json();
|
|
100
|
+
if (!tx || !tx.txID) {
|
|
101
|
+
return null;
|
|
102
|
+
}
|
|
103
|
+
return {
|
|
104
|
+
...tx,
|
|
105
|
+
txid: tx.txID,
|
|
106
|
+
blockHash: '',
|
|
107
|
+
blockHeight: 0,
|
|
108
|
+
timestamp: 0,
|
|
109
|
+
confirmations: 0,
|
|
110
|
+
value: '0',
|
|
111
|
+
fee: '0',
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
async getBlock(params) {
|
|
115
|
+
await this.throttle();
|
|
116
|
+
const response = await fetch(`${this.rpcUrl}/wallet/getblockbynum`, {
|
|
117
|
+
method: 'POST',
|
|
118
|
+
headers: { 'Content-Type': 'application/json' },
|
|
119
|
+
body: JSON.stringify({ num: params.height }),
|
|
120
|
+
});
|
|
121
|
+
if (!response.ok) {
|
|
122
|
+
return null;
|
|
123
|
+
}
|
|
124
|
+
return await response.json();
|
|
125
|
+
}
|
|
126
|
+
async sendTx(params) {
|
|
127
|
+
const tronWeb = this.getTronWeb();
|
|
128
|
+
try {
|
|
129
|
+
const signedTxJson = JSON.parse(params.sendTxBody.hex);
|
|
130
|
+
const result = await tronWeb.trx.sendRawTransaction(signedTxJson);
|
|
131
|
+
if (!result.result) {
|
|
132
|
+
throw new Error(result.message || JSON.stringify(result) || 'Failed to broadcast transaction');
|
|
133
|
+
}
|
|
134
|
+
const txid = result.txid || result.transaction?.txID;
|
|
135
|
+
if (!txid) {
|
|
136
|
+
throw new Error('Transaction ID not found in broadcast result');
|
|
137
|
+
}
|
|
138
|
+
return txid;
|
|
139
|
+
}
|
|
140
|
+
catch (error) {
|
|
141
|
+
throw new Error(`Failed to broadcast TRON transaction: ${error}`);
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
async getChainPrices() {
|
|
145
|
+
try {
|
|
146
|
+
const tronWeb = await this.getTronWeb();
|
|
147
|
+
const params = await tronWeb.trx.getChainParameters();
|
|
148
|
+
const bandwidthPrice = params.find(p => p.key === 'getTransactionFee')?.value ?? 1000;
|
|
149
|
+
const energyPrice = params.find(p => p.key === 'getEnergyFee')?.value ?? 420;
|
|
150
|
+
return { bandwidthPrice, energyPrice };
|
|
151
|
+
}
|
|
152
|
+
catch (_err) {
|
|
153
|
+
return { bandwidthPrice: 1000, energyPrice: 420 };
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
async estimateFees(params) {
|
|
157
|
+
try {
|
|
158
|
+
const { bandwidthPrice } = await this.getChainPrices();
|
|
159
|
+
const rawDataBytes = Buffer.from(params.estimateFeesBody.serializedTx, 'hex').length;
|
|
160
|
+
const signatureBytes = 65;
|
|
161
|
+
const totalBytes = rawDataBytes + signatureBytes;
|
|
162
|
+
const feeInSun = totalBytes * bandwidthPrice;
|
|
163
|
+
return String(feeInSun);
|
|
164
|
+
}
|
|
165
|
+
catch (err) {
|
|
166
|
+
throw new Error(`Failed to estimate fees: ${err}`);
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
async estimateTRC20TransferFee(params) {
|
|
170
|
+
try {
|
|
171
|
+
const tronWeb = await this.getTronWeb();
|
|
172
|
+
const { energyPrice } = await this.getChainPrices();
|
|
173
|
+
const result = await tronWeb.transactionBuilder.triggerConstantContract(params.contractAddress, 'transfer(address,uint256)', {}, [
|
|
174
|
+
{ type: 'address', value: params.to },
|
|
175
|
+
{ type: 'uint256', value: params.amount },
|
|
176
|
+
], params.from);
|
|
177
|
+
const energyUsed = result.energy_used ?? 0;
|
|
178
|
+
const feeInSun = energyUsed * energyPrice;
|
|
179
|
+
return String(feeInSun);
|
|
180
|
+
}
|
|
181
|
+
catch (_err) {
|
|
182
|
+
return '31000000';
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
async getPriorityFees() {
|
|
186
|
+
try {
|
|
187
|
+
const { bandwidthPrice } = await this.getChainPrices();
|
|
188
|
+
const estimatedBytes = 268;
|
|
189
|
+
const baseFee = String(estimatedBytes * bandwidthPrice);
|
|
190
|
+
return {
|
|
191
|
+
baseFee,
|
|
192
|
+
fast: baseFee,
|
|
193
|
+
average: baseFee,
|
|
194
|
+
slow: baseFee,
|
|
195
|
+
estimatedBandwidth: String(estimatedBytes),
|
|
196
|
+
};
|
|
197
|
+
}
|
|
198
|
+
catch (_err) {
|
|
199
|
+
const defaultFee = '268000';
|
|
200
|
+
return {
|
|
201
|
+
baseFee: defaultFee,
|
|
202
|
+
fast: defaultFee,
|
|
203
|
+
average: defaultFee,
|
|
204
|
+
slow: defaultFee,
|
|
205
|
+
estimatedBandwidth: '268',
|
|
206
|
+
};
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
exports.TronApi = TronApi;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./api"), exports);
|
|
18
|
+
__exportStar(require("./parser"), exports);
|
|
19
|
+
__exportStar(require("./types"), exports);
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { AssetId, ChainId } from '@shapeshiftoss/caip';
|
|
2
|
+
import type { ParsedTx } from '../../types';
|
|
3
|
+
import type { TronTx } from '../types';
|
|
4
|
+
export interface TransactionParserArgs {
|
|
5
|
+
assetId: AssetId;
|
|
6
|
+
chainId: ChainId;
|
|
7
|
+
}
|
|
8
|
+
export declare class TransactionParser {
|
|
9
|
+
assetId: AssetId;
|
|
10
|
+
chainId: ChainId;
|
|
11
|
+
assetReference: string;
|
|
12
|
+
constructor(args: TransactionParserArgs);
|
|
13
|
+
parse(tx: TronTx, address: string): Promise<ParsedTx>;
|
|
14
|
+
private getStatus;
|
|
15
|
+
private getTransfers;
|
|
16
|
+
}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.TransactionParser = void 0;
|
|
4
|
+
const caip_1 = require("@shapeshiftoss/caip");
|
|
5
|
+
const types_1 = require("../../types");
|
|
6
|
+
class TransactionParser {
|
|
7
|
+
assetId;
|
|
8
|
+
chainId;
|
|
9
|
+
assetReference;
|
|
10
|
+
constructor(args) {
|
|
11
|
+
this.assetId = args.assetId;
|
|
12
|
+
this.chainId = args.chainId;
|
|
13
|
+
const { assetReference } = (0, caip_1.fromAssetId)(this.assetId);
|
|
14
|
+
this.assetReference = assetReference;
|
|
15
|
+
}
|
|
16
|
+
parse(tx, address) {
|
|
17
|
+
const parsedTx = {
|
|
18
|
+
address,
|
|
19
|
+
blockHash: tx.blockHash || '',
|
|
20
|
+
blockHeight: tx.blockHeight || 0,
|
|
21
|
+
blockTime: tx.timestamp ? Math.floor(tx.timestamp / 1000) : 0,
|
|
22
|
+
chainId: this.chainId,
|
|
23
|
+
confirmations: tx.confirmations || 0,
|
|
24
|
+
status: this.getStatus(tx),
|
|
25
|
+
transfers: this.getTransfers(tx, address),
|
|
26
|
+
txid: tx.txID,
|
|
27
|
+
trade: undefined,
|
|
28
|
+
fee: {
|
|
29
|
+
assetId: this.assetId,
|
|
30
|
+
value: tx.fee || '0',
|
|
31
|
+
},
|
|
32
|
+
};
|
|
33
|
+
return Promise.resolve(parsedTx);
|
|
34
|
+
}
|
|
35
|
+
getStatus(tx) {
|
|
36
|
+
if (tx.confirmations && tx.confirmations > 0) {
|
|
37
|
+
return types_1.TxStatus.Confirmed;
|
|
38
|
+
}
|
|
39
|
+
return types_1.TxStatus.Pending;
|
|
40
|
+
}
|
|
41
|
+
getTransfers(tx, address) {
|
|
42
|
+
const transfers = [];
|
|
43
|
+
if (!tx.raw_data || !tx.raw_data.contract) {
|
|
44
|
+
return transfers;
|
|
45
|
+
}
|
|
46
|
+
for (const contract of tx.raw_data.contract) {
|
|
47
|
+
if (contract.type === 'TransferContract') {
|
|
48
|
+
const { owner_address, to_address, amount } = contract.parameter.value;
|
|
49
|
+
const value = String(amount || 0);
|
|
50
|
+
if (owner_address === address) {
|
|
51
|
+
transfers.push({
|
|
52
|
+
assetId: this.assetId,
|
|
53
|
+
from: owner_address || '',
|
|
54
|
+
to: to_address || '',
|
|
55
|
+
type: types_1.TransferType.Send,
|
|
56
|
+
totalValue: value,
|
|
57
|
+
components: [{ value }],
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
else if (to_address === address) {
|
|
61
|
+
transfers.push({
|
|
62
|
+
assetId: this.assetId,
|
|
63
|
+
from: owner_address || '',
|
|
64
|
+
to: to_address || '',
|
|
65
|
+
type: types_1.TransferType.Receive,
|
|
66
|
+
totalValue: value,
|
|
67
|
+
components: [{ value }],
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
return transfers;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
exports.TransactionParser = TransactionParser;
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
export interface TronTx {
|
|
2
|
+
txid: string;
|
|
3
|
+
blockHash: string;
|
|
4
|
+
blockHeight: number;
|
|
5
|
+
timestamp: number;
|
|
6
|
+
confirmations: number;
|
|
7
|
+
value: string;
|
|
8
|
+
fee: string;
|
|
9
|
+
raw_data: {
|
|
10
|
+
contract: {
|
|
11
|
+
parameter: {
|
|
12
|
+
value: {
|
|
13
|
+
amount?: number;
|
|
14
|
+
owner_address?: string;
|
|
15
|
+
to_address?: string;
|
|
16
|
+
};
|
|
17
|
+
type_url: string;
|
|
18
|
+
};
|
|
19
|
+
type: string;
|
|
20
|
+
}[];
|
|
21
|
+
ref_block_bytes: string;
|
|
22
|
+
ref_block_hash: string;
|
|
23
|
+
expiration: number;
|
|
24
|
+
timestamp: number;
|
|
25
|
+
};
|
|
26
|
+
raw_data_hex: string;
|
|
27
|
+
txID: string;
|
|
28
|
+
signature?: string[];
|
|
29
|
+
}
|
|
30
|
+
type TRC20Token = {
|
|
31
|
+
key: string;
|
|
32
|
+
value: number;
|
|
33
|
+
};
|
|
34
|
+
export interface TronAccount {
|
|
35
|
+
address: string;
|
|
36
|
+
balance: number;
|
|
37
|
+
create_time?: number;
|
|
38
|
+
latest_opration_time?: number;
|
|
39
|
+
account_resource?: {
|
|
40
|
+
energy_usage?: number;
|
|
41
|
+
frozen_balance_for_energy?: {
|
|
42
|
+
frozen_balance?: number;
|
|
43
|
+
};
|
|
44
|
+
net_usage?: number;
|
|
45
|
+
frozen_balance_for_net?: {
|
|
46
|
+
frozen_balance?: number;
|
|
47
|
+
};
|
|
48
|
+
};
|
|
49
|
+
assetV2?: TRC20Token[];
|
|
50
|
+
}
|
|
51
|
+
export interface TronBlock {
|
|
52
|
+
blockID: string;
|
|
53
|
+
block_header: {
|
|
54
|
+
raw_data: {
|
|
55
|
+
number: number;
|
|
56
|
+
txTrieRoot: string;
|
|
57
|
+
witness_address: string;
|
|
58
|
+
parentHash: string;
|
|
59
|
+
version: number;
|
|
60
|
+
timestamp: number;
|
|
61
|
+
};
|
|
62
|
+
witness_signature: string;
|
|
63
|
+
};
|
|
64
|
+
transactions?: TronTx[];
|
|
65
|
+
}
|
|
66
|
+
export {};
|