From 7b8342de263b7356b1e1fd061332266af8ee8205 Mon Sep 17 00:00:00 2001 From: hopeyen Date: Mon, 20 Mar 2023 15:15:44 -0500 Subject: [PATCH] feat: execute mode --- src/AllocationOpt.jl | 9 +++++-- src/reporting.jl | 57 +++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 63 insertions(+), 3 deletions(-) diff --git a/src/AllocationOpt.jl b/src/AllocationOpt.jl index 13fb554..bc1a13c 100644 --- a/src/AllocationOpt.jl +++ b/src/AllocationOpt.jl @@ -76,6 +76,7 @@ function main(config::Dict) g = config["gas"] # Get optimal values + # TODO: Handle pinned stake xs, nonzeros, profitmatrix = optimize(Ω, ψ, σ, K, Φ, Ψ, g) # Write the result values @@ -88,13 +89,17 @@ function main(config::Dict) nreport = min(config["num_reported_options"], length(popts)) # Create JSON string - strategies = strategydict.(popts, Ref(xs), Ref(nonzeros), Ref(fs), Ref(profitmatrix)) + strategies = strategydict.( + popts[1:nreport], Ref(xs), Ref(nonzeros), Ref(fs), Ref(profitmatrix) + ) reportdata = JSON.json(Dict("strategies" => strategies)) # Write JSON string to file writejson(reportdata, config) - # Use config to see if actionqueue or rules with the top profit batch + # Use config for using actionqueue or rules with the top profit batch + ix = first(popts)[:index] + execute(a, ix, fs, xs, ps, config) return nothing end diff --git a/src/reporting.jl b/src/reporting.jl index 01f50f2..30e1b52 100644 --- a/src/reporting.jl +++ b/src/reporting.jl @@ -432,7 +432,9 @@ function reallocate( t::FlexTable, config::AbstractDict, ) - ti = SAC.innerjoin(getproperty(:ipfshash), getproperty(Symbol("subgraphDeployment.ipfsHash")), t, a) + ti = SAC.innerjoin( + getproperty(:ipfshash), getproperty(Symbol("subgraphDeployment.ipfsHash")), t, a + ) actions::Vector{Dict{String, Any}} = map( r -> Dict( @@ -500,3 +502,56 @@ function allocate( return actions end + +""" + execute( + a::FlexTable, + ix::Integer, + s::FlexTable, + xs::AbstractMatrix{T}, + ps::AbstractMatrix{T}, + config::AbstractDict + ) where {T<:Real} + +Execute the actions picked by the optimiser. + +```julia +julia> using AllocationOpt +julia> using TheGraphData +julia> a = flextable([ + Dict("subgraphDeployment.ipfsHash" => "Qma", "id" => "0xa") + ]) +julia> xs = [[2.5 5.0]; [2.5 0.0]] +julia> ps = [[3.0 5.0]; [3.0 0.0]] +julia> s = flextable([ + Dict("stakedTokens" => "1", "signalledTokens" => "0", "ipfsHash" => "Qma"), + Dict("stakedTokens" => "2", "signalledTokens" => "0", "ipfsHash" => "Qmb"), + ]) +julia> config = Dict("execution_mode" => "none") +julia> ix = 1 +julia> AllocationOpt.execute(a, ix, s, xs, ps, config) +``` +""" +function execute( + a::FlexTable, + ix::Integer, + s::FlexTable, + xs::AbstractMatrix{T}, + ps::AbstractMatrix{T}, + config::AbstractDict +) where {T<:Real} + # Construct t + t = reportingtable(s, xs, ps, ix) + + mode = Val(Symbol(config["execution_mode"])) + + indexerurlclient(mode, config) + _ = unallocate(mode, a, t, config) + _ = reallocate(mode, a, t, config) + _ = allocate(mode, a, t, config) + + return nothing +end + +indexerurlclient(::Val{:actionqueue}, config::AbstractDict) = client!(config["indexer_url"]) +indexerurlclient(::Any, config::AbstractDict) = nothing