From b900c752a630368e3948f3549ec86c2ac2e9018c Mon Sep 17 00:00:00 2001 From: mike Date: Wed, 29 Nov 2023 12:43:45 -0800 Subject: [PATCH] Remove Hard-Coded Addresses (#13) * remove networks * remove networks, update tests * adding neg risk test cases --------- Co-authored-by: Rodrigo <95635797+poly-rodr@users.noreply.github.com> --- package.json | 6 +- src/index.ts | 2 - src/networks.ts | 31 - tests/src/exchange.order.builder.test.ts | 1212 +++++++++++++++------- tests/src/networks.test.ts | 48 - 5 files changed, 838 insertions(+), 461 deletions(-) delete mode 100644 src/networks.ts delete mode 100644 tests/src/networks.test.ts diff --git a/package.json b/package.json index f2ee985..d808f91 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@polymarket/order-utils", - "version": "1.3.1", + "version": "2.0.0", "description": "Typescript utility for creating orders for Polymarket's CLOB", "author": "Liam Kovatch ", "homepage": "https://github.com/Polymarket/clob-order-utils", @@ -27,6 +27,10 @@ { "name": "Rodrigo Calvo", "url": "https://github.com/poly-rodr" + }, + { + "name": "Mike Shrieve", + "url": "https://github.com/mshrieve" } ], "scripts": { diff --git a/src/index.ts b/src/index.ts index 1b1b0a0..2d1bd72 100644 --- a/src/index.ts +++ b/src/index.ts @@ -6,5 +6,3 @@ export * from './model/eip712.model'; export * from './model/order.model'; export * from './model/order-side.model'; export * from './model/signature-types.model'; - -export * from './networks'; diff --git a/src/networks.ts b/src/networks.ts deleted file mode 100644 index 2d89c6d..0000000 --- a/src/networks.ts +++ /dev/null @@ -1,31 +0,0 @@ -export interface Contracts { - Exchange: string; - Collateral: string; - Conditional: string; -} - -const MUMBAI_CONTRACTS: Contracts = { - Exchange: '0x4bFb41d5B3570DeFd03C39a9A4D8dE6Bd8B8982E', - Collateral: '0x2E8DCfE708D44ae2e406a1c02DFE2Fa13012f961', - Conditional: '0x7D8610E9567d2a6C9FBf66a5A13E9Ba8bb120d43', -}; - -const MATIC_CONTRACTS: Contracts = { - Exchange: '0x4bFb41d5B3570DeFd03C39a9A4D8dE6Bd8B8982E', - Collateral: '0x2791Bca1f2de4661ED88A30C99A7a9449Aa84174', - Conditional: '0x4D97DCd97eC945f40cF65F87097ACe5EA0476045', -}; - -export const COLLATERAL_TOKEN_DECIMALS = 6; -export const CONDITIONAL_TOKEN_DECIMALS = 6; - -export const getContracts = (chainID: number): Contracts => { - switch (chainID) { - case 137: - return MATIC_CONTRACTS; - case 80001: - return MUMBAI_CONTRACTS; - default: - throw new Error('Invalid network'); - } -}; diff --git a/tests/src/exchange.order.builder.test.ts b/tests/src/exchange.order.builder.test.ts index c300f29..b4e8ffe 100644 --- a/tests/src/exchange.order.builder.test.ts +++ b/tests/src/exchange.order.builder.test.ts @@ -1,154 +1,88 @@ import { expect } from 'chai'; import { Wallet } from '@ethersproject/wallet'; -import { getContracts } from '../../src/networks'; import { ExchangeOrderBuilder } from '../../src/exchange.order.builder'; import { generateOrderSalt } from '../../src/utils'; import { Order, OrderData } from '../../src/model/order.model'; import { Side } from '../../src/model/order-side.model'; -describe('exchange order builder', () => { - let wallet: Wallet; - let exchangeOrderBuilder: ExchangeOrderBuilder; - - beforeEach(async () => { - const chainId = 80001; - const contracts = getContracts(chainId); - - // publicly known private key - const privateKey = - '0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80'; - wallet = new Wallet(privateKey); - - exchangeOrderBuilder = new ExchangeOrderBuilder( - contracts.Exchange, - chainId, - wallet, - generateOrderSalt - ); - }); +describe('order builder', () => { + describe('CTFexchange', () => { + let wallet: Wallet; + let exchangeOrderBuilder: ExchangeOrderBuilder; + + beforeEach(async () => { + const chainId = 80001; + + // exchange address + const exchangeAddress = + '0x4bFb41d5B3570DeFd03C39a9A4D8dE6Bd8B8982E'; + + // publicly known private key + const privateKey = + '0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80'; + wallet = new Wallet(privateKey); - describe('buildOrder', () => { - it('random salt', async () => { - const order = await exchangeOrderBuilder.buildOrder({ - maker: wallet.address, - taker: '0x0000000000000000000000000000000000000000', - tokenId: '1234', - makerAmount: '100000000', - takerAmount: '50000000', - side: Side.BUY, - feeRateBps: '100', - nonce: '0', - } as OrderData); - - expect(order).not.null; - expect(order).not.undefined; - - expect(order.salt).not.empty; - expect(order.maker).equal(wallet.address); - expect(order.signer).equal(wallet.address); - expect(order.taker).equal( - '0x0000000000000000000000000000000000000000' + exchangeOrderBuilder = new ExchangeOrderBuilder( + exchangeAddress, + chainId, + wallet, + generateOrderSalt ); - expect(order.tokenId).equal('1234'); - expect(order.makerAmount).equal('100000000'); - expect(order.takerAmount).equal('50000000'); - expect(order.side).equal(0); - expect(order.expiration).equal('0'); - expect(order.nonce).equal('0'); - expect(order.feeRateBps).equal('100'); - expect(order.signatureType).equal(0); }); - it('specific salt', async () => { - (exchangeOrderBuilder as any)['generateSalt'] = () => { - return '479249096354'; - }; - - const order = await exchangeOrderBuilder.buildOrder({ - maker: wallet.address, - taker: '0x0000000000000000000000000000000000000000', - tokenId: '1234', - makerAmount: '100000000', - takerAmount: '50000000', - side: Side.BUY, - feeRateBps: '100', - nonce: '0', - } as OrderData); - - expect(order).not.null; - expect(order).not.undefined; - - expect(order).deep.equal({ - salt: '479249096354', - maker: wallet.address, - signer: wallet.address, - taker: '0x0000000000000000000000000000000000000000', - tokenId: '1234', - makerAmount: '100000000', - takerAmount: '50000000', - expiration: '0', - nonce: '0', - feeRateBps: '100', - side: 0, - signatureType: 0, - } as Order); - }); - }); + describe('buildOrder', () => { + it('random salt', async () => { + const order = await exchangeOrderBuilder.buildOrder({ + maker: wallet.address, + taker: '0x0000000000000000000000000000000000000000', + tokenId: '1234', + makerAmount: '100000000', + takerAmount: '50000000', + side: Side.BUY, + feeRateBps: '100', + nonce: '0', + } as OrderData); + + expect(order).not.null; + expect(order).not.undefined; + + expect(order.salt).not.empty; + expect(order.maker).equal(wallet.address); + expect(order.signer).equal(wallet.address); + expect(order.taker).equal( + '0x0000000000000000000000000000000000000000' + ); + expect(order.tokenId).equal('1234'); + expect(order.makerAmount).equal('100000000'); + expect(order.takerAmount).equal('50000000'); + expect(order.side).equal(0); + expect(order.expiration).equal('0'); + expect(order.nonce).equal('0'); + expect(order.feeRateBps).equal('100'); + expect(order.signatureType).equal(0); + }); - describe('buildLimitOrderTypedData', async () => { - it('random salt', async () => { - const order = await exchangeOrderBuilder.buildOrder({ - maker: wallet.address, - taker: '0x0000000000000000000000000000000000000000', - tokenId: '1234', - makerAmount: '100000000', - takerAmount: '50000000', - side: Side.BUY, - feeRateBps: '100', - nonce: '0', - } as OrderData); - - expect(order).not.null; - expect(order).not.undefined; - - const orderTypedData = - exchangeOrderBuilder.buildOrderTypedData(order); - expect(orderTypedData).not.null; - expect(orderTypedData).not.undefined; - - expect(orderTypedData).deep.equal({ - primaryType: 'Order', - types: { - EIP712Domain: [ - { name: 'name', type: 'string' }, - { name: 'version', type: 'string' }, - { name: 'chainId', type: 'uint256' }, - { name: 'verifyingContract', type: 'address' }, - ], - Order: [ - { name: 'salt', type: 'uint256' }, - { name: 'maker', type: 'address' }, - { name: 'signer', type: 'address' }, - { name: 'taker', type: 'address' }, - { name: 'tokenId', type: 'uint256' }, - { name: 'makerAmount', type: 'uint256' }, - { name: 'takerAmount', type: 'uint256' }, - { name: 'expiration', type: 'uint256' }, - { name: 'nonce', type: 'uint256' }, - { name: 'feeRateBps', type: 'uint256' }, - { name: 'side', type: 'uint8' }, - { name: 'signatureType', type: 'uint8' }, - ], - }, - domain: { - name: 'Polymarket CTF Exchange', - version: '1', - chainId: 80001, - verifyingContract: - '0x4bFb41d5B3570DeFd03C39a9A4D8dE6Bd8B8982E', - }, - message: { - salt: orderTypedData.message.salt, + it('specific salt', async () => { + (exchangeOrderBuilder as any)['generateSalt'] = () => { + return '479249096354'; + }; + + const order = await exchangeOrderBuilder.buildOrder({ + maker: wallet.address, + taker: '0x0000000000000000000000000000000000000000', + tokenId: '1234', + makerAmount: '100000000', + takerAmount: '50000000', + side: Side.BUY, + feeRateBps: '100', + nonce: '0', + } as OrderData); + + expect(order).not.null; + expect(order).not.undefined; + + expect(order).deep.equal({ + salt: '479249096354', maker: wallet.address, signer: wallet.address, taker: '0x0000000000000000000000000000000000000000', @@ -160,66 +94,338 @@ describe('exchange order builder', () => { feeRateBps: '100', side: 0, signatureType: 0, - }, + } as Order); + }); + }); + + describe('buildLimitOrderTypedData', async () => { + it('random salt', async () => { + const order = await exchangeOrderBuilder.buildOrder({ + maker: wallet.address, + taker: '0x0000000000000000000000000000000000000000', + tokenId: '1234', + makerAmount: '100000000', + takerAmount: '50000000', + side: Side.BUY, + feeRateBps: '100', + nonce: '0', + } as OrderData); + + expect(order).not.null; + expect(order).not.undefined; + + const orderTypedData = + exchangeOrderBuilder.buildOrderTypedData(order); + expect(orderTypedData).not.null; + expect(orderTypedData).not.undefined; + + expect(orderTypedData).deep.equal({ + primaryType: 'Order', + types: { + EIP712Domain: [ + { name: 'name', type: 'string' }, + { name: 'version', type: 'string' }, + { name: 'chainId', type: 'uint256' }, + { name: 'verifyingContract', type: 'address' }, + ], + Order: [ + { name: 'salt', type: 'uint256' }, + { name: 'maker', type: 'address' }, + { name: 'signer', type: 'address' }, + { name: 'taker', type: 'address' }, + { name: 'tokenId', type: 'uint256' }, + { name: 'makerAmount', type: 'uint256' }, + { name: 'takerAmount', type: 'uint256' }, + { name: 'expiration', type: 'uint256' }, + { name: 'nonce', type: 'uint256' }, + { name: 'feeRateBps', type: 'uint256' }, + { name: 'side', type: 'uint8' }, + { name: 'signatureType', type: 'uint8' }, + ], + }, + domain: { + name: 'Polymarket CTF Exchange', + version: '1', + chainId: 80001, + verifyingContract: + '0x4bFb41d5B3570DeFd03C39a9A4D8dE6Bd8B8982E', + }, + message: { + salt: orderTypedData.message.salt, + maker: wallet.address, + signer: wallet.address, + taker: '0x0000000000000000000000000000000000000000', + tokenId: '1234', + makerAmount: '100000000', + takerAmount: '50000000', + expiration: '0', + nonce: '0', + feeRateBps: '100', + side: 0, + signatureType: 0, + }, + }); + }); + + it('specific salt', async () => { + (exchangeOrderBuilder as any)['generateSalt'] = () => { + return '479249096354'; + }; + + const order = await exchangeOrderBuilder.buildOrder({ + maker: wallet.address, + taker: '0x0000000000000000000000000000000000000000', + tokenId: '1234', + makerAmount: '100000000', + takerAmount: '50000000', + side: Side.BUY, + feeRateBps: '100', + nonce: '0', + } as OrderData); + + expect(order).not.null; + expect(order).not.undefined; + + const orderTypedData = + exchangeOrderBuilder.buildOrderTypedData(order); + expect(orderTypedData).not.null; + expect(orderTypedData).not.undefined; + + expect(orderTypedData).deep.equal({ + primaryType: 'Order', + types: { + EIP712Domain: [ + { name: 'name', type: 'string' }, + { name: 'version', type: 'string' }, + { name: 'chainId', type: 'uint256' }, + { name: 'verifyingContract', type: 'address' }, + ], + Order: [ + { name: 'salt', type: 'uint256' }, + { name: 'maker', type: 'address' }, + { name: 'signer', type: 'address' }, + { name: 'taker', type: 'address' }, + { name: 'tokenId', type: 'uint256' }, + { name: 'makerAmount', type: 'uint256' }, + { name: 'takerAmount', type: 'uint256' }, + { name: 'expiration', type: 'uint256' }, + { name: 'nonce', type: 'uint256' }, + { name: 'feeRateBps', type: 'uint256' }, + { name: 'side', type: 'uint8' }, + { name: 'signatureType', type: 'uint8' }, + ], + }, + domain: { + name: 'Polymarket CTF Exchange', + version: '1', + chainId: 80001, + verifyingContract: + '0x4bFb41d5B3570DeFd03C39a9A4D8dE6Bd8B8982E', + }, + message: { + salt: '479249096354', + maker: wallet.address, + signer: wallet.address, + taker: '0x0000000000000000000000000000000000000000', + tokenId: '1234', + makerAmount: '100000000', + takerAmount: '50000000', + expiration: '0', + nonce: '0', + feeRateBps: '100', + side: 0, + signatureType: 0, + }, + }); }); }); - it('specific salt', async () => { - (exchangeOrderBuilder as any)['generateSalt'] = () => { - return '479249096354'; - }; - - const order = await exchangeOrderBuilder.buildOrder({ - maker: wallet.address, - taker: '0x0000000000000000000000000000000000000000', - tokenId: '1234', - makerAmount: '100000000', - takerAmount: '50000000', - side: Side.BUY, - feeRateBps: '100', - nonce: '0', - } as OrderData); - - expect(order).not.null; - expect(order).not.undefined; - - const orderTypedData = - exchangeOrderBuilder.buildOrderTypedData(order); - expect(orderTypedData).not.null; - expect(orderTypedData).not.undefined; - - expect(orderTypedData).deep.equal({ - primaryType: 'Order', - types: { - EIP712Domain: [ - { name: 'name', type: 'string' }, - { name: 'version', type: 'string' }, - { name: 'chainId', type: 'uint256' }, - { name: 'verifyingContract', type: 'address' }, - ], - Order: [ - { name: 'salt', type: 'uint256' }, - { name: 'maker', type: 'address' }, - { name: 'signer', type: 'address' }, - { name: 'taker', type: 'address' }, - { name: 'tokenId', type: 'uint256' }, - { name: 'makerAmount', type: 'uint256' }, - { name: 'takerAmount', type: 'uint256' }, - { name: 'expiration', type: 'uint256' }, - { name: 'nonce', type: 'uint256' }, - { name: 'feeRateBps', type: 'uint256' }, - { name: 'side', type: 'uint8' }, - { name: 'signatureType', type: 'uint8' }, - ], - }, - domain: { - name: 'Polymarket CTF Exchange', - version: '1', - chainId: 80001, - verifyingContract: - '0x4bFb41d5B3570DeFd03C39a9A4D8dE6Bd8B8982E', - }, - message: { + describe('buildOrderSignature', async () => { + it('random salt', async () => { + const order = await exchangeOrderBuilder.buildOrder({ + maker: wallet.address, + taker: '0x0000000000000000000000000000000000000000', + tokenId: '1234', + makerAmount: '100000000', + takerAmount: '50000000', + side: Side.BUY, + feeRateBps: '100', + nonce: '0', + } as OrderData); + + expect(order).not.null; + expect(order).not.undefined; + + const orderTypedData = + exchangeOrderBuilder.buildOrderTypedData(order); + expect(orderTypedData).not.null; + expect(orderTypedData).not.undefined; + + const orderSignature = + await exchangeOrderBuilder.buildOrderSignature( + orderTypedData + ); + expect(orderSignature).not.null; + expect(orderSignature).not.undefined; + expect(orderSignature).not.empty; + }); + + it('specific salt', async () => { + (exchangeOrderBuilder as any)['generateSalt'] = () => { + return '479249096354'; + }; + + const order = await exchangeOrderBuilder.buildOrder({ + maker: wallet.address, + taker: '0x0000000000000000000000000000000000000000', + tokenId: '1234', + makerAmount: '100000000', + takerAmount: '50000000', + side: Side.BUY, + feeRateBps: '100', + nonce: '0', + } as OrderData); + + expect(order).not.null; + expect(order).not.undefined; + + const orderTypedData = + exchangeOrderBuilder.buildOrderTypedData(order); + expect(orderTypedData).not.null; + expect(orderTypedData).not.undefined; + + const orderSignature = + await exchangeOrderBuilder.buildOrderSignature( + orderTypedData + ); + expect(orderSignature).not.null; + expect(orderSignature).not.undefined; + expect(orderSignature).not.empty; + + expect(orderSignature).deep.equal( + // eslint-disable-next-line max-len + '0x3874d2cfe79c0e82577f4f76ec58d950522ee5923a6f08441927d382c8178a5a2190fd4d5c49705e94d75999a58ec843f94a432e87ebc15cdb2186d315b3cc201b' + ); + }); + }); + + describe('buildOrderHash', async () => { + it('random salt', async () => { + const order = await exchangeOrderBuilder.buildOrder({ + maker: wallet.address, + taker: '0x0000000000000000000000000000000000000000', + tokenId: '1234', + makerAmount: '100000000', + takerAmount: '50000000', + side: Side.BUY, + feeRateBps: '100', + nonce: '0', + } as OrderData); + + expect(order).not.null; + expect(order).not.undefined; + + const orderTypedData = + exchangeOrderBuilder.buildOrderTypedData(order); + expect(orderTypedData).not.null; + expect(orderTypedData).not.undefined; + + const orderHash = + exchangeOrderBuilder.buildOrderHash(orderTypedData); + expect(orderHash).not.null; + expect(orderHash).not.undefined; + }); + + it('specific salt', async () => { + (exchangeOrderBuilder as any)['generateSalt'] = () => { + return '479249096354'; + }; + + const order = await exchangeOrderBuilder.buildOrder({ + maker: wallet.address, + taker: '0x0000000000000000000000000000000000000000', + tokenId: '1234', + makerAmount: '100000000', + takerAmount: '50000000', + side: Side.BUY, + feeRateBps: '100', + nonce: '0', + } as OrderData); + + expect(order).not.null; + expect(order).not.undefined; + + const orderTypedData = + exchangeOrderBuilder.buildOrderTypedData(order); + expect(orderTypedData).not.null; + expect(orderTypedData).not.undefined; + + const orderHash = + exchangeOrderBuilder.buildOrderHash(orderTypedData); + expect(orderHash).not.null; + expect(orderHash).not.undefined; + + expect(orderHash).deep.equal( + '0xbf58957703791db2ab057528d03d1cff5375d9a475b14a9073bb7d892398dc96' + ); + }); + }); + + describe('buildSignedOrder', async () => { + it('random salt', async () => { + const signedOrder = await exchangeOrderBuilder.buildSignedOrder( + { + maker: wallet.address, + taker: '0x0000000000000000000000000000000000000000', + tokenId: '1234', + makerAmount: '100000000', + takerAmount: '50000000', + side: Side.BUY, + feeRateBps: '100', + nonce: '0', + } as OrderData + ); + expect(signedOrder).not.null; + expect(signedOrder).not.undefined; + + expect(signedOrder.salt).not.empty; + expect(signedOrder.maker).equal(wallet.address); + expect(signedOrder.signer).equal(wallet.address); + expect(signedOrder.taker).equal( + '0x0000000000000000000000000000000000000000' + ); + expect(signedOrder.tokenId).equal('1234'); + expect(signedOrder.makerAmount).equal('100000000'); + expect(signedOrder.takerAmount).equal('50000000'); + expect(signedOrder.side).equal(0); + expect(signedOrder.expiration).equal('0'); + expect(signedOrder.nonce).equal('0'); + expect(signedOrder.feeRateBps).equal('100'); + expect(signedOrder.signatureType).equal(0); + expect(signedOrder.signature).not.empty; + }); + + it('specific salt', async () => { + (exchangeOrderBuilder as any)['generateSalt'] = () => { + return '479249096354'; + }; + + const signedOrder = await exchangeOrderBuilder.buildSignedOrder( + { + maker: wallet.address, + taker: '0x0000000000000000000000000000000000000000', + tokenId: '1234', + makerAmount: '100000000', + takerAmount: '50000000', + side: Side.BUY, + feeRateBps: '100', + nonce: '0', + } as OrderData + ); + expect(signedOrder).not.null; + expect(signedOrder).not.undefined; + + expect(signedOrder).deep.equal({ salt: '479249096354', maker: wallet.address, signer: wallet.address, @@ -227,207 +433,455 @@ describe('exchange order builder', () => { tokenId: '1234', makerAmount: '100000000', takerAmount: '50000000', + side: 0, expiration: '0', nonce: '0', feeRateBps: '100', - side: 0, signatureType: 0, - }, + signature: + // eslint-disable-next-line max-len + '0x3874d2cfe79c0e82577f4f76ec58d950522ee5923a6f08441927d382c8178a5a2190fd4d5c49705e94d75999a58ec843f94a432e87ebc15cdb2186d315b3cc201b', + }); }); }); }); - describe('buildOrderSignature', async () => { - it('random salt', async () => { - const order = await exchangeOrderBuilder.buildOrder({ - maker: wallet.address, - taker: '0x0000000000000000000000000000000000000000', - tokenId: '1234', - makerAmount: '100000000', - takerAmount: '50000000', - side: Side.BUY, - feeRateBps: '100', - nonce: '0', - } as OrderData); - - expect(order).not.null; - expect(order).not.undefined; - - const orderTypedData = - exchangeOrderBuilder.buildOrderTypedData(order); - expect(orderTypedData).not.null; - expect(orderTypedData).not.undefined; - - const orderSignature = - await exchangeOrderBuilder.buildOrderSignature(orderTypedData); - expect(orderSignature).not.null; - expect(orderSignature).not.undefined; - expect(orderSignature).not.empty; - }); + describe('Neg Risk CTFExchange', () => { + let wallet: Wallet; + let exchangeOrderBuilder: ExchangeOrderBuilder; + + beforeEach(async () => { + const chainId = 80001; - it('specific salt', async () => { - (exchangeOrderBuilder as any)['generateSalt'] = () => { - return '479249096354'; - }; - - const order = await exchangeOrderBuilder.buildOrder({ - maker: wallet.address, - taker: '0x0000000000000000000000000000000000000000', - tokenId: '1234', - makerAmount: '100000000', - takerAmount: '50000000', - side: Side.BUY, - feeRateBps: '100', - nonce: '0', - } as OrderData); - - expect(order).not.null; - expect(order).not.undefined; - - const orderTypedData = - exchangeOrderBuilder.buildOrderTypedData(order); - expect(orderTypedData).not.null; - expect(orderTypedData).not.undefined; - - const orderSignature = - await exchangeOrderBuilder.buildOrderSignature(orderTypedData); - expect(orderSignature).not.null; - expect(orderSignature).not.undefined; - expect(orderSignature).not.empty; - - expect(orderSignature).deep.equal( - '0x3874d2cfe79c0e82577f4f76ec58d950522ee5923a6f08441927d382c8178a5a2190fd4d5c49705e94d75999a58ec843f94a432e87ebc15cdb2186d315b3cc201b' + // exchange address + const negRiskExchangeAddress = + '0xC5d563A36AE78145C45a50134d48A1215220f80a'; + + // publicly known private key + const privateKey = + '0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80'; + wallet = new Wallet(privateKey); + + exchangeOrderBuilder = new ExchangeOrderBuilder( + negRiskExchangeAddress, + chainId, + wallet, + generateOrderSalt ); }); - }); - describe('buildOrderHash', async () => { - it('random salt', async () => { - const order = await exchangeOrderBuilder.buildOrder({ - maker: wallet.address, - taker: '0x0000000000000000000000000000000000000000', - tokenId: '1234', - makerAmount: '100000000', - takerAmount: '50000000', - side: Side.BUY, - feeRateBps: '100', - nonce: '0', - } as OrderData); - - expect(order).not.null; - expect(order).not.undefined; - - const orderTypedData = - exchangeOrderBuilder.buildOrderTypedData(order); - expect(orderTypedData).not.null; - expect(orderTypedData).not.undefined; - - const orderHash = - exchangeOrderBuilder.buildOrderHash(orderTypedData); - expect(orderHash).not.null; - expect(orderHash).not.undefined; + describe('buildOrder', () => { + it('random salt', async () => { + const order = await exchangeOrderBuilder.buildOrder({ + maker: wallet.address, + taker: '0x0000000000000000000000000000000000000000', + tokenId: '1234', + makerAmount: '100000000', + takerAmount: '50000000', + side: Side.BUY, + feeRateBps: '100', + nonce: '0', + } as OrderData); + + expect(order).not.null; + expect(order).not.undefined; + + expect(order.salt).not.empty; + expect(order.maker).equal(wallet.address); + expect(order.signer).equal(wallet.address); + expect(order.taker).equal( + '0x0000000000000000000000000000000000000000' + ); + expect(order.tokenId).equal('1234'); + expect(order.makerAmount).equal('100000000'); + expect(order.takerAmount).equal('50000000'); + expect(order.side).equal(0); + expect(order.expiration).equal('0'); + expect(order.nonce).equal('0'); + expect(order.feeRateBps).equal('100'); + expect(order.signatureType).equal(0); + }); + + it('specific salt', async () => { + (exchangeOrderBuilder as any)['generateSalt'] = () => { + return '479249096354'; + }; + + const order = await exchangeOrderBuilder.buildOrder({ + maker: wallet.address, + taker: '0x0000000000000000000000000000000000000000', + tokenId: '1234', + makerAmount: '100000000', + takerAmount: '50000000', + side: Side.BUY, + feeRateBps: '100', + nonce: '0', + } as OrderData); + + expect(order).not.null; + expect(order).not.undefined; + + expect(order).deep.equal({ + salt: '479249096354', + maker: wallet.address, + signer: wallet.address, + taker: '0x0000000000000000000000000000000000000000', + tokenId: '1234', + makerAmount: '100000000', + takerAmount: '50000000', + expiration: '0', + nonce: '0', + feeRateBps: '100', + side: 0, + signatureType: 0, + } as Order); + }); }); - it('specific salt', async () => { - (exchangeOrderBuilder as any)['generateSalt'] = () => { - return '479249096354'; - }; - - const order = await exchangeOrderBuilder.buildOrder({ - maker: wallet.address, - taker: '0x0000000000000000000000000000000000000000', - tokenId: '1234', - makerAmount: '100000000', - takerAmount: '50000000', - side: Side.BUY, - feeRateBps: '100', - nonce: '0', - } as OrderData); - - expect(order).not.null; - expect(order).not.undefined; - - const orderTypedData = - exchangeOrderBuilder.buildOrderTypedData(order); - expect(orderTypedData).not.null; - expect(orderTypedData).not.undefined; - - const orderHash = - exchangeOrderBuilder.buildOrderHash(orderTypedData); - expect(orderHash).not.null; - expect(orderHash).not.undefined; - - expect(orderHash).deep.equal( - '0xbf58957703791db2ab057528d03d1cff5375d9a475b14a9073bb7d892398dc96' - ); + describe('buildLimitOrderTypedData', async () => { + it('random salt', async () => { + const order = await exchangeOrderBuilder.buildOrder({ + maker: wallet.address, + taker: '0x0000000000000000000000000000000000000000', + tokenId: '1234', + makerAmount: '100000000', + takerAmount: '50000000', + side: Side.BUY, + feeRateBps: '100', + nonce: '0', + } as OrderData); + + expect(order).not.null; + expect(order).not.undefined; + + const orderTypedData = + exchangeOrderBuilder.buildOrderTypedData(order); + expect(orderTypedData).not.null; + expect(orderTypedData).not.undefined; + + expect(orderTypedData).deep.equal({ + primaryType: 'Order', + types: { + EIP712Domain: [ + { name: 'name', type: 'string' }, + { name: 'version', type: 'string' }, + { name: 'chainId', type: 'uint256' }, + { name: 'verifyingContract', type: 'address' }, + ], + Order: [ + { name: 'salt', type: 'uint256' }, + { name: 'maker', type: 'address' }, + { name: 'signer', type: 'address' }, + { name: 'taker', type: 'address' }, + { name: 'tokenId', type: 'uint256' }, + { name: 'makerAmount', type: 'uint256' }, + { name: 'takerAmount', type: 'uint256' }, + { name: 'expiration', type: 'uint256' }, + { name: 'nonce', type: 'uint256' }, + { name: 'feeRateBps', type: 'uint256' }, + { name: 'side', type: 'uint8' }, + { name: 'signatureType', type: 'uint8' }, + ], + }, + domain: { + name: 'Polymarket CTF Exchange', + version: '1', + chainId: 80001, + verifyingContract: + '0xC5d563A36AE78145C45a50134d48A1215220f80a', + }, + message: { + salt: orderTypedData.message.salt, + maker: wallet.address, + signer: wallet.address, + taker: '0x0000000000000000000000000000000000000000', + tokenId: '1234', + makerAmount: '100000000', + takerAmount: '50000000', + expiration: '0', + nonce: '0', + feeRateBps: '100', + side: 0, + signatureType: 0, + }, + }); + }); + + it('specific salt', async () => { + (exchangeOrderBuilder as any)['generateSalt'] = () => { + return '479249096354'; + }; + + const order = await exchangeOrderBuilder.buildOrder({ + maker: wallet.address, + taker: '0x0000000000000000000000000000000000000000', + tokenId: '1234', + makerAmount: '100000000', + takerAmount: '50000000', + side: Side.BUY, + feeRateBps: '100', + nonce: '0', + } as OrderData); + + expect(order).not.null; + expect(order).not.undefined; + + const orderTypedData = + exchangeOrderBuilder.buildOrderTypedData(order); + expect(orderTypedData).not.null; + expect(orderTypedData).not.undefined; + + expect(orderTypedData).deep.equal({ + primaryType: 'Order', + types: { + EIP712Domain: [ + { name: 'name', type: 'string' }, + { name: 'version', type: 'string' }, + { name: 'chainId', type: 'uint256' }, + { name: 'verifyingContract', type: 'address' }, + ], + Order: [ + { name: 'salt', type: 'uint256' }, + { name: 'maker', type: 'address' }, + { name: 'signer', type: 'address' }, + { name: 'taker', type: 'address' }, + { name: 'tokenId', type: 'uint256' }, + { name: 'makerAmount', type: 'uint256' }, + { name: 'takerAmount', type: 'uint256' }, + { name: 'expiration', type: 'uint256' }, + { name: 'nonce', type: 'uint256' }, + { name: 'feeRateBps', type: 'uint256' }, + { name: 'side', type: 'uint8' }, + { name: 'signatureType', type: 'uint8' }, + ], + }, + domain: { + name: 'Polymarket CTF Exchange', + version: '1', + chainId: 80001, + verifyingContract: + '0xC5d563A36AE78145C45a50134d48A1215220f80a', + }, + message: { + salt: '479249096354', + maker: wallet.address, + signer: wallet.address, + taker: '0x0000000000000000000000000000000000000000', + tokenId: '1234', + makerAmount: '100000000', + takerAmount: '50000000', + expiration: '0', + nonce: '0', + feeRateBps: '100', + side: 0, + signatureType: 0, + }, + }); + }); }); - }); - describe('buildSignedOrder', async () => { - it('random salt', async () => { - const signedOrder = await exchangeOrderBuilder.buildSignedOrder({ - maker: wallet.address, - taker: '0x0000000000000000000000000000000000000000', - tokenId: '1234', - makerAmount: '100000000', - takerAmount: '50000000', - side: Side.BUY, - feeRateBps: '100', - nonce: '0', - } as OrderData); - expect(signedOrder).not.null; - expect(signedOrder).not.undefined; - - expect(signedOrder.salt).not.empty; - expect(signedOrder.maker).equal(wallet.address); - expect(signedOrder.signer).equal(wallet.address); - expect(signedOrder.taker).equal( - '0x0000000000000000000000000000000000000000' - ); - expect(signedOrder.tokenId).equal('1234'); - expect(signedOrder.makerAmount).equal('100000000'); - expect(signedOrder.takerAmount).equal('50000000'); - expect(signedOrder.side).equal(0); - expect(signedOrder.expiration).equal('0'); - expect(signedOrder.nonce).equal('0'); - expect(signedOrder.feeRateBps).equal('100'); - expect(signedOrder.signatureType).equal(0); - expect(signedOrder.signature).not.empty; + describe('buildOrderSignature', async () => { + it('random salt', async () => { + const order = await exchangeOrderBuilder.buildOrder({ + maker: wallet.address, + taker: '0x0000000000000000000000000000000000000000', + tokenId: '1234', + makerAmount: '100000000', + takerAmount: '50000000', + side: Side.BUY, + feeRateBps: '100', + nonce: '0', + } as OrderData); + + expect(order).not.null; + expect(order).not.undefined; + + const orderTypedData = + exchangeOrderBuilder.buildOrderTypedData(order); + expect(orderTypedData).not.null; + expect(orderTypedData).not.undefined; + + const orderSignature = + await exchangeOrderBuilder.buildOrderSignature( + orderTypedData + ); + expect(orderSignature).not.null; + expect(orderSignature).not.undefined; + expect(orderSignature).not.empty; + }); + + it('specific salt', async () => { + (exchangeOrderBuilder as any)['generateSalt'] = () => { + return '479249096354'; + }; + + const order = await exchangeOrderBuilder.buildOrder({ + maker: wallet.address, + taker: '0x0000000000000000000000000000000000000000', + tokenId: '1234', + makerAmount: '100000000', + takerAmount: '50000000', + side: Side.BUY, + feeRateBps: '100', + nonce: '0', + } as OrderData); + + expect(order).not.null; + expect(order).not.undefined; + + const orderTypedData = + exchangeOrderBuilder.buildOrderTypedData(order); + expect(orderTypedData).not.null; + expect(orderTypedData).not.undefined; + + const orderSignature = + await exchangeOrderBuilder.buildOrderSignature( + orderTypedData + ); + expect(orderSignature).not.null; + expect(orderSignature).not.undefined; + expect(orderSignature).not.empty; + + expect(orderSignature).deep.equal( + // eslint-disable-next-line max-len + '0x92bb74d70a3c82cff3a9ccf10c9c547bb4ec71cff8cbb6cb308ca975162df7ba65837d5b57f50568fbd6bba9f2b919089268ace609cc33935b0bb5483e1092c31c' + ); + }); }); - it('specific salt', async () => { - (exchangeOrderBuilder as any)['generateSalt'] = () => { - return '479249096354'; - }; - - const signedOrder = await exchangeOrderBuilder.buildSignedOrder({ - maker: wallet.address, - taker: '0x0000000000000000000000000000000000000000', - tokenId: '1234', - makerAmount: '100000000', - takerAmount: '50000000', - side: Side.BUY, - feeRateBps: '100', - nonce: '0', - } as OrderData); - expect(signedOrder).not.null; - expect(signedOrder).not.undefined; - - expect(signedOrder).deep.equal({ - salt: '479249096354', - maker: wallet.address, - signer: wallet.address, - taker: '0x0000000000000000000000000000000000000000', - tokenId: '1234', - makerAmount: '100000000', - takerAmount: '50000000', - side: 0, - expiration: '0', - nonce: '0', - feeRateBps: '100', - signatureType: 0, - signature: - '0x3874d2cfe79c0e82577f4f76ec58d950522ee5923a6f08441927d382c8178a5a2190fd4d5c49705e94d75999a58ec843f94a432e87ebc15cdb2186d315b3cc201b', + describe('buildOrderHash', async () => { + it('random salt', async () => { + const order = await exchangeOrderBuilder.buildOrder({ + maker: wallet.address, + taker: '0x0000000000000000000000000000000000000000', + tokenId: '1234', + makerAmount: '100000000', + takerAmount: '50000000', + side: Side.BUY, + feeRateBps: '100', + nonce: '0', + } as OrderData); + + expect(order).not.null; + expect(order).not.undefined; + + const orderTypedData = + exchangeOrderBuilder.buildOrderTypedData(order); + expect(orderTypedData).not.null; + expect(orderTypedData).not.undefined; + + const orderHash = + exchangeOrderBuilder.buildOrderHash(orderTypedData); + expect(orderHash).not.null; + expect(orderHash).not.undefined; + }); + + it('specific salt', async () => { + (exchangeOrderBuilder as any)['generateSalt'] = () => { + return '479249096354'; + }; + + const order = await exchangeOrderBuilder.buildOrder({ + maker: wallet.address, + taker: '0x0000000000000000000000000000000000000000', + tokenId: '1234', + makerAmount: '100000000', + takerAmount: '50000000', + side: Side.BUY, + feeRateBps: '100', + nonce: '0', + } as OrderData); + + expect(order).not.null; + expect(order).not.undefined; + + const orderTypedData = + exchangeOrderBuilder.buildOrderTypedData(order); + expect(orderTypedData).not.null; + expect(orderTypedData).not.undefined; + + const orderHash = + exchangeOrderBuilder.buildOrderHash(orderTypedData); + expect(orderHash).not.null; + expect(orderHash).not.undefined; + + expect(orderHash).deep.equal( + '0xdcf7c472379a0d50cff6cb72ac72bb4d42b20b9fa4ed56dd01b9f0398d17e773' + ); + }); + }); + + describe('buildSignedOrder', async () => { + it('random salt', async () => { + const signedOrder = await exchangeOrderBuilder.buildSignedOrder( + { + maker: wallet.address, + taker: '0x0000000000000000000000000000000000000000', + tokenId: '1234', + makerAmount: '100000000', + takerAmount: '50000000', + side: Side.BUY, + feeRateBps: '100', + nonce: '0', + } as OrderData + ); + expect(signedOrder).not.null; + expect(signedOrder).not.undefined; + + expect(signedOrder.salt).not.empty; + expect(signedOrder.maker).equal(wallet.address); + expect(signedOrder.signer).equal(wallet.address); + expect(signedOrder.taker).equal( + '0x0000000000000000000000000000000000000000' + ); + expect(signedOrder.tokenId).equal('1234'); + expect(signedOrder.makerAmount).equal('100000000'); + expect(signedOrder.takerAmount).equal('50000000'); + expect(signedOrder.side).equal(0); + expect(signedOrder.expiration).equal('0'); + expect(signedOrder.nonce).equal('0'); + expect(signedOrder.feeRateBps).equal('100'); + expect(signedOrder.signatureType).equal(0); + expect(signedOrder.signature).not.empty; + }); + + it('specific salt', async () => { + (exchangeOrderBuilder as any)['generateSalt'] = () => { + return '479249096354'; + }; + + const signedOrder = await exchangeOrderBuilder.buildSignedOrder( + { + maker: wallet.address, + taker: '0x0000000000000000000000000000000000000000', + tokenId: '1234', + makerAmount: '100000000', + takerAmount: '50000000', + side: Side.BUY, + feeRateBps: '100', + nonce: '0', + } as OrderData + ); + expect(signedOrder).not.null; + expect(signedOrder).not.undefined; + + expect(signedOrder).deep.equal({ + salt: '479249096354', + maker: wallet.address, + signer: wallet.address, + taker: '0x0000000000000000000000000000000000000000', + tokenId: '1234', + makerAmount: '100000000', + takerAmount: '50000000', + side: 0, + expiration: '0', + nonce: '0', + feeRateBps: '100', + signatureType: 0, + signature: + // eslint-disable-next-line max-len + '0x92bb74d70a3c82cff3a9ccf10c9c547bb4ec71cff8cbb6cb308ca975162df7ba65837d5b57f50568fbd6bba9f2b919089268ace609cc33935b0bb5483e1092c31c', + }); }); }); }); diff --git a/tests/src/networks.test.ts b/tests/src/networks.test.ts deleted file mode 100644 index 7bd89b3..0000000 --- a/tests/src/networks.test.ts +++ /dev/null @@ -1,48 +0,0 @@ -import { getContracts } from '../../src/networks'; -import { expect } from 'chai'; - -describe('getContracts', () => { - it('mumbai', () => { - const contracts = getContracts(80001); - expect(contracts).not.null; - expect(contracts).not.undefined; - - expect(contracts.Exchange).equal( - '0x4bFb41d5B3570DeFd03C39a9A4D8dE6Bd8B8982E' - ); - expect(contracts.Collateral).equal( - '0x2E8DCfE708D44ae2e406a1c02DFE2Fa13012f961' - ); - expect(contracts.Conditional).equal( - '0x7D8610E9567d2a6C9FBf66a5A13E9Ba8bb120d43' - ); - }); - - it('polygon', () => { - const contracts = getContracts(137); - expect(contracts).not.null; - expect(contracts).not.undefined; - - expect(contracts.Exchange).equal( - '0x4bFb41d5B3570DeFd03C39a9A4D8dE6Bd8B8982E' - ); - expect(contracts.Collateral).equal( - '0x2791Bca1f2de4661ED88A30C99A7a9449Aa84174' - ); - expect(contracts.Conditional).equal( - '0x4D97DCd97eC945f40cF65F87097ACe5EA0476045' - ); - }); - - it('wrong chain', () => { - try { - const contracts = getContracts(1); - expect(contracts).null; - expect(contracts).undefined; - } catch (e: any) { - expect(e).not.null; - expect(e).not.undefined; - expect(e.toString()).equal('Error: Invalid network'); - } - }); -});