@clonegod/ttd-sui-common 1.0.22 → 1.0.23
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/grpc/gas-price-cache.js +2 -2
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/trade/check/index.d.ts +1 -0
- package/dist/trade/check/index.js +5 -0
- package/dist/trade/check/tx_result_checker.d.ts +13 -0
- package/dist/trade/check/tx_result_checker.js +102 -0
- package/dist/trade/index.d.ts +3 -0
- package/dist/trade/index.js +19 -0
- package/dist/trade/parse/index.d.ts +1 -0
- package/dist/trade/parse/index.js +5 -0
- package/dist/trade/parse/sui_tx_parser.d.ts +13 -0
- package/dist/trade/parse/sui_tx_parser.js +165 -0
- package/dist/trade/send_tx/index.d.ts +10 -0
- package/dist/trade/send_tx/index.js +48 -0
- package/package.json +1 -1
|
@@ -15,7 +15,7 @@ class GasPriceCache {
|
|
|
15
15
|
constructor(ledgerService) {
|
|
16
16
|
this.cachedGasPrice = 0;
|
|
17
17
|
this.lastUpdateTime = 0;
|
|
18
|
-
this.updateInterval =
|
|
18
|
+
this.updateInterval = 60 * 60 * 1000;
|
|
19
19
|
this.ledgerService = ledgerService;
|
|
20
20
|
this.startAutoRefresh();
|
|
21
21
|
}
|
|
@@ -39,7 +39,7 @@ class GasPriceCache {
|
|
|
39
39
|
const epochRes = yield this.ledgerService.getEpoch();
|
|
40
40
|
this.cachedGasPrice = Number(epochRes['epoch']['reference_gas_price']);
|
|
41
41
|
this.lastUpdateTime = Date.now();
|
|
42
|
-
(0, dist_1.log_info)(`
|
|
42
|
+
(0, dist_1.log_info)(`refreshGasPrice success: ${this.cachedGasPrice} MIST`);
|
|
43
43
|
}
|
|
44
44
|
catch (error) {
|
|
45
45
|
(0, dist_1.log_error)('refresh gas price failed', error);
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { TransactionResultChecker } from "./tx_result_checker";
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.TransactionResultChecker = void 0;
|
|
4
|
+
var tx_result_checker_1 = require("./tx_result_checker");
|
|
5
|
+
Object.defineProperty(exports, "TransactionResultChecker", { enumerable: true, get: function () { return tx_result_checker_1.TransactionResultChecker; } });
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { EnvArgs } from "@clonegod/ttd-core/dist";
|
|
2
|
+
import { AbstractTransactionResultCheck } from "@clonegod/ttd-core/dist/trade";
|
|
3
|
+
import { EventEmitter } from 'events';
|
|
4
|
+
import { SuiTransactionParser } from "../parse/sui_tx_parser";
|
|
5
|
+
import { LedgerService } from "../../index";
|
|
6
|
+
export declare class TransactionResultChecker extends AbstractTransactionResultCheck {
|
|
7
|
+
transactionParser: SuiTransactionParser;
|
|
8
|
+
leadgerService: LedgerService;
|
|
9
|
+
constructor(env_args: EnvArgs, event_emitter: EventEmitter, wallet_address: string, ledgerService: LedgerService);
|
|
10
|
+
check_tx_result_interval(): Promise<void>;
|
|
11
|
+
on_subscibe_transaction(): void;
|
|
12
|
+
private processTransactionResult;
|
|
13
|
+
}
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.TransactionResultChecker = void 0;
|
|
13
|
+
const dist_1 = require("@clonegod/ttd-core/dist");
|
|
14
|
+
const trade_1 = require("@clonegod/ttd-core/dist/trade");
|
|
15
|
+
const sui_tx_parser_1 = require("../parse/sui_tx_parser");
|
|
16
|
+
const index_1 = require("../../index");
|
|
17
|
+
class TransactionResultChecker extends trade_1.AbstractTransactionResultCheck {
|
|
18
|
+
constructor(env_args, event_emitter, wallet_address, ledgerService) {
|
|
19
|
+
super(env_args, event_emitter);
|
|
20
|
+
this.transactionParser = new sui_tx_parser_1.SuiTransactionParser(wallet_address);
|
|
21
|
+
this.leadgerService = ledgerService;
|
|
22
|
+
}
|
|
23
|
+
check_tx_result_interval() {
|
|
24
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
25
|
+
const check_start_time = Date.now();
|
|
26
|
+
const check_interval = parseInt(process.env.CHECK_TX_RESULT_INTERVAL_MILLS || '3000');
|
|
27
|
+
const check_timeout = parseInt(process.env.CHECK_TX_RESULT_TIMEOUT_MILLS || '30000');
|
|
28
|
+
if (check_interval >= check_timeout) {
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
const intervalId = setInterval(() => __awaiter(this, void 0, void 0, function* () {
|
|
32
|
+
this.check_count += 1;
|
|
33
|
+
(0, dist_1.log_info)(`check transaction start: seq=[${this.check_count}], txhash= ${this.txid}, trace_id=${this.trace_id}`);
|
|
34
|
+
try {
|
|
35
|
+
if (Date.now() - check_start_time < check_timeout) {
|
|
36
|
+
let txReceipt = yield this.leadgerService.getTransaction(this.txid, ['*']);
|
|
37
|
+
if (!txReceipt || (0, dist_1.isEmpty)(txReceipt)) {
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
txReceipt['transaction']['signatures'] = [];
|
|
41
|
+
txReceipt = (0, index_1.decodeBytes)(txReceipt);
|
|
42
|
+
(0, dist_1.log_info)(`Received transaction result via polling: ${this.txid}`);
|
|
43
|
+
clearInterval(intervalId);
|
|
44
|
+
yield this.processTransactionResult(txReceipt, 'interval');
|
|
45
|
+
}
|
|
46
|
+
else {
|
|
47
|
+
clearInterval(intervalId);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
catch (err) {
|
|
51
|
+
clearInterval(intervalId);
|
|
52
|
+
(0, dist_1.log_error)('parse transaction error!', err);
|
|
53
|
+
}
|
|
54
|
+
}), check_interval);
|
|
55
|
+
this.intervalId = intervalId;
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
on_subscibe_transaction() {
|
|
59
|
+
this.event_emitter.once(`SUI_TX_RESULT_${this.txid}`, (response) => {
|
|
60
|
+
(0, dist_1.log_info)(`receive tx result notification, txid=${this.txid}`);
|
|
61
|
+
if (response.transaction) {
|
|
62
|
+
this.processTransactionResult(response, 'grpc');
|
|
63
|
+
}
|
|
64
|
+
else {
|
|
65
|
+
(0, dist_1.log_warn)(`re-fetch tx result by ledgerService, txid=${this.txid}`);
|
|
66
|
+
this.leadgerService.getTransaction(this.txid, ['*']).then((txReceipt) => {
|
|
67
|
+
this.processTransactionResult(txReceipt, 'grpc2');
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
processTransactionResult(txReceipt, source) {
|
|
73
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
74
|
+
if (this.trade_result_already_processed) {
|
|
75
|
+
(0, dist_1.log_warn)(`trade_result_already_processed=${this.trade_result_already_processed}, ignore trade result fetch by ${source} check!`);
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
78
|
+
const swap_detail = yield this.transactionParser.parseTransaction(txReceipt, this.pool_info);
|
|
79
|
+
let trade_result = this.map_swap_result_to_tx_result(swap_detail);
|
|
80
|
+
this.trade_result_already_processed = true;
|
|
81
|
+
if (this.intervalId) {
|
|
82
|
+
clearInterval(this.intervalId);
|
|
83
|
+
this.intervalId = null;
|
|
84
|
+
}
|
|
85
|
+
if (trade_result.success) {
|
|
86
|
+
this.event_emitter.emit(dist_1.TRANSACTION_STATE_SUCCESS, trade_result);
|
|
87
|
+
}
|
|
88
|
+
else {
|
|
89
|
+
this.event_emitter.emit(dist_1.TRANSACTION_STATE_FAILED, trade_result);
|
|
90
|
+
}
|
|
91
|
+
if (source === 'interval') {
|
|
92
|
+
console.log('--------------------- Transaction Result from Polling ---------------------');
|
|
93
|
+
}
|
|
94
|
+
else {
|
|
95
|
+
console.log('--------------------- Transaction Result from GRPC ---------------------');
|
|
96
|
+
}
|
|
97
|
+
console.log(JSON.stringify(trade_result, null, 2));
|
|
98
|
+
console.log('-----------------------------------------------------------------------------');
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
exports.TransactionResultChecker = TransactionResultChecker;
|
|
@@ -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("./send_tx"), exports);
|
|
18
|
+
__exportStar(require("./check"), exports);
|
|
19
|
+
__exportStar(require("./parse"), exports);
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { SuiTransactionParser } from "./sui_tx_parser";
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.SuiTransactionParser = void 0;
|
|
4
|
+
var sui_tx_parser_1 = require("./sui_tx_parser");
|
|
5
|
+
Object.defineProperty(exports, "SuiTransactionParser", { enumerable: true, get: function () { return sui_tx_parser_1.SuiTransactionParser; } });
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { StandardPoolInfoType, StandardSwapDetailType } from "@clonegod/ttd-core";
|
|
2
|
+
export declare class SuiTransactionParser {
|
|
3
|
+
private walletAddress;
|
|
4
|
+
constructor(walletAddress: string);
|
|
5
|
+
parseTransaction(receipt: any, poolInfo: StandardPoolInfoType): Promise<StandardSwapDetailType>;
|
|
6
|
+
parse_transaction_result(tx_receipt: any, pool_info: StandardPoolInfoType): Promise<StandardSwapDetailType>;
|
|
7
|
+
private calculateGasFee;
|
|
8
|
+
private calc_tx_price;
|
|
9
|
+
private create_failed_result;
|
|
10
|
+
private parseErrorMsgToErrorCode;
|
|
11
|
+
private extractTimestamp;
|
|
12
|
+
private extractCheckpoint;
|
|
13
|
+
}
|
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
+
};
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
exports.SuiTransactionParser = void 0;
|
|
16
|
+
const dist_1 = require("@clonegod/ttd-core/dist");
|
|
17
|
+
const decimal_js_1 = __importDefault(require("decimal.js"));
|
|
18
|
+
const index_1 = require("../../index");
|
|
19
|
+
class SuiTransactionParser {
|
|
20
|
+
constructor(walletAddress) {
|
|
21
|
+
this.walletAddress = walletAddress;
|
|
22
|
+
}
|
|
23
|
+
parseTransaction(receipt, poolInfo) {
|
|
24
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
25
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o;
|
|
26
|
+
try {
|
|
27
|
+
if (((_c = (_b = (_a = receipt.transaction) === null || _a === void 0 ? void 0 : _a.effects) === null || _b === void 0 ? void 0 : _b.status) === null || _c === void 0 ? void 0 : _c.success) !== true) {
|
|
28
|
+
console.log('Transaction failed', {
|
|
29
|
+
txid: (_d = receipt.transaction) === null || _d === void 0 ? void 0 : _d.digest,
|
|
30
|
+
success: (_g = (_f = (_e = receipt.transaction) === null || _e === void 0 ? void 0 : _e.effects) === null || _f === void 0 ? void 0 : _f.status) === null || _g === void 0 ? void 0 : _g.success,
|
|
31
|
+
balance_changes: (_j = (_h = receipt.transaction) === null || _h === void 0 ? void 0 : _h.balance_changes) === null || _j === void 0 ? void 0 : _j.length,
|
|
32
|
+
reason: (_o = (_m = (_l = (_k = receipt.transaction) === null || _k === void 0 ? void 0 : _k.effects) === null || _l === void 0 ? void 0 : _l.status) === null || _m === void 0 ? void 0 : _m.error) === null || _o === void 0 ? void 0 : _o.kind
|
|
33
|
+
});
|
|
34
|
+
throw new Error('Transaction failed');
|
|
35
|
+
}
|
|
36
|
+
return yield this.parse_transaction_result(receipt, poolInfo);
|
|
37
|
+
}
|
|
38
|
+
catch (error) {
|
|
39
|
+
console.error('Error parsing transaction:', error);
|
|
40
|
+
return this.create_failed_result(receipt, poolInfo, error.message);
|
|
41
|
+
}
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
parse_transaction_result(tx_receipt, pool_info) {
|
|
45
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
46
|
+
var _a, _b, _c, _d, _e, _f;
|
|
47
|
+
let swap_detail = null;
|
|
48
|
+
let tokenAChangeAmount = new decimal_js_1.default(0);
|
|
49
|
+
let tokenBChangeAmount = new decimal_js_1.default(0);
|
|
50
|
+
const balanceChanges = ((_a = tx_receipt.transaction) === null || _a === void 0 ? void 0 : _a.balance_changes) || [];
|
|
51
|
+
for (const change of balanceChanges) {
|
|
52
|
+
if (change.address.toLowerCase() !== this.walletAddress.toLowerCase()) {
|
|
53
|
+
continue;
|
|
54
|
+
}
|
|
55
|
+
const amount = new decimal_js_1.default(change.amount);
|
|
56
|
+
const coinType = change.coin_type;
|
|
57
|
+
const normalizedCoinType = (0, index_1.normalizeSuiTokenAddress)(coinType);
|
|
58
|
+
const poolTokenAAddress = (0, index_1.normalizeSuiTokenAddress)(pool_info.tokenA.address);
|
|
59
|
+
const poolTokenBAddress = (0, index_1.normalizeSuiTokenAddress)(pool_info.tokenB.address);
|
|
60
|
+
if (normalizedCoinType === poolTokenAAddress) {
|
|
61
|
+
tokenAChangeAmount = amount.div(Math.pow(10, pool_info.tokenA.decimals));
|
|
62
|
+
}
|
|
63
|
+
if (normalizedCoinType === poolTokenBAddress) {
|
|
64
|
+
tokenBChangeAmount = amount.div(Math.pow(10, pool_info.tokenB.decimals));
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
let tx_price = this.calc_tx_price(tokenAChangeAmount, tokenBChangeAmount, pool_info);
|
|
68
|
+
swap_detail = {
|
|
69
|
+
tokenA: Object.assign(Object.assign({}, pool_info.tokenA), { pre_bal: 0, post_bal: 0, change: tokenAChangeAmount }),
|
|
70
|
+
tokenB: Object.assign(Object.assign({}, pool_info.tokenB), { pre_bal: 0, post_bal: 0, change: tokenBChangeAmount }),
|
|
71
|
+
tx_price,
|
|
72
|
+
};
|
|
73
|
+
const gasFee = this.calculateGasFee(tx_receipt);
|
|
74
|
+
return {
|
|
75
|
+
success: true,
|
|
76
|
+
error_code: '',
|
|
77
|
+
wallet: this.walletAddress,
|
|
78
|
+
txid: ((_b = tx_receipt.transaction) === null || _b === void 0 ? void 0 : _b.digest) || '',
|
|
79
|
+
block_time: this.extractTimestamp(tx_receipt),
|
|
80
|
+
block_number: this.extractCheckpoint(tx_receipt),
|
|
81
|
+
pool_address: pool_info.pool_address,
|
|
82
|
+
tokenA: Object.assign(Object.assign({}, pool_info.tokenA), { pre_bal: 0, post_bal: 0, change: ((_d = (_c = swap_detail === null || swap_detail === void 0 ? void 0 : swap_detail.tokenA) === null || _c === void 0 ? void 0 : _c.change) === null || _d === void 0 ? void 0 : _d.toNumber()) || 0 }),
|
|
83
|
+
tokenB: Object.assign(Object.assign({}, pool_info.tokenB), { pre_bal: 0, post_bal: 0, change: ((_f = (_e = swap_detail === null || swap_detail === void 0 ? void 0 : swap_detail.tokenB) === null || _e === void 0 ? void 0 : _e.change) === null || _f === void 0 ? void 0 : _f.toNumber()) || 0 }),
|
|
84
|
+
tx_price: (swap_detail === null || swap_detail === void 0 ? void 0 : swap_detail.tx_price) || '0',
|
|
85
|
+
gas_fee: gasFee
|
|
86
|
+
};
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
calculateGasFee(receipt) {
|
|
90
|
+
var _a, _b;
|
|
91
|
+
const gasUsed = (_b = (_a = receipt.transaction) === null || _a === void 0 ? void 0 : _a.effects) === null || _b === void 0 ? void 0 : _b.gas_used;
|
|
92
|
+
if (!gasUsed) {
|
|
93
|
+
return {
|
|
94
|
+
base_fee: 0,
|
|
95
|
+
priority_fee: 0,
|
|
96
|
+
total_fee: 0
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
const computationCost = new decimal_js_1.default(gasUsed.computation_cost || 0);
|
|
100
|
+
const storageCost = new decimal_js_1.default(gasUsed.storage_cost || 0);
|
|
101
|
+
const storageRebate = new decimal_js_1.default(gasUsed.storage_rebate || 0);
|
|
102
|
+
const nonRefundableStorageFee = new decimal_js_1.default(gasUsed.non_refundable_storage_fee || 0);
|
|
103
|
+
const totalCost = computationCost.plus(storageCost).minus(storageRebate).plus(nonRefundableStorageFee);
|
|
104
|
+
const totalCostInSui = totalCost.div(Math.pow(10, 9));
|
|
105
|
+
return {
|
|
106
|
+
base_fee: computationCost.div(Math.pow(10, 9)).toNumber(),
|
|
107
|
+
priority_fee: storageCost.div(Math.pow(10, 9)).toNumber(),
|
|
108
|
+
total_fee: totalCostInSui.toNumber()
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
calc_tx_price(tokenAChange, tokenBChange, pool_info) {
|
|
112
|
+
if (tokenAChange.eq(0) || tokenBChange.eq(0)) {
|
|
113
|
+
return '0';
|
|
114
|
+
}
|
|
115
|
+
if (pool_info.quote_token === pool_info.tokenA.symbol) {
|
|
116
|
+
return tokenAChange.abs().div(tokenBChange.abs()).toFixed(12);
|
|
117
|
+
}
|
|
118
|
+
if (pool_info.quote_token === pool_info.tokenB.symbol) {
|
|
119
|
+
return tokenBChange.abs().div(tokenAChange.abs()).toFixed(12);
|
|
120
|
+
}
|
|
121
|
+
return '0';
|
|
122
|
+
}
|
|
123
|
+
create_failed_result(txReceipt, poolInfo, errorMessage) {
|
|
124
|
+
var _a;
|
|
125
|
+
const gasFee = this.calculateGasFee(txReceipt);
|
|
126
|
+
return {
|
|
127
|
+
success: false,
|
|
128
|
+
error_code: this.parseErrorMsgToErrorCode(errorMessage),
|
|
129
|
+
wallet: this.walletAddress,
|
|
130
|
+
txid: ((_a = txReceipt.transaction) === null || _a === void 0 ? void 0 : _a.digest) || '',
|
|
131
|
+
block_time: this.extractTimestamp(txReceipt),
|
|
132
|
+
block_number: this.extractCheckpoint(txReceipt),
|
|
133
|
+
pool_address: poolInfo.pool_address,
|
|
134
|
+
tokenA: Object.assign(Object.assign({}, poolInfo.tokenA), { pre_bal: 0, post_bal: 0, change: 0 }),
|
|
135
|
+
tokenB: Object.assign(Object.assign({}, poolInfo.tokenB), { pre_bal: 0, post_bal: 0, change: 0 }),
|
|
136
|
+
tx_price: '0',
|
|
137
|
+
gas_fee: gasFee
|
|
138
|
+
};
|
|
139
|
+
}
|
|
140
|
+
parseErrorMsgToErrorCode(errorMessage) {
|
|
141
|
+
(0, dist_1.log_info)(`parseErrorMsgToErrorCode: errorMessage=${errorMessage}`);
|
|
142
|
+
if (errorMessage.includes('Insufficient funds')) {
|
|
143
|
+
return dist_1.TradeErrorCodeType.InsufficientFunds;
|
|
144
|
+
}
|
|
145
|
+
if (errorMessage.includes('Amount out below minimum')) {
|
|
146
|
+
return dist_1.TradeErrorCodeType.AmountOutBelowMinimum;
|
|
147
|
+
}
|
|
148
|
+
if (errorMessage.includes('Slippage tolerance exceeded')) {
|
|
149
|
+
return dist_1.TradeErrorCodeType.AmountOutBelowMinimum;
|
|
150
|
+
}
|
|
151
|
+
return dist_1.TradeErrorCodeType.UNKOWN;
|
|
152
|
+
}
|
|
153
|
+
extractTimestamp(receipt) {
|
|
154
|
+
var _a;
|
|
155
|
+
const timestamp = (_a = receipt.transaction) === null || _a === void 0 ? void 0 : _a.timestamp;
|
|
156
|
+
if (timestamp === null || timestamp === void 0 ? void 0 : timestamp.seconds) {
|
|
157
|
+
return parseInt(timestamp.seconds) * 1000;
|
|
158
|
+
}
|
|
159
|
+
return Date.now();
|
|
160
|
+
}
|
|
161
|
+
extractCheckpoint(receipt) {
|
|
162
|
+
return parseInt(receipt.checkpoint || '0');
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
exports.SuiTransactionParser = SuiTransactionParser;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { AppConfig } from "@clonegod/ttd-core/dist";
|
|
2
|
+
import { SuiClient } from "@mysten/sui/client";
|
|
3
|
+
import { SuiGrpcClient } from "../../index";
|
|
4
|
+
export declare class SuiTxSender {
|
|
5
|
+
appConfig: AppConfig;
|
|
6
|
+
sui_client: SuiClient;
|
|
7
|
+
grpcClient: SuiGrpcClient;
|
|
8
|
+
constructor(appConfig: AppConfig, sui_client: SuiClient, grpcClient: SuiGrpcClient);
|
|
9
|
+
send_tx: (signedTxBytes: string, signature: string, txDigest: string) => void;
|
|
10
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.SuiTxSender = void 0;
|
|
4
|
+
const dist_1 = require("@clonegod/ttd-core/dist");
|
|
5
|
+
class SuiTxSender {
|
|
6
|
+
constructor(appConfig, sui_client, grpcClient) {
|
|
7
|
+
this.send_tx = (signedTxBytes, signature, txDigest) => {
|
|
8
|
+
const promises = [];
|
|
9
|
+
const bcsBytes = Buffer.from(signedTxBytes, 'base64');
|
|
10
|
+
const signatureBytes = Buffer.from(signature, 'base64');
|
|
11
|
+
const grpcPromise = this.grpcClient.transactionService.executeTransaction(bcsBytes, signatureBytes)
|
|
12
|
+
.then((response) => { return { type: 'grpc', success: true, response }; })
|
|
13
|
+
.catch((error) => {
|
|
14
|
+
(0, dist_1.log_error)(`submit tx to grpc failed`, error);
|
|
15
|
+
return { type: 'grpc', success: false, error };
|
|
16
|
+
});
|
|
17
|
+
promises.push(grpcPromise);
|
|
18
|
+
const rpcPromise = this.sui_client.executeTransactionBlock({
|
|
19
|
+
transactionBlock: signedTxBytes,
|
|
20
|
+
signature: signature,
|
|
21
|
+
options: {
|
|
22
|
+
showEvents: true,
|
|
23
|
+
showBalanceChanges: true,
|
|
24
|
+
showObjectChanges: true,
|
|
25
|
+
showEffects: true
|
|
26
|
+
}
|
|
27
|
+
}).then((response) => {
|
|
28
|
+
return { type: 'rpc', success: true, response };
|
|
29
|
+
}).catch((error) => {
|
|
30
|
+
(0, dist_1.log_error)(`submit tx to sui_client failed`, error);
|
|
31
|
+
return { type: 'rpc', success: false, error };
|
|
32
|
+
});
|
|
33
|
+
promises.push(rpcPromise);
|
|
34
|
+
Promise.race(promises).then((result) => {
|
|
35
|
+
if (result.type === 'rpc') {
|
|
36
|
+
this.appConfig.emit(`SUI_TX_RESULT_${txDigest}`, txDigest);
|
|
37
|
+
}
|
|
38
|
+
if (result.type === 'grpc') {
|
|
39
|
+
this.appConfig.emit(`SUI_TX_RESULT_${txDigest}`, result.response);
|
|
40
|
+
}
|
|
41
|
+
});
|
|
42
|
+
};
|
|
43
|
+
this.appConfig = appConfig;
|
|
44
|
+
this.sui_client = sui_client;
|
|
45
|
+
this.grpcClient = grpcClient;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
exports.SuiTxSender = SuiTxSender;
|