Skip to content

Commit

Permalink
Format, docstring, and graphrepository refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
hopeyen committed May 2, 2022
1 parent 331f09e commit af28307
Show file tree
Hide file tree
Showing 11 changed files with 590 additions and 200 deletions.
29 changes: 29 additions & 0 deletions Manifest.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,24 @@ uuid = "56f22d72-fd6d-98f1-02f0-08ddc0907c33"
[[deps.Base64]]
uuid = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f"

[[deps.CSTParser]]
deps = ["Tokenize"]
git-tree-sha1 = "b66abc140f8b90a1d6bc7bfad5c80070f8c1ddc6"
uuid = "00ebfdb7-1f24-5e51-bd34-a7502290713f"
version = "3.3.3"

[[deps.CodeTracking]]
deps = ["InteractiveUtils", "UUIDs"]
git-tree-sha1 = "9fb640864691a0936f94f89150711c36072b0e8f"
uuid = "da1fd8a2-8d9e-5ec2-8556-3022fb5608a2"
version = "1.0.8"

[[deps.CommonMark]]
deps = ["Crayons", "JSON", "URIs"]
git-tree-sha1 = "4cd7063c9bdebdbd55ede1af70f3c2f48fab4215"
uuid = "a80b9123-70ca-4bc0-993e-6e3bcb318db6"
version = "0.8.6"

[[deps.CommonSolve]]
git-tree-sha1 = "68a0743f578349ada8bc911a5cbd5a2ef6ed6d1f"
uuid = "38540f10-b2f7-11e9-35d8-d573e4eb0ff2"
Expand Down Expand Up @@ -132,12 +144,24 @@ git-tree-sha1 = "a3f24677c21f5bbe9d2a714f95dcd58337fb2856"
uuid = "82899510-4779-5014-852e-03e436cf321d"
version = "1.0.0"

[[deps.JSON]]
deps = ["Dates", "Mmap", "Parsers", "Unicode"]
git-tree-sha1 = "3c837543ddb02250ef42f4738347454f95079d4e"
uuid = "682c06a0-de6a-54ab-a142-c8b1cf79cde6"
version = "0.21.3"

[[deps.JSON3]]
deps = ["Dates", "Mmap", "Parsers", "StructTypes", "UUIDs"]
git-tree-sha1 = "8c1f668b24d999fb47baf80436194fdccec65ad2"
uuid = "0f8b85d8-7281-11e9-16c2-39a750bddbf1"
version = "1.9.4"

[[deps.JuliaFormatter]]
deps = ["CSTParser", "CommonMark", "DataStructures", "Pkg", "Tokenize"]
git-tree-sha1 = "64613299ded610bc40afeea529918d4baf3cf4e5"
uuid = "98e50ef6-434e-11e9-1051-2b60c6c9e899"
version = "0.22.10"

[[deps.JuliaInterpreter]]
deps = ["CodeTracking", "InteractiveUtils", "Random", "UUIDs"]
git-tree-sha1 = "9c43a2eb47147a8776ca2ba489f15a9f6f2906f8"
Expand Down Expand Up @@ -340,6 +364,11 @@ uuid = "a4e569a6-e804-4fa4-b0f3-eef7a1d5b13e"
deps = ["InteractiveUtils", "Logging", "Random", "Serialization"]
uuid = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[[deps.Tokenize]]
git-tree-sha1 = "0952c9cee34988092d73a5708780b3917166a0dd"
uuid = "0796e94c-ce3b-5d07-9a54-7f471281c624"
version = "0.5.21"

[[deps.URIs]]
git-tree-sha1 = "97bbe755a53fe859669cd907f2d96aee8d2c1355"
uuid = "5c2747f8-b7ea-4ff2-ba2e-563bfd36b1d4"
Expand Down
1 change: 1 addition & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ version = "1.2.0"
DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0"
Formatting = "59287772-0a20-5a39-b81b-1366585eb4c0"
GraphQLClient = "09d831e3-9c21-47a9-bfd8-076871817219"
JuliaFormatter = "98e50ef6-434e-11e9-1051-2b60c6c9e899"
Revise = "295af30f-e4ad-537b-8983-00126c2a3abe"
Roots = "f2b01f46-fcfa-551c-844a-d8ac1e96c665"

Expand Down
101 changes: 75 additions & 26 deletions src/AllocationOpt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ using Formatting

export optimize_indexer


include("exceptions.jl")
include("graphrepository.jl")
include("data.jl")
Expand All @@ -24,14 +23,14 @@ You can either specify `whitelist`, `blacklist`, or neither (setting both to `no
The optimizer will return a Julia DataFrame.
"""
#TODO: We might remove this since it is not being used
function optimize_indexer(;
id::String,
whitelist::Union{Nothing,Vector{String}},
blacklist::Union{Nothing,Vector{String}},
)
url = "https://api.thegraph.com/subgraphs/name/graphprotocol/graph-network-mainnet"
repository = snapshot(; url=url, indexer_query=nothing, subgraph_query=nothing)
network = network_issuance(; url=url, network_id=nothing, network_query=nothing)

repository, network = snapshot()

alloc, filtered = optimize(id, repository, whitelist, blacklist)

Expand All @@ -40,31 +39,44 @@ function optimize_indexer(;
)
df[!, "Subgraph Signal"] = map(x -> x.signal, filtered.subgraphs)
df[!, "Subgraph Indexing Reward"] = subgraph_rewards(filtered, network, alloc_lifetime)
df[!, "Estimated to Indexer"] = indexer_subgraph_rewards(filtered, network, alloc, alloc_lifetime)
df[!, "Estimated to Indexer"] = indexer_subgraph_rewards(
filtered, network, alloc, alloc_lifetime
)
#TODO: incorporate indexer reward cut to account for reward efficiency, indexing indexer rewards, and indexing delegator rewards
df[!, "Subgraph Indexing Reward to Indexer"] = map(x -> x.signal, filtered.subgraphs)

return df
end


"""
optimize_indexer(;id::String, grtgas::Float64, alloc_lifetime::Int64,
whitelist::Union{Nothing,Vector{String}}, blacklist::Union{Nothing,Vector{String}},
alloc_lifetime_threshold::Int64, preference_ratio::Float64)::DataFrame
# Arguments
- `id::String`: indexer address
- `grtgas::Float64`: the price you would want pay for each allocation transaction (grtgas * 1 for open, grtgas * 1 for close, grtgas * 0.3 for claim). Higher setting will lead to smaller set of allocations.
- `alloc_lifetime::Int64`: the frequency to renew a specific allocation. Smaller alloction lifetime allows less time to accumulate indexing rewards, thus lead to smaller set of allocations.
- `whitelist::Union{Nothing,Vector{String}}`: when set, optimizer will only consider these subgraphs (Qm...), and you cannot set blacklist.
- `blacklist::Union{Nothing,Vector{String}}`: when set, optimizer will not consider these subgraphs (Qm...), and you cannot set whitelist.
- `alloc_lifetime_threshold::Int64`: determine which current allocations should be re-considered during optimization. With higher threshold, the optimizer blacklist more subgrpahs. If you set this to 0, all current allocations will be considered in optimization.
- `preference_ratio::Float64`: the ratio between reallocating or keep open the current allocations. If you set to 1.0, optimizer simply takes the higher reward. If you set this to >1.0, you prefer to reallocate, if you set this to <1.0, you prefer to keep current allocation until it expires.
"""
function optimize_indexer(;
id::String,
grtgas::Float64,
alloc_lifetime::Int64,
whitelist::Union{Nothing,Vector{String}},
blacklist::Union{Nothing,Vector{String}},
alloc_lifetime_threshold::Int64,
preference_threshold::Float64,
preference_ratio::Float64,
)
if (alloc_lifetime_threshold <= 0)
optimize_indexer(; id, grtgas, alloc_lifetime, whitelist, blacklist)
throw("alloc_lifetime_threshold minimum value is 1, please change it.")
end

# Fetch data
url = "https://api.thegraph.com/subgraphs/name/graphprotocol/graph-network-mainnet"
repository = snapshot(; url=url, indexer_query=nothing, subgraph_query=nothing)
network = network_issuance(; url=url, network_id=nothing, network_query=nothing)
repository, network = snapshot()
indexer::Indexer = repository.indexers[findfirst(x -> x.id == id, repository.indexers)]

# Do not consider any allocations younger than alloc_lifetime_threshold
Expand All @@ -76,31 +88,68 @@ function optimize_indexer(;
end

# Optimize and create summary
alloc, filtered = optimize(id, repository, grtgas, network, alloc_lifetime, preference_threshold, whitelist, blacklist)
alloc_list = filter(a -> a.amount != 0, map(alloc_id -> Allocation(alloc_id, alloc[alloc_id], network.current_epoch), collect(keys(alloc))))
actions = create_actions(id, filtered, repository, network, alloc_list, alloc_lifetime, grtgas, preference_threshold, young_list)
alloc, filtered = optimize(
id,
repository,
grtgas,
network,
alloc_lifetime,
preference_ratio,
whitelist,
blacklist,
)
alloc_list = filter(
a -> a.amount != 0,
map(
alloc_id -> Allocation(alloc_id, alloc[alloc_id], network.current_epoch),
collect(keys(alloc)),
),
)
actions = create_actions(
id,
filtered,
repository,
network,
alloc_list,
alloc_lifetime,
grtgas,
preference_ratio,
young_list,
)

df = DataFrame(
"Subgraph ID" => map(a -> a.id, alloc_list), "Allocation in GRT" => map(a -> a.amount, alloc_list)
"Subgraph ID" => map(a -> a.id, alloc_list),
"Allocation in GRT" => map(a -> a.amount, alloc_list),
)
df[!, "Subgraph Signal"] = map(
x -> x.signal, filter(sg -> sg.id in map(a -> a.id, alloc_list), filtered.subgraphs)
)
df[!, "Indexing Reward"] = indexer_subgraph_rewards(
filtered, network, alloc_list, alloc_lifetime
)
df[!, "Subgraph Signal"] = map(x -> x.signal, filter(sg -> sg.id in map(a->a.id,alloc_list), filtered.subgraphs))
df[!, "Indexing Reward"] = indexer_subgraph_rewards(filtered, network, alloc_list, alloc_lifetime)
# 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") , alloc_list)
df[!, "Action"] = map(
a -> a in actions[2] ? "Open" : (a in actions[3] ? "Reallocate" : "Do not open"),
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

function print_summary(indexer, df, alloc_list, close_actions, alloc_lifetime)
println("""- brief summary -
indexer: $(indexer.id) , available_stake: $(indexer.stake + indexer.delegation)
use allocation lifetime: $(alloc_lifetime), number of allocations: $(length(alloc_list))
dataframe: $(df)
""")
println(
"""- brief summary -
indexer: $(indexer.id) , available_stake: $(indexer.stake + indexer.delegation)
use allocation lifetime: $(alloc_lifetime), number of allocations: $(length(alloc_list))
dataframe: $(df)
""",
)
for a in alloc_list
println("graph indexer rules set $(a.id) decisionBasis always allocationLifetime $(alloc_lifetime) allocationAmount $(format(a.amount))")
println(
"graph indexer rules set $(a.id) decisionBasis always allocationAmount $(format(a.amount))",
)
end
for a in close_actions
println("graph indexer rules stop ", a.id)
Expand Down
Loading

0 comments on commit af28307

Please sign in to comment.