@whiterresearch/litvmswap-aggregator 1.0.4
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/README.md +208 -0
- package/dist/abi/AGGFlowEntrypoint.d.ts +190 -0
- package/dist/abi/AGGFlowEntrypoint.js +114 -0
- package/dist/abi/AGGFlowRouter.d.ts +69 -0
- package/dist/abi/AGGFlowRouter.js +46 -0
- package/dist/abi/ERC20.d.ts +127 -0
- package/dist/abi/ERC20.js +96 -0
- package/dist/abi/UniswapV2Pair.d.ts +77 -0
- package/dist/abi/UniswapV2Pair.js +61 -0
- package/dist/abi/index.d.ts +4 -0
- package/dist/abi/index.js +20 -0
- package/dist/builder/opcodes.d.ts +21 -0
- package/dist/builder/opcodes.js +24 -0
- package/dist/builder/programBuilder.d.ts +12 -0
- package/dist/builder/programBuilder.js +99 -0
- package/dist/client.d.ts +90 -0
- package/dist/client.js +123 -0
- package/dist/constants/index.d.ts +67 -0
- package/dist/constants/index.js +168 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.js +51 -0
- package/dist/quoter/quoter.d.ts +19 -0
- package/dist/quoter/quoter.js +206 -0
- package/dist/swap/swapBuilder.d.ts +15 -0
- package/dist/swap/swapBuilder.js +136 -0
- package/dist/types/index.d.ts +80 -0
- package/dist/types/index.js +2 -0
- package/dist/utils/format.d.ts +18 -0
- package/dist/utils/format.js +51 -0
- package/package.json +56 -0
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
export declare const ERC20ABI: readonly [{
|
|
2
|
+
readonly type: "function";
|
|
3
|
+
readonly name: "name";
|
|
4
|
+
readonly inputs: readonly [];
|
|
5
|
+
readonly outputs: readonly [{
|
|
6
|
+
readonly name: "";
|
|
7
|
+
readonly type: "string";
|
|
8
|
+
}];
|
|
9
|
+
readonly stateMutability: "view";
|
|
10
|
+
}, {
|
|
11
|
+
readonly type: "function";
|
|
12
|
+
readonly name: "symbol";
|
|
13
|
+
readonly inputs: readonly [];
|
|
14
|
+
readonly outputs: readonly [{
|
|
15
|
+
readonly name: "";
|
|
16
|
+
readonly type: "string";
|
|
17
|
+
}];
|
|
18
|
+
readonly stateMutability: "view";
|
|
19
|
+
}, {
|
|
20
|
+
readonly type: "function";
|
|
21
|
+
readonly name: "decimals";
|
|
22
|
+
readonly inputs: readonly [];
|
|
23
|
+
readonly outputs: readonly [{
|
|
24
|
+
readonly name: "";
|
|
25
|
+
readonly type: "uint8";
|
|
26
|
+
}];
|
|
27
|
+
readonly stateMutability: "view";
|
|
28
|
+
}, {
|
|
29
|
+
readonly type: "function";
|
|
30
|
+
readonly name: "totalSupply";
|
|
31
|
+
readonly inputs: readonly [];
|
|
32
|
+
readonly outputs: readonly [{
|
|
33
|
+
readonly name: "";
|
|
34
|
+
readonly type: "uint256";
|
|
35
|
+
}];
|
|
36
|
+
readonly stateMutability: "view";
|
|
37
|
+
}, {
|
|
38
|
+
readonly type: "function";
|
|
39
|
+
readonly name: "balanceOf";
|
|
40
|
+
readonly inputs: readonly [{
|
|
41
|
+
readonly name: "account";
|
|
42
|
+
readonly type: "address";
|
|
43
|
+
}];
|
|
44
|
+
readonly outputs: readonly [{
|
|
45
|
+
readonly name: "";
|
|
46
|
+
readonly type: "uint256";
|
|
47
|
+
}];
|
|
48
|
+
readonly stateMutability: "view";
|
|
49
|
+
}, {
|
|
50
|
+
readonly type: "function";
|
|
51
|
+
readonly name: "allowance";
|
|
52
|
+
readonly inputs: readonly [{
|
|
53
|
+
readonly name: "owner";
|
|
54
|
+
readonly type: "address";
|
|
55
|
+
}, {
|
|
56
|
+
readonly name: "spender";
|
|
57
|
+
readonly type: "address";
|
|
58
|
+
}];
|
|
59
|
+
readonly outputs: readonly [{
|
|
60
|
+
readonly name: "";
|
|
61
|
+
readonly type: "uint256";
|
|
62
|
+
}];
|
|
63
|
+
readonly stateMutability: "view";
|
|
64
|
+
}, {
|
|
65
|
+
readonly type: "function";
|
|
66
|
+
readonly name: "approve";
|
|
67
|
+
readonly inputs: readonly [{
|
|
68
|
+
readonly name: "spender";
|
|
69
|
+
readonly type: "address";
|
|
70
|
+
}, {
|
|
71
|
+
readonly name: "amount";
|
|
72
|
+
readonly type: "uint256";
|
|
73
|
+
}];
|
|
74
|
+
readonly outputs: readonly [{
|
|
75
|
+
readonly name: "";
|
|
76
|
+
readonly type: "bool";
|
|
77
|
+
}];
|
|
78
|
+
readonly stateMutability: "nonpayable";
|
|
79
|
+
}, {
|
|
80
|
+
readonly type: "function";
|
|
81
|
+
readonly name: "transfer";
|
|
82
|
+
readonly inputs: readonly [{
|
|
83
|
+
readonly name: "to";
|
|
84
|
+
readonly type: "address";
|
|
85
|
+
}, {
|
|
86
|
+
readonly name: "amount";
|
|
87
|
+
readonly type: "uint256";
|
|
88
|
+
}];
|
|
89
|
+
readonly outputs: readonly [{
|
|
90
|
+
readonly name: "";
|
|
91
|
+
readonly type: "bool";
|
|
92
|
+
}];
|
|
93
|
+
readonly stateMutability: "nonpayable";
|
|
94
|
+
}, {
|
|
95
|
+
readonly type: "function";
|
|
96
|
+
readonly name: "transferFrom";
|
|
97
|
+
readonly inputs: readonly [{
|
|
98
|
+
readonly name: "from";
|
|
99
|
+
readonly type: "address";
|
|
100
|
+
}, {
|
|
101
|
+
readonly name: "to";
|
|
102
|
+
readonly type: "address";
|
|
103
|
+
}, {
|
|
104
|
+
readonly name: "amount";
|
|
105
|
+
readonly type: "uint256";
|
|
106
|
+
}];
|
|
107
|
+
readonly outputs: readonly [{
|
|
108
|
+
readonly name: "";
|
|
109
|
+
readonly type: "bool";
|
|
110
|
+
}];
|
|
111
|
+
readonly stateMutability: "nonpayable";
|
|
112
|
+
}, {
|
|
113
|
+
readonly type: "function";
|
|
114
|
+
readonly name: "deposit";
|
|
115
|
+
readonly inputs: readonly [];
|
|
116
|
+
readonly outputs: readonly [];
|
|
117
|
+
readonly stateMutability: "payable";
|
|
118
|
+
}, {
|
|
119
|
+
readonly type: "function";
|
|
120
|
+
readonly name: "withdraw";
|
|
121
|
+
readonly inputs: readonly [{
|
|
122
|
+
readonly name: "wad";
|
|
123
|
+
readonly type: "uint256";
|
|
124
|
+
}];
|
|
125
|
+
readonly outputs: readonly [];
|
|
126
|
+
readonly stateMutability: "nonpayable";
|
|
127
|
+
}];
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ERC20ABI = void 0;
|
|
4
|
+
exports.ERC20ABI = [
|
|
5
|
+
{
|
|
6
|
+
type: "function",
|
|
7
|
+
name: "name",
|
|
8
|
+
inputs: [],
|
|
9
|
+
outputs: [{ name: "", type: "string" }],
|
|
10
|
+
stateMutability: "view"
|
|
11
|
+
},
|
|
12
|
+
{
|
|
13
|
+
type: "function",
|
|
14
|
+
name: "symbol",
|
|
15
|
+
inputs: [],
|
|
16
|
+
outputs: [{ name: "", type: "string" }],
|
|
17
|
+
stateMutability: "view"
|
|
18
|
+
},
|
|
19
|
+
{
|
|
20
|
+
type: "function",
|
|
21
|
+
name: "decimals",
|
|
22
|
+
inputs: [],
|
|
23
|
+
outputs: [{ name: "", type: "uint8" }],
|
|
24
|
+
stateMutability: "view"
|
|
25
|
+
},
|
|
26
|
+
{
|
|
27
|
+
type: "function",
|
|
28
|
+
name: "totalSupply",
|
|
29
|
+
inputs: [],
|
|
30
|
+
outputs: [{ name: "", type: "uint256" }],
|
|
31
|
+
stateMutability: "view"
|
|
32
|
+
},
|
|
33
|
+
{
|
|
34
|
+
type: "function",
|
|
35
|
+
name: "balanceOf",
|
|
36
|
+
inputs: [{ name: "account", type: "address" }],
|
|
37
|
+
outputs: [{ name: "", type: "uint256" }],
|
|
38
|
+
stateMutability: "view"
|
|
39
|
+
},
|
|
40
|
+
{
|
|
41
|
+
type: "function",
|
|
42
|
+
name: "allowance",
|
|
43
|
+
inputs: [
|
|
44
|
+
{ name: "owner", type: "address" },
|
|
45
|
+
{ name: "spender", type: "address" }
|
|
46
|
+
],
|
|
47
|
+
outputs: [{ name: "", type: "uint256" }],
|
|
48
|
+
stateMutability: "view"
|
|
49
|
+
},
|
|
50
|
+
{
|
|
51
|
+
type: "function",
|
|
52
|
+
name: "approve",
|
|
53
|
+
inputs: [
|
|
54
|
+
{ name: "spender", type: "address" },
|
|
55
|
+
{ name: "amount", type: "uint256" }
|
|
56
|
+
],
|
|
57
|
+
outputs: [{ name: "", type: "bool" }],
|
|
58
|
+
stateMutability: "nonpayable"
|
|
59
|
+
},
|
|
60
|
+
{
|
|
61
|
+
type: "function",
|
|
62
|
+
name: "transfer",
|
|
63
|
+
inputs: [
|
|
64
|
+
{ name: "to", type: "address" },
|
|
65
|
+
{ name: "amount", type: "uint256" }
|
|
66
|
+
],
|
|
67
|
+
outputs: [{ name: "", type: "bool" }],
|
|
68
|
+
stateMutability: "nonpayable"
|
|
69
|
+
},
|
|
70
|
+
{
|
|
71
|
+
type: "function",
|
|
72
|
+
name: "transferFrom",
|
|
73
|
+
inputs: [
|
|
74
|
+
{ name: "from", type: "address" },
|
|
75
|
+
{ name: "to", type: "address" },
|
|
76
|
+
{ name: "amount", type: "uint256" }
|
|
77
|
+
],
|
|
78
|
+
outputs: [{ name: "", type: "bool" }],
|
|
79
|
+
stateMutability: "nonpayable"
|
|
80
|
+
},
|
|
81
|
+
// WETH deposit & withdraw
|
|
82
|
+
{
|
|
83
|
+
type: "function",
|
|
84
|
+
name: "deposit",
|
|
85
|
+
inputs: [],
|
|
86
|
+
outputs: [],
|
|
87
|
+
stateMutability: "payable"
|
|
88
|
+
},
|
|
89
|
+
{
|
|
90
|
+
type: "function",
|
|
91
|
+
name: "withdraw",
|
|
92
|
+
inputs: [{ name: "wad", type: "uint256" }],
|
|
93
|
+
outputs: [],
|
|
94
|
+
stateMutability: "nonpayable"
|
|
95
|
+
}
|
|
96
|
+
];
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
export declare const UniswapV2PairABI: readonly [{
|
|
2
|
+
readonly type: "function";
|
|
3
|
+
readonly name: "token0";
|
|
4
|
+
readonly inputs: readonly [];
|
|
5
|
+
readonly outputs: readonly [{
|
|
6
|
+
readonly name: "";
|
|
7
|
+
readonly type: "address";
|
|
8
|
+
}];
|
|
9
|
+
readonly stateMutability: "view";
|
|
10
|
+
}, {
|
|
11
|
+
readonly type: "function";
|
|
12
|
+
readonly name: "token1";
|
|
13
|
+
readonly inputs: readonly [];
|
|
14
|
+
readonly outputs: readonly [{
|
|
15
|
+
readonly name: "";
|
|
16
|
+
readonly type: "address";
|
|
17
|
+
}];
|
|
18
|
+
readonly stateMutability: "view";
|
|
19
|
+
}, {
|
|
20
|
+
readonly type: "function";
|
|
21
|
+
readonly name: "getReserves";
|
|
22
|
+
readonly inputs: readonly [];
|
|
23
|
+
readonly outputs: readonly [{
|
|
24
|
+
readonly name: "reserve0";
|
|
25
|
+
readonly type: "uint112";
|
|
26
|
+
}, {
|
|
27
|
+
readonly name: "reserve1";
|
|
28
|
+
readonly type: "uint112";
|
|
29
|
+
}, {
|
|
30
|
+
readonly name: "blockTimestampLast";
|
|
31
|
+
readonly type: "uint32";
|
|
32
|
+
}];
|
|
33
|
+
readonly stateMutability: "view";
|
|
34
|
+
}, {
|
|
35
|
+
readonly type: "function";
|
|
36
|
+
readonly name: "swap";
|
|
37
|
+
readonly inputs: readonly [{
|
|
38
|
+
readonly name: "amount0Out";
|
|
39
|
+
readonly type: "uint256";
|
|
40
|
+
}, {
|
|
41
|
+
readonly name: "amount1Out";
|
|
42
|
+
readonly type: "uint256";
|
|
43
|
+
}, {
|
|
44
|
+
readonly name: "to";
|
|
45
|
+
readonly type: "address";
|
|
46
|
+
}, {
|
|
47
|
+
readonly name: "data";
|
|
48
|
+
readonly type: "bytes";
|
|
49
|
+
}];
|
|
50
|
+
readonly outputs: readonly [];
|
|
51
|
+
readonly stateMutability: "nonpayable";
|
|
52
|
+
}];
|
|
53
|
+
export declare const UniswapV2FactoryABI: readonly [{
|
|
54
|
+
readonly type: "function";
|
|
55
|
+
readonly name: "getPair";
|
|
56
|
+
readonly inputs: readonly [{
|
|
57
|
+
readonly name: "tokenA";
|
|
58
|
+
readonly type: "address";
|
|
59
|
+
}, {
|
|
60
|
+
readonly name: "tokenB";
|
|
61
|
+
readonly type: "address";
|
|
62
|
+
}];
|
|
63
|
+
readonly outputs: readonly [{
|
|
64
|
+
readonly name: "pair";
|
|
65
|
+
readonly type: "address";
|
|
66
|
+
}];
|
|
67
|
+
readonly stateMutability: "view";
|
|
68
|
+
}, {
|
|
69
|
+
readonly type: "function";
|
|
70
|
+
readonly name: "allPairsLength";
|
|
71
|
+
readonly inputs: readonly [];
|
|
72
|
+
readonly outputs: readonly [{
|
|
73
|
+
readonly name: "";
|
|
74
|
+
readonly type: "uint256";
|
|
75
|
+
}];
|
|
76
|
+
readonly stateMutability: "view";
|
|
77
|
+
}];
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.UniswapV2FactoryABI = exports.UniswapV2PairABI = void 0;
|
|
4
|
+
exports.UniswapV2PairABI = [
|
|
5
|
+
{
|
|
6
|
+
type: "function",
|
|
7
|
+
name: "token0",
|
|
8
|
+
inputs: [],
|
|
9
|
+
outputs: [{ name: "", type: "address" }],
|
|
10
|
+
stateMutability: "view"
|
|
11
|
+
},
|
|
12
|
+
{
|
|
13
|
+
type: "function",
|
|
14
|
+
name: "token1",
|
|
15
|
+
inputs: [],
|
|
16
|
+
outputs: [{ name: "", type: "address" }],
|
|
17
|
+
stateMutability: "view"
|
|
18
|
+
},
|
|
19
|
+
{
|
|
20
|
+
type: "function",
|
|
21
|
+
name: "getReserves",
|
|
22
|
+
inputs: [],
|
|
23
|
+
outputs: [
|
|
24
|
+
{ name: "reserve0", type: "uint112" },
|
|
25
|
+
{ name: "reserve1", type: "uint112" },
|
|
26
|
+
{ name: "blockTimestampLast", type: "uint32" }
|
|
27
|
+
],
|
|
28
|
+
stateMutability: "view"
|
|
29
|
+
},
|
|
30
|
+
{
|
|
31
|
+
type: "function",
|
|
32
|
+
name: "swap",
|
|
33
|
+
inputs: [
|
|
34
|
+
{ name: "amount0Out", type: "uint256" },
|
|
35
|
+
{ name: "amount1Out", type: "uint256" },
|
|
36
|
+
{ name: "to", type: "address" },
|
|
37
|
+
{ name: "data", type: "bytes" }
|
|
38
|
+
],
|
|
39
|
+
outputs: [],
|
|
40
|
+
stateMutability: "nonpayable"
|
|
41
|
+
}
|
|
42
|
+
];
|
|
43
|
+
exports.UniswapV2FactoryABI = [
|
|
44
|
+
{
|
|
45
|
+
type: "function",
|
|
46
|
+
name: "getPair",
|
|
47
|
+
inputs: [
|
|
48
|
+
{ name: "tokenA", type: "address" },
|
|
49
|
+
{ name: "tokenB", type: "address" }
|
|
50
|
+
],
|
|
51
|
+
outputs: [{ name: "pair", type: "address" }],
|
|
52
|
+
stateMutability: "view"
|
|
53
|
+
},
|
|
54
|
+
{
|
|
55
|
+
type: "function",
|
|
56
|
+
name: "allPairsLength",
|
|
57
|
+
inputs: [],
|
|
58
|
+
outputs: [{ name: "", type: "uint256" }],
|
|
59
|
+
stateMutability: "view"
|
|
60
|
+
}
|
|
61
|
+
];
|
|
@@ -0,0 +1,20 @@
|
|
|
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("./AGGFlowEntrypoint.js"), exports);
|
|
18
|
+
__exportStar(require("./AGGFlowRouter.js"), exports);
|
|
19
|
+
__exportStar(require("./ERC20.js"), exports);
|
|
20
|
+
__exportStar(require("./UniswapV2Pair.js"), exports);
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
export declare const OPCODES: {
|
|
2
|
+
readonly PROCESS_ROUTER_ERC20: 1;
|
|
3
|
+
readonly PROCESS_USER_ERC20: 2;
|
|
4
|
+
readonly PROCESS_NATIVE: 3;
|
|
5
|
+
readonly PROCESS_ONE_POOL: 4;
|
|
6
|
+
readonly APPLY_PERMIT: 5;
|
|
7
|
+
};
|
|
8
|
+
export declare const POOL_TYPES: {
|
|
9
|
+
readonly UNIV2: 0;
|
|
10
|
+
readonly UNIV3: 1;
|
|
11
|
+
readonly WRAP: 2;
|
|
12
|
+
readonly AGG: 3;
|
|
13
|
+
readonly CRYSTAL: 4;
|
|
14
|
+
readonly CURVE: 5;
|
|
15
|
+
readonly PALINDROME_FI: 6;
|
|
16
|
+
readonly LIQUIDITY_BOOK: 7;
|
|
17
|
+
};
|
|
18
|
+
export declare const WRAP_FLAGS: {
|
|
19
|
+
readonly WRAP: 1;
|
|
20
|
+
readonly UNWRAP: 0;
|
|
21
|
+
};
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.WRAP_FLAGS = exports.POOL_TYPES = exports.OPCODES = void 0;
|
|
4
|
+
exports.OPCODES = {
|
|
5
|
+
PROCESS_ROUTER_ERC20: 0x01,
|
|
6
|
+
PROCESS_USER_ERC20: 0x02,
|
|
7
|
+
PROCESS_NATIVE: 0x03,
|
|
8
|
+
PROCESS_ONE_POOL: 0x04,
|
|
9
|
+
APPLY_PERMIT: 0x05,
|
|
10
|
+
};
|
|
11
|
+
exports.POOL_TYPES = {
|
|
12
|
+
UNIV2: 0x00,
|
|
13
|
+
UNIV3: 0x01,
|
|
14
|
+
WRAP: 0x02,
|
|
15
|
+
AGG: 0x03,
|
|
16
|
+
CRYSTAL: 0x04,
|
|
17
|
+
CURVE: 0x05,
|
|
18
|
+
PALINDROME_FI: 0x06,
|
|
19
|
+
LIQUIDITY_BOOK: 0x07,
|
|
20
|
+
};
|
|
21
|
+
exports.WRAP_FLAGS = {
|
|
22
|
+
WRAP: 0x01, // Wrap native -> WETH
|
|
23
|
+
UNWRAP: 0x00, // Unwrap WETH -> native
|
|
24
|
+
};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { Address, Hex } from 'viem';
|
|
2
|
+
import type { RouteLeg } from '../types/index.js';
|
|
3
|
+
import type { TokenInfo } from '../constants/index.js';
|
|
4
|
+
/**
|
|
5
|
+
* Converts a hex address (0x...) into an array of 20 bytes
|
|
6
|
+
*/
|
|
7
|
+
export declare function addressToBytes(address: Address | string): number[];
|
|
8
|
+
/**
|
|
9
|
+
* Encodes the bytecode program for AGGFlowRouter.executeRoute
|
|
10
|
+
* Compatible with LitecoinVM (Chain 4441)
|
|
11
|
+
*/
|
|
12
|
+
export declare function buildAggregatorProgram(fromToken: TokenInfo, toToken: TokenInfo, route: RouteLeg, wethAddress: Address): Hex;
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.addressToBytes = addressToBytes;
|
|
4
|
+
exports.buildAggregatorProgram = buildAggregatorProgram;
|
|
5
|
+
const opcodes_js_1 = require("./opcodes.js");
|
|
6
|
+
/**
|
|
7
|
+
* Converts a hex address (0x...) into an array of 20 bytes
|
|
8
|
+
*/
|
|
9
|
+
function addressToBytes(address) {
|
|
10
|
+
const clean = address.toLowerCase().replace('0x', '').padStart(40, '0');
|
|
11
|
+
const bytes = [];
|
|
12
|
+
for (let i = 0; i < 40; i += 2) {
|
|
13
|
+
bytes.push(parseInt(clean.slice(i, i + 2), 16));
|
|
14
|
+
}
|
|
15
|
+
return bytes;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Encodes the bytecode program for AGGFlowRouter.executeRoute
|
|
19
|
+
* Compatible with LitecoinVM (Chain 4441)
|
|
20
|
+
*/
|
|
21
|
+
function buildAggregatorProgram(fromToken, toToken, route, wethAddress) {
|
|
22
|
+
if (!route?.poolAddress) {
|
|
23
|
+
throw new Error('Invalid route: poolAddress is required');
|
|
24
|
+
}
|
|
25
|
+
const isNativeIn = !!fromToken.isNative;
|
|
26
|
+
const isNativeOut = !!toToken.isNative;
|
|
27
|
+
const tokenInSwap = isNativeIn ? wethAddress : fromToken.address;
|
|
28
|
+
const tokenOutSwap = isNativeOut ? wethAddress : toToken.address;
|
|
29
|
+
const bytes = [];
|
|
30
|
+
// 1. Process Input Opcode
|
|
31
|
+
if (isNativeIn) {
|
|
32
|
+
// Opcode 0x03: PROCESS_NATIVE
|
|
33
|
+
bytes.push(opcodes_js_1.OPCODES.PROCESS_NATIVE);
|
|
34
|
+
bytes.push(0x01); // 1 pool leg
|
|
35
|
+
bytes.push(0xff, 0xff); // 100% share (65535)
|
|
36
|
+
bytes.push(opcodes_js_1.POOL_TYPES.WRAP); // PT_WRAP
|
|
37
|
+
bytes.push(opcodes_js_1.WRAP_FLAGS.WRAP); // Flag: 1 = wrap deposit
|
|
38
|
+
}
|
|
39
|
+
else {
|
|
40
|
+
// Opcode 0x02: PROCESS_USER_ERC20
|
|
41
|
+
bytes.push(opcodes_js_1.OPCODES.PROCESS_USER_ERC20);
|
|
42
|
+
bytes.push(...addressToBytes(fromToken.address));
|
|
43
|
+
bytes.push(0x01); // 1 pool leg
|
|
44
|
+
bytes.push(0xff, 0xff); // 100% share
|
|
45
|
+
addSwapLeg(bytes, route, tokenInSwap, tokenOutSwap);
|
|
46
|
+
}
|
|
47
|
+
// 2. If Native Input, now swap the wrapped WETH token
|
|
48
|
+
if (isNativeIn) {
|
|
49
|
+
// Opcode 0x01: PROCESS_ROUTER_ERC20
|
|
50
|
+
bytes.push(opcodes_js_1.OPCODES.PROCESS_ROUTER_ERC20);
|
|
51
|
+
bytes.push(...addressToBytes(wethAddress));
|
|
52
|
+
bytes.push(0x01); // 1 pool leg
|
|
53
|
+
bytes.push(0xff, 0xff); // 100% share
|
|
54
|
+
addSwapLeg(bytes, route, tokenInSwap, tokenOutSwap);
|
|
55
|
+
}
|
|
56
|
+
// 3. If Native Output, unwrap WETH -> native
|
|
57
|
+
if (isNativeOut) {
|
|
58
|
+
// Opcode 0x01: PROCESS_ROUTER_ERC20
|
|
59
|
+
bytes.push(opcodes_js_1.OPCODES.PROCESS_ROUTER_ERC20);
|
|
60
|
+
bytes.push(...addressToBytes(wethAddress));
|
|
61
|
+
bytes.push(0x01); // 1 leg
|
|
62
|
+
bytes.push(0xff, 0xff); // 100% share
|
|
63
|
+
bytes.push(opcodes_js_1.POOL_TYPES.WRAP); // PT_WRAP
|
|
64
|
+
bytes.push(opcodes_js_1.WRAP_FLAGS.UNWRAP); // Flag: 0 = unwrap withdraw
|
|
65
|
+
}
|
|
66
|
+
const hexString = ('0x' + bytes.map((b) => b.toString(16).padStart(2, '0')).join(''));
|
|
67
|
+
return hexString;
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Appends the swap configuration bytes based on pool type (V2, V3, Curve)
|
|
71
|
+
*/
|
|
72
|
+
function addSwapLeg(bytes, route, tokenIn, tokenOut) {
|
|
73
|
+
const isV2 = route.poolType === 'v2';
|
|
74
|
+
const isCurve = route.poolType === 'curve';
|
|
75
|
+
const zeroForOne = tokenIn.toLowerCase() < tokenOut.toLowerCase();
|
|
76
|
+
if (isV2) {
|
|
77
|
+
// PT_UNIV2 = 0x00
|
|
78
|
+
bytes.push(opcodes_js_1.POOL_TYPES.UNIV2);
|
|
79
|
+
bytes.push(...addressToBytes(route.poolAddress));
|
|
80
|
+
bytes.push(zeroForOne ? 0 : 1);
|
|
81
|
+
// fee in 24 bits (e.g. 3000 fee = 0.3%)
|
|
82
|
+
const fee = Number(route.fee || 3000);
|
|
83
|
+
bytes.push((fee >> 16) & 0xff, (fee >> 8) & 0xff, fee & 0xff);
|
|
84
|
+
}
|
|
85
|
+
else if (isCurve) {
|
|
86
|
+
// PT_CURVE = 0x05
|
|
87
|
+
bytes.push(opcodes_js_1.POOL_TYPES.CURVE);
|
|
88
|
+
bytes.push(...addressToBytes(route.poolAddress));
|
|
89
|
+
bytes.push(route.curvePoolType ?? 0);
|
|
90
|
+
bytes.push(route.curveIndices?.from ?? 0);
|
|
91
|
+
bytes.push(route.curveIndices?.to ?? 1);
|
|
92
|
+
}
|
|
93
|
+
else {
|
|
94
|
+
// PT_UNIV3 = 0x01 (Uniswap V3, Inky V3, Algebra)
|
|
95
|
+
bytes.push(opcodes_js_1.POOL_TYPES.UNIV3);
|
|
96
|
+
bytes.push(...addressToBytes(route.poolAddress));
|
|
97
|
+
bytes.push(zeroForOne ? 1 : 0);
|
|
98
|
+
}
|
|
99
|
+
}
|
package/dist/client.d.ts
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import { type Address, type PublicClient, type WalletClient, type Hex } from 'viem';
|
|
2
|
+
import { type TokenInfo } from './constants/index.js';
|
|
3
|
+
import { LitVMQuoter } from './quoter/quoter.js';
|
|
4
|
+
import type { QuoteParams, QuoteResult, BuildSwapTxParams, PreparedTransaction, FeeConfig } from './types/index.js';
|
|
5
|
+
export interface LitVMAggregatorConfig {
|
|
6
|
+
rpcUrl?: string;
|
|
7
|
+
publicClient?: PublicClient;
|
|
8
|
+
defaultFeeConfig?: FeeConfig;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Main SDK Client for LitecoinVM (LitVM) DEX Aggregator
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```ts
|
|
15
|
+
* import { LitVMAggregator } from '@whiterresearch/litvmswap-aggregator';
|
|
16
|
+
*
|
|
17
|
+
* const aggregator = new LitVMAggregator();
|
|
18
|
+
*
|
|
19
|
+
* // 1. Get best quote across LitVM liquidity pools
|
|
20
|
+
* const quote = await aggregator.getQuote({
|
|
21
|
+
* tokenIn: 'zkLTC',
|
|
22
|
+
* tokenOut: 'ZKUSDC',
|
|
23
|
+
* amountIn: '1.0',
|
|
24
|
+
* slippageTolerancePercent: 0.5,
|
|
25
|
+
* });
|
|
26
|
+
*
|
|
27
|
+
* // 2. Build ready-to-execute swap transaction
|
|
28
|
+
* const tx = aggregator.buildSwapTx({
|
|
29
|
+
* quote,
|
|
30
|
+
* userAddress: '0x1234...',
|
|
31
|
+
* });
|
|
32
|
+
* ```
|
|
33
|
+
*/
|
|
34
|
+
export declare class LitVMAggregator {
|
|
35
|
+
readonly quoter: LitVMQuoter;
|
|
36
|
+
readonly publicClient: PublicClient;
|
|
37
|
+
private defaultFeeConfig?;
|
|
38
|
+
constructor(config?: LitVMAggregatorConfig);
|
|
39
|
+
/**
|
|
40
|
+
* Fetches best quote and generates execution program across LitVM pools
|
|
41
|
+
*/
|
|
42
|
+
getQuote(params: QuoteParams): Promise<QuoteResult>;
|
|
43
|
+
/**
|
|
44
|
+
* Prepares a ready-to-sign transaction object (to, data, value, chainId)
|
|
45
|
+
*/
|
|
46
|
+
buildSwapTx(params: BuildSwapTxParams): PreparedTransaction;
|
|
47
|
+
/**
|
|
48
|
+
* Checks ERC20 token allowance for a given spender (defaults to Aggregator Entrypoint)
|
|
49
|
+
*/
|
|
50
|
+
checkAllowance(params: {
|
|
51
|
+
tokenAddress: Address;
|
|
52
|
+
ownerAddress: Address;
|
|
53
|
+
spenderAddress?: Address;
|
|
54
|
+
}): Promise<bigint>;
|
|
55
|
+
/**
|
|
56
|
+
* Checks if an ERC20 token needs approval for a swap
|
|
57
|
+
*/
|
|
58
|
+
needsApproval(params: {
|
|
59
|
+
tokenAddress: Address;
|
|
60
|
+
ownerAddress: Address;
|
|
61
|
+
amount: bigint;
|
|
62
|
+
spenderAddress?: Address;
|
|
63
|
+
}): Promise<boolean>;
|
|
64
|
+
/**
|
|
65
|
+
* Generates ERC20 approval transaction data
|
|
66
|
+
*/
|
|
67
|
+
buildApproveTx(params: {
|
|
68
|
+
tokenAddress: Address;
|
|
69
|
+
spenderAddress?: Address;
|
|
70
|
+
amount?: bigint;
|
|
71
|
+
}): PreparedTransaction;
|
|
72
|
+
/**
|
|
73
|
+
* Resolves token metadata from address or symbol
|
|
74
|
+
*/
|
|
75
|
+
getToken(addressOrSymbol: string): Promise<TokenInfo>;
|
|
76
|
+
/**
|
|
77
|
+
* Returns list of verified tokens on LitecoinVM
|
|
78
|
+
*/
|
|
79
|
+
getKnownTokens(): TokenInfo[];
|
|
80
|
+
/**
|
|
81
|
+
* Directly executes a swap using a Viem WalletClient
|
|
82
|
+
*/
|
|
83
|
+
executeSwapWithWallet(params: {
|
|
84
|
+
walletClient: WalletClient;
|
|
85
|
+
quote: QuoteResult;
|
|
86
|
+
userAddress: Address;
|
|
87
|
+
receiverAddress?: Address;
|
|
88
|
+
feeConfig?: FeeConfig;
|
|
89
|
+
}): Promise<Hex>;
|
|
90
|
+
}
|