Skip to content

Commit 479c551

Browse files
authored
Merge pull request #264 from expand-network/feature_ENC-2673_Limit-Order-Signing
Feature enc 2673 limit order signing
2 parents 8762526 + 1cd845f commit 479c551

File tree

4 files changed

+180
-0
lines changed

4 files changed

+180
-0
lines changed

configuration/config.json

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,60 @@
325325
"dexName": "UniswapX",
326326
"chainName": "Ethereum",
327327
"chainId": "5"
328+
},
329+
"2200": {
330+
"dexName": "Kyberswap",
331+
"localDexName": "Kyberswap",
332+
"chainName": "Ethereum",
333+
"chainId": "1"
334+
},
335+
"2201": {
336+
"dexName": "Kyberswap",
337+
"localDexName": "KyberswapBsc",
338+
"chainName": "Ethereum",
339+
"chainId": "56"
340+
},
341+
"2202": {
342+
"dexName": "Kyberswap",
343+
"localDexName": "KyberswapPolygon",
344+
"chainName": "Ethereum",
345+
"chainId": "137"
346+
},
347+
"2203": {
348+
"dexName": "Kyberswap",
349+
"localDexName": "KyberswapArbitrum",
350+
"chainName": "Ethereum",
351+
"chainId": "42161"
352+
},
353+
"2204": {
354+
"dexName": "Kyberswap",
355+
"localDexName": "KyberswapAvalanche",
356+
"chainName": "Ethereum",
357+
"chainId": "43114"
358+
},
359+
"2205": {
360+
"dexName": "Kyberswap",
361+
"localDexName": "KyberswapOptimism",
362+
"chainName": "Ethereum",
363+
"chainId": "10"
364+
},
365+
"2206": {
366+
"dexName": "Kyberswap",
367+
"localDexName": "KyberswapBase",
368+
"chainName": "Ethereum",
369+
"chainId": "8453"
370+
},
371+
"2207": {
372+
"dexName": "Kyberswap",
373+
"localDexName": "KyberswapFantom",
374+
"chainName": "Ethereum",
375+
"chainId": "250"
376+
},
377+
"2208": {
378+
"dexName": "Kyberswap",
379+
"localDexName": "KyberswapCronos",
380+
"chainName": "Ethereum",
381+
"chainId": "25"
328382
}
329383
},
330384

configuration/schema.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,25 @@ exports.jsonSchema = {
278278
required: ["domain", "types", "values"]
279279
},
280280
},
281+
282+
// Field Mapping for signLimitOrder() function
283+
{
284+
if: {
285+
properties: {
286+
function: { type:"string", pattern: "signLimitOrder()" },
287+
}
288+
},
289+
then: {
290+
properties: {
291+
dexId: { type: "string", enum: ["2200", "2201", "2202", "2203", "2204", "2205", "2206", "2207", "2208"], default: "2200" },
292+
domain: { type: "object" },
293+
types: { type: "object" },
294+
message: { type: "object" },
295+
orderType: { type: "string", enum: ["create", "cancel"], default: "create"},
296+
},
297+
required: ["domain", "types", "message"]
298+
},
299+
},
281300

282301
// Field Mapping for placeOrderDYDX() function
283302
{

src/adapters/Wallet/index.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,42 @@ class Wallet {
151151
return { signature };
152152
};
153153

154+
signLimitOrder = async (options) => {
155+
const filterOptions = options;
156+
filterOptions.function = "signLimitOrder()";
157+
const validJson = await schemaValidator.validateInput(options);
158+
if (!validJson.valid) {
159+
return (validJson);
160+
}
161+
162+
const { dexId, orderType, domain, types, message } = options;
163+
const { chainId } = config.dexes[dexId]
164+
165+
let apiConfig = {
166+
method: 'get',
167+
maxBodyLength: Infinity,
168+
url: `${config.url.apiurl}/chain/getpublicrpc?chainId=${chainId}`,
169+
headers: {
170+
'x-api-key': this.xApiKey
171+
}
172+
};
173+
174+
let rpc = await axios.request(apiConfig);
175+
rpc = rpc.data.data.rpc;
176+
177+
const provider = new ethers.providers.JsonRpcProvider(rpc);
178+
const signer = new ethers.Wallet(this.privateKey, provider);
179+
const signature = orderType === "create" ? await signer._signTypedData(
180+
domain,
181+
{ Order: types.Order },
182+
message
183+
) : await signer._signTypedData(
184+
domain,
185+
{ CancelOrder: types.CancelOrder },
186+
message
187+
);
188+
return { signature, ...(orderType === "create" && {salt: message.salt}) };
189+
};
154190
}
155191

156192
module.exports = { Wallet };

test/signLimitOrder.js

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
const { default: axios } = require("axios");
2+
const { Wallet } = require("../src");
3+
const dotenv = require('dotenv');
4+
5+
dotenv.config();
6+
7+
const wallet = new Wallet({
8+
xApiKey: process.env.xApiKey,
9+
privateKey: process.env.privateKey
10+
});
11+
12+
const signCreateLimitOrder = async () => {
13+
const apiConfig = JSON.stringify({
14+
"dexId": "2200",
15+
"makerAsset": "0x853d955aCEf822Db058eb8505911ED77F175b99e",
16+
"takerAsset": "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48",
17+
"from": "0xB409cB0b5DB9f148d0bc491E140D9E0FDd789C11",
18+
"to": "0xB409cB0b5DB9f148d0bc491E140D9E0FDd789C11",
19+
"amountIn": "2000000000000000000",
20+
"amountOut": "2000000",
21+
"deadline": "1738898848"
22+
});
23+
24+
let config = {
25+
method: 'post',
26+
maxBodyLength: Infinity,
27+
url: 'https://api.expand.network/dex/createorder',
28+
headers: {
29+
'accept': 'application/json',
30+
'Content-Type': 'application/json',
31+
'x-api-key': process.env.xApiKey
32+
},
33+
data: apiConfig
34+
};
35+
36+
const res = await axios.request(config);
37+
const { domain, types, message } = res.data.data.unsignedCreateOrder;
38+
39+
const orderType = "create";
40+
const sig = await wallet.signLimitOrder({ domain, types, message, orderType });
41+
console.log(sig);
42+
}
43+
44+
const signCancelLimitOrder = async () => {
45+
const apiConfig = JSON.stringify({
46+
"dexId": "2200",
47+
"orderId": "<orderId>",
48+
"from": "0xB409cB0b5DB9f148d0bc491E140D9E0FDd789C11"
49+
});
50+
51+
let config = {
52+
method: 'post',
53+
maxBodyLength: Infinity,
54+
url: 'https://api.expand.network/dex/cancelorder',
55+
headers: {
56+
'accept': 'application/json',
57+
'Content-Type': 'application/json',
58+
'x-api-key': process.env.xApiKey
59+
},
60+
data: apiConfig
61+
};
62+
63+
const res = await axios.request(config);
64+
const { domain, types, message } = res.data.data.unsignedCancelOrder;
65+
const orderType = "cancel";
66+
const sig = await wallet.signLimitOrder({ domain, types, message, orderType });
67+
console.log(sig);
68+
}
69+
70+
// signCreateLimitOrder();
71+
// signCancelLimitOrder();

0 commit comments

Comments
 (0)