Skip to content

Commit 8d750de

Browse files
authored
Merge pull request #564 from tonkeeper/extra-currency
Extra currency
2 parents 1c9c1ee + 8d0139b commit 8d750de

18 files changed

+1254
-138
lines changed

api/openapi.json

+99
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"
@@ -1057,6 +1063,9 @@
10571063
"ElectionsRecoverStake": {
10581064
"$ref": "#/components/schemas/ElectionsRecoverStakeAction"
10591065
},
1066+
"ExtraCurrencyTransfer": {
1067+
"$ref": "#/components/schemas/ExtraCurrencyTransferAction"
1068+
},
10601069
"InscriptionMint": {
10611070
"$ref": "#/components/schemas/InscriptionMintAction"
10621071
},
@@ -1121,6 +1130,7 @@
11211130
"type": {
11221131
"enum": [
11231132
"TonTransfer",
1133+
"ExtraCurrencyTransfer",
11241134
"JettonTransfer",
11251135
"JettonBurn",
11261136
"JettonMint",
@@ -3113,6 +3123,28 @@
31133123
],
31143124
"type": "object"
31153125
},
3126+
"EcPreview": {
3127+
"properties": {
3128+
"decimals": {
3129+
"example": 5,
3130+
"type": "integer"
3131+
},
3132+
"image": {
3133+
"example": "https://cache.tonapi.io/images/extra.jpg",
3134+
"type": "string"
3135+
},
3136+
"symbol": {
3137+
"example": "FMS",
3138+
"type": "string"
3139+
}
3140+
},
3141+
"required": [
3142+
"symbol",
3143+
"decimals",
3144+
"image"
3145+
],
3146+
"type": "object"
3147+
},
31163148
"ElectionsDepositStakeAction": {
31173149
"properties": {
31183150
"amount": {
@@ -3229,6 +3261,67 @@
32293261
],
32303262
"type": "object"
32313263
},
3264+
"ExtraCurrency": {
3265+
"properties": {
3266+
"amount": {
3267+
"example": "1000000000",
3268+
"type": "string",
3269+
"x-js-format": "bigint"
3270+
},
3271+
"decimals": {
3272+
"example": 5,
3273+
"type": "integer"
3274+
},
3275+
"id": {
3276+
"example": 239,
3277+
"format": "int32",
3278+
"type": "integer"
3279+
},
3280+
"name": {
3281+
"example": "FMS",
3282+
"type": "string"
3283+
}
3284+
},
3285+
"required": [
3286+
"id",
3287+
"amount",
3288+
"decimals"
3289+
],
3290+
"type": "object"
3291+
},
3292+
"ExtraCurrencyTransferAction": {
3293+
"properties": {
3294+
"amount": {
3295+
"description": "amount in quanta of tokens",
3296+
"example": "1000000000",
3297+
"type": "string",
3298+
"x-js-format": "bigint"
3299+
},
3300+
"comment": {
3301+
"example": "Hi! This is your salary. \nFrom accounting with love.",
3302+
"type": "string"
3303+
},
3304+
"currency": {
3305+
"$ref": "#/components/schemas/EcPreview"
3306+
},
3307+
"encrypted_comment": {
3308+
"$ref": "#/components/schemas/EncryptedComment"
3309+
},
3310+
"recipient": {
3311+
"$ref": "#/components/schemas/AccountAddress"
3312+
},
3313+
"sender": {
3314+
"$ref": "#/components/schemas/AccountAddress"
3315+
}
3316+
},
3317+
"required": [
3318+
"sender",
3319+
"recipient",
3320+
"amount",
3321+
"currency"
3322+
],
3323+
"type": "object"
3324+
},
32323325
"FoundAccounts": {
32333326
"properties": {
32343327
"addresses": {
@@ -4177,6 +4270,12 @@
41774270
"format": "int64",
41784271
"type": "integer",
41794272
"x-js-format": "bigint"
4273+
},
4274+
"value_extra": {
4275+
"items": {
4276+
"$ref": "#/components/schemas/ExtraCurrency"
4277+
},
4278+
"type": "array"
41804279
}
41814280
},
41824281
"required": [

api/openapi.yml

+72
Original file line numberDiff line numberDiff line change
@@ -3723,6 +3723,10 @@ components:
37233723
format: int64
37243724
x-js-format: bigint
37253725
example: 60000000
3726+
value_extra:
3727+
type: array
3728+
items:
3729+
$ref: '#/components/schemas/ExtraCurrency'
37263730
fwd_fee:
37273731
type: integer
37283732
format: int64
@@ -4600,6 +4604,10 @@ components:
46004604
format: int64
46014605
example: 123456789
46024606
x-js-format: bigint
4607+
extra_balance:
4608+
type: array
4609+
items:
4610+
$ref: '#/components/schemas/ExtraCurrency'
46034611
currencies_balance:
46044612
description: "{'USD': 1, 'IDR': 1000}"
46054613
type: object
@@ -5736,6 +5744,7 @@ components:
57365744
example: "TonTransfer"
57375745
enum:
57385746
- TonTransfer
5747+
- ExtraCurrencyTransfer
57395748
- JettonTransfer
57405749
- JettonBurn
57415750
- JettonMint
@@ -5764,6 +5773,8 @@ components:
57645773
- failed
57655774
TonTransfer:
57665775
$ref: '#/components/schemas/TonTransferAction'
5776+
ExtraCurrencyTransfer:
5777+
$ref: '#/components/schemas/ExtraCurrencyTransferAction'
57675778
ContractDeploy:
57685779
$ref: '#/components/schemas/ContractDeployAction'
57695780
JettonTransfer:
@@ -5834,6 +5845,46 @@ components:
58345845
$ref: '#/components/schemas/EncryptedComment'
58355846
refund:
58365847
$ref: '#/components/schemas/Refund'
5848+
EcPreview:
5849+
type: object
5850+
required:
5851+
- symbol
5852+
- decimals
5853+
- image
5854+
properties:
5855+
symbol:
5856+
type: string
5857+
example: FMS
5858+
decimals:
5859+
type: integer
5860+
example: 5
5861+
image:
5862+
type: string
5863+
example: https://cache.tonapi.io/images/extra.jpg
5864+
ExtraCurrencyTransferAction:
5865+
type: object
5866+
required:
5867+
- sender
5868+
- recipient
5869+
- amount
5870+
- currency
5871+
properties:
5872+
sender:
5873+
$ref: '#/components/schemas/AccountAddress'
5874+
recipient:
5875+
$ref: '#/components/schemas/AccountAddress'
5876+
amount:
5877+
type: string
5878+
x-js-format: bigint
5879+
example: "1000000000"
5880+
description: amount in quanta of tokens
5881+
comment:
5882+
type: string
5883+
example: "Hi! This is your salary. \nFrom accounting with love."
5884+
encrypted_comment:
5885+
$ref: '#/components/schemas/EncryptedComment'
5886+
currency:
5887+
$ref: '#/components/schemas/EcPreview'
58375888
SmartContractAction:
58385889
type: object
58395890
required:
@@ -7389,6 +7440,27 @@ components:
73897440
type: integer
73907441
format: int64
73917442
example: 1668436763
7443+
ExtraCurrency:
7444+
type: object
7445+
required:
7446+
- id
7447+
- amount
7448+
- decimals
7449+
properties:
7450+
id:
7451+
type: integer
7452+
example: 239
7453+
format: int32
7454+
amount:
7455+
type: string
7456+
x-js-format: bigint
7457+
example: "1000000000"
7458+
name:
7459+
type: string
7460+
example: FMS
7461+
decimals:
7462+
type: integer
7463+
example: 5
73927464

73937465
responses:
73947466
Error:

pkg/api/account_converters.go

+22-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package api
33
import (
44
"fmt"
55
imgGenerator "github.com/tonkeeper/opentonapi/pkg/image"
6+
"github.com/tonkeeper/opentonapi/pkg/references"
7+
"math/big"
68
"sort"
79

810
"github.com/tonkeeper/tongo/abi"
@@ -59,7 +61,8 @@ func convertToRawAccount(account *core.Account) (oas.BlockchainRawAccount, error
5961
if account.ExtraBalances != nil {
6062
balances := make(map[string]string, len(account.ExtraBalances))
6163
for key, value := range account.ExtraBalances {
62-
balances[fmt.Sprintf("%v", key)] = fmt.Sprintf("%v", value)
64+
v := big.Int(value)
65+
balances[fmt.Sprintf("%v", key)] = fmt.Sprintf("%v", v.String())
6366
}
6467
rawAccount.ExtraBalance = oas.NewOptBlockchainRawAccountExtraBalance(balances)
6568
}
@@ -72,6 +75,24 @@ func convertToRawAccount(account *core.Account) (oas.BlockchainRawAccount, error
7275
return rawAccount, nil
7376
}
7477

78+
func convertExtraCurrencies(extraBalances core.ExtraCurrencies) []oas.ExtraCurrency {
79+
res := make([]oas.ExtraCurrency, 0, len(extraBalances))
80+
for k, v := range extraBalances {
81+
amount := big.Int(v)
82+
meta := references.GetExtraCurrencyMeta(k)
83+
cur := oas.ExtraCurrency{
84+
ID: k,
85+
Amount: amount.String(),
86+
Decimals: meta.Decimals,
87+
}
88+
if meta.Name != "" {
89+
cur.Name.SetTo(meta.Name)
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/blockchain_converters.go

+3
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,9 @@ func convertMessage(m core.Message, book addressBook) oas.Message {
269269
value, _ := json.Marshal(m.DecodedBody.Value)
270270
msg.DecodedBody = g.ChangeJsonKeys(value, g.CamelToSnake)
271271
}
272+
if m.ValueExtra != nil {
273+
msg.ValueExtra = convertExtraCurrencies(m.ValueExtra)
274+
}
272275
return msg
273276
}
274277

pkg/api/event_converters.go

+27
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,31 @@ func (h *Handler) convertActionTonTransfer(t *bath.TonTransferAction, acceptLang
151151
return action, simplePreview
152152
}
153153

154+
func (h *Handler) convertActionExtraCurrencyTransfer(t *bath.ExtraCurrencyTransferAction, acceptLanguage string, viewer *tongo.AccountID) (oas.OptExtraCurrencyTransferAction, oas.ActionSimplePreview) {
155+
var action oas.OptExtraCurrencyTransferAction
156+
amount := big.Int(t.Amount)
157+
meta := references.GetExtraCurrencyMeta(t.CurrencyID)
158+
action.SetTo(oas.ExtraCurrencyTransferAction{
159+
Amount: amount.String(),
160+
Comment: g.Opt(t.Comment),
161+
Recipient: convertAccountAddress(t.Recipient, h.addressBook),
162+
Sender: convertAccountAddress(t.Sender, h.addressBook),
163+
EncryptedComment: convertEncryptedComment(t.EncryptedComment),
164+
Currency: oas.EcPreview{
165+
Symbol: meta.Symbol,
166+
Decimals: meta.Decimals,
167+
Image: meta.Image,
168+
},
169+
})
170+
simplePreview := oas.ActionSimplePreview{
171+
Name: "Extra Currency Transfer",
172+
Description: "", // TODO: add description
173+
Accounts: distinctAccounts(viewer, h.addressBook, &t.Sender, &t.Recipient),
174+
// TODO: add value
175+
}
176+
return action, simplePreview
177+
}
178+
154179
func (h *Handler) convertActionNftTransfer(t *bath.NftTransferAction, acceptLanguage string, viewer *tongo.AccountID) (oas.OptNftItemTransferAction, oas.ActionSimplePreview) {
155180
var action oas.OptNftItemTransferAction
156181
action.SetTo(oas.NftItemTransferAction{
@@ -428,6 +453,8 @@ func (h *Handler) convertAction(ctx context.Context, viewer *tongo.AccountID, a
428453
switch a.Type {
429454
case bath.TonTransfer:
430455
action.TonTransfer, action.SimplePreview = h.convertActionTonTransfer(a.TonTransfer, acceptLanguage.Value, viewer)
456+
case bath.ExtraCurrencyTransfer:
457+
action.ExtraCurrencyTransfer, action.SimplePreview = h.convertActionExtraCurrencyTransfer(a.ExtraCurrencyTransfer, acceptLanguage.Value, viewer)
431458
case bath.NftItemTransfer:
432459
action.NftItemTransfer, action.SimplePreview = h.convertActionNftTransfer(a.NftItemTransfer, acceptLanguage.Value, viewer)
433460
case bath.JettonTransfer:

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

0 commit comments

Comments
 (0)