Skip to content

Commit

Permalink
allocationOpt: add gas cost and concurrent plans
Browse files Browse the repository at this point in the history
  • Loading branch information
hopeyen committed Apr 5, 2022
1 parent 38849d8 commit e4bb10f
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 9 deletions.
1 change: 1 addition & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ version = "0.1.0"
[deps]
CSV = "336ed68f-0bac-5ca0-87d4-7b16caf5d00b"
DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0"
Distributed = "8ba89e20-285c-5b6f-9357-94700520ee1b"
GraphQLClient = "09d831e3-9c21-47a9-bfd8-076871817219"
Roots = "f2b01f46-fcfa-551c-844a-d8ac1e96c665"

Expand Down
36 changes: 36 additions & 0 deletions src/AllocationOpt.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
module AllocationOpt
using Distributed
export allocation_optimization

export optimize_indexer_to_csv!

Expand All @@ -8,6 +10,7 @@ using DataFrames
include("exceptions.jl")
include("graphrepository.jl")
include("data.jl")
include("gascost.jl")
include("optimize.jl")

function optimize_indexer_to_csv!(;
Expand All @@ -26,4 +29,37 @@ function optimize_indexer_to_csv!(;
return CSV.write(csv_write_path, df)
end

function filter_subgraphs(repo::Repository, alloc_list::Dict{String, Float64}, threshold::Float64)
return Repository(
repo.indexers,
filter(x -> x.id in keys(alloc_list) && alloc_list[x.id] > threshold, repo.subgraphs)
)
end

function allocation_optimization(optimizeID::String, repository::Repository, gas_base_fee::Float64)
# initial run for profit priority
alloc_list::Dict{String, Float64} = optimize(optimizeID, repository, nothing, nothing)[1]

# preset parameters
allocation_min_thresholds::Vector{Float64} = [0, gas_base_fee, gas_base_fee*3, gas_base_fee*10, gas_base_fee*50]

# spawn jobs
for threshold in allocation_min_thresholds
filtered_repo = filter_subgraphs(repository, alloc_list, threshold)
if length(filtered_repo.subgraphs) > 0
plan = @spawn optimize(
optimizeID,
filtered_repo,
nothing, nothing)
result = fetch(plan)[1]
profit = estimated_profit(filtered_repo, result, gas_base_fee)

# keep track of the most profitable allocation
if profit >= estimated_profit(repository, alloc_list, gas_base_fee)
alloc_list = result
end
end
end
return alloc_list
end
end
22 changes: 15 additions & 7 deletions src/data.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,9 @@ function allocations(id::String, sgraph_ids::Vector{String}, repo::Repository)
end
alloc = zeros(length(sgraph_ids))
for al in indexer_allocations
ix = nothing
try
ix = first(findall(x -> x == al.id, sgraph_ids))
catch err
if isa(err, BoundsError)
throw(UnknownSubgraphError(al.id))
end
ix = findfirst(x -> x == al.id, sgraph_ids)
if (isnothing(ix))
throw(UnknownSubgraphError(al.id))
end
alloc[ix] = al.amount
end
Expand All @@ -37,10 +33,22 @@ function allocations(repo::Repository)
return mat
end

function allocations(repo::Repository, subg_id::String)
allocAmount = 0
for indexer in repo.indexers
allocAmount += first(filter(alloc -> alloc.id == subg_id, first(filter(x -> x.id == indexer.id, repo.indexers)).allocations)).amount
end
return allocAmount
end

function signals(repo::Repository)
return map(x -> x.signal, repo.subgraphs)
end

function signals(repo::Repository, subg_id::String)
return repo.subgraphs[findfirst(x -> x.id == subg_id, repo.subgraphs)].signal
end

function stakes(repo::Repository)
return map(x -> x.stake + x.delegation, repo.indexers)
end
Expand Down
25 changes: 25 additions & 0 deletions src/gascost.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
export calculate_gas_fee, sum_gas_fee, estimated_profit

function calculate_gas_fee(allocs::Dict{String, Float64}, txGas)
sum(map(x -> x > 0 ? txGas : 0, values(allocs)))
end

function sum_gas_fee(allocs::Dict{String, Float64}, txGas)
# open + close + claim
calculate_gas_fee(allocs, txGas) + calculate_gas_fee(allocs, txGas) + calculate_gas_fee(allocs, 0.3 * txGas)
end

function estimated_profit(repo::Repository, allocs::Dict{String, Float64}, txGas)
repository = Repository(repo.indexers, filter(x -> x.id in keys(allocs) ,repo.subgraphs))
rewards(repository, allocs) - sum_gas_fee(allocs, txGas)
end

function rewards(repo::Repository, allocs::Dict{String, Float64})
total = 0
for (id, amount) in allocs
total += signals(repo, id) * amount / (allocations(repo, id) + amount)
end
total
end

#TODO: add query to get real time gas rates for each tx -> this can be connected to indexer making gas estimations
2 changes: 0 additions & 2 deletions src/optimize.jl
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,5 @@ function optimize(optimize_id::String, repository::Repository, whitelist, blackl
sgraph_ids = map(x -> x.id, filtered_repo.subgraphs)
alloc = Dict(sgraph_ids .=> ω)

# TODO: Hope's gas algorithm

return alloc, filtered_repo
end
19 changes: 19 additions & 0 deletions test/allocationopt-test.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
@testset "AllocationOpt" begin

gas_fee = 0.3
repository = Repository(
[
Indexer(
"0x000", 5.0, 0.0, [Allocation("0x010", 2.5), Allocation("0x011", 2.5)]
),
Indexer(
"0x001", 10.0, 0.0, [Allocation("0x010", 2.0), Allocation("0x011", 8.0)]
),
],
[Subgraph("0x011", 10.0), Subgraph("0x010", 5.0)],
)

allocations = allocation_optimization("0x000", repository, gas_fee)

@test allocations["0x011"] 5.0
end
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ using Test
include("graphrepository-test.jl")
include("data-test.jl")
include("optimize-test.jl")
include("allocationopt-test.jl")
end

0 comments on commit e4bb10f

Please sign in to comment.