Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extra currency #564

Merged
merged 5 commits into from
Dec 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading