Skip to content

Commit 4b7f55c

Browse files
committedDec 10, 2024
extra balances
1 parent 1c9c1ee commit 4b7f55c

8 files changed

+356
-20
lines changed
 

‎api/openapi.json

+34
Original file line numberDiff line numberDiff line change
@@ -735,6 +735,12 @@
735735
"example": {},
736736
"type": "object"
737737
},
738+
"extra_balance": {
739+
"items": {
740+
"$ref": "#/components/schemas/ExtraCurrency"
741+
},
742+
"type": "array"
743+
},
738744
"get_methods": {
739745
"example": [
740746
"get_item_data"
@@ -3229,6 +3235,34 @@
32293235
],
32303236
"type": "object"
32313237
},
3238+
"ExtraCurrency": {
3239+
"properties": {
3240+
"amount": {
3241+
"example": "1000000000",
3242+
"type": "string",
3243+
"x-js-format": "bigint"
3244+
},
3245+
"decimals": {
3246+
"example": 5,
3247+
"type": "integer"
3248+
},
3249+
"id": {
3250+
"example": 239,
3251+
"format": "int32",
3252+
"type": "integer"
3253+
},
3254+
"name": {
3255+
"example": "FMS",
3256+
"type": "string"
3257+
}
3258+
},
3259+
"required": [
3260+
"id",
3261+
"amount",
3262+
"decimals"
3263+
],
3264+
"type": "object"
3265+
},
32323266
"FoundAccounts": {
32333267
"properties": {
32343268
"addresses": {

‎api/openapi.yml

+25
Original file line numberDiff line numberDiff line change
@@ -4600,6 +4600,10 @@ components:
46004600
format: int64
46014601
example: 123456789
46024602
x-js-format: bigint
4603+
extra_balance:
4604+
type: array
4605+
items:
4606+
$ref: '#/components/schemas/ExtraCurrency'
46034607
currencies_balance:
46044608
description: "{'USD': 1, 'IDR': 1000}"
46054609
type: object
@@ -7389,6 +7393,27 @@ components:
73897393
type: integer
73907394
format: int64
73917395
example: 1668436763
7396+
ExtraCurrency:
7397+
type: object
7398+
required:
7399+
- id
7400+
- amount
7401+
- decimals
7402+
properties:
7403+
id:
7404+
type: integer
7405+
example: 239
7406+
format: int32
7407+
amount:
7408+
type: string
7409+
x-js-format: bigint
7410+
example: "1000000000"
7411+
name:
7412+
type: string
7413+
example: FMS
7414+
decimals:
7415+
type: integer
7416+
example: 5
73927417

73937418
responses:
73947419
Error:

‎pkg/api/account_converters.go

+21
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ package api
22

33
import (
44
"fmt"
5+
"github.com/shopspring/decimal"
56
imgGenerator "github.com/tonkeeper/opentonapi/pkg/image"
7+
"github.com/tonkeeper/opentonapi/pkg/references"
68
"sort"
79

810
"github.com/tonkeeper/tongo/abi"
@@ -57,6 +59,7 @@ func convertToRawAccount(account *core.Account) (oas.BlockchainRawAccount, error
5759
}
5860
}
5961
if account.ExtraBalances != nil {
62+
// TODO: use int instead of uint?
6063
balances := make(map[string]string, len(account.ExtraBalances))
6164
for key, value := range account.ExtraBalances {
6265
balances[fmt.Sprintf("%v", key)] = fmt.Sprintf("%v", value)
@@ -72,6 +75,24 @@ func convertToRawAccount(account *core.Account) (oas.BlockchainRawAccount, error
7275
return rawAccount, nil
7376
}
7477

78+
func convertExtraCurrencies(extraBalances map[uint32]decimal.Decimal) []oas.ExtraCurrency {
79+
res := make([]oas.ExtraCurrency, 0, len(extraBalances))
80+
for k, v := range extraBalances {
81+
cur := oas.ExtraCurrency{
82+
ID: int32(k),
83+
Amount: v.String(),
84+
Decimals: 9, // TODO: or replace with default const
85+
}
86+
meta, ok := references.ExtraCurrencies[int32(k)]
87+
if ok {
88+
cur.Name.SetTo(meta.Name)
89+
cur.Decimals = meta.Decimals
90+
}
91+
res = append(res, cur)
92+
}
93+
return res
94+
}
95+
7596
func convertToAccount(account *core.Account, ab *addressbook.KnownAddress, state chainState) oas.Account {
7697
acc := oas.Account{
7798
Address: account.AccountAddress.ToRaw(),

‎pkg/api/account_handlers.go

+6
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,9 @@ func (h *Handler) GetAccount(ctx context.Context, params oas.GetAccountParams) (
7272
} else {
7373
res = convertToAccount(rawAccount, nil, h.state)
7474
}
75+
if rawAccount.ExtraBalances != nil {
76+
res.ExtraBalance = convertExtraCurrencies(rawAccount.ExtraBalances)
77+
}
7578
return &res, nil
7679
}
7780

@@ -114,6 +117,9 @@ func (h *Handler) GetAccounts(ctx context.Context, request oas.OptGetAccountsReq
114117
} else {
115118
res = convertToAccount(account, nil, h.state)
116119
}
120+
if account.ExtraBalances != nil {
121+
res.ExtraBalance = convertExtraCurrencies(account.ExtraBalances)
122+
}
117123
results[account.AccountAddress] = res
118124
}
119125
// if we don't find an account, we return it with "nonexist" status

‎pkg/api/wallet_handlers.go

+3
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ func (h *Handler) GetWalletsByPublicKey(ctx context.Context, params oas.GetWalle
3838
} else {
3939
res = convertToAccount(account, nil, h.state)
4040
}
41+
if account.ExtraBalances != nil {
42+
res.ExtraBalance = convertExtraCurrencies(account.ExtraBalances)
43+
}
4144
results = append(results, res)
4245
}
4346
return &oas.Accounts{Accounts: results}, nil

‎pkg/oas/oas_json_gen.go

+193-18
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎pkg/oas/oas_schemas_gen.go

+61-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎pkg/references/extra_currency.go

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package references
2+
3+
type ExtraCurrencyMeta struct {
4+
Name string
5+
Decimals int
6+
}
7+
8+
var ExtraCurrencies = map[int32]ExtraCurrencyMeta{
9+
239: {
10+
Name: "FMS",
11+
Decimals: 5,
12+
},
13+
}

0 commit comments

Comments
 (0)
Please sign in to comment.