Skip to content

Commit

Permalink
Neg Risk (#32)
Browse files Browse the repository at this point in the history
* add ContractConfig class

* remove config, update tests

* remove contract config import

* extra tests

* fixing tests

* update black, reformat

---------

Co-authored-by: Rodrigo <[email protected]>
  • Loading branch information
mshrieve and poly-rodr authored Dec 7, 2023
1 parent 079595c commit f3429db
Show file tree
Hide file tree
Showing 6 changed files with 137 additions and 67 deletions.
38 changes: 0 additions & 38 deletions py_order_utils/config.py

This file was deleted.

2 changes: 1 addition & 1 deletion py_order_utils/model/order.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ def dict(self):
d["signature"] = self.signature
if d["side"] == 0:
d["side"] = "BUY"
else:
else:
d["side"] = "SELL"
d["expiration"] = str(d["expiration"])
d["nonce"] = str(d["nonce"])
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,4 @@ varint==1.0.2
web3==5.26.0
websockets==9.1
yarl==1.7.2
black==22.1.0
black==22.3.0
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setuptools.setup(
name="py_order_utils",
version="0.1.3",
version="0.2.0",
author="Polymarket Engineering",
author_email="[email protected]",
maintainer="Polymarket Engineering",
Expand Down
19 changes: 0 additions & 19 deletions tests/test_config.py

This file was deleted.

141 changes: 134 additions & 7 deletions tests/test_order_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
from py_order_utils.builders import OrderBuilder
from py_order_utils.model.signatures import EOA
from py_order_utils.signer import Signer
from py_order_utils.config import get_contract_config
from py_order_utils.constants import ZERO_ADDRESS

# publicly known private key
Expand All @@ -14,7 +13,12 @@
maker_address = "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266"
salt = 479249096354
chain_id = 80001
mumbai_contracts = get_contract_config(chain_id)
mumbai_contracts = {
"exchange": "0x4bFb41d5B3570DeFd03C39a9A4D8dE6Bd8B8982E",
"negRiskExchange": "0x87d1A0DdB4C63a6301916F02090A51a7241571e4",
"collateral": "0x2E8DCfE708D44ae2e406a1c02DFE2Fa13012f961",
"conditional": "0x7D8610E9567d2a6C9FBf66a5A13E9Ba8bb120d43",
}


def mock_salt_generator():
Expand All @@ -23,7 +27,33 @@ def mock_salt_generator():

class TestOrderBuilder(TestCase):
def test_validate_order(self):
builder = OrderBuilder(mumbai_contracts.exchange, chain_id, signer)
builder = OrderBuilder(mumbai_contracts["exchange"], chain_id, signer)

# Valid order
data = self.generate_data()
self.assertTrue(builder._validate_inputs(data))

# Invalid if any of the required fields are missing
data = self.generate_data()
data.maker = None
self.assertFalse(builder._validate_inputs(data))

# Invalid if any of the required fields are invalid
data = self.generate_data()
data.nonce = "-1"
self.assertFalse(builder._validate_inputs(data))

data = self.generate_data()
data.expiration = "not a number"
self.assertFalse(builder._validate_inputs(data))

# Invalid signature type
data = self.generate_data()
data.signatureType = 100
self.assertFalse(builder._validate_inputs(data))

def test_validate_order_neg_risk(self):
builder = OrderBuilder(mumbai_contracts["negRiskExchange"], chain_id, signer)

# Valid order
data = self.generate_data()
Expand All @@ -49,7 +79,62 @@ def test_validate_order(self):
self.assertFalse(builder._validate_inputs(data))

def test_build_order(self):
builder = OrderBuilder(mumbai_contracts.exchange, chain_id, signer)
builder = OrderBuilder(mumbai_contracts["exchange"], chain_id, signer)

invalid_data_input = self.generate_data()
invalid_data_input.tokenId = None

# throw if invalid order input
with self.assertRaises(ValidationException):
builder.build_order(invalid_data_input)

invalid_data_input = self.generate_data()
invalid_data_input.signer = ZERO_ADDRESS

# throw if invalid signer
with self.assertRaises(ValidationException):
builder.build_order(invalid_data_input)

_order = builder.build_order(self.generate_data())

# Ensure correct values on order
self.assertTrue(isinstance(_order["salt"], int))
self.assertEqual(maker_address, _order["maker"])
self.assertEqual(maker_address, _order["signer"])
self.assertEqual(ZERO_ADDRESS, _order["taker"])
self.assertEqual(1234, _order["tokenId"])
self.assertEqual(100000000, _order["makerAmount"])
self.assertEqual(50000000, _order["takerAmount"])
self.assertEqual(0, _order["expiration"])
self.assertEqual(0, _order["nonce"])
self.assertEqual(100, _order["feeRateBps"])
self.assertEqual(BUY, _order["side"])
self.assertEqual(EOA, _order["signatureType"])

# specific salt
builder = OrderBuilder(
mumbai_contracts["exchange"], chain_id, signer, mock_salt_generator
)

_order = builder.build_order(self.generate_data())

# Ensure correct values on order
self.assertTrue(isinstance(_order["salt"], int))
self.assertEqual(salt, _order["salt"])
self.assertEqual(maker_address, _order["maker"])
self.assertEqual(maker_address, _order["signer"])
self.assertEqual(ZERO_ADDRESS, _order["taker"])
self.assertEqual(1234, _order["tokenId"])
self.assertEqual(100000000, _order["makerAmount"])
self.assertEqual(50000000, _order["takerAmount"])
self.assertEqual(0, _order["expiration"])
self.assertEqual(0, _order["nonce"])
self.assertEqual(100, _order["feeRateBps"])
self.assertEqual(BUY, _order["side"])
self.assertEqual(EOA, _order["signatureType"])

def test_build_order_neg_risk(self):
builder = OrderBuilder(mumbai_contracts["negRiskExchange"], chain_id, signer)

invalid_data_input = self.generate_data()
invalid_data_input.tokenId = None
Expand Down Expand Up @@ -83,7 +168,7 @@ def test_build_order(self):

# specific salt
builder = OrderBuilder(
mumbai_contracts.exchange, chain_id, signer, mock_salt_generator
mumbai_contracts["negRiskExchange"], chain_id, signer, mock_salt_generator
)

_order = builder.build_order(self.generate_data())
Expand All @@ -105,7 +190,7 @@ def test_build_order(self):

def test_build_prder_signature(self):
builder = OrderBuilder(
mumbai_contracts.exchange, chain_id, signer, mock_salt_generator
mumbai_contracts["exchange"], chain_id, signer, mock_salt_generator
)

_order = builder.build_order(self.generate_data())
Expand All @@ -121,9 +206,28 @@ def test_build_prder_signature(self):
sig = builder.build_order_signature(_order)
self.assertEqual(expected_signature, sig)

def test_build_prder_signature_neg_risk(self):
builder = OrderBuilder(
mumbai_contracts["negRiskExchange"], chain_id, signer, mock_salt_generator
)

_order = builder.build_order(self.generate_data())

# Ensure struct hash is expected(generated via ethers)
expected_struct_hash = (
"0x053c3169ec6c9e99e3640cb12b9c9245917daf36bd4fd39ea09d201a07b53952"
)
struct_hash = builder._create_struct_hash(_order)
self.assertEqual(expected_struct_hash, struct_hash.hex())

expected_signature = "0xb47e588cfb8630ffa255d1a04a4bb2b996967c2143fc107ab443282ed7dcc123288842968fa29e6f2e789e39ea02f13654d4dd55002b8672e9a91e2ba9045aa21b"
sig = builder.build_order_signature(_order)
print(sig)
self.assertEqual(expected_signature, sig)

def test_build_signed_order(self):
builder = OrderBuilder(
mumbai_contracts.exchange, chain_id, signer, mock_salt_generator
mumbai_contracts["exchange"], chain_id, signer, mock_salt_generator
)

signed_order = builder.build_signed_order(self.generate_data())
Expand All @@ -144,6 +248,29 @@ def test_build_signed_order(self):
self.assertEqual(BUY, signed_order.order["side"])
self.assertEqual(EOA, signed_order.order["signatureType"])

def test_build_signed_order_neg_risk(self):
builder = OrderBuilder(
mumbai_contracts["negRiskExchange"], chain_id, signer, mock_salt_generator
)

signed_order = builder.build_signed_order(self.generate_data())

expected_signature = "0xb47e588cfb8630ffa255d1a04a4bb2b996967c2143fc107ab443282ed7dcc123288842968fa29e6f2e789e39ea02f13654d4dd55002b8672e9a91e2ba9045aa21b"
self.assertEqual(expected_signature, signed_order.signature)
self.assertTrue(isinstance(signed_order.order["salt"], int))
self.assertEqual(salt, signed_order.order["salt"])
self.assertEqual(maker_address, signed_order.order["maker"])
self.assertEqual(maker_address, signed_order.order["signer"])
self.assertEqual(ZERO_ADDRESS, signed_order.order["taker"])
self.assertEqual(1234, signed_order.order["tokenId"])
self.assertEqual(100000000, signed_order.order["makerAmount"])
self.assertEqual(50000000, signed_order.order["takerAmount"])
self.assertEqual(0, signed_order.order["expiration"])
self.assertEqual(0, signed_order.order["nonce"])
self.assertEqual(100, signed_order.order["feeRateBps"])
self.assertEqual(BUY, signed_order.order["side"])
self.assertEqual(EOA, signed_order.order["signatureType"])

def generate_data(self) -> OrderData:
return OrderData(
maker=maker_address,
Expand Down

0 comments on commit f3429db

Please sign in to comment.