@clonegod/ttd-bsc-common 1.0.0
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/common/abi.d.ts +31 -0
- package/dist/common/abi.js +68 -0
- package/dist/common/constants.d.ts +21 -0
- package/dist/common/constants.js +37 -0
- package/dist/common/index.d.ts +2 -0
- package/dist/common/index.js +18 -0
- package/dist/config/bsc_env_args.d.ts +11 -0
- package/dist/config/bsc_env_args.js +19 -0
- package/dist/config/env_args.d.ts +13 -0
- package/dist/config/env_args.js +23 -0
- package/dist/config/index.d.ts +1 -0
- package/dist/config/index.js +17 -0
- package/dist/constants/networks.d.ts +10 -0
- package/dist/constants/networks.js +28 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.js +22 -0
- package/dist/providers/index.d.ts +9 -0
- package/dist/providers/index.js +21 -0
- package/dist/quote/event/index.d.ts +1 -0
- package/dist/quote/event/index.js +17 -0
- package/dist/quote/event/pool_event_listener.d.ts +39 -0
- package/dist/quote/event/pool_event_listener.js +231 -0
- package/dist/quote/event/verify_clmm_swap_event.d.ts +1 -0
- package/dist/quote/event/verify_clmm_swap_event.js +178 -0
- package/dist/quote/index.d.ts +2 -0
- package/dist/quote/index.js +18 -0
- package/dist/quote/pricing/index.d.ts +2 -0
- package/dist/quote/pricing/index.js +18 -0
- package/dist/quote/pricing/pool.d.ts +13 -0
- package/dist/quote/pricing/pool.js +21 -0
- package/dist/quote/pricing/token_price_cache.d.ts +9 -0
- package/dist/quote/pricing/token_price_cache.js +40 -0
- package/dist/trade/abstract_dex_trade.d.ts +27 -0
- package/dist/trade/abstract_dex_trade.js +152 -0
- package/dist/trade/check/index.d.ts +1 -0
- package/dist/trade/check/index.js +17 -0
- package/dist/trade/check/tx_websocket_manager.d.ts +23 -0
- package/dist/trade/check/tx_websocket_manager.js +119 -0
- package/dist/trade/index.d.ts +4 -0
- package/dist/trade/index.js +20 -0
- package/dist/trade/parse/abstract_parser.d.ts +8 -0
- package/dist/trade/parse/abstract_parser.js +2 -0
- package/dist/trade/parse/base_parser.d.ts +30 -0
- package/dist/trade/parse/base_parser.js +108 -0
- package/dist/trade/parse/index.d.ts +1 -0
- package/dist/trade/parse/index.js +5 -0
- package/dist/trade/send/48club.d.ts +18 -0
- package/dist/trade/send/48club.js +97 -0
- package/dist/trade/send/blockrazor.d.ts +7 -0
- package/dist/trade/send/blockrazor.js +79 -0
- package/dist/trade/send/bsc_rpc.d.ts +6 -0
- package/dist/trade/send/bsc_rpc.js +43 -0
- package/dist/trade/send/index.d.ts +5 -0
- package/dist/trade/send/index.js +23 -0
- package/dist/trade/send/send_tx.d.ts +8 -0
- package/dist/trade/send/send_tx.js +85 -0
- package/dist/types/config_types.d.ts +28 -0
- package/dist/types/config_types.js +2 -0
- package/dist/types/event_types.d.ts +30 -0
- package/dist/types/event_types.js +2 -0
- package/dist/types/index.d.ts +4 -0
- package/dist/types/index.js +20 -0
- package/dist/types/quote_types.d.ts +27 -0
- package/dist/types/quote_types.js +2 -0
- package/dist/types/trade_types.d.ts +38 -0
- package/dist/types/trade_types.js +8 -0
- package/dist/utils/gas_helper.d.ts +5 -0
- package/dist/utils/gas_helper.js +72 -0
- package/dist/utils/index.d.ts +7 -0
- package/dist/utils/index.js +43 -0
- package/dist/utils/redis_lock.d.ts +14 -0
- package/dist/utils/redis_lock.js +100 -0
- package/package.json +30 -0
|
@@ -0,0 +1,100 @@
|
|
|
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.RedisLock = void 0;
|
|
13
|
+
const dist_1 = require("@clonegod/ttd-core/dist");
|
|
14
|
+
class RedisLock {
|
|
15
|
+
constructor(lock_prefix) {
|
|
16
|
+
this.lock_prefix = lock_prefix;
|
|
17
|
+
this.redisClient = null;
|
|
18
|
+
this.lockMaxRetries = 10;
|
|
19
|
+
this.lockRetryDelayMs = 300;
|
|
20
|
+
this.lockExpireSeconds = 10;
|
|
21
|
+
}
|
|
22
|
+
getRedisClient() {
|
|
23
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
24
|
+
if (!this.redisClient) {
|
|
25
|
+
this.redisClient = yield (0, dist_1.getRedisCache)();
|
|
26
|
+
}
|
|
27
|
+
return this.redisClient;
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
getLockKey(lock_identifier) {
|
|
31
|
+
return `${this.lock_prefix}:lock:${lock_identifier}`;
|
|
32
|
+
}
|
|
33
|
+
acquireLock(lock_key_1, lock_value_1) {
|
|
34
|
+
return __awaiter(this, arguments, void 0, function* (lock_key, lock_value, expireSeconds = this.lockExpireSeconds) {
|
|
35
|
+
const redisClient = yield this.getRedisClient();
|
|
36
|
+
const result = yield redisClient.set(lock_key, lock_value, {
|
|
37
|
+
NX: true,
|
|
38
|
+
EX: expireSeconds
|
|
39
|
+
});
|
|
40
|
+
(0, dist_1.log_info)(`try acquireLock: lock_key=${lock_key}, lock_value=${lock_value}, expireSeconds=${expireSeconds}, result=${result}`);
|
|
41
|
+
const success = result === 'OK';
|
|
42
|
+
if (success) {
|
|
43
|
+
(0, dist_1.log_info)(`acquire lock success: lock_key=${lock_key}, lock_value=${lock_value}`);
|
|
44
|
+
}
|
|
45
|
+
return success;
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
releaseLock(lock_key, lock_value) {
|
|
49
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
50
|
+
const redisClient = yield this.getRedisClient();
|
|
51
|
+
const script = `
|
|
52
|
+
if redis.call('get', KEYS[1]) == ARGV[1] then
|
|
53
|
+
return redis.call('del', KEYS[1])
|
|
54
|
+
else
|
|
55
|
+
return 0
|
|
56
|
+
end
|
|
57
|
+
`;
|
|
58
|
+
const result = yield redisClient.eval(script, {
|
|
59
|
+
keys: [lock_key],
|
|
60
|
+
arguments: [lock_value]
|
|
61
|
+
});
|
|
62
|
+
const success = Number(result) === 1;
|
|
63
|
+
if (success) {
|
|
64
|
+
(0, dist_1.log_info)(`release lock success: lock_key=${lock_key}, lock_value=${lock_value}`);
|
|
65
|
+
}
|
|
66
|
+
else {
|
|
67
|
+
(0, dist_1.log_info)(`release lock failed: lock_key=${lock_key}, lock_value=${lock_value}, maybe expired or locked by other process`);
|
|
68
|
+
}
|
|
69
|
+
return success;
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
withLock(lock_identifier, callback) {
|
|
73
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
74
|
+
const lock_key = this.getLockKey(lock_identifier);
|
|
75
|
+
const lock_value = `${Date.now()}-${Math.random().toString(36).substring(2, 15)}`;
|
|
76
|
+
let retries = 0;
|
|
77
|
+
let acquired = false;
|
|
78
|
+
try {
|
|
79
|
+
while (retries < this.lockMaxRetries) {
|
|
80
|
+
acquired = yield this.acquireLock(lock_key, lock_value);
|
|
81
|
+
if (acquired)
|
|
82
|
+
break;
|
|
83
|
+
yield new Promise(resolve => setTimeout(resolve, this.lockRetryDelayMs));
|
|
84
|
+
retries++;
|
|
85
|
+
}
|
|
86
|
+
if (!acquired) {
|
|
87
|
+
throw new Error(`acquire lock failed: lock_key=${lock_key}, lock_value=${lock_value}, after ${this.lockMaxRetries} retries, maybe locked by other process`);
|
|
88
|
+
}
|
|
89
|
+
return yield callback();
|
|
90
|
+
}
|
|
91
|
+
finally {
|
|
92
|
+
if (acquired) {
|
|
93
|
+
yield (0, dist_1.sleep)(parseInt(process.env.NONCE_LOCK_RELEASE_DELAY_MS || '1000'));
|
|
94
|
+
yield this.releaseLock(lock_key, lock_value);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
exports.RedisLock = RedisLock;
|
package/package.json
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@clonegod/ttd-bsc-common",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "BSC common library",
|
|
5
|
+
"license": "UNLICENSED",
|
|
6
|
+
"main": "dist/index.js",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
|
+
"files": [
|
|
9
|
+
"dist"
|
|
10
|
+
],
|
|
11
|
+
"scripts": {
|
|
12
|
+
"clean": "rm -rf dist node_modules",
|
|
13
|
+
"build": "npx tsc --outDir ./dist",
|
|
14
|
+
"push": "npm run build && npm publish"
|
|
15
|
+
},
|
|
16
|
+
"dependencies": {
|
|
17
|
+
"@clonegod/ttd-core": "2.0.19",
|
|
18
|
+
"axios": "^1.8.4",
|
|
19
|
+
"dotenv": "^16.4.7",
|
|
20
|
+
"ethers": "^5.8.0"
|
|
21
|
+
},
|
|
22
|
+
"devDependencies": {
|
|
23
|
+
"@types/node": "^22.14.0",
|
|
24
|
+
"typescript": "^5.8.2"
|
|
25
|
+
},
|
|
26
|
+
"overrides": {},
|
|
27
|
+
"publishConfig": {
|
|
28
|
+
"access": "public"
|
|
29
|
+
}
|
|
30
|
+
}
|