Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,3 @@ aggregator.
export INFURA_PROJECT_ID=<key>
make run
```

7 changes: 6 additions & 1 deletion contracts/Trader.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,16 @@
"internalType": "uint256",
"name": "executedOut",
"type": "uint256"
},
{
"internalType": "uint256",
"name": "gasUsed",
"type": "uint256"
}
],
"stateMutability": "nonpayable",
"type": "function"
}
],
"bin-runtime": "608060405234801561001057600080fd5b506004361061002b5760003560e01c8063906fb45214610030575b600080fd5b61004361003e36600461054e565b61005c565b6040805192835260208301919091520160405180910390f35b6040517f70a082310000000000000000000000000000000000000000000000000000000081523060048201526000908190819073ffffffffffffffffffffffffffffffffffffffff8a16906370a0823190602401602060405180830381865afa1580156100cd573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906100f1919061060a565b6040517f70a0823100000000000000000000000000000000000000000000000000000000815230600482015290915060009073ffffffffffffffffffffffffffffffffffffffff8a16906370a0823190602401602060405180830381865afa158015610161573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190610185919061060a565b905073ffffffffffffffffffffffffffffffffffffffff8816156101e4576101e473ffffffffffffffffffffffffffffffffffffffff8b16897fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff610380565b61023b86868080601f0160208091040260200160405190810160405280939291908181526020018383808284376000920191909152505073ffffffffffffffffffffffffffffffffffffffff8b16929150506104a5565b506040517f70a0823100000000000000000000000000000000000000000000000000000000815230600482015273ffffffffffffffffffffffffffffffffffffffff8b16906370a0823190602401602060405180830381865afa1580156102a6573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906102ca919061060a565b6102d49083610623565b6040517f70a08231000000000000000000000000000000000000000000000000000000008152306004820152909450819073ffffffffffffffffffffffffffffffffffffffff8b16906370a0823190602401602060405180830381865afa158015610343573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190610367919061060a565b6103719190610623565b92505050965096945050505050565b6040805173ffffffffffffffffffffffffffffffffffffffff848116602483015260448083018590528351808403909101815260649092019092526020810180517bffffffffffffffffffffffffffffffffffffffffffffffffffffffff167f095ea7b30000000000000000000000000000000000000000000000000000000017905290600090610413908616836104a5565b90508051600014806104345750808060200190518101906104349190610663565b61049e576040517f08c379a000000000000000000000000000000000000000000000000000000000815260206004820152601a60248201527f5361666545524332303a20617070726f76616c206661696c6564000000000000604482015260640160405180910390fd5b5050505050565b606060008373ffffffffffffffffffffffffffffffffffffffff16836040516104ce919061068c565b6000604051808303816000865af19150503d806000811461050b576040519150601f19603f3d011682016040523d82523d6000602084013e610510565b606091505b50925090508061052257815160208301fd5b5092915050565b73ffffffffffffffffffffffffffffffffffffffff8116811461054b57600080fd5b50565b60008060008060008060a0878903121561056757600080fd5b863561057281610529565b9550602087013561058281610529565b9450604087013561059281610529565b935060608701356105a281610529565b9250608087013567ffffffffffffffff808211156105bf57600080fd5b818901915089601f8301126105d357600080fd5b8135818111156105e257600080fd5b8a60208285010111156105f457600080fd5b6020830194508093505050509295509295509295565b60006020828403121561061c57600080fd5b5051919050565b8181038181111561065d577f4e487b7100000000000000000000000000000000000000000000000000000000600052601160045260246000fd5b92915050565b60006020828403121561067557600080fd5b8151801515811461068557600080fd5b9392505050565b6000825160005b818110156106ad5760208186018101518583015201610693565b50600092019182525091905056fea164736f6c6343000810000a"
"bin-runtime": "608060405234801561001057600080fd5b506004361061002b5760003560e01c8063906fb45214610030575b600080fd5b61004361003e366004610568565b610062565b6040805193845260208401929092529082015260600160405180910390f35b6040517f70a0823100000000000000000000000000000000000000000000000000000000815230600482015260009081908190819073ffffffffffffffffffffffffffffffffffffffff8b16906370a0823190602401602060405180830381865afa1580156100d5573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906100f99190610624565b6040517f70a0823100000000000000000000000000000000000000000000000000000000815230600482015290915060009073ffffffffffffffffffffffffffffffffffffffff8b16906370a0823190602401602060405180830381865afa158015610169573d6000803e3d6000fd5b505050506040513d601f19601f8201168201806040525081019061018d9190610624565b905073ffffffffffffffffffffffffffffffffffffffff8916156101ec576101ec73ffffffffffffffffffffffffffffffffffffffff8c168a7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff61039a565b5a925061024687878080601f0160208091040260200160405190810160405280939291908181526020018383808284376000920191909152505073ffffffffffffffffffffffffffffffffffffffff8c16929150506104bf565b505a610252908461063d565b6040517f70a0823100000000000000000000000000000000000000000000000000000000815230600482015290935073ffffffffffffffffffffffffffffffffffffffff8c16906370a0823190602401602060405180830381865afa1580156102bf573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906102e39190610624565b6102ed908361063d565b6040517f70a08231000000000000000000000000000000000000000000000000000000008152306004820152909550819073ffffffffffffffffffffffffffffffffffffffff8c16906370a0823190602401602060405180830381865afa15801561035c573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906103809190610624565b61038a919061063d565b9350505096509650969350505050565b6040805173ffffffffffffffffffffffffffffffffffffffff848116602483015260448083018590528351808403909101815260649092019092526020810180517bffffffffffffffffffffffffffffffffffffffffffffffffffffffff167f095ea7b3000000000000000000000000000000000000000000000000000000001790529060009061042d908616836104bf565b905080516000148061044e57508080602001905181019061044e919061067d565b6104b8576040517f08c379a000000000000000000000000000000000000000000000000000000000815260206004820152601a60248201527f5361666545524332303a20617070726f76616c206661696c6564000000000000604482015260640160405180910390fd5b5050505050565b606060008373ffffffffffffffffffffffffffffffffffffffff16836040516104e891906106a6565b6000604051808303816000865af19150503d8060008114610525576040519150601f19603f3d011682016040523d82523d6000602084013e61052a565b606091505b50925090508061053c57815160208301fd5b5092915050565b73ffffffffffffffffffffffffffffffffffffffff8116811461056557600080fd5b50565b60008060008060008060a0878903121561058157600080fd5b863561058c81610543565b9550602087013561059c81610543565b945060408701356105ac81610543565b935060608701356105bc81610543565b9250608087013567ffffffffffffffff808211156105d957600080fd5b818901915089601f8301126105ed57600080fd5b8135818111156105fc57600080fd5b8a602082850101111561060e57600080fd5b6020830194508093505050509295509295509295565b60006020828403121561063657600080fd5b5051919050565b81810381811115610677577f4e487b7100000000000000000000000000000000000000000000000000000000600052601160045260246000fd5b92915050565b60006020828403121561068f57600080fd5b8151801515811461069f57600080fd5b9392505050565b6000825160005b818110156106c757602081860181015185830152016106ad565b50600092019182525091905056fea164736f6c6343000810000a"
}
4 changes: 3 additions & 1 deletion contracts/Trader.sol
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,16 @@ contract Trader {
address spender,
address exchange,
bytes calldata cdata
) external returns (uint256 executedIn, uint256 executedOut) {
) external returns (uint256 executedIn, uint256 executedOut, uint256 gasUsed) {
uint256 balanceIn = tokenIn.balanceOf(address(this));
uint256 balanceOut = tokenOut.balanceOf(address(this));

if (spender != address(0)) {
tokenIn.safeApprove(spender, type(uint256).max);
}
gasUsed = gasleft();
exchange.doCall(cdata);
gasUsed -= gasleft();

executedIn = balanceIn - tokenIn.balanceOf(address(this));
executedOut = tokenOut.balanceOf(address(this)) - balanceOut;
Expand Down
35 changes: 23 additions & 12 deletions src/exchange.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export class Exchange {
}
}

async simulateTrade(order, swap, block_number, gasPrice) {
async simulateTrade(order, swap, block_number, gasPrice, ethPrice) {
if (!swap) {
return {
uid: order.uid,
Expand All @@ -67,18 +67,19 @@ export class Exchange {
};
}

const data = this.trader.encodeFunctionData("trade", [
order.sellToken,
order.buyToken,
swap.spender,
swap.exchange,
swap.data,
]);
const eth_call_promise = this.provider
.send("eth_call", [
{
from: order.owner,
to: order.owner,
data: this.trader.encodeFunctionData("trade", [
order.sellToken,
order.buyToken,
swap.spender,
swap.exchange,
swap.data,
]),
data: data,
},
block_number,
{
Expand All @@ -90,17 +91,26 @@ export class Exchange {
.catch((err) => {
if (`${err.body}`.indexOf("execution reverted") >= 0) {
log.warning(`${this.name} trade reverted`);
return this.trader.encodeFunctionResult("trade", [0, 0]);
return this.trader.encodeFunctionResult("trade", [0, 0, 0]);
} else {
throw err;
}
});

const eth_call_result = await eth_call_promise;

const [executedSellAmount, executedBuyAmount] = this.trader
const [executedSellAmount, executedBuyAmount, gasUsed] = this.trader
.decodeFunctionResult("trade", eth_call_result);
const txInitiationGasAmount = ethers.BigNumber.from("21000");
const gasCostJumpIntoExchangeContract = ethers.BigNumber.from("2100");
console.log(gasUsed.toString())

const realGasUsed = gasUsed.add(txInitiationGasAmount.sub(
gasCostJumpIntoExchangeContract));

console.log(realGasUsed.toString())
console.log( swap.exchange, "simulation gas costs", (realGasUsed * gasPrice) / ethPrice );
console.log(swap.exchange, "provided gas costs", swap.feeUsd );
return {
uid: order.uid,
sellAmount: swap.sellAmount,
Expand All @@ -109,7 +119,7 @@ export class Exchange {
executedBuyAmount,
exchange: swap.exchange,
data: swap.data,
gasCost: swap.feeUsd,
gasCost: (realGasUsed * gasPrice) / ethPrice,
};
}

Expand Down Expand Up @@ -150,12 +160,13 @@ export class Exchange {
sellTokenPrice,
);
if (swap != null) {
let feeUsd = swap.feeUsd;
const feeUsd = swap.feeUsd;
const trade = await this.simulateTrade(
order,
swap,
block_number,
gasPrice,
etherPrice,
);
let outPutValue = 0;
if (this.name == "cowswap") {
Expand Down
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ const db = new DB("orders.db");
const provider = new ethers.providers.JsonRpcProvider(
`https://mainnet.infura.io/v3/${Deno.env.get("INFURA_PROJECT_ID")}`,
);
const orderbook = new Orderbook(db, false);
const parameterStore = new ParameterStore(db);
const orderbook = new Orderbook(db, true);
await setup_log(log);
const exchanges = generateExchangeConfig(db, provider).filter((exchange) =>
["zeroex", "ocean", "oneinch", "paraswap", "cowswap"].includes(exchange.name)
Expand Down
39 changes: 39 additions & 0 deletions src/reader/best_solution_shared.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { UNION_RAW_DATA_DEX_AG_ONLY } from "./shared.js";
export function display_histogram_shared_solutions(db) {
const winners_query = `
raw_data_filtered as (
select * from raw_data where executed_buy_amount != 0
),
winner_table as (
select uid, max(executed_buy_amount) as winning_buy_amount from raw_data_filtered group by uid
),
raw_data_filtered_with_winning_bid as (
select rd.uid, * from raw_data_filtered rd left join winner_table on rd.uid = winner_table.uid where rd.executed_buy_amount = winning_buy_amount order by 1
),
winning_party_count as (
select uid, count(*) as num_winners from raw_data_filtered_with_winning_bid group by 1
),
solution_count as (
select uid, count(*) overall_count from raw_data_filtered group by uid
),
results as (
select sc.uid, num_winners from solution_count sc left outer join winning_party_count on sc.uid = winning_party_count.uid where overall_count > 2
)
select num_winners, count(*) from results group by num_winners
`;
const rows = db.query(
UNION_RAW_DATA_DEX_AG_ONLY + winners_query,
);
console.log(
"Histogram of shared best solution between dex-ags(wihout gas cost considerations): The following histogram shows how frequently the best solution was provided by 1 dex-ag, 2 dex-ags,...",
);
console.log(rows);
const x_val = [];
const labels = [];
for (let i = 0; i < rows.length; i++) {
x_val.push(rows[i][1]);
labels.push(rows[i][0]);
}
console.log(x_val);
console.log(labels);
}
56 changes: 56 additions & 0 deletions src/reader/histogram_bang_for_buck.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { UNION_RAW_DATA_HIGH_GAS, UNION_RAW_DATA_LOW_GAS } from "./shared.js";
export function display_histogram_winners(db) {
const winner_count_query = `
raw_data_filtered as (
select * from raw_data where executed_buy_amount != 0 or name='cowswap'
),
result_count as (
select uid, count(*) as number_of_results from raw_data_filtered group by uid
),
ranked_by_ouput as(
select rf.uid, output_value_usd, name, gas_cost_usd,
rank() over( partition by rf.uid order by output_value_usd DESC ) as rank
from raw_data_filtered rf left join result_count rc on rc.uid = rf.uid where rc.number_of_results > 2
),
winner_count as (
select name, count(*) from ranked_by_ouput where name='cowswap' and rank=1
UNION
select name, count(*) from ranked_by_ouput where name='oneinch' and rank=1
UNION
select name, count(*) from ranked_by_ouput where name='zeroex' and rank=1
UNION
select name, count(*) from ranked_by_ouput where name='ocean' and rank=1
UNION
select name, count(*) from ranked_by_ouput where name='paraswap' and rank=1
)
select * from winner_count
`;
let rows = db.query(
UNION_RAW_DATA_LOW_GAS + winner_count_query,
);
console.log(
"Histogram of competition win per exchange (on bang for buck, considering gas costs, in low gas cost env):",
);
let x_val = [];
let labels = [];
for (let i = 0; i < rows.length; i++) {
x_val.push(rows[i][1]);
labels.push(rows[i][0]);
}
console.log(x_val);
console.log(labels);
rows = db.query(
UNION_RAW_DATA_HIGH_GAS + winner_count_query,
);
console.log(
"Histogram of competition win per exchange (on bang for buck, considering gas costs, in high gas cost env):",
);
x_val = [];
labels = [];
for (let i = 0; i < rows.length; i++) {
x_val.push(rows[i][1]);
labels.push(rows[i][0]);
}
console.log(x_val);
console.log(labels);
}
57 changes: 57 additions & 0 deletions src/reader/histogram_bang_for_buck_big_orders.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { UNION_RAW_DATA_HIGH_GAS, UNION_RAW_DATA_LOW_GAS } from "./shared.js";
export function display_histogram_winners_big_trades(db) {
const winners_query = `
raw_data_filtered as (
select * from raw_data where (executed_buy_amount != 0 or name='cowswap') and CAST(output_value_usd as INTEGER) > 100000.0
),
result_count as (
select uid, count(*) as number_of_results from raw_data_filtered group by uid
),
ranked_by_ouput as(
select rf.uid, output_value_usd, name, gas_cost_usd,
rank() over( partition by rf.uid order by output_value_usd DESC ) as rank
from raw_data_filtered rf left join result_count rc on rc.uid = rf.uid where rc.number_of_results > 2
),
winner_count as (
select name, count(*) from ranked_by_ouput where name='cowswap' and rank=1
UNION
select name, count(*) from ranked_by_ouput where name='oneinch' and rank=1
UNION
select name, count(*) from ranked_by_ouput where name='zeroex' and rank=1
UNION
select name, count(*) from ranked_by_ouput where name='ocean' and rank=1
UNION
select name, count(*) from ranked_by_ouput where name='paraswap' and rank=1
)
select * from winner_count
`;
let rows = db.query(
UNION_RAW_DATA_LOW_GAS + winners_query,
);
console.log(
"Histogram of competition win per exchange on big orders(on bang for buck, considering gas costs, in low gas cost environment):",
);
let x_val = [];
let labels = [];
for (let i = 0; i < rows.length; i++) {
x_val.push(rows[i][1]);
labels.push(rows[i][0]);
}
console.log(x_val);
console.log(labels);
rows = db.query(
UNION_RAW_DATA_HIGH_GAS + winners_query,
);

console.log(
"Histogram of competition win per exchange on big orders(on bang for buck, considering gas costs, in high gas cost environment):",
);
x_val = [];
labels = [];
for (let i = 0; i < rows.length; i++) {
x_val.push(rows[i][1]);
labels.push(rows[i][0]);
}
console.log(x_val);
console.log(labels);
}
73 changes: 73 additions & 0 deletions src/reader/histogram_diff_to_cowswap.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import { UNION_RAW_DATA_LOW_GAS, UNION_RAW_DATA_HIGH_GAS } from "./shared.js";
export function display_histogram_winners_diff_to_cowswap(db) {
const query = `
raw_data_filtered as (
select * from raw_data where executed_buy_amount != 0 or name='cowswap'
),
ranked_by_ouput as(
select uid, output_value_usd, name,

rank() over( partition by uid order by output_value_usd DESC ) as rank
from raw_data_filtered
),
winner as (
select * from ranked_by_ouput where rank=1
),
difference_to_cowswap as(
select ro.uid, w.output_value_usd - ro.output_value_usd as diff from ranked_by_ouput ro left join winner w on ro.uid = w.uid
where ro.name='cowswap'
),
winner_count as (
select '[0,0]' as category, count(*) from difference_to_cowswap where diff=0
UNION
select '[0,0.001]' as category, count(*) from difference_to_cowswap where diff < 0.001 and diff >0
UNION
select '[0.001,0.01]' as category, count(*) from difference_to_cowswap where diff > 0.001 and diff < 0.01
UNION
select '[0.01,0.1]' as category, count(*) from difference_to_cowswap where diff > 0.01 and diff < 0.1
UNION
select '[0.1,1]' as category, count(*) from difference_to_cowswap where diff > 0.1 and diff < 1
UNION
select '[1,2]' as category, count(*) from difference_to_cowswap where diff >1 and diff < 2
UNION
select '[2,4]' as category, count(*) from difference_to_cowswap where diff >2 and diff < 4
UNION
select '[4,10]' as category, count(*) from difference_to_cowswap where diff >4 and diff < 10
UNION
select '[10,10000]' as category, count(*) from difference_to_cowswap where diff >10
)
select * from winner_count
`;
let rows = db.query(
UNION_RAW_DATA_LOW_GAS+ query,
[],
);
console.log(
"Histogram of output difference between cowswap and best solution in [usd] (on bang for the buck considering gas costs, in low gas cost env):",
);
console.log(rows);
let x_val = [];
let labels = [];
for (let i = 0; i < rows.length; i++) {
x_val.push(rows[i][1]);
labels.push(rows[i][0]);
}
console.log(x_val);
console.log(labels);
rows = db.query(
UNION_RAW_DATA_HIGH_GAS + query,
[],
);
console.log(
"Histogram of output difference between cowswap and best solution in [usd] (on bang for the buck considering gas costs, in high gas cost env):",
);
console.log(rows);
x_val = [];
labels = [];
for (let i = 0; i < rows.length; i++) {
x_val.push(rows[i][1]);
labels.push(rows[i][0]);
}
console.log(x_val);
console.log(labels);
}
Loading