Skip to content

Commit

Permalink
First cut. Fixing integration and deployment code next
Browse files Browse the repository at this point in the history
  • Loading branch information
nuevoalex committed Jul 19, 2018
1 parent f296da5 commit 4b9174f
Show file tree
Hide file tree
Showing 4 changed files with 196 additions and 1 deletion.
101 changes: 101 additions & 0 deletions source/contracts/external/OrdersFinder.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
pragma solidity 0.4.20;

import 'trading/IOrders.sol';
import 'reporting/IMarket.sol';


contract OrdersFinder {
IOrders public orders;

function OrdersFinder(IOrders _orders) {
orders = _orders;
}

function getExistingOrders5(Order.Types _type, IMarket _market, uint256 _outcome) external view returns (bytes32[5] _results) {
bytes32 _orderId = orders.getBestOrderId(_type, _market, _outcome);
uint256 _index = 0;
while (_orderId != 0) {
_results[_index] = _orderId;
_index++;
_orderId = orders.getWorseOrderId(_orderId);
}
return _results;
}

function getExistingOrders10(Order.Types _type, IMarket _market, uint256 _outcome) external view returns (bytes32[10] _results) {
bytes32 _orderId = orders.getBestOrderId(_type, _market, _outcome);
uint256 _index = 0;
while (_orderId != 0) {
_results[_index] = _orderId;
_index++;
_orderId = orders.getWorseOrderId(_orderId);
}
return _results;
}

function getExistingOrders20(Order.Types _type, IMarket _market, uint256 _outcome) external view returns (bytes32[20] _results) {
bytes32 _orderId = orders.getBestOrderId(_type, _market, _outcome);
uint256 _index = 0;
while (_orderId != 0) {
_results[_index] = _orderId;
_index++;
_orderId = orders.getWorseOrderId(_orderId);
}
return _results;
}

function getExistingOrders50(Order.Types _type, IMarket _market, uint256 _outcome) external view returns (bytes32[50] _results) {
bytes32 _orderId = orders.getBestOrderId(_type, _market, _outcome);
uint256 _index = 0;
while (_orderId != 0) {
_results[_index] = _orderId;
_index++;
_orderId = orders.getWorseOrderId(_orderId);
}
return _results;
}

function getExistingOrders100(Order.Types _type, IMarket _market, uint256 _outcome) external view returns (bytes32[100] _results) {
bytes32 _orderId = orders.getBestOrderId(_type, _market, _outcome);
uint256 _index = 0;
while (_orderId != 0) {
_results[_index] = _orderId;
_index++;
_orderId = orders.getWorseOrderId(_orderId);
}
return _results;
}

function getExistingOrders200(Order.Types _type, IMarket _market, uint256 _outcome) external view returns (bytes32[200] _results) {
bytes32 _orderId = orders.getBestOrderId(_type, _market, _outcome);
uint256 _index = 0;
while (_orderId != 0) {
_results[_index] = _orderId;
_index++;
_orderId = orders.getWorseOrderId(_orderId);
}
return _results;
}

function getExistingOrders500(Order.Types _type, IMarket _market, uint256 _outcome) external view returns (bytes32[500] _results) {
bytes32 _orderId = orders.getBestOrderId(_type, _market, _outcome);
uint256 _index = 0;
while (_orderId != 0) {
_results[_index] = _orderId;
_index++;
_orderId = orders.getWorseOrderId(_orderId);
}
return _results;
}

function getExistingOrders1000(Order.Types _type, IMarket _market, uint256 _outcome) external view returns (bytes32[1000] _results) {
bytes32 _orderId = orders.getBestOrderId(_type, _market, _outcome);
uint256 _index = 0;
while (_orderId != 0) {
_results[_index] = _orderId;
_index++;
_orderId = orders.getWorseOrderId(_orderId);
}
return _results;
}
}
12 changes: 11 additions & 1 deletion source/libraries/ContractDeployer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { CompilerOutput } from "solc";
import { Abi, AbiFunction } from 'ethereum';
import { DeployerConfiguration } from './DeployerConfiguration';
import { Connector } from './Connector';
import { Augur, ContractFactory, Controller, Controlled, Universe, ReputationToken, LegacyReputationToken, TimeControlled } from './ContractInterfaces';
import { Augur, ContractFactory, Controller, Controlled, Universe, ReputationToken, LegacyReputationToken, TimeControlled, OrdersFinder } from './ContractInterfaces';
import { NetworkConfiguration } from './NetworkConfiguration';
import { AccountManager } from './AccountManager';
import { Contracts, Contract } from './Contracts';
Expand Down Expand Up @@ -54,6 +54,7 @@ Deploying to: ${networkConfiguration.networkName}
this.controller = await this.uploadController();
await this.uploadAugur();
await this.uploadAllContracts();
await this.uploadOrdersFinder();

if (this.configuration.isProduction) {
console.log(`Registering Legacy Rep Contract at ${this.configuration.legacyRepAddress}`);
Expand Down Expand Up @@ -155,6 +156,14 @@ Deploying to: ${networkConfiguration.networkName}
await this.controller.registerContract(stringTo32ByteHex("Augur"), address, commitHash, bytecodeHash);
}

private async uploadOrdersFinder(): Promise<void> {
const contract = await this.contracts.get("OrdersFinder");
const ordersAddress = this.contracts.get("Orders").address;
if (ordersAddress === undefined) throw new Error("Orders contract not uploaded");
const address = await this.construct(contract, [ordersAddress], `Uploading ${contract.contractName}`);
contract.address = address;
}

private async uploadAllContracts(): Promise<void> {
console.log('Uploading contracts...');
const promises: Array<Promise<any>> = [];
Expand Down Expand Up @@ -329,6 +338,7 @@ Deploying to: ${networkConfiguration.networkName}
if (this.universe) mapping['Universe'] = this.universe.address;
if (this.contracts.get('Augur').address === undefined) throw new Error(`Augur not uploaded.`);
mapping['Augur'] = this.contracts.get('Augur').address!;
mapping['OrdersFinder'] = this.contracts.get('OrdersFinder').address!;
mapping['LegacyReputationToken'] = this.contracts.get('LegacyReputationToken').address!;
for (let contract of this.contracts) {
if (!contract.relativeFilePath.startsWith('trading/')) continue;
Expand Down
13 changes: 13 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,12 +214,14 @@ def __init__(self):
remove_file('./allFiredEvents')
self.relativeContractsPath = '../source/contracts'
self.relativeTestContractsPath = 'solidity_test_helpers'
self.externalContractsPath = '../source/contracts/external'
self.coverageMode = pytest.config.option.cover
self.subFork = pytest.config.option.subFork
if self.coverageMode:
self.chain.head_state.log_listeners.append(self.writeLogToFile)
self.relativeContractsPath = '../coverageEnv/contracts'
self.relativeTestContractsPath = '../coverageEnv/solidity_test_helpers'
self.externalContractsPath = '../coverageEnv/contracts/external'


def writeLogToFile(self, message):
Expand Down Expand Up @@ -306,6 +308,7 @@ def uploadAllContracts(self, legacyRepForRedeployTest=None):
for directory, _, filenames in walk(resolveRelativePath(self.relativeContractsPath)):
# skip the legacy reputation directory since it is unnecessary and we don't support uploads of contracts with constructors yet
if 'legacy_reputation' in directory: continue
if 'external' in directory: continue
for filename in filenames:
name = path.splitext(filename)[0]
extension = path.splitext(filename)[1]
Expand Down Expand Up @@ -342,6 +345,15 @@ def uploadAllMockContracts(self):
else:
self.uploadAndAddToController(path.join(directory, filename))

def uploadExternalContracts(self):
for directory, _, filenames in walk(resolveRelativePath(self.externalContractsPath)):
for filename in filenames:
name = path.splitext(filename)[0]
extension = path.splitext(filename)[1]
if extension != '.sol': continue
constructorArgs = []
if name == "OrdersFinder": constructorArgs = [self.contracts["Orders"].address]
self.upload(path.join(directory, filename), constructorArgs=constructorArgs)

def whitelistTradingContracts(self):
for filename in listdir(resolveRelativePath('../source/contracts/trading')):
Expand Down Expand Up @@ -491,6 +503,7 @@ def augurInitializedSnapshot(fixture, controllerSnapshot):
fixture.initializeAllContracts()
fixture.whitelistTradingContracts()
fixture.approveCentralAuthority()
fixture.uploadExternalContracts()
return fixture.createSnapshot()

@pytest.fixture(scope="session")
Expand Down
71 changes: 71 additions & 0 deletions tests/trading/test_ordersFinder.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#!/usr/bin/env python

from ethereum.tools import tester
from ethereum.tools.tester import TransactionFailed
from utils import longTo32Bytes, longToHexString, bytesToHexString, fix, AssertLog, stringToBytes, EtherDelta, PrintGasUsed
from constants import ASK, BID, YES, NO
from pytest import raises, fixture, mark
from pprint import pprint

def test_case_1(contractsFixture, cash, market, universe):
createOrder = contractsFixture.contracts['CreateOrder']
orders = contractsFixture.contracts['Orders']
ordersFinder = contractsFixture.contracts['OrdersFinder']
tradeGroupID = "42"
nullAddress = longTo32Bytes(0)

# Orphaned order due to early exit logic from bad data caused by Orders worst order setting bug
assert orders.getBestOrderId(BID, market.address, 1) == nullAddress
orderID1 = createOrder.publicCreateOrder(BID, fix(1), 3000, market.address, YES, longTo32Bytes(0), longTo32Bytes(0), tradeGroupID, value=fix('1', '3000'))
orderID2 = createOrder.publicCreateOrder(BID, fix(2), 3000, market.address, YES, longTo32Bytes(0), longTo32Bytes(0), tradeGroupID, value=fix('2', '3000'))
orderID3 = createOrder.publicCreateOrder(BID, fix(3), 2999, market.address, YES, longTo32Bytes(0), longTo32Bytes(0), tradeGroupID, value=fix('3', '2999'))
assert orders.getWorseOrderId(orderID1) == orderID3
assert orders.getWorseOrderId(orderID3) == longTo32Bytes(0) # order 2 orphaned

orderIds = ordersFinder.getExistingOrders5(BID, market.address, 1)

assert orderID1 in orderIds
assert orderID3 in orderIds
assert orderID2 not in orderIds

def test_case_2(contractsFixture, cash, market, universe):
createOrder = contractsFixture.contracts['CreateOrder']
cancelOrder = contractsFixture.contracts['CancelOrder']
orders = contractsFixture.contracts['Orders']
ordersFinder = contractsFixture.contracts['OrdersFinder']
tradeGroupID = "42"
nullAddress = longTo32Bytes(0)

# create orders
assert orders.getBestOrderId(BID, market.address, 1) == nullAddress
orderID1 = createOrder.publicCreateOrder(BID, fix(1), 3000, market.address, YES, longTo32Bytes(0), longTo32Bytes(0), tradeGroupID, value=fix('1', '3000'))
orderID2 = createOrder.publicCreateOrder(BID, fix(2), 3000, market.address, YES, longTo32Bytes(0), longTo32Bytes(0), tradeGroupID, value=fix('2', '3000'))
assert cancelOrder.cancelOrder(orderID1)
orderID3 = createOrder.publicCreateOrder(BID, fix(3), 4000, market.address, YES, longTo32Bytes(0), longTo32Bytes(0), tradeGroupID, value=fix('3', '4000'))
assert orders.getBestOrderId(BID, market.address, 1) == orderID3
assert orders.getWorseOrderId(orderID3) == nullAddress # order 2 orphaned

orderIds = ordersFinder.getExistingOrders5(BID, market.address, 1)

assert orderID1 not in orderIds
assert orderID2 not in orderIds
assert orderID3 in orderIds

def test_overflow(contractsFixture, cash, market, universe):
createOrder = contractsFixture.contracts['CreateOrder']
ordersFinder = contractsFixture.contracts['OrdersFinder']
tradeGroupID = "42"
nullAddress = longTo32Bytes(0)

# create orders
orderID1 = createOrder.publicCreateOrder(BID, fix(1), 1, market.address, YES, longTo32Bytes(0), longTo32Bytes(0), tradeGroupID, value=fix('1', '1'))
orderID2 = createOrder.publicCreateOrder(BID, fix(1), 2, market.address, YES, longTo32Bytes(0), longTo32Bytes(0), tradeGroupID, value=fix('1', '2'))
orderID3 = createOrder.publicCreateOrder(BID, fix(1), 3, market.address, YES, longTo32Bytes(0), longTo32Bytes(0), tradeGroupID, value=fix('1', '3'))
orderID4 = createOrder.publicCreateOrder(BID, fix(1), 4, market.address, YES, longTo32Bytes(0), longTo32Bytes(0), tradeGroupID, value=fix('1', '4'))
orderID5 = createOrder.publicCreateOrder(BID, fix(1), 5, market.address, YES, longTo32Bytes(0), longTo32Bytes(0), tradeGroupID, value=fix('1', '5'))
orderID6 = createOrder.publicCreateOrder(BID, fix(1), 6, market.address, YES, longTo32Bytes(0), longTo32Bytes(0), tradeGroupID, value=fix('1', '6'))

with raises(TransactionFailed):
orderIds = ordersFinder.getExistingOrders5(BID, market.address, 1)

orderIds = ordersFinder.getExistingOrders10(BID, market.address, 1)

0 comments on commit 4b9174f

Please sign in to comment.