Skip to content

Commit

Permalink
feat: Don't hold onto allocations table
Browse files Browse the repository at this point in the history
Signed-off-by: Anirudh <[email protected]>
  • Loading branch information
anirudh2 committed Mar 13, 2023
1 parent 047f9f6 commit 8a4c86d
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 23 deletions.
33 changes: 18 additions & 15 deletions src/data.jl
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,7 @@ julia> paths = AllocationOpt.path(path)
"""
function savenames(p::AbstractString)
return Base.Generator(
x -> joinpath(p, x),
("indexer.csv", "allocation.csv", "subgraph.csv", "network.csv"),
x -> joinpath(p, x), ("indexer.csv", "subgraph.csv", "network.csv")
)
end

Expand All @@ -129,7 +128,7 @@ Read the CSV files from `f` and return the tables from those files.
```julia
julia> using AllocationOpt
julia> i, a, s, n = AllocationOpt.read("myreaddir", config("verbose" => true))
julia> i, s, n = AllocationOpt.read("myreaddir", config("verbose" => true))
```
"""
function read(f::AbstractString, config::AbstractDict)
Expand All @@ -138,8 +137,8 @@ function read(f::AbstractString, config::AbstractDict)
config["verbose"] && @info "Reading data from $p"
push!(d, flextable(@mock(TheGraphData.read(p))))
end
i, a, s, n = d
return i, a, s, n
i, s, n = d
return i, s, n
end

"""
Expand All @@ -153,7 +152,7 @@ julia> config = Dict(
"verbose" => true,
"network_subgraph_endpoint" => "https://api.thegraph.com/subgraphs/name/graphprotocol/graph-network-mainnet",
)
julia> i, a, s, n = AllocationOpt.read(nothing, config)
julia> i, s, n = AllocationOpt.read(nothing, config)
```
"""
function read(::Nothing, config::AbstractDict{String,Any})
Expand All @@ -165,9 +164,13 @@ function read(::Nothing, config::AbstractDict{String,Any})
s = flextable(@mock(paginated_query(squery()...)))
n = flextable(@mock(query(nquery()...)))

# Convert string types to GRT
i, a, s, n = correcttypes!(i, a, s, n)

return i, a, s, n
# Subtract indexer allocations from total allocation on subgraph
a, s = subtractindexer!(a, s)

return i, s, n
end

"""
Expand All @@ -181,7 +184,7 @@ directory. Otherwise, this will query the specified `"network_subgraph_endpoint"
```julia
julia> using AllocationOpt
julia> config = Dict("verbose" => false, "readdir" => "mydatadir")
julia> i, a, s, n = AllocationOpt.read(config) # Read data from CSVs
julia> i, s, n = AllocationOpt.read(config) # Read data from CSVs
```
```julia
Expand All @@ -191,13 +194,13 @@ julia> config = Dict(
"network_subgraph_endpoint" => "https://api.thegraph.com/subgraphs/name/graphprotocol/graph-network-mainnet",
"readdir" => nothing,
)
julia> i, a, s, n = AllocationOpt.read(config) # Query GQL endpoint
julia> i, s, n = AllocationOpt.read(config) # Query GQL endpoint
```
"""
function read(config::AbstractDict{String,Any})
readdir::Union{String,Nothing} = config["readdir"]
i, a, s, n = read(readdir, config)
return i, a, s, n
i, s, n = read(readdir, config)
return i, s, n
end

"""
Expand All @@ -214,14 +217,14 @@ julia> t = flextable([
Dict("ipfsHash" => "Qma", "signalledTokens" => "1"),
Dict("ipfsHash" => "Qmb", "signalledTokens" => "2"),
])
julia> i, a, s, n = repeat([t,], 4)
juila> AllocationOpt.write(i, a, s, n, config)
julia> i, s, n = repeat([t,], 4)
juila> AllocationOpt.write(i, s, n, config)
```
"""
function write(i::FlexTable, a::FlexTable, s::FlexTable, n::FlexTable, config::AbstractDict)
function write(i::FlexTable, s::FlexTable, n::FlexTable, config::AbstractDict)
writedir = config["writedir"]
ps = String[]
for (d, p) in zip((i, a, s, n), savenames(writedir))
for (d, p) in zip((i, s, n), savenames(writedir))
config["verbose"] && @info "Writing table to $p"
push!(ps, @mock(TheGraphData.write(p, d)))
end
Expand Down
13 changes: 5 additions & 8 deletions test/data.jl
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@
@testset "savenames" begin
paths = (
"mypath/indexer.csv",
"mypath/allocation.csv",
"mypath/subgraph.csv",
"mypath/network.csv",
)
Expand Down Expand Up @@ -195,7 +194,7 @@
@testset "from files" begin
config = Dict("verbose" => false, "readdir" => "")
apply(read_csv_success_patch) do
i, a, s, n = AllocationOpt.read(config)
i, s, n = AllocationOpt.read(config)
@test i.X == ["b", "c", "a", "c"]
end
end
Expand All @@ -209,11 +208,10 @@
)
apply(paginated_query_success_patch) do
apply(query_success_patch) do
i, a, s, n = AllocationOpt.read(config)
i, s, n = AllocationOpt.read(config)
@test i.stakedTokens == [1e-17, 2e-17]
@test s.signalledTokens == [1e-18, 2e-18]
@test s.stakedTokens == [1e-18, 2e-18]
@test a.allocatedTokens == [1e-18]
@test s.stakedTokens == [0, 2e-18]
@test n.totalTokensSignalled == [1e-16]
end
end
Expand All @@ -223,12 +221,11 @@
@testset "write" begin
config = Dict("verbose" => false, "writedir" => "tmp")
t = flextable([Dict("foo" => 1, "bar" => 2)])
i, a, s, n = repeat([t], 4)
i, s, n = repeat([t], 3)
apply(write_success_patch) do
ps = AllocationOpt.write(i, a, s, n, config)
ps = AllocationOpt.write(i, s, n, config)
@test ps == [
"tmp/indexer.csv",
"tmp/allocation.csv",
"tmp/subgraph.csv",
"tmp/network.csv",
]
Expand Down

0 comments on commit 8a4c86d

Please sign in to comment.