Skip to content

Commit

Permalink
Merge pull request #564 from tonkeeper/extra-currency
Browse files Browse the repository at this point in the history
Extra currency
  • Loading branch information
mr-tron authored Dec 11, 2024
2 parents 1c9c1ee + 8d0139b commit 8d750de
Show file tree
Hide file tree
Showing 18 changed files with 1,254 additions and 138 deletions.
99 changes: 99 additions & 0 deletions api/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -735,6 +735,12 @@
"example": {},
"type": "object"
},
"extra_balance": {
"items": {
"$ref": "#/components/schemas/ExtraCurrency"
},
"type": "array"
},
"get_methods": {
"example": [
"get_item_data"
Expand Down Expand Up @@ -1057,6 +1063,9 @@
"ElectionsRecoverStake": {
"$ref": "#/components/schemas/ElectionsRecoverStakeAction"
},
"ExtraCurrencyTransfer": {
"$ref": "#/components/schemas/ExtraCurrencyTransferAction"
},
"InscriptionMint": {
"$ref": "#/components/schemas/InscriptionMintAction"
},
Expand Down Expand Up @@ -1121,6 +1130,7 @@
"type": {
"enum": [
"TonTransfer",
"ExtraCurrencyTransfer",
"JettonTransfer",
"JettonBurn",
"JettonMint",
Expand Down Expand Up @@ -3113,6 +3123,28 @@
],
"type": "object"
},
"EcPreview": {
"properties": {
"decimals": {
"example": 5,
"type": "integer"
},
"image": {
"example": "https://cache.tonapi.io/images/extra.jpg",
"type": "string"
},
"symbol": {
"example": "FMS",
"type": "string"
}
},
"required": [
"symbol",
"decimals",
"image"
],
"type": "object"
},
"ElectionsDepositStakeAction": {
"properties": {
"amount": {
Expand Down Expand Up @@ -3229,6 +3261,67 @@
],
"type": "object"
},
"ExtraCurrency": {
"properties": {
"amount": {
"example": "1000000000",
"type": "string",
"x-js-format": "bigint"
},
"decimals": {
"example": 5,
"type": "integer"
},
"id": {
"example": 239,
"format": "int32",
"type": "integer"
},
"name": {
"example": "FMS",
"type": "string"
}
},
"required": [
"id",
"amount",
"decimals"
],
"type": "object"
},
"ExtraCurrencyTransferAction": {
"properties": {
"amount": {
"description": "amount in quanta of tokens",
"example": "1000000000",
"type": "string",
"x-js-format": "bigint"
},
"comment": {
"example": "Hi! This is your salary. \nFrom accounting with love.",
"type": "string"
},
"currency": {
"$ref": "#/components/schemas/EcPreview"
},
"encrypted_comment": {
"$ref": "#/components/schemas/EncryptedComment"
},
"recipient": {
"$ref": "#/components/schemas/AccountAddress"
},
"sender": {
"$ref": "#/components/schemas/AccountAddress"
}
},
"required": [
"sender",
"recipient",
"amount",
"currency"
],
"type": "object"
},
"FoundAccounts": {
"properties": {
"addresses": {
Expand Down Expand Up @@ -4177,6 +4270,12 @@
"format": "int64",
"type": "integer",
"x-js-format": "bigint"
},
"value_extra": {
"items": {
"$ref": "#/components/schemas/ExtraCurrency"
},
"type": "array"
}
},
"required": [
Expand Down
72 changes: 72 additions & 0 deletions api/openapi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3723,6 +3723,10 @@ components:
format: int64
x-js-format: bigint
example: 60000000
value_extra:
type: array
items:
$ref: '#/components/schemas/ExtraCurrency'
fwd_fee:
type: integer
format: int64
Expand Down Expand Up @@ -4600,6 +4604,10 @@ components:
format: int64
example: 123456789
x-js-format: bigint
extra_balance:
type: array
items:
$ref: '#/components/schemas/ExtraCurrency'
currencies_balance:
description: "{'USD': 1, 'IDR': 1000}"
type: object
Expand Down Expand Up @@ -5736,6 +5744,7 @@ components:
example: "TonTransfer"
enum:
- TonTransfer
- ExtraCurrencyTransfer
- JettonTransfer
- JettonBurn
- JettonMint
Expand Down Expand Up @@ -5764,6 +5773,8 @@ components:
- failed
TonTransfer:
$ref: '#/components/schemas/TonTransferAction'
ExtraCurrencyTransfer:
$ref: '#/components/schemas/ExtraCurrencyTransferAction'
ContractDeploy:
$ref: '#/components/schemas/ContractDeployAction'
JettonTransfer:
Expand Down Expand Up @@ -5834,6 +5845,46 @@ components:
$ref: '#/components/schemas/EncryptedComment'
refund:
$ref: '#/components/schemas/Refund'
EcPreview:
type: object
required:
- symbol
- decimals
- image
properties:
symbol:
type: string
example: FMS
decimals:
type: integer
example: 5
image:
type: string
example: https://cache.tonapi.io/images/extra.jpg
ExtraCurrencyTransferAction:
type: object
required:
- sender
- recipient
- amount
- currency
properties:
sender:
$ref: '#/components/schemas/AccountAddress'
recipient:
$ref: '#/components/schemas/AccountAddress'
amount:
type: string
x-js-format: bigint
example: "1000000000"
description: amount in quanta of tokens
comment:
type: string
example: "Hi! This is your salary. \nFrom accounting with love."
encrypted_comment:
$ref: '#/components/schemas/EncryptedComment'
currency:
$ref: '#/components/schemas/EcPreview'
SmartContractAction:
type: object
required:
Expand Down Expand Up @@ -7389,6 +7440,27 @@ components:
type: integer
format: int64
example: 1668436763
ExtraCurrency:
type: object
required:
- id
- amount
- decimals
properties:
id:
type: integer
example: 239
format: int32
amount:
type: string
x-js-format: bigint
example: "1000000000"
name:
type: string
example: FMS
decimals:
type: integer
example: 5

responses:
Error:
Expand Down
23 changes: 22 additions & 1 deletion pkg/api/account_converters.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package api
import (
"fmt"
imgGenerator "github.com/tonkeeper/opentonapi/pkg/image"
"github.com/tonkeeper/opentonapi/pkg/references"
"math/big"
"sort"

"github.com/tonkeeper/tongo/abi"
Expand Down Expand Up @@ -59,7 +61,8 @@ func convertToRawAccount(account *core.Account) (oas.BlockchainRawAccount, error
if account.ExtraBalances != nil {
balances := make(map[string]string, len(account.ExtraBalances))
for key, value := range account.ExtraBalances {
balances[fmt.Sprintf("%v", key)] = fmt.Sprintf("%v", value)
v := big.Int(value)
balances[fmt.Sprintf("%v", key)] = fmt.Sprintf("%v", v.String())
}
rawAccount.ExtraBalance = oas.NewOptBlockchainRawAccountExtraBalance(balances)
}
Expand All @@ -72,6 +75,24 @@ func convertToRawAccount(account *core.Account) (oas.BlockchainRawAccount, error
return rawAccount, nil
}

func convertExtraCurrencies(extraBalances core.ExtraCurrencies) []oas.ExtraCurrency {
res := make([]oas.ExtraCurrency, 0, len(extraBalances))
for k, v := range extraBalances {
amount := big.Int(v)
meta := references.GetExtraCurrencyMeta(k)
cur := oas.ExtraCurrency{
ID: k,
Amount: amount.String(),
Decimals: meta.Decimals,
}
if meta.Name != "" {
cur.Name.SetTo(meta.Name)
}
res = append(res, cur)
}
return res
}

func convertToAccount(account *core.Account, ab *addressbook.KnownAddress, state chainState) oas.Account {
acc := oas.Account{
Address: account.AccountAddress.ToRaw(),
Expand Down
6 changes: 6 additions & 0 deletions pkg/api/account_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ func (h *Handler) GetAccount(ctx context.Context, params oas.GetAccountParams) (
} else {
res = convertToAccount(rawAccount, nil, h.state)
}
if rawAccount.ExtraBalances != nil {
res.ExtraBalance = convertExtraCurrencies(rawAccount.ExtraBalances)
}
return &res, nil
}

Expand Down Expand Up @@ -114,6 +117,9 @@ func (h *Handler) GetAccounts(ctx context.Context, request oas.OptGetAccountsReq
} else {
res = convertToAccount(account, nil, h.state)
}
if account.ExtraBalances != nil {
res.ExtraBalance = convertExtraCurrencies(account.ExtraBalances)
}
results[account.AccountAddress] = res
}
// if we don't find an account, we return it with "nonexist" status
Expand Down
3 changes: 3 additions & 0 deletions pkg/api/blockchain_converters.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,9 @@ func convertMessage(m core.Message, book addressBook) oas.Message {
value, _ := json.Marshal(m.DecodedBody.Value)
msg.DecodedBody = g.ChangeJsonKeys(value, g.CamelToSnake)
}
if m.ValueExtra != nil {
msg.ValueExtra = convertExtraCurrencies(m.ValueExtra)
}
return msg
}

Expand Down
27 changes: 27 additions & 0 deletions pkg/api/event_converters.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,31 @@ func (h *Handler) convertActionTonTransfer(t *bath.TonTransferAction, acceptLang
return action, simplePreview
}

func (h *Handler) convertActionExtraCurrencyTransfer(t *bath.ExtraCurrencyTransferAction, acceptLanguage string, viewer *tongo.AccountID) (oas.OptExtraCurrencyTransferAction, oas.ActionSimplePreview) {
var action oas.OptExtraCurrencyTransferAction
amount := big.Int(t.Amount)
meta := references.GetExtraCurrencyMeta(t.CurrencyID)
action.SetTo(oas.ExtraCurrencyTransferAction{
Amount: amount.String(),
Comment: g.Opt(t.Comment),
Recipient: convertAccountAddress(t.Recipient, h.addressBook),
Sender: convertAccountAddress(t.Sender, h.addressBook),
EncryptedComment: convertEncryptedComment(t.EncryptedComment),
Currency: oas.EcPreview{
Symbol: meta.Symbol,
Decimals: meta.Decimals,
Image: meta.Image,
},
})
simplePreview := oas.ActionSimplePreview{
Name: "Extra Currency Transfer",
Description: "", // TODO: add description
Accounts: distinctAccounts(viewer, h.addressBook, &t.Sender, &t.Recipient),
// TODO: add value
}
return action, simplePreview
}

func (h *Handler) convertActionNftTransfer(t *bath.NftTransferAction, acceptLanguage string, viewer *tongo.AccountID) (oas.OptNftItemTransferAction, oas.ActionSimplePreview) {
var action oas.OptNftItemTransferAction
action.SetTo(oas.NftItemTransferAction{
Expand Down Expand Up @@ -428,6 +453,8 @@ func (h *Handler) convertAction(ctx context.Context, viewer *tongo.AccountID, a
switch a.Type {
case bath.TonTransfer:
action.TonTransfer, action.SimplePreview = h.convertActionTonTransfer(a.TonTransfer, acceptLanguage.Value, viewer)
case bath.ExtraCurrencyTransfer:
action.ExtraCurrencyTransfer, action.SimplePreview = h.convertActionExtraCurrencyTransfer(a.ExtraCurrencyTransfer, acceptLanguage.Value, viewer)
case bath.NftItemTransfer:
action.NftItemTransfer, action.SimplePreview = h.convertActionNftTransfer(a.NftItemTransfer, acceptLanguage.Value, viewer)
case bath.JettonTransfer:
Expand Down
3 changes: 3 additions & 0 deletions pkg/api/wallet_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ func (h *Handler) GetWalletsByPublicKey(ctx context.Context, params oas.GetWalle
} else {
res = convertToAccount(account, nil, h.state)
}
if account.ExtraBalances != nil {
res.ExtraBalance = convertExtraCurrencies(account.ExtraBalances)
}
results = append(results, res)
}
return &oas.Accounts{Accounts: results}, nil
Expand Down
Loading

0 comments on commit 8d750de

Please sign in to comment.