intents-1click-rule-engine 1.0.4 → 1.0.6
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/index.js +20 -4
- package/dist/src/matcher.js +5 -4
- package/dist/src/rule-engine.js +17 -13
- package/dist/src/token-registry.js +8 -5
- package/dist/src/types.js +2 -1
- package/dist/src/validator.js +4 -1
- package/package.json +3 -5
package/dist/index.js
CHANGED
|
@@ -1,4 +1,20 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
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("./src/matcher"), exports);
|
|
18
|
+
__exportStar(require("./src/token-registry"), exports);
|
|
19
|
+
__exportStar(require("./src/validator"), exports);
|
|
20
|
+
__exportStar(require("./src/rule-engine"), exports);
|
package/dist/src/matcher.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.RuleMatcher = void 0;
|
|
4
|
+
class RuleMatcher {
|
|
5
5
|
constructor(config, tokenRegistry) {
|
|
6
6
|
this.defaultFee = config.default_fee;
|
|
7
7
|
this.rules = this.sortRulesByPriority(config.rules);
|
|
@@ -108,3 +108,4 @@ export class RuleMatcher {
|
|
|
108
108
|
};
|
|
109
109
|
}
|
|
110
110
|
}
|
|
111
|
+
exports.RuleMatcher = RuleMatcher;
|
package/dist/src/rule-engine.js
CHANGED
|
@@ -1,11 +1,17 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.RuleEngine = void 0;
|
|
4
|
+
exports.getTotalBps = getTotalBps;
|
|
5
|
+
exports.calculateFee = calculateFee;
|
|
6
|
+
exports.calculateAmountAfterFee = calculateAmountAfterFee;
|
|
7
|
+
const matcher_1 = require("./matcher");
|
|
8
|
+
const token_registry_1 = require("./token-registry");
|
|
9
|
+
const validator_1 = require("./validator");
|
|
4
10
|
const DEFAULT_TOKEN_REGISTRY_URL = "https://1click.chaindefuser.com/v0/tokens";
|
|
5
11
|
const DEFAULT_CACHE_TTL_MS = 3600000; // 1 hour
|
|
6
12
|
const BPS_DIVISOR = 10000n;
|
|
7
13
|
const MAX_BPS = 10000; // 100% fee cap
|
|
8
|
-
|
|
14
|
+
function getTotalBps(fee) {
|
|
9
15
|
if (Array.isArray(fee)) {
|
|
10
16
|
return fee.reduce((sum, f) => sum + f.bps, 0);
|
|
11
17
|
}
|
|
@@ -44,33 +50,30 @@ function validateBps(bps) {
|
|
|
44
50
|
throw new Error(`Invalid bps: ${bps} exceeds maximum of ${MAX_BPS} (100%)`);
|
|
45
51
|
}
|
|
46
52
|
}
|
|
47
|
-
|
|
53
|
+
function calculateFee(amount, bps) {
|
|
48
54
|
const amountBigInt = parseAmount(amount);
|
|
49
55
|
validateBps(bps);
|
|
50
56
|
const fee = (amountBigInt * BigInt(bps)) / BPS_DIVISOR;
|
|
51
57
|
return fee.toString();
|
|
52
58
|
}
|
|
53
|
-
|
|
59
|
+
function calculateAmountAfterFee(amount, bps) {
|
|
54
60
|
const amountBigInt = parseAmount(amount);
|
|
55
61
|
validateBps(bps);
|
|
56
62
|
const fee = BigInt(calculateFee(amountBigInt, bps));
|
|
57
63
|
return (amountBigInt - fee).toString();
|
|
58
64
|
}
|
|
59
|
-
|
|
60
|
-
matcher;
|
|
61
|
-
tokenRegistry;
|
|
62
|
-
feeConfig;
|
|
65
|
+
class RuleEngine {
|
|
63
66
|
constructor(feeConfig, options) {
|
|
64
|
-
const validation = validateConfig(feeConfig);
|
|
67
|
+
const validation = (0, validator_1.validateConfig)(feeConfig);
|
|
65
68
|
if (!validation.valid) {
|
|
66
69
|
throw new Error(`Invalid fee config: ${validation.errors.map((e) => `${e.path}: ${e.message}`).join(", ")}`);
|
|
67
70
|
}
|
|
68
71
|
this.feeConfig = feeConfig;
|
|
69
|
-
this.tokenRegistry = new CachedTokenRegistry({
|
|
72
|
+
this.tokenRegistry = new token_registry_1.CachedTokenRegistry({
|
|
70
73
|
url: options?.tokenRegistryUrl ?? DEFAULT_TOKEN_REGISTRY_URL,
|
|
71
74
|
cacheTtlMs: options?.tokenRegistryCacheTtlMs ?? DEFAULT_CACHE_TTL_MS,
|
|
72
75
|
});
|
|
73
|
-
this.matcher = new RuleMatcher(this.feeConfig, this.tokenRegistry);
|
|
76
|
+
this.matcher = new matcher_1.RuleMatcher(this.feeConfig, this.tokenRegistry);
|
|
74
77
|
}
|
|
75
78
|
async initialize() {
|
|
76
79
|
await this.tokenRegistry.refresh();
|
|
@@ -92,3 +95,4 @@ export class RuleEngine {
|
|
|
92
95
|
return this.feeConfig;
|
|
93
96
|
}
|
|
94
97
|
}
|
|
98
|
+
exports.RuleEngine = RuleEngine;
|
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.CachedTokenRegistry = void 0;
|
|
1
4
|
function isValidTokenResponse(token) {
|
|
2
5
|
if (typeof token !== "object" || token === null)
|
|
3
6
|
return false;
|
|
@@ -7,12 +10,11 @@ function isValidTokenResponse(token) {
|
|
|
7
10
|
typeof t.symbol === "string" &&
|
|
8
11
|
typeof t.decimals === "number");
|
|
9
12
|
}
|
|
10
|
-
|
|
11
|
-
config;
|
|
12
|
-
cache = new Map();
|
|
13
|
-
lastFetchTime = 0;
|
|
14
|
-
refreshPromise = null;
|
|
13
|
+
class CachedTokenRegistry {
|
|
15
14
|
constructor(config) {
|
|
15
|
+
this.cache = new Map();
|
|
16
|
+
this.lastFetchTime = 0;
|
|
17
|
+
this.refreshPromise = null;
|
|
16
18
|
this.config = config;
|
|
17
19
|
}
|
|
18
20
|
isCacheValid() {
|
|
@@ -76,3 +78,4 @@ export class CachedTokenRegistry {
|
|
|
76
78
|
return this.cache.size;
|
|
77
79
|
}
|
|
78
80
|
}
|
|
81
|
+
exports.CachedTokenRegistry = CachedTokenRegistry;
|
package/dist/src/types.js
CHANGED
|
@@ -1 +1,2 @@
|
|
|
1
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
package/dist/src/validator.js
CHANGED
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.validateConfig = validateConfig;
|
|
1
4
|
const NEAR_ACCOUNT_REGEX = /^(?:[a-z\d]+[-_])*[a-z\d]+(?:\.[a-z\d]+[-_]*[a-z\d]+)*$/;
|
|
2
5
|
const NEAR_IMPLICIT_ACCOUNT_REGEX = /^[a-f0-9]{64}$/;
|
|
3
6
|
const MAX_BPS = 10000; // 100% fee cap
|
|
@@ -141,7 +144,7 @@ function validateRule(rule, index) {
|
|
|
141
144
|
}
|
|
142
145
|
return errors;
|
|
143
146
|
}
|
|
144
|
-
|
|
147
|
+
function validateConfig(config) {
|
|
145
148
|
const errors = [];
|
|
146
149
|
if (!config.version || typeof config.version !== "string") {
|
|
147
150
|
errors.push({ path: "version", message: "version is required and must be a string" });
|
package/package.json
CHANGED
|
@@ -1,15 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "intents-1click-rule-engine",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.6",
|
|
4
4
|
"description": "Fee configuration rule engine for NEAR Intents 1Click API",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
|
-
"module": "./dist/index.js",
|
|
7
6
|
"types": "./dist/index.d.ts",
|
|
8
|
-
"type": "module",
|
|
9
7
|
"exports": {
|
|
10
8
|
".": {
|
|
11
9
|
"types": "./dist/index.d.ts",
|
|
12
|
-
"
|
|
10
|
+
"require": "./dist/index.js",
|
|
13
11
|
"default": "./dist/index.js"
|
|
14
12
|
}
|
|
15
13
|
},
|
|
@@ -17,7 +15,7 @@
|
|
|
17
15
|
"dist"
|
|
18
16
|
],
|
|
19
17
|
"scripts": {
|
|
20
|
-
"build": "tsc -p tsconfig.build.json",
|
|
18
|
+
"build": "rm -rf dist && tsc -p tsconfig.build.json",
|
|
21
19
|
"prepublishOnly": "bun run build",
|
|
22
20
|
"test": "bun test",
|
|
23
21
|
"typecheck": "tsc --noEmit"
|