@clonegod/ttd-sui-common 1.0.69 → 1.0.71
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/grpc-connection.js +16 -7
- package/dist/test/test_grpc.js +12 -2
- package/dist/trade/test/test_parse_sui_tx_result.js +24 -24
- package/package.json +1 -1
- package/dist/rpc/account-api.d.ts +0 -14
- package/dist/rpc/account-api.js +0 -113
- package/dist/rpc/coin-api.d.ts +0 -0
- package/dist/rpc/coin-api.js +0 -0
- package/dist/rpc/rpc-connection.d.ts +0 -8
- package/dist/rpc/rpc-connection.js +0 -54
- package/dist/test/test_checkpoint_processor.d.ts +0 -1
- package/dist/test/test_checkpoint_processor.js +0 -112
- package/dist/test/test_checkpoint_subscription.d.ts +0 -1
- package/dist/test/test_checkpoint_subscription.js +0 -132
- package/dist/test/test_checkpoint_with_schema.d.ts +0 -1
- package/dist/test/test_checkpoint_with_schema.js +0 -250
- package/dist/test/test_dynamic_bcs.d.ts +0 -1
- package/dist/test/test_dynamic_bcs.js +0 -82
- package/dist/test/test_github_extractor.d.ts +0 -1
- package/dist/test/test_github_extractor.js +0 -69
- package/dist/test/test_hybrid_bcs.d.ts +0 -1
- package/dist/test/test_hybrid_bcs.js +0 -98
- package/dist/test/test_momentum_events.d.ts +0 -1
- package/dist/test/test_momentum_events.js +0 -113
- package/dist/test/test_rpc.d.ts +0 -1
- package/dist/test/test_rpc.js +0 -34
- package/dist/test/test_schema_database.d.ts +0 -1
- package/dist/test/test_schema_database.js +0 -91
- package/dist/test/test_simple_schema.d.ts +0 -1
- package/dist/test/test_simple_schema.js +0 -78
- package/dist/test/test_single_momentum_event.d.ts +0 -1
- package/dist/test/test_single_momentum_event.js +0 -44
- package/dist/ttd-sui-common/src/constants/index.d.ts +0 -4
- package/dist/ttd-sui-common/src/constants/index.js +0 -8
- package/dist/ttd-sui-common/src/grpc/gas-price-cache.d.ts +0 -21
- package/dist/ttd-sui-common/src/grpc/gas-price-cache.js +0 -83
- package/dist/ttd-sui-common/src/grpc/grpc-connection.d.ts +0 -13
- package/dist/ttd-sui-common/src/grpc/grpc-connection.js +0 -99
- package/dist/ttd-sui-common/src/grpc/index.d.ts +0 -7
- package/dist/ttd-sui-common/src/grpc/index.js +0 -17
- package/dist/ttd-sui-common/src/grpc/ledger-service.d.ts +0 -12
- package/dist/ttd-sui-common/src/grpc/ledger-service.js +0 -161
- package/dist/ttd-sui-common/src/grpc/live-data-service.d.ts +0 -10
- package/dist/ttd-sui-common/src/grpc/live-data-service.js +0 -107
- package/dist/ttd-sui-common/src/grpc/subscription-service.d.ts +0 -7
- package/dist/ttd-sui-common/src/grpc/subscription-service.js +0 -69
- package/dist/ttd-sui-common/src/grpc/sui-grpc-client.d.ts +0 -16
- package/dist/ttd-sui-common/src/grpc/sui-grpc-client.js +0 -38
- package/dist/ttd-sui-common/src/grpc/transaction-service.d.ts +0 -7
- package/dist/ttd-sui-common/src/grpc/transaction-service.js +0 -49
- package/dist/ttd-sui-common/src/index.d.ts +0 -9
- package/dist/ttd-sui-common/src/index.js +0 -25
- package/dist/ttd-sui-common/src/quote/index.d.ts +0 -1
- package/dist/ttd-sui-common/src/quote/index.js +0 -17
- package/dist/ttd-sui-common/src/quote/pricing/index.d.ts +0 -1
- package/dist/ttd-sui-common/src/quote/pricing/index.js +0 -17
- package/dist/ttd-sui-common/src/quote/pricing/token_price_cache.d.ts +0 -9
- package/dist/ttd-sui-common/src/quote/pricing/token_price_cache.js +0 -42
- package/dist/ttd-sui-common/src/redis/index.d.ts +0 -1
- package/dist/ttd-sui-common/src/redis/index.js +0 -17
- package/dist/ttd-sui-common/src/redis/redis_client.d.ts +0 -21
- package/dist/ttd-sui-common/src/redis/redis_client.js +0 -155
- package/dist/ttd-sui-common/src/test/test.d.ts +0 -1
- package/dist/ttd-sui-common/src/test/test.js +0 -126
- package/dist/ttd-sui-common/src/test/test_checkpoint.d.ts +0 -1
- package/dist/ttd-sui-common/src/test/test_checkpoint.js +0 -64
- package/dist/ttd-sui-common/src/test/test_grpc.d.ts +0 -1
- package/dist/ttd-sui-common/src/test/test_grpc.js +0 -84
- package/dist/ttd-sui-common/src/trade/abstract_sui_dex_trade_plus.d.ts +0 -32
- package/dist/ttd-sui-common/src/trade/abstract_sui_dex_trade_plus.js +0 -216
- package/dist/ttd-sui-common/src/trade/check/index.d.ts +0 -1
- package/dist/ttd-sui-common/src/trade/check/index.js +0 -5
- package/dist/ttd-sui-common/src/trade/check/tx_result_checker.d.ts +0 -13
- package/dist/ttd-sui-common/src/trade/check/tx_result_checker.js +0 -111
- package/dist/ttd-sui-common/src/trade/index.d.ts +0 -4
- package/dist/ttd-sui-common/src/trade/index.js +0 -20
- package/dist/ttd-sui-common/src/trade/parse/index.d.ts +0 -1
- package/dist/ttd-sui-common/src/trade/parse/index.js +0 -5
- package/dist/ttd-sui-common/src/trade/parse/sui_tx_parser.d.ts +0 -13
- package/dist/ttd-sui-common/src/trade/parse/sui_tx_parser.js +0 -167
- package/dist/ttd-sui-common/src/trade/send_tx/index.d.ts +0 -10
- package/dist/ttd-sui-common/src/trade/send_tx/index.js +0 -48
- package/dist/ttd-sui-common/src/trade/test/test_parse_sui_tx_result.d.ts +0 -1
- package/dist/ttd-sui-common/src/trade/test/test_parse_sui_tx_result.js +0 -105
- package/dist/ttd-sui-common/src/type/index.d.ts +0 -18
- package/dist/ttd-sui-common/src/type/index.js +0 -2
- package/dist/ttd-sui-common/src/utils/checkpoint_parse.d.ts +0 -0
- package/dist/ttd-sui-common/src/utils/checkpoint_parse.js +0 -0
- package/dist/ttd-sui-common/src/utils/decode.d.ts +0 -2
- package/dist/ttd-sui-common/src/utils/decode.js +0 -43
- package/dist/ttd-sui-common/src/utils/format.d.ts +0 -1
- package/dist/ttd-sui-common/src/utils/format.js +0 -18
- package/dist/ttd-sui-common/src/utils/index.d.ts +0 -2
- package/dist/ttd-sui-common/src/utils/index.js +0 -18
- package/dist/ttd-sui-market-data/src/sui_objects/config_manager.d.ts +0 -22
- package/dist/ttd-sui-market-data/src/sui_objects/config_manager.js +0 -169
- package/dist/ttd-sui-market-data/src/sui_objects/example.d.ts +0 -1
- package/dist/ttd-sui-market-data/src/sui_objects/example.js +0 -72
- package/dist/ttd-sui-market-data/src/sui_objects/index.d.ts +0 -6
- package/dist/ttd-sui-market-data/src/sui_objects/index.js +0 -15
- package/dist/ttd-sui-market-data/src/sui_objects/object_mgt.d.ts +0 -47
- package/dist/ttd-sui-market-data/src/sui_objects/object_mgt.js +0 -483
- package/dist/ttd-sui-market-data/src/sui_objects/types.d.ts +0 -60
- package/dist/ttd-sui-market-data/src/sui_objects/types.js +0 -24
- package/dist/ttd-sui-market-data/src/sui_objects/version_checker.d.ts +0 -30
- package/dist/ttd-sui-market-data/src/sui_objects/version_checker.js +0 -207
- package/dist/utils/bcs-parser.d.ts +0 -8
- package/dist/utils/bcs-parser.js +0 -121
- package/dist/utils/checkpoint-processor.d.ts +0 -7
- package/dist/utils/checkpoint-processor.js +0 -123
- package/dist/utils/checkpoint.d.ts +0 -7
- package/dist/utils/checkpoint.js +0 -26
- package/dist/utils/dex-parser-manager.d.ts +0 -7
- package/dist/utils/dex-parser-manager.js +0 -138
- package/dist/utils/dex_event_parser.d.ts +0 -34
- package/dist/utils/dex_event_parser.js +0 -132
- package/dist/utils/dex_schema_database.d.ts +0 -206
- package/dist/utils/dex_schema_database.js +0 -169
- package/dist/utils/dynamic_bcs_parser.d.ts +0 -21
- package/dist/utils/dynamic_bcs_parser.js +0 -166
- package/dist/utils/github_schema_extractor.d.ts +0 -20
- package/dist/utils/github_schema_extractor.js +0 -153
- package/dist/utils/hybrid_bcs_parser.d.ts +0 -22
- package/dist/utils/hybrid_bcs_parser.js +0 -194
- package/dist/utils/simple_schema_parser.d.ts +0 -76
- package/dist/utils/simple_schema_parser.js +0 -193
|
@@ -1,126 +0,0 @@
|
|
|
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 __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
-
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
-
}) : function(o, v) {
|
|
16
|
-
o["default"] = v;
|
|
17
|
-
});
|
|
18
|
-
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
-
var ownKeys = function(o) {
|
|
20
|
-
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
-
var ar = [];
|
|
22
|
-
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
-
return ar;
|
|
24
|
-
};
|
|
25
|
-
return ownKeys(o);
|
|
26
|
-
};
|
|
27
|
-
return function (mod) {
|
|
28
|
-
if (mod && mod.__esModule) return mod;
|
|
29
|
-
var result = {};
|
|
30
|
-
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
-
__setModuleDefault(result, mod);
|
|
32
|
-
return result;
|
|
33
|
-
};
|
|
34
|
-
})();
|
|
35
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
-
const dotenv = __importStar(require("dotenv"));
|
|
37
|
-
dotenv.config();
|
|
38
|
-
const grpc = __importStar(require("@grpc/grpc-js"));
|
|
39
|
-
const protoLoader = __importStar(require("@grpc/proto-loader"));
|
|
40
|
-
const path = __importStar(require("path"));
|
|
41
|
-
const PROTO_PATH = path.join(__dirname, 'protos/sui/rpc/v2beta2/ledger_service.proto');
|
|
42
|
-
const packageDefinition = protoLoader.loadSync(PROTO_PATH, {
|
|
43
|
-
keepCase: true,
|
|
44
|
-
longs: String,
|
|
45
|
-
enums: String,
|
|
46
|
-
defaults: true,
|
|
47
|
-
oneofs: true,
|
|
48
|
-
includeDirs: [path.join(__dirname, 'protos')],
|
|
49
|
-
});
|
|
50
|
-
const suiProto = grpc.loadPackageDefinition(packageDefinition);
|
|
51
|
-
const LedgerService = suiProto.sui.rpc.v2beta2.LedgerService;
|
|
52
|
-
const endpoint = process.env.SUI_GRPC_ENDPOINT;
|
|
53
|
-
const token = process.env.SUI_GRPC_TOKEN;
|
|
54
|
-
if (!endpoint || !token) {
|
|
55
|
-
throw new Error('SUI_GRPC_ENDPOINT and SUI_GRPC_TOKEN must be set');
|
|
56
|
-
}
|
|
57
|
-
const client = new LedgerService(endpoint, grpc.credentials.createSsl(), {
|
|
58
|
-
'grpc.keepalive_time_ms': 30000,
|
|
59
|
-
'grpc.keepalive_timeout_ms': 5000,
|
|
60
|
-
'grpc.keepalive_permit_without_calls': true,
|
|
61
|
-
'grpc.http2.max_pings_without_data': 0,
|
|
62
|
-
'grpc.http2.min_time_between_pings_ms': 10000,
|
|
63
|
-
'grpc.http2.min_ping_interval_without_data_ms': 300000
|
|
64
|
-
});
|
|
65
|
-
const metadata = new grpc.Metadata();
|
|
66
|
-
metadata.add('x-token', token);
|
|
67
|
-
const suiSystemPackageId = '0x0000000000000000000000000000000000000000000000000000000000000002';
|
|
68
|
-
const request = {
|
|
69
|
-
object_id: suiSystemPackageId,
|
|
70
|
-
read_mask: {
|
|
71
|
-
paths: [
|
|
72
|
-
'object_id',
|
|
73
|
-
'version',
|
|
74
|
-
'digest',
|
|
75
|
-
'owner',
|
|
76
|
-
'object_type',
|
|
77
|
-
'has_public_transfer',
|
|
78
|
-
'previous_transaction',
|
|
79
|
-
'storage_rebate'
|
|
80
|
-
],
|
|
81
|
-
},
|
|
82
|
-
};
|
|
83
|
-
function decodeBytes(obj) {
|
|
84
|
-
if (Array.isArray(obj)) {
|
|
85
|
-
return obj.map(decodeBytes);
|
|
86
|
-
}
|
|
87
|
-
else if (obj && typeof obj === 'object') {
|
|
88
|
-
const result = {};
|
|
89
|
-
for (const key in obj) {
|
|
90
|
-
const val = obj[key];
|
|
91
|
-
if (val instanceof Uint8Array || ((val === null || val === void 0 ? void 0 : val.type) === 'Buffer' && Array.isArray(val.data))) {
|
|
92
|
-
const buffer = val instanceof Uint8Array ? val : Buffer.from(val.data);
|
|
93
|
-
result[key] = buffer.toString('hex');
|
|
94
|
-
}
|
|
95
|
-
else {
|
|
96
|
-
result[key] = decodeBytes(val);
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
return result;
|
|
100
|
-
}
|
|
101
|
-
else {
|
|
102
|
-
return obj;
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
client.GetObject(request, metadata, (err, response) => {
|
|
106
|
-
if (err) {
|
|
107
|
-
console.error('gRPC Error:', err);
|
|
108
|
-
console.error('Error details:', {
|
|
109
|
-
code: err.code,
|
|
110
|
-
details: err.details,
|
|
111
|
-
metadata: err.metadata
|
|
112
|
-
});
|
|
113
|
-
if (err.code === 14) {
|
|
114
|
-
console.error('Connection failed. Please check:');
|
|
115
|
-
console.error('1. QuickNode endpoint is correct');
|
|
116
|
-
console.error('2. Token is valid');
|
|
117
|
-
console.error('3. Network connection is stable');
|
|
118
|
-
console.error('4. Port 9000 is accessible');
|
|
119
|
-
}
|
|
120
|
-
}
|
|
121
|
-
else {
|
|
122
|
-
const decoded = decodeBytes(response);
|
|
123
|
-
console.log('Response:', JSON.stringify(decoded, null, 2));
|
|
124
|
-
}
|
|
125
|
-
client.close();
|
|
126
|
-
});
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1,64 +0,0 @@
|
|
|
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
|
-
const dist_1 = require("@clonegod/ttd-core/dist");
|
|
13
|
-
const fs_1 = require("fs");
|
|
14
|
-
const grpc_connection_1 = require("../grpc/grpc-connection");
|
|
15
|
-
const subscription_service_1 = require("../grpc/subscription-service");
|
|
16
|
-
const grpc_endpoint = process.env.SUI_GRPC_ENDPOINT;
|
|
17
|
-
const grpc_token = process.env.SUI_GRPC_TOKEN;
|
|
18
|
-
const test_subscribe_checkpoints = () => __awaiter(void 0, void 0, void 0, function* () {
|
|
19
|
-
const connection = grpc_connection_1.GrpcConnection.getInstance(grpc_endpoint, grpc_token);
|
|
20
|
-
const subscriptionService = new subscription_service_1.SubscriptionService(connection);
|
|
21
|
-
const readMask = ['*'];
|
|
22
|
-
subscriptionService.subscribeCheckpoints((response) => {
|
|
23
|
-
const blockHash = response.checkpoint.digest;
|
|
24
|
-
const blockNumber = response.checkpoint.sequence_number;
|
|
25
|
-
const blockTime = response.checkpoint.summary.timestamp.seconds * 1000 + Math.floor(response.checkpoint.summary.timestamp.nanos / 1000000);
|
|
26
|
-
(0, dist_1.log_info)(blockNumber + ' - ' + response.checkpoint.transactions.length);
|
|
27
|
-
response.checkpoint.contents = {};
|
|
28
|
-
response.checkpoint.summary = {};
|
|
29
|
-
response.checkpoint.signature = {};
|
|
30
|
-
response.checkpoint.transactions.forEach((transaction) => {
|
|
31
|
-
transaction.signatures = [];
|
|
32
|
-
transaction.input_objects = [];
|
|
33
|
-
transaction.output_objects = [];
|
|
34
|
-
});
|
|
35
|
-
response.checkpoint.transactions = response.checkpoint.transactions.filter((transaction) => {
|
|
36
|
-
return transaction.transaction.kind.kind === 'programmable_transaction';
|
|
37
|
-
});
|
|
38
|
-
response.checkpoint.transactions = response.checkpoint.transactions.filter((transaction) => {
|
|
39
|
-
return transaction.events && transaction.events.events && transaction.events.events.length > 0;
|
|
40
|
-
});
|
|
41
|
-
let blockUpdateEvent = {
|
|
42
|
-
blockNumber,
|
|
43
|
-
blockHash,
|
|
44
|
-
blockTime,
|
|
45
|
-
transactions: []
|
|
46
|
-
};
|
|
47
|
-
response.checkpoint.transactions.forEach((transaction) => {
|
|
48
|
-
let event_types = transaction.events.events.map((event) => event.event_type) || [];
|
|
49
|
-
});
|
|
50
|
-
if (blockUpdateEvent.transactions.length > 0) {
|
|
51
|
-
(0, fs_1.writeFileSync)(`./dist/checkpoint_${response.cursor}_decoded.json`, JSON.stringify(response, null, 2));
|
|
52
|
-
console.dir(blockUpdateEvent, { depth: null });
|
|
53
|
-
}
|
|
54
|
-
else {
|
|
55
|
-
console.log(`no liquidity related events: ${response.cursor}, ${response.checkpoint.digest}`);
|
|
56
|
-
}
|
|
57
|
-
}, (error) => {
|
|
58
|
-
(0, dist_1.log_info)(`error`, error);
|
|
59
|
-
}, readMask);
|
|
60
|
-
});
|
|
61
|
-
const main = () => __awaiter(void 0, void 0, void 0, function* () {
|
|
62
|
-
test_subscribe_checkpoints();
|
|
63
|
-
});
|
|
64
|
-
main();
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1,84 +0,0 @@
|
|
|
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
|
-
const dist_1 = require("@clonegod/ttd-core/dist");
|
|
13
|
-
const grpc_connection_1 = require("../grpc/grpc-connection");
|
|
14
|
-
const ledger_service_1 = require("../grpc/ledger-service");
|
|
15
|
-
const live_data_service_1 = require("../grpc/live-data-service");
|
|
16
|
-
const grpc_endpoint = process.env.SUI_GRPC_ENDPOINT;
|
|
17
|
-
const grpc_token = process.env.SUI_GRPC_TOKEN;
|
|
18
|
-
const test_get_service_info = () => __awaiter(void 0, void 0, void 0, function* () {
|
|
19
|
-
const connection = grpc_connection_1.GrpcConnection.getInstance(grpc_endpoint, grpc_token);
|
|
20
|
-
const ledgerService = new ledger_service_1.LedgerService(connection);
|
|
21
|
-
const serviceInfo = yield ledgerService.getServiceInfo();
|
|
22
|
-
(0, dist_1.log_info)(`serviceInfo`, serviceInfo);
|
|
23
|
-
});
|
|
24
|
-
const test_get_epoch = () => __awaiter(void 0, void 0, void 0, function* () {
|
|
25
|
-
const connection = grpc_connection_1.GrpcConnection.getInstance(grpc_endpoint, grpc_token);
|
|
26
|
-
const ledgerService = new ledger_service_1.LedgerService(connection);
|
|
27
|
-
const epoch = yield ledgerService.getEpoch();
|
|
28
|
-
(0, dist_1.log_info)(`epoch`, epoch);
|
|
29
|
-
});
|
|
30
|
-
const test_get_checkpoint = () => __awaiter(void 0, void 0, void 0, function* () {
|
|
31
|
-
const connection = grpc_connection_1.GrpcConnection.getInstance(grpc_endpoint, grpc_token);
|
|
32
|
-
const ledgerService = new ledger_service_1.LedgerService(connection);
|
|
33
|
-
const checkpoint = yield ledgerService.getCheckpoint();
|
|
34
|
-
(0, dist_1.log_info)(`checkpoint`, checkpoint);
|
|
35
|
-
});
|
|
36
|
-
const test_get_object = (object_id) => __awaiter(void 0, void 0, void 0, function* () {
|
|
37
|
-
const connection = grpc_connection_1.GrpcConnection.getInstance(grpc_endpoint, grpc_token);
|
|
38
|
-
const ledgerService = new ledger_service_1.LedgerService(connection);
|
|
39
|
-
const object = yield ledgerService.getObject(object_id);
|
|
40
|
-
(0, dist_1.log_info)(`object`, object);
|
|
41
|
-
});
|
|
42
|
-
const test_get_objects = (object_ids) => __awaiter(void 0, void 0, void 0, function* () {
|
|
43
|
-
const connection = grpc_connection_1.GrpcConnection.getInstance(grpc_endpoint, grpc_token);
|
|
44
|
-
const ledgerService = new ledger_service_1.LedgerService(connection);
|
|
45
|
-
const objects = yield ledgerService.batchGetObjects(object_ids);
|
|
46
|
-
(0, dist_1.log_info)(`objects`, objects);
|
|
47
|
-
});
|
|
48
|
-
const test_get_transaction = (txid) => __awaiter(void 0, void 0, void 0, function* () {
|
|
49
|
-
const connection = grpc_connection_1.GrpcConnection.getInstance(grpc_endpoint, grpc_token);
|
|
50
|
-
const ledgerService = new ledger_service_1.LedgerService(connection);
|
|
51
|
-
const transaction = yield ledgerService.getTransaction(txid);
|
|
52
|
-
(0, dist_1.log_info)(`transaction`, transaction);
|
|
53
|
-
});
|
|
54
|
-
const test_get_balance = (wallet_address, coin_type) => __awaiter(void 0, void 0, void 0, function* () {
|
|
55
|
-
const connection = grpc_connection_1.GrpcConnection.getInstance(grpc_endpoint, grpc_token);
|
|
56
|
-
const liveDataService = new live_data_service_1.LiveDataService(connection);
|
|
57
|
-
const balance = yield liveDataService.getBalance(wallet_address, coin_type);
|
|
58
|
-
(0, dist_1.log_info)(`balance`, balance);
|
|
59
|
-
});
|
|
60
|
-
const test_list_balances = (wallet_address) => __awaiter(void 0, void 0, void 0, function* () {
|
|
61
|
-
const connection = grpc_connection_1.GrpcConnection.getInstance(grpc_endpoint, grpc_token);
|
|
62
|
-
const liveDataService = new live_data_service_1.LiveDataService(connection);
|
|
63
|
-
const balances = yield liveDataService.listBalances(wallet_address);
|
|
64
|
-
(0, dist_1.log_info)(`balances`, balances);
|
|
65
|
-
});
|
|
66
|
-
const test_get_coin_info = (coin_type) => __awaiter(void 0, void 0, void 0, function* () {
|
|
67
|
-
const connection = grpc_connection_1.GrpcConnection.getInstance(grpc_endpoint, grpc_token);
|
|
68
|
-
const liveDataService = new live_data_service_1.LiveDataService(connection);
|
|
69
|
-
const coinInfo = yield liveDataService.getCoinInfo(coin_type);
|
|
70
|
-
(0, dist_1.log_info)(`coinInfo`, coinInfo);
|
|
71
|
-
});
|
|
72
|
-
const test_list_owned_objects = (wallet_address, coin_type) => __awaiter(void 0, void 0, void 0, function* () {
|
|
73
|
-
const connection = grpc_connection_1.GrpcConnection.getInstance(grpc_endpoint, grpc_token);
|
|
74
|
-
const liveDataService = new live_data_service_1.LiveDataService(connection);
|
|
75
|
-
const ownedObjects = yield liveDataService.listOwnedObjects(wallet_address, coin_type);
|
|
76
|
-
(0, dist_1.log_info)(`ownedObjects`, ownedObjects);
|
|
77
|
-
});
|
|
78
|
-
const main = () => __awaiter(void 0, void 0, void 0, function* () {
|
|
79
|
-
const wallet_address = '0x6367c8755b8c39cab7305bfa75cb17d050508d2e55f6862a7682377ad6d46ee7';
|
|
80
|
-
const usdc_coin_type = '0xdba34672e30cb065b1f93e3ab55318768fd6fef66c15942c9f7cb846e2f900e7::usdc::USDC';
|
|
81
|
-
const take_coin_type = '0x76a49ebaf991fa2d4cb6a352af14425d453fe2ba6802b5ed2361b227150b6689::take::TAKE';
|
|
82
|
-
test_list_owned_objects(wallet_address, take_coin_type);
|
|
83
|
-
});
|
|
84
|
-
main();
|
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
import { AbastrcatTrade, AppConfig, TradeContext } from "@clonegod/ttd-core/dist";
|
|
2
|
-
import { Ed25519Keypair } from '@mysten/sui/keypairs/ed25519';
|
|
3
|
-
import { SuiClient } from "@mysten/sui/client";
|
|
4
|
-
import { SuiGrpcClient, SuiTxSender } from '../index';
|
|
5
|
-
import { SimpleRedisClient } from '../redis';
|
|
6
|
-
export declare abstract class AbstractSuiDexTradePlus extends AbastrcatTrade {
|
|
7
|
-
protected appConfig: AppConfig;
|
|
8
|
-
protected group_wallets: Ed25519Keypair[];
|
|
9
|
-
protected wallet: Ed25519Keypair;
|
|
10
|
-
protected walletMode: 'single' | 'multi';
|
|
11
|
-
sui_client: SuiClient;
|
|
12
|
-
grpcClient: SuiGrpcClient;
|
|
13
|
-
transactionSender: SuiTxSender;
|
|
14
|
-
protected redisClient: SimpleRedisClient;
|
|
15
|
-
walletAddresses: string[];
|
|
16
|
-
currentWalletAddress: string;
|
|
17
|
-
protected chainNameLower: string;
|
|
18
|
-
constructor(appConfig: AppConfig, grpcClient: SuiGrpcClient);
|
|
19
|
-
private initSuiClient;
|
|
20
|
-
init(): Promise<void>;
|
|
21
|
-
protected getWalletMode(): 'single' | 'multi';
|
|
22
|
-
protected getSingleWallet(): Ed25519Keypair;
|
|
23
|
-
protected getGroupWallets(): Ed25519Keypair[];
|
|
24
|
-
protected getWalletAddresses(): string[];
|
|
25
|
-
protected chooseWallet(context: TradeContext): Promise<Ed25519Keypair>;
|
|
26
|
-
protected determineInputOutputTokens(order_msg: any, pool_info: any): {
|
|
27
|
-
inputToken: any;
|
|
28
|
-
outputToken: any;
|
|
29
|
-
};
|
|
30
|
-
abstract execute(context: TradeContext, retryCount?: number): Promise<string>;
|
|
31
|
-
protected abstract initConfigs(): void;
|
|
32
|
-
}
|
|
@@ -1,216 +0,0 @@
|
|
|
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.AbstractSuiDexTradePlus = void 0;
|
|
16
|
-
const dist_1 = require("@clonegod/ttd-core/dist");
|
|
17
|
-
const ed25519_1 = require("@mysten/sui/keypairs/ed25519");
|
|
18
|
-
const client_1 = require("@mysten/sui/client");
|
|
19
|
-
const index_1 = require("../index");
|
|
20
|
-
const redis_1 = require("../redis");
|
|
21
|
-
const decimal_js_1 = __importDefault(require("decimal.js"));
|
|
22
|
-
class AbstractSuiDexTradePlus extends dist_1.AbastrcatTrade {
|
|
23
|
-
constructor(appConfig, grpcClient) {
|
|
24
|
-
super();
|
|
25
|
-
this.appConfig = appConfig;
|
|
26
|
-
this.walletMode = 'multi';
|
|
27
|
-
this.walletAddresses = [];
|
|
28
|
-
this.currentWalletAddress = '';
|
|
29
|
-
this.grpcClient = grpcClient;
|
|
30
|
-
this.initConfigs();
|
|
31
|
-
this.chainNameLower = this.appConfig.env_args.chain_id.toLowerCase();
|
|
32
|
-
this.redisClient = new redis_1.SimpleRedisClient(`${this.chainNameLower}:tx`);
|
|
33
|
-
}
|
|
34
|
-
initSuiClient() {
|
|
35
|
-
const sui_rpc_url = process.env.SUI_RPC_URL || 'https://fullnode.mainnet.sui.io:443';
|
|
36
|
-
this.sui_client = new client_1.SuiClient({ url: sui_rpc_url });
|
|
37
|
-
(0, dist_1.log_info)(`Sui客户端已初始化: ${sui_rpc_url}`);
|
|
38
|
-
}
|
|
39
|
-
init() {
|
|
40
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
41
|
-
this.initSuiClient();
|
|
42
|
-
const walletGroupIds = process.env.SUI_WALLET_GROUP_IDS || '';
|
|
43
|
-
this.walletMode = walletGroupIds && walletGroupIds.trim().split(',').length > 0 ? 'multi' : 'single';
|
|
44
|
-
if (this.walletMode === 'multi') {
|
|
45
|
-
let wallet_infos = (0, dist_1.load_wallet_multi)(walletGroupIds.split(','), false);
|
|
46
|
-
this.group_wallets = wallet_infos.map(info => ed25519_1.Ed25519Keypair.fromSecretKey(info.private_key));
|
|
47
|
-
this.walletAddresses = this.group_wallets.map(keypair => keypair.getPublicKey().toSuiAddress());
|
|
48
|
-
(0, dist_1.log_info)(`组钱包已初始化,数量: ${this.group_wallets.length}`, this.walletAddresses);
|
|
49
|
-
}
|
|
50
|
-
else {
|
|
51
|
-
const { private_key } = this.appConfig.trade_runtime.wallet;
|
|
52
|
-
this.wallet = ed25519_1.Ed25519Keypair.fromSecretKey(private_key);
|
|
53
|
-
this.currentWalletAddress = this.wallet.getPublicKey().toSuiAddress();
|
|
54
|
-
this.walletAddresses = [this.currentWalletAddress];
|
|
55
|
-
(0, dist_1.log_info)(`单钱包已初始化,walletAddress= ${this.currentWalletAddress}`);
|
|
56
|
-
}
|
|
57
|
-
this.transactionSender = new index_1.SuiTxSender(this.appConfig, this.sui_client, this.grpcClient);
|
|
58
|
-
});
|
|
59
|
-
}
|
|
60
|
-
getWalletMode() {
|
|
61
|
-
return this.walletMode;
|
|
62
|
-
}
|
|
63
|
-
getSingleWallet() {
|
|
64
|
-
if (this.walletMode !== 'single' || !this.wallet) {
|
|
65
|
-
throw new Error('Single wallet not available in multi-wallet mode or not initialized');
|
|
66
|
-
}
|
|
67
|
-
return this.wallet;
|
|
68
|
-
}
|
|
69
|
-
getGroupWallets() {
|
|
70
|
-
if (this.walletMode !== 'multi' || !this.group_wallets) {
|
|
71
|
-
throw new Error('Group wallets not available in single-wallet mode or not initialized');
|
|
72
|
-
}
|
|
73
|
-
return this.group_wallets;
|
|
74
|
-
}
|
|
75
|
-
getWalletAddresses() {
|
|
76
|
-
return this.walletAddresses;
|
|
77
|
-
}
|
|
78
|
-
chooseWallet(context) {
|
|
79
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
80
|
-
var _a, _b;
|
|
81
|
-
if (this.getWalletMode() === 'single') {
|
|
82
|
-
return this.getSingleWallet();
|
|
83
|
-
}
|
|
84
|
-
const { inputToken } = this.determineInputOutputTokens(context.order_msg, context.pool_info);
|
|
85
|
-
console.log('inputToken', inputToken);
|
|
86
|
-
const requiredAmount = new decimal_js_1.default(context.order_msg.amount);
|
|
87
|
-
const wallet_assets = yield this.redisClient.hgetall('sui:wallet:assets');
|
|
88
|
-
if (!wallet_assets || Object.keys(wallet_assets).length === 0) {
|
|
89
|
-
throw new Error('没有找到钱包资产信息,请确保钱包监控服务正在运行');
|
|
90
|
-
}
|
|
91
|
-
const wallet_assets_map = {};
|
|
92
|
-
for (const [walletAddress, assetsJson] of Object.entries(wallet_assets)) {
|
|
93
|
-
try {
|
|
94
|
-
wallet_assets_map[walletAddress] = JSON.parse(assetsJson);
|
|
95
|
-
}
|
|
96
|
-
catch (error) {
|
|
97
|
-
console.warn(`解析钱包资产数据失败: ${walletAddress}`, error);
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
const availableWallets = [];
|
|
101
|
-
const allWallets = [];
|
|
102
|
-
for (const [walletAddress, assets] of Object.entries(wallet_assets_map)) {
|
|
103
|
-
try {
|
|
104
|
-
const wallet = (_a = this.group_wallets) === null || _a === void 0 ? void 0 : _a.find(w => w.getPublicKey().toSuiAddress().toLowerCase() === walletAddress.toLowerCase());
|
|
105
|
-
if (!wallet) {
|
|
106
|
-
continue;
|
|
107
|
-
}
|
|
108
|
-
const tokenAsset = (_b = assets.tokens) === null || _b === void 0 ? void 0 : _b.find((token) => token.tokenaddr.toLowerCase() === inputToken.address.toLowerCase());
|
|
109
|
-
let tokenBalance = (tokenAsset === null || tokenAsset === void 0 ? void 0 : tokenAsset.balance) || '0';
|
|
110
|
-
const balanceDecimal = new decimal_js_1.default(tokenBalance);
|
|
111
|
-
if (balanceDecimal.gte(requiredAmount)) {
|
|
112
|
-
availableWallets.push({
|
|
113
|
-
wallet,
|
|
114
|
-
balance: tokenBalance
|
|
115
|
-
});
|
|
116
|
-
}
|
|
117
|
-
else {
|
|
118
|
-
console.log(`钱包 ${walletAddress} 余额不足: ${tokenBalance} ${inputToken.symbol} < ${requiredAmount} ${inputToken.symbol}`);
|
|
119
|
-
}
|
|
120
|
-
allWallets.push({
|
|
121
|
-
wallet,
|
|
122
|
-
balance: tokenBalance
|
|
123
|
-
});
|
|
124
|
-
}
|
|
125
|
-
catch (error) {
|
|
126
|
-
console.warn(`处理钱包资产时出错: ${walletAddress}`, error);
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
if (availableWallets.length === 0) {
|
|
130
|
-
let error_msg = `all wallets are insufficient, required: ${requiredAmount} ${inputToken.symbol}`;
|
|
131
|
-
(0, dist_1.log_warn)(error_msg);
|
|
132
|
-
allWallets.forEach((walletInfo, index) => {
|
|
133
|
-
const walletAddress = walletInfo.wallet.getPublicKey().toSuiAddress();
|
|
134
|
-
const shortAddress = walletAddress.substring(0, 6) + '...' + walletAddress.substring(walletAddress.length - 4);
|
|
135
|
-
console.log(`${index + 1}. ${shortAddress}: ${walletInfo.balance} ${inputToken.symbol}`);
|
|
136
|
-
});
|
|
137
|
-
throw new Error(error_msg);
|
|
138
|
-
}
|
|
139
|
-
console.log('availableWallets', availableWallets.map(w => w.wallet.getPublicKey().toSuiAddress()));
|
|
140
|
-
const wallet_last_used = yield this.redisClient.hgetall('sui:wallet:last_used');
|
|
141
|
-
for (const availableWallet of availableWallets) {
|
|
142
|
-
const walletAddress = availableWallet.wallet.getPublicKey().toSuiAddress();
|
|
143
|
-
const lastUsedData = wallet_last_used === null || wallet_last_used === void 0 ? void 0 : wallet_last_used[walletAddress];
|
|
144
|
-
if (lastUsedData) {
|
|
145
|
-
try {
|
|
146
|
-
const lastUsedInfo = JSON.parse(lastUsedData);
|
|
147
|
-
availableWallet.lastUsedAt = lastUsedInfo.lastUsedAt;
|
|
148
|
-
}
|
|
149
|
-
catch (error) {
|
|
150
|
-
console.warn(`解析钱包最后使用时间失败: ${walletAddress}`, error);
|
|
151
|
-
}
|
|
152
|
-
}
|
|
153
|
-
else {
|
|
154
|
-
yield this.redisClient.hsetValue('sui:wallet:last_used', walletAddress, JSON.stringify({
|
|
155
|
-
lastUsedAt: 0,
|
|
156
|
-
lastUsedTime: new Date(0).toISOString()
|
|
157
|
-
}), 24 * 60 * 60);
|
|
158
|
-
availableWallet.lastUsedAt = 0;
|
|
159
|
-
}
|
|
160
|
-
}
|
|
161
|
-
let selectedWallet;
|
|
162
|
-
let selectionStrategy;
|
|
163
|
-
if (availableWallets.length === 1) {
|
|
164
|
-
selectedWallet = availableWallets[0].wallet;
|
|
165
|
-
selectionStrategy = 'OnlyOne';
|
|
166
|
-
}
|
|
167
|
-
else {
|
|
168
|
-
const hasUsageHistory = wallet_last_used && Object.keys(wallet_last_used).length > 0;
|
|
169
|
-
if (hasUsageHistory) {
|
|
170
|
-
availableWallets.sort((a, b) => {
|
|
171
|
-
const aTime = a.lastUsedAt || 0;
|
|
172
|
-
const bTime = b.lastUsedAt || 0;
|
|
173
|
-
return aTime - bTime;
|
|
174
|
-
});
|
|
175
|
-
selectedWallet = availableWallets[0].wallet;
|
|
176
|
-
selectionStrategy = 'MaxLastUsed';
|
|
177
|
-
}
|
|
178
|
-
else {
|
|
179
|
-
availableWallets.sort((a, b) => {
|
|
180
|
-
const aBalance = new decimal_js_1.default(a.balance);
|
|
181
|
-
const bBalance = new decimal_js_1.default(b.balance);
|
|
182
|
-
return bBalance.cmp(aBalance);
|
|
183
|
-
});
|
|
184
|
-
selectedWallet = availableWallets[0].wallet;
|
|
185
|
-
selectionStrategy = 'MaxBalance';
|
|
186
|
-
}
|
|
187
|
-
}
|
|
188
|
-
const selectedWalletAddress = selectedWallet.getPublicKey().toSuiAddress();
|
|
189
|
-
yield this.redisClient.hsetValue('sui:wallet:last_used', selectedWalletAddress, JSON.stringify({
|
|
190
|
-
lastUsedAt: Date.now(),
|
|
191
|
-
lastUsedTime: new Date().toISOString()
|
|
192
|
-
}), 24 * 60 * 60);
|
|
193
|
-
const selectedWalletInfo = availableWallets.find(w => w.wallet.getPublicKey().toSuiAddress() === selectedWalletAddress);
|
|
194
|
-
(0, dist_1.log_info)(`Choose wallet: ${selectionStrategy} -> ${selectedWalletAddress}, balance: ${selectedWalletInfo === null || selectedWalletInfo === void 0 ? void 0 : selectedWalletInfo.balance} ${inputToken.symbol}, required: ${requiredAmount} ${inputToken.symbol}`);
|
|
195
|
-
return selectedWallet;
|
|
196
|
-
});
|
|
197
|
-
}
|
|
198
|
-
determineInputOutputTokens(order_msg, pool_info) {
|
|
199
|
-
const { aToB } = order_msg;
|
|
200
|
-
const { tokenA, tokenB, quote_token } = pool_info;
|
|
201
|
-
const quoteToken = [tokenA, tokenB].find(token => token.symbol === quote_token);
|
|
202
|
-
const nonQuoteToken = [tokenA, tokenB].find(token => token.symbol !== quote_token);
|
|
203
|
-
const inputToken = aToB ? nonQuoteToken : quoteToken;
|
|
204
|
-
const outputToken = aToB ? quoteToken : nonQuoteToken;
|
|
205
|
-
if (!inputToken || !outputToken) {
|
|
206
|
-
throw new Error(`无法确定输入/输出代币: ${JSON.stringify({
|
|
207
|
-
tokenA: tokenA.symbol,
|
|
208
|
-
tokenB: tokenB.symbol,
|
|
209
|
-
quote: quote_token,
|
|
210
|
-
aToB
|
|
211
|
-
})}`);
|
|
212
|
-
}
|
|
213
|
-
return { inputToken, outputToken };
|
|
214
|
-
}
|
|
215
|
-
}
|
|
216
|
-
exports.AbstractSuiDexTradePlus = AbstractSuiDexTradePlus;
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export { TransactionResultChecker } from "./tx_result_checker";
|
|
@@ -1,5 +0,0 @@
|
|
|
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; } });
|
|
@@ -1,13 +0,0 @@
|
|
|
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
|
-
}
|
|
@@ -1,111 +0,0 @@
|
|
|
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) => __awaiter(this, void 0, void 0, function* () {
|
|
60
|
-
(0, dist_1.log_info)(`receive tx result notification, txid=${this.txid}`);
|
|
61
|
-
if (response.transaction) {
|
|
62
|
-
this.processTransactionResult(response, 'grpc');
|
|
63
|
-
return;
|
|
64
|
-
}
|
|
65
|
-
for (let i = 1; i <= 6; i++) {
|
|
66
|
-
(0, dist_1.log_info)(`re-fetch tx result by ledgerService start, i=${i}, txid=${this.txid}`);
|
|
67
|
-
try {
|
|
68
|
-
let txReceipt = yield this.leadgerService.getTransaction(this.txid, ['*']);
|
|
69
|
-
if (txReceipt) {
|
|
70
|
-
this.processTransactionResult(txReceipt, 'grpc2');
|
|
71
|
-
break;
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
catch (error) {
|
|
75
|
-
(0, dist_1.log_warn)(`re-fetch tx result by ledgerService failed, i=${i}, txid=${this.txid}, error=${error.message}`);
|
|
76
|
-
}
|
|
77
|
-
yield (0, dist_1.sleep)(1000);
|
|
78
|
-
}
|
|
79
|
-
}));
|
|
80
|
-
}
|
|
81
|
-
processTransactionResult(txReceipt, source) {
|
|
82
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
83
|
-
if (this.trade_result_already_processed) {
|
|
84
|
-
(0, dist_1.log_warn)(`trade_result_already_processed=${this.trade_result_already_processed}, ignore trade result fetch by ${source} check!`);
|
|
85
|
-
return;
|
|
86
|
-
}
|
|
87
|
-
const swap_detail = yield this.transactionParser.parseTransaction(txReceipt, this.pool_info);
|
|
88
|
-
let trade_result = this.map_swap_result_to_tx_result(swap_detail);
|
|
89
|
-
this.trade_result_already_processed = true;
|
|
90
|
-
if (this.intervalId) {
|
|
91
|
-
clearInterval(this.intervalId);
|
|
92
|
-
this.intervalId = null;
|
|
93
|
-
}
|
|
94
|
-
if (trade_result.success) {
|
|
95
|
-
this.event_emitter.emit(dist_1.TRANSACTION_STATE_SUCCESS, trade_result);
|
|
96
|
-
}
|
|
97
|
-
else {
|
|
98
|
-
this.event_emitter.emit(dist_1.TRANSACTION_STATE_FAILED, trade_result);
|
|
99
|
-
}
|
|
100
|
-
if (source === 'interval') {
|
|
101
|
-
console.log('--------------------- Transaction Result from Polling ---------------------');
|
|
102
|
-
}
|
|
103
|
-
else {
|
|
104
|
-
console.log('--------------------- Transaction Result from GRPC ---------------------');
|
|
105
|
-
}
|
|
106
|
-
console.log(JSON.stringify(trade_result, null, 2));
|
|
107
|
-
console.log('-----------------------------------------------------------------------------');
|
|
108
|
-
});
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
|
-
exports.TransactionResultChecker = TransactionResultChecker;
|