Skip to content

Commit

Permalink
Merge pull request #151 from gnoswap-labs/GSW-838-feat-add-query-meta…
Browse files Browse the repository at this point in the history
…data-in-every-api-function

GSW-838 feat add query metadata in every api function #2
  • Loading branch information
notJoon authored Feb 5, 2024
2 parents 76b1d13 + b05145f commit 94bb782
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 20 deletions.
39 changes: 20 additions & 19 deletions router/_RPC_api.gno
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// EXTERNAL API
package router

import (
Expand All @@ -11,32 +10,31 @@ import (
"gno.land/p/demo/ufmt"
)

type ApiQueryBase struct {
type TokenPathPrice struct {
Path string `json:"path"`
Price bigint `json:"price"`
}

type ResponseQueryBase struct {
Height int64 `json:"height"`
Timestamp int64 `json:"timestamp"`
}

type ResponseGetRatiosFromBase struct {
Stat ApiQueryBase `json:"stat"`
Response struct {
Data []map[string]bigint `json:"data"`
} `json:"response"`
Stat ResponseQueryBase `json:"stat"`
Response []TokenPathPrice `json:"response"`
}

func ApiGetRatiosFromBase() string {
qb := ApiQueryBase{
qb := ResponseQueryBase{
Height: GetHeight(),
Timestamp: GetTimestamp(),
}

ratios := getRatiosFromBase(3)
r := ResponseGetRatiosFromBase{
Stat: qb,
Response: struct {
Data []map[string]bigint `json:"data"`
}{
Data: ratios,
},
Stat: qb,
Response: ratios,
}

rr, err := json.Marshal(r)
Expand All @@ -47,7 +45,7 @@ func ApiGetRatiosFromBase() string {
return string(rr)
}

func getRatiosFromBase(maxHops int) []map[string]bigint {
func getRatiosFromBase(maxHops int) []TokenPathPrice {
tokenPrice := make(map[string]bigint, 0)

// BASE
Expand Down Expand Up @@ -92,9 +90,12 @@ func getRatiosFromBase(maxHops int) []map[string]bigint {
// TOKEN ENDS
}

var tokenPrices []map[string]bigint
tokenPrices := []TokenPathPrice{}
for token, price := range tokenPrice {
tokenPrices = append(tokenPrices, map[string]bigint{token: price})
tokenPrices = append(tokenPrices, TokenPathPrice{
Path: token,
Price: price,
})
}

return tokenPrices
Expand Down Expand Up @@ -185,19 +186,19 @@ func makePoolPath(poolPath string, poolIndex int) string {

func poolPathWithFeeDivide(poolPath string) (string, string, int) {
poolPathSplit := strings.Split(poolPath, ":")
require(len(poolPathSplit) == 3, ufmt.Sprintf("[ROUTER] util.gno__poolPathWithFeeDivide() || len(poolPathSplit) != 3, poolPath: %s", poolPath))
require(len(poolPathSplit) == 3, ufmt.Sprintf("[ROUTER] _RPC_api.gno__poolPathWithFeeDivide() || len(poolPathSplit) != 3, poolPath: %s", poolPath))

feeInt, err := strconv.Atoi(poolPathSplit[2])
if err != nil {
panic(ufmt.Sprintf("[ROUTER] util.gno__poolPathWithFeeDivide() || cannot convert fee(%s) to int", poolPathSplit[2]))
panic(ufmt.Sprintf("[ROUTER] _RPC_api.gno__poolPathWithFeeDivide() || cannot convert fee(%s) to int", poolPathSplit[2]))
}

return poolPathSplit[0], poolPathSplit[1], feeInt
}

func singlePoolPathWithFeeDivide(poolPath string) (string, string) {
singlePoolPathSplit := strings.Split(poolPath, ":")
require(len(singlePoolPathSplit) == 2, ufmt.Sprintf("[ROUTER] util.gno__singlePoolPathWithFeeDivide || len(singlePoolPathSplit) != 2, poolPath: %s", poolPath))
require(len(singlePoolPathSplit) == 2, ufmt.Sprintf("[ROUTER] _RPC_api.gno__singlePoolPathWithFeeDivide || len(singlePoolPathSplit) != 2, poolPath: %s", poolPath))

return singlePoolPathSplit[0], singlePoolPathSplit[1]
}
Expand Down
2 changes: 1 addition & 1 deletion router/_TEST_router_api_getter_test.gno
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func TestPositionMint(t *testing.T) {
func TestApiGetRatiosFromBase(t *testing.T) {
jsonOutput := ApiGetRatiosFromBase()
jsonStr := gjson.Parse(jsonOutput)
jsonArr := jsonStr.Get("response.data").Array()
jsonArr := jsonStr.Get("response").Array()
shouldEQ(t, len(jsonArr), 4)

shouldEQ(t, jsonArr[0].String(), "{\"gno.land/r/demo/wugnot\":79228162514264337593543950336}") // 1
Expand Down

0 comments on commit 94bb782

Please sign in to comment.