Skip to content

Commit

Permalink
fix: uses existing allocation created_at_epoch
Browse files Browse the repository at this point in the history
  • Loading branch information
hopeyen committed May 3, 2022
1 parent 38838d0 commit 11cb9f7
Show file tree
Hide file tree
Showing 9 changed files with 215 additions and 82 deletions.
23 changes: 20 additions & 3 deletions src/AllocationOpt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ function optimize_indexer(;
whitelist::Union{Nothing,Vector{String}},
blacklist::Union{Nothing,Vector{String}},
)

repository, network = snapshot()

alloc, filtered = optimize(id, repository, whitelist, blacklist)
Expand Down Expand Up @@ -123,13 +122,31 @@ function optimize_indexer(;
df[!, "Indexing Reward"] = indexer_subgraph_rewards(
filtered, network, alloc_list, alloc_lifetime
)
df[!, "Estimated Profit"] = estimated_profit(
filtered, alloc_list, grtgas, network, alloc_lifetime
)
df[!, "Marginal Profit"] = compare_rewards(
id,
filtered,
repository,
network,
alloc_list,
alloc_lifetime,
grtgas,
preference_ratio,
)

# Add alloc rows for close actions? Currently only have allocations that want to Open or Reallocate
df[!, "Action"] = map(
a -> a in actions[2] ? "Open" : (a in actions[3] ? "Reallocate" : "Do not open"),
a -> if a in actions[2]
"Open"
else
(a in actions[3] ? "Reallocate" : "Close, do not open")
end,
alloc_list,
)

# print_summary(indexer, df, alloc_list, actions[1], alloc_lifetime)
print_summary(indexer, df, alloc_list, actions[1], alloc_lifetime)

return df
end
Expand Down
74 changes: 40 additions & 34 deletions src/costbenefit.jl
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,9 @@ function issued_token(network::GraphNetworkParameters, alloc_lifetime::Int)
) - network.principle_supply
end

function subgraph_rewards(repo::Repository, network::GraphNetworkParameters, alloc_lifetime::Int)
function subgraph_rewards(
repo::Repository, network::GraphNetworkParameters, alloc_lifetime::Int
)
return signal_shares(repo, network) * issued_token(network, alloc_lifetime)
end

Expand All @@ -106,7 +108,10 @@ function subgraph_rewards(
end

function subgraph_rewards(
repo::Repository, network::GraphNetworkParameters, alloc_lifetime::Int, alloc_list::Vector{Allocation}
repo::Repository,
network::GraphNetworkParameters,
alloc_lifetime::Int,
alloc_list::Vector{Allocation},
)
return signal_shares(repo, network, alloc_list) * issued_token(network, alloc_lifetime)
end
Expand Down Expand Up @@ -139,7 +144,10 @@ function indexer_subgraph_rewards(
end

function indexer_subgraph_rewards(
repo::Repository, network::GraphNetworkParameters, alloc_list::Vector{Allocation}, alloc_lifetime::Int
repo::Repository,
network::GraphNetworkParameters,
alloc_list::Vector{Allocation},
alloc_lifetime::Int,
)
ω_i = map(a -> a.amount, alloc_list)
Ω = ω_i + subgraph_allocations(repo, map(a -> a.id, alloc_list)) # Does not include optimised indexer
Expand All @@ -165,28 +173,27 @@ function compare_rewards(
gas::Float64,
preference_ratio::Float64,
)
existing_allocation_ids = map(
a -> a.id,
filter(
a -> (a.id in map(a -> a.id, allocations_by_indexer(indexer_id, current_repo))),
alloc_list,
),
current_allocations = allocations_by_indexer(indexer_id, current_repo)
overlapping_allocation_ids = map(
a -> a.id, filter(a -> (a.id in map(a -> a.id, current_allocations)), alloc_list)
)

reward_without_reallocate = map(
a -> (
if !(a.id in existing_allocation_ids)
if !(a.id in overlapping_allocation_ids)
0.0
else
(
preference_ratio * indexer_subgraph_rewards(
current_repo,
network,
a.id,
allocation_amounts(indexer_id, current_repo, a.id).amount,
(a.created_at_epoch + alloc_lifetime - network.current_epoch),
) - gas * 1.5
)
preference_ratio * indexer_subgraph_rewards(
filtered_repo,
network,
a.id,
allocation_amounts(indexer_id, current_repo, a.id).amount,
current_allocations[findfirst(
x -> x == a.id, overlapping_allocation_ids
)].created_at_epoch + alloc_lifetime - network.current_epoch,
) - gas * 1.5
)
end
),
alloc_list,
Expand Down Expand Up @@ -215,23 +222,27 @@ function create_actions(
alloc_id_current = map(a -> a.id, alloc_current)

# Reallocate current allocations if compared rewards is positive
indicators = allocation_indicator(
compare_rewards(
indexer_id,
filtered_repo,
current_repo,
network,
alloc_list,
alloc_lifetime,
gas,
preference_ratio,
),
compared_rewards = compare_rewards(
indexer_id,
filtered_repo,
current_repo,
network,
alloc_list,
alloc_lifetime,
gas,
preference_ratio,
)
indicators = allocation_indicator(compared_rewards)
worthy_allocations = map(i -> alloc_list[i], findall(isone, indicators))
worthy_allocation_ids = map(i -> i.id, worthy_allocations)
realloc_actions::Vector{Allocation} = filter(
a -> a.id in alloc_id_current, worthy_allocations
)
# Open the ones worthy not in current repo
open_actions::Vector{Allocation} = filter(
a -> !(a.id in alloc_id_current), worthy_allocations
)

# Close current allocations in indexer's current repo that are not in alloc_list and make sure unworthy allocations cannot open, filtering out younger allocations
do_not_close_ids = if isnothing(young_allocation_ids)
worthy_allocation_ids
Expand All @@ -242,11 +253,6 @@ function create_actions(
a -> !(a.id in do_not_close_ids), alloc_current
)

# Open the ones worthy not in current repo
open_actions::Vector{Allocation} = filter(
a -> !(a.id in alloc_id_current), worthy_allocations
)

@assert length(close_actions open_actions realloc_actions) == 0
return (close_actions, open_actions, realloc_actions)
end
30 changes: 25 additions & 5 deletions src/data.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
export allocation_amounts,
signals, stakes, indexer, young_allocations, allocations_by_indexer
export ids,
allocation_amounts,
signals,
stakes,
indexer,
young_allocations,
allocations_by_indexer,
signal_shares,
remaining_lifetime

function ids(v::Vector)
return map(a -> a.id, v)
end

function ids(indexer::Indexer)
return ids(indexer.allocations)
end

function allocations_by_indexer(id::String, repo::Repository)
try
Expand Down Expand Up @@ -65,7 +80,8 @@ function young_allocations(
id::String, repo::Repository, alloc_lifetime::Int, network::GraphNetworkParameters
)
young_allocations = filter(
ix -> ix.created_at_epoch + alloc_lifetime > network.current_epoch, allocations_by_indexer(id, repo)
ix -> ix.created_at_epoch + alloc_lifetime > network.current_epoch,
allocations_by_indexer(id, repo),
)

return map(x -> x.id, young_allocations)
Expand All @@ -84,13 +100,17 @@ function signal_shares(repo::Repository, network::GraphNetworkParameters)
return map(x -> x.signal / total_signalled, repo.subgraphs)
end

function signal_shares(repo::Repository, network::GraphNetworkParameters, alloc_list::Dict{String,Float64})
function signal_shares(
repo::Repository, network::GraphNetworkParameters, alloc_list::Dict{String,Float64}
)
total_signalled = network.total_tokens_signalled
ids = collect(keys(alloc_list))
return map(x -> x.signal / total_signalled, filter(x -> x.id in ids, repo.subgraphs))
end

function signal_shares(repo::Repository, network::GraphNetworkParameters, alloc_list::Vector{Allocation})
function signal_shares(
repo::Repository, network::GraphNetworkParameters, alloc_list::Vector{Allocation}
)
total_signalled = network.total_tokens_signalled
ids = map(a -> a.id, alloc_list)
return map(x -> x.signal / total_signalled, filter(x -> x.id in ids, repo.subgraphs))
Expand Down
8 changes: 7 additions & 1 deletion src/graphrepository.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
using GraphQLClient

export Allocation,
Indexer, Subgraph, GraphNetworkParameters, GQLQuery, Repository, snapshot, network_issuance
Indexer,
Subgraph,
GraphNetworkParameters,
GQLQuery,
Repository,
snapshot,
network_issuance

function togrt(x)::Float64
return parse(Float64, x) / 1e18
Expand Down
10 changes: 5 additions & 5 deletions src/optimize.jl
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,12 @@ function optimize(optimize_id::String, repository::Repository, whitelist, blackl
v = solve_dual(Ω, ψ, σ)
ω = solve_primal(Ω, ψ, v)

# Output as a dict mapping subgraph ids to allocations
sgraph_ids = map(x -> x.id, filtered_repo.subgraphs)
alloc = Dict(sgraph_ids .=> ω)
# Output as a dict mapping subgraph ids to allocations
sgraph_ids = map(x -> x.id, filtered_repo.subgraphs)
alloc = Dict(sgraph_ids .=> ω)

# Check the constraint as a test (+1 due to small rounding error
@assert (sum(values(alloc)) >= σ - 1 && (sum(values(alloc)) <= σ + 1))
# Check the constraint as a test (+1 due to small rounding error
@assert (sum(values(alloc)) >= σ - 1 && (sum(values(alloc)) <= σ + 1))

return alloc, filtered_repo
end
Expand Down
Loading

0 comments on commit 11cb9f7

Please sign in to comment.