/
postCreateOrder.ts
64 lines (53 loc) · 2.23 KB
/
postCreateOrder.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import axios from "axios";
import { LimitOrderDomain, makerAsset } from "../../libs/constants";
import { getSigner } from "../../libs/signer";
import { CreateOrderUnsignedBody, postCreateOrderUnsigned } from "./postCreateOrdersUnsigned";
import { getContracts } from "../getContracts";
import { getTokenApproval } from "../../libs/approval";
import { getMakerActiveAmount } from "./getMakerActiveAmount";
interface CreateOrderSignedBody extends CreateOrderUnsignedBody {
salt: string,
signature: string
};
export async function postCreateOrder() {
const targetPath = `/write/api/v1/orders`;
// Get the ethers signer for signing the creation order
const signer = getSigner();
// Get the request body and the EIP712 order creation data
const unsignedOrderReqBody = (await postCreateOrderUnsigned()).requestBody;
const unsignedOrderReturnData = (await postCreateOrderUnsigned()).returnedData;
// Get the Maker current making amount to ensure contract has sufficient allowance across all orders
const currentMakingAmount = await getMakerActiveAmount();
const newMakingAmount = Number(currentMakingAmount) + Number(unsignedOrderReqBody.makingAmount);
// Get the LO contract address to interact with on-chain
const limitOrderContract = (await getContracts()).latest;
// Check if LO contract has sufficient allowance to spend makerAsset
await getTokenApproval(
makerAsset.address,
limitOrderContract,
newMakingAmount
);
// Sign the EIP712 order creation
const signature = await signer.signTypedData(
unsignedOrderReturnData.domain,
{ Order: unsignedOrderReturnData.types.Order },
unsignedOrderReturnData.message
);
// Structure the request to be sent in POST body
const requestBody: CreateOrderSignedBody = {
...unsignedOrderReqBody,
salt: unsignedOrderReturnData.message.salt,
signature: signature
};
try {
console.log(`\nPosting the create order...`)
const {data} = await axios.post(
LimitOrderDomain+targetPath,
requestBody
);
console.log(`KyberSwap server response:`)
console.log(data);
} catch (error) {
throw(error);
};
};