Skip to content

Commit

Permalink
buy rounding direction fix
Browse files Browse the repository at this point in the history
  • Loading branch information
L-Kov committed Nov 30, 2022
1 parent 1ca193e commit 1aa0a93
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/order-builder/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
CONDITIONAL_TOKEN_DECIMALS,
} from "@polymarket/order-utils";
import { UserOrder, Side, Chain, UserMarketOrder } from "../types";
import { roundDown } from "../utilities";
import { roundDown, roundUp } from "../utilities";

/**
* Generate and sign a order
Expand Down Expand Up @@ -53,7 +53,7 @@ export const buildOrderCreationArgs = async (
// force 2 decimals places
const rawTakerAmt = roundDown(userOrder.size, 4);
const rawPrice = roundDown(userOrder.price, 4);
const rawMakerAmt = roundDown(rawTakerAmt * rawPrice, 6);
const rawMakerAmt = roundUp(rawTakerAmt * rawPrice, 6);

makerAmount = parseUnits(rawMakerAmt.toString(), COLLATERAL_TOKEN_DECIMALS).toString();
takerAmount = parseUnits(rawTakerAmt.toString(), CONDITIONAL_TOKEN_DECIMALS).toString();
Expand Down
39 changes: 39 additions & 0 deletions tests/order-builder/helpers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,45 @@ describe("helpers", () => {
expect(signedOrder.signature).not.empty;
});

it("buy order precision", async () => {
const order: UserOrder = {
tokenID: "123",
price: 0.82,
size: 20.0,
side: Side.BUY,
feeRateBps: 0,
nonce: 123,
expiration: 50000,
taker: "0x0000000000000000000000000000000000000003",
};
const orderData: OrderData = await buildOrderCreationArgs(
"0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266",
"0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266",
SignatureType.EOA,
order,
);
expect(orderData).not.null;
expect(orderData).not.undefined;

const signedOrder = await buildOrder(wallet, contracts.Exchange, chainId, orderData);
expect(signedOrder).not.null;
expect(signedOrder).not.undefined;

expect(signedOrder.salt).not.empty;
expect(signedOrder.maker).equal("0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266");
expect(signedOrder.signer).equal("0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266");
expect(signedOrder.taker).equal("0x0000000000000000000000000000000000000003");
expect(signedOrder.tokenId).equal("123");
expect(signedOrder.makerAmount).equal("16400000");
expect(signedOrder.takerAmount).equal("20000000");
expect(signedOrder.side).equal(UtilsSide.BUY);
expect(signedOrder.expiration).equal("50000");
expect(signedOrder.nonce).equal("123");
expect(signedOrder.feeRateBps).equal("0");
expect(signedOrder.signatureType).equal(SignatureType.EOA);
expect(signedOrder.signature).not.empty;
});

it("sell order", async () => {
const order: UserOrder = {
tokenID: "5",
Expand Down

0 comments on commit 1aa0a93

Please sign in to comment.