From 01ed4a2d0088e6f2af7d5e30ffd93705e490f5cd Mon Sep 17 00:00:00 2001 From: Anirudh Date: Mon, 13 Mar 2023 12:47:48 -0700 Subject: [PATCH] feat: Compute frozen stake Signed-off-by: Anirudh --- src/AllocationOpt.jl | 32 ++++++++++++++++++++++++++++++++ src/domain.jl | 21 +++++++++++++++++++++ test/domain.jl | 11 +++++++++++ 3 files changed, 64 insertions(+) diff --git a/src/AllocationOpt.jl b/src/AllocationOpt.jl index a4803ee..98b33d7 100644 --- a/src/AllocationOpt.jl +++ b/src/AllocationOpt.jl @@ -8,8 +8,40 @@ using TOML using TypedTables using TheGraphData +import SplitApplyCombine as SAC + include("configuration.jl") include("data.jl") include("domain.jl") +function julia_main()::Cint + try + main(ARGS) + catch + Base.invokelatest(Base.display_error, Base.catch_stack()) + return 1 + end + return 0 # if things finished successfully +end + +main(args::Vector{String}) = main(first(args)) + +function main(path::AbstractString) + # Read config and set defaults + config = path |> readconfig |> configuredefaults! + return main(config) +end + +function main(config::Dict) + # Read data + i, a, s, n = AllocationOpt.read(config) + + # Queried data has not yet been converted to GRT, so + # if this isn't data that we've queried, correct the + # types and write the data out to CSVs + isnothing(config["readdir"]) && write(i, a, s, n, config) + + return nothing +end + end diff --git a/src/domain.jl b/src/domain.jl index e2a4932..cd0cb93 100644 --- a/src/domain.jl +++ b/src/domain.jl @@ -160,3 +160,24 @@ julia> AllocationOpt.locked(Val(:indexer), x) ``` """ locked(::Val{:indexer}, x) = x.lockedTokens |> only + +""" + frozen(a::FlexTable, config::AbstractDict) + +The frozen stake of the indexer with allocations `a`. + +```julia +julia> using AllocationOpt +julia> using TheGraphData +julia> a = flextable([ + Dict("subgraphDeployment.ipfsHash" => "Qma", "allocatedTokens" => 5), + Dict("subgraphDeployment.ipfsHash" => "Qmb", "allocatedTokens" => 10), + ]) +julia> config = Dict("frozenlist" => ["Qma", "Qmb"]) +julia> AllocationOpt.frozen(a, config) +``` +""" +function frozen(a::FlexTable, config::AbstractDict) + frozenallocs = SAC.filterview(r -> ipfshash(Val(:allocation), r) ∈ config["frozenlist"], a) + return stake(Val(:allocation), frozenallocs) |> sum +end diff --git a/test/domain.jl b/test/domain.jl index 72ba12a..4469df0 100644 --- a/test/domain.jl +++ b/test/domain.jl @@ -32,4 +32,15 @@ end + @testset "frozen" begin + a = flextable([ + Dict("subgraphDeployment.ipfsHash" => "Qma", "allocatedTokens" => 5), + Dict("subgraphDeployment.ipfsHash" => "Qmb", "allocatedTokens" => 10), + ]) + config = Dict("frozenlist" => ["Qma", "Qmb"]) + @test AllocationOpt.frozen(a, config) == 15 + config = Dict("frozenlist" => ["Qmb"]) + @test AllocationOpt.frozen(a, config) == 10 + end + end