Skip to content

Commit

Permalink
Merge pull request #221 from tonkeeper/wallet_jetton_price
Browse files Browse the repository at this point in the history
add field to get jetton wallet ballance
  • Loading branch information
mr-tron authored Oct 15, 2023
2 parents 9ec6d92 + 92492ec commit d8852a0
Show file tree
Hide file tree
Showing 12 changed files with 315 additions and 103 deletions.
17 changes: 17 additions & 0 deletions api/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,16 @@
"type": "integer"
}
},
"currenciesQuery": {
"description": "accept ton and all possible fiat currencies, separated by commas",
"in": "query",
"name": "currencies",
"required": false,
"schema": {
"example": "ton,usd,rub",
"type": "string"
}
},
"domainFilterQuery": {
"description": "domain filter for current auctions \"ton\" or \"t.me\"",
"in": "query",
Expand Down Expand Up @@ -1931,6 +1941,10 @@
"jetton": {
"$ref": "#/components/schemas/JettonPreview"
},
"price": {
"additionalProperties": true,
"example": {}
},
"wallet_address": {
"$ref": "#/components/schemas/AccountAddress"
}
Expand Down Expand Up @@ -3994,6 +4008,9 @@
"parameters": [
{
"$ref": "#/components/parameters/accountIDParameter"
},
{
"$ref": "#/components/parameters/currenciesQuery"
}
],
"responses": {
Expand Down
12 changes: 12 additions & 0 deletions api/openapi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,7 @@ paths:
- Accounts
parameters:
- $ref: '#/components/parameters/accountIDParameter'
- $ref: '#/components/parameters/currenciesQuery'
responses:
'200':
description: account jettons balances
Expand Down Expand Up @@ -2340,6 +2341,14 @@ components:
schema:
type: boolean
default: false
currenciesQuery:
in: query
name: currencies
description: accept ton and all possible fiat currencies, separated by commas
required: false
schema:
type: string
example: ton,usd,rub

requestBodies:
MethodParameters:
Expand Down Expand Up @@ -3277,6 +3286,9 @@ components:
balance:
type: string
example: 597968399
price:
additionalProperties: true
example: { }
wallet_address:
$ref: '#/components/schemas/AccountAddress'
jetton:
Expand Down
21 changes: 21 additions & 0 deletions client/oas_client_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 24 additions & 6 deletions client/oas_json_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions client/oas_parameters_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions client/oas_schemas_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 36 additions & 4 deletions pkg/api/jetton_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,23 @@ package api

import (
"context"
"encoding/json"
"errors"
"fmt"
"math/big"
"net/http"
"strings"

"github.com/tonkeeper/opentonapi/pkg/bath"
"github.com/tonkeeper/tongo"
"github.com/tonkeeper/tongo/liteapi"

"github.com/tonkeeper/opentonapi/pkg/core"
"github.com/tonkeeper/opentonapi/pkg/oas"
"github.com/tonkeeper/tongo"
"github.com/tonkeeper/tongo/liteapi"
"github.com/tonkeeper/tongo/ton"
)

func (h *Handler) GetAccountJettonsBalances(ctx context.Context, params oas.GetAccountJettonsBalancesParams) (*oas.JettonsBalances, error) {
accountID, err := tongo.ParseAccountID(params.AccountID)
accountID, err := ton.ParseAccountID(params.AccountID)
if err != nil {
return nil, toError(http.StatusBadRequest, err)
}
Expand All @@ -30,11 +32,41 @@ func (h *Handler) GetAccountJettonsBalances(ctx context.Context, params oas.GetA
var balances = oas.JettonsBalances{
Balances: make([]oas.JettonBalance, 0, len(wallets)),
}
var currencies []string
if params.Currencies.IsSet() {
params.Currencies.Value = strings.TrimSpace(params.Currencies.Value)
currencies = strings.Split(params.Currencies.Value, ",")
if len(currencies) > 100 {
return nil, toError(http.StatusBadRequest, fmt.Errorf("max params limit is 100 items"))
}
for idx, currency := range currencies {
if jetton, err := ton.ParseAccountID(currency); err == nil {
currency = jetton.ToRaw()
} else {
currency = strings.ToUpper(currency)
}
currencies[idx] = currency
}
}
todayRates, yesterdayRates, weekRates, monthRates, _ := h.getRates()
for _, wallet := range wallets {
jettonBalance := oas.JettonBalance{
Balance: wallet.Balance.String(),
WalletAddress: convertAccountAddress(wallet.Address, h.addressBook),
}
rates := make(map[string]TokenRates)
for _, currency := range currencies {
if rates, err = convertRates(rates, wallet.JettonAddress.ToRaw(), currency, todayRates, yesterdayRates, weekRates, monthRates); err != nil {
continue
}
}
price := rates[wallet.JettonAddress.ToRaw()]
if len(rates) > 0 && len(price.Prices) > 0 {
jettonBalance.Price, err = json.Marshal(price)
if err != nil {
return nil, toError(http.StatusInternalServerError, err)
}
}
meta, err := h.storage.GetJettonMasterMetadata(ctx, wallet.JettonAddress)
if err != nil && err.Error() == "not enough refs" {
// happens when metadata is broken, for example.
Expand Down
Loading

0 comments on commit d8852a0

Please sign in to comment.