@etcswapv2/sdk-legacy 1.0.0 → 1.0.1
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 +29 -2
- package/dist/index.mjs +29 -2
- package/package.json +11 -9
package/dist/index.js
CHANGED
|
@@ -46,6 +46,23 @@ __export(index_exports, {
|
|
|
46
46
|
currencyEquals: () => currencyEquals
|
|
47
47
|
});
|
|
48
48
|
module.exports = __toCommonJS(index_exports);
|
|
49
|
+
var import_viem = require("viem");
|
|
50
|
+
function computeCreate2Address(factoryAddress, token0Address, token1Address, initCodeHash) {
|
|
51
|
+
const salt = (0, import_viem.keccak256)(
|
|
52
|
+
(0, import_viem.concat)([
|
|
53
|
+
(0, import_viem.toBytes)(token0Address),
|
|
54
|
+
(0, import_viem.toBytes)(token1Address)
|
|
55
|
+
])
|
|
56
|
+
);
|
|
57
|
+
const create2Input = (0, import_viem.concat)([
|
|
58
|
+
(0, import_viem.toBytes)("0xff"),
|
|
59
|
+
(0, import_viem.toBytes)(factoryAddress),
|
|
60
|
+
(0, import_viem.toBytes)(salt),
|
|
61
|
+
(0, import_viem.toBytes)(initCodeHash)
|
|
62
|
+
]);
|
|
63
|
+
const hash = (0, import_viem.keccak256)(create2Input);
|
|
64
|
+
return (0, import_viem.getAddress)(`0x${hash.slice(-40)}`);
|
|
65
|
+
}
|
|
49
66
|
var ChainId = /* @__PURE__ */ ((ChainId2) => {
|
|
50
67
|
ChainId2[ChainId2["MAINNET"] = 1] = "MAINNET";
|
|
51
68
|
ChainId2[ChainId2["ROPSTEN"] = 3] = "ROPSTEN";
|
|
@@ -345,8 +362,18 @@ var Pair = class _Pair {
|
|
|
345
362
|
}
|
|
346
363
|
static getAddress(tokenA, tokenB) {
|
|
347
364
|
const [token0, token1] = tokenA.sortsBefore(tokenB) ? [tokenA, tokenB] : [tokenB, tokenA];
|
|
348
|
-
const
|
|
349
|
-
|
|
365
|
+
const chainId = token0.chainId;
|
|
366
|
+
const factoryAddress = FACTORY_ADDRESS[chainId];
|
|
367
|
+
const initCodeHash = INIT_CODE_HASH[chainId];
|
|
368
|
+
if (!factoryAddress || !initCodeHash) {
|
|
369
|
+
throw new Error(`Unsupported chain ${chainId}`);
|
|
370
|
+
}
|
|
371
|
+
return computeCreate2Address(
|
|
372
|
+
factoryAddress,
|
|
373
|
+
token0.address,
|
|
374
|
+
token1.address,
|
|
375
|
+
initCodeHash
|
|
376
|
+
);
|
|
350
377
|
}
|
|
351
378
|
get token0Price() {
|
|
352
379
|
return new Price(this.token0, this.token1, this.reserve0.raw, this.reserve1.raw);
|
package/dist/index.mjs
CHANGED
|
@@ -1,4 +1,21 @@
|
|
|
1
1
|
// src/index.ts
|
|
2
|
+
import { getAddress, keccak256, concat, toBytes } from "viem";
|
|
3
|
+
function computeCreate2Address(factoryAddress, token0Address, token1Address, initCodeHash) {
|
|
4
|
+
const salt = keccak256(
|
|
5
|
+
concat([
|
|
6
|
+
toBytes(token0Address),
|
|
7
|
+
toBytes(token1Address)
|
|
8
|
+
])
|
|
9
|
+
);
|
|
10
|
+
const create2Input = concat([
|
|
11
|
+
toBytes("0xff"),
|
|
12
|
+
toBytes(factoryAddress),
|
|
13
|
+
toBytes(salt),
|
|
14
|
+
toBytes(initCodeHash)
|
|
15
|
+
]);
|
|
16
|
+
const hash = keccak256(create2Input);
|
|
17
|
+
return getAddress(`0x${hash.slice(-40)}`);
|
|
18
|
+
}
|
|
2
19
|
var ChainId = /* @__PURE__ */ ((ChainId2) => {
|
|
3
20
|
ChainId2[ChainId2["MAINNET"] = 1] = "MAINNET";
|
|
4
21
|
ChainId2[ChainId2["ROPSTEN"] = 3] = "ROPSTEN";
|
|
@@ -298,8 +315,18 @@ var Pair = class _Pair {
|
|
|
298
315
|
}
|
|
299
316
|
static getAddress(tokenA, tokenB) {
|
|
300
317
|
const [token0, token1] = tokenA.sortsBefore(tokenB) ? [tokenA, tokenB] : [tokenB, tokenA];
|
|
301
|
-
const
|
|
302
|
-
|
|
318
|
+
const chainId = token0.chainId;
|
|
319
|
+
const factoryAddress = FACTORY_ADDRESS[chainId];
|
|
320
|
+
const initCodeHash = INIT_CODE_HASH[chainId];
|
|
321
|
+
if (!factoryAddress || !initCodeHash) {
|
|
322
|
+
throw new Error(`Unsupported chain ${chainId}`);
|
|
323
|
+
}
|
|
324
|
+
return computeCreate2Address(
|
|
325
|
+
factoryAddress,
|
|
326
|
+
token0.address,
|
|
327
|
+
token1.address,
|
|
328
|
+
initCodeHash
|
|
329
|
+
);
|
|
303
330
|
}
|
|
304
331
|
get token0Price() {
|
|
305
332
|
return new Price(this.token0, this.token1, this.reserve0.raw, this.reserve1.raw);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@etcswapv2/sdk-legacy",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.1",
|
|
4
4
|
"description": "Legacy V2 SDK for ETCswap - non-generic API compatible with Uniswap V2 interface",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -34,13 +34,8 @@
|
|
|
34
34
|
"engines": {
|
|
35
35
|
"node": ">=18"
|
|
36
36
|
},
|
|
37
|
-
"
|
|
38
|
-
"
|
|
39
|
-
"clean": "rimraf dist",
|
|
40
|
-
"lint": "eslint src --ext .ts",
|
|
41
|
-
"test": "vitest run",
|
|
42
|
-
"typecheck": "tsc --noEmit",
|
|
43
|
-
"prepublishOnly": "pnpm run build"
|
|
37
|
+
"dependencies": {
|
|
38
|
+
"viem": "^2.0.0"
|
|
44
39
|
},
|
|
45
40
|
"devDependencies": {
|
|
46
41
|
"@types/node": "^20.0.0",
|
|
@@ -51,5 +46,12 @@
|
|
|
51
46
|
},
|
|
52
47
|
"publishConfig": {
|
|
53
48
|
"access": "public"
|
|
49
|
+
},
|
|
50
|
+
"scripts": {
|
|
51
|
+
"build": "tsup src/index.ts --format cjs,esm --dts --clean",
|
|
52
|
+
"clean": "rimraf dist",
|
|
53
|
+
"lint": "eslint src --ext .ts",
|
|
54
|
+
"test": "vitest run",
|
|
55
|
+
"typecheck": "tsc --noEmit"
|
|
54
56
|
}
|
|
55
|
-
}
|
|
57
|
+
}
|