Skip to content

Commit

Permalink
extra currency transfer action
Browse files Browse the repository at this point in the history
  • Loading branch information
akos-tk committed Dec 10, 2024
1 parent 30eb0a1 commit 4a2d6c8
Show file tree
Hide file tree
Showing 13 changed files with 907 additions and 67 deletions.
67 changes: 67 additions & 0 deletions api/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -1063,6 +1063,9 @@
"ElectionsRecoverStake": {
"$ref": "#/components/schemas/ElectionsRecoverStakeAction"
},
"ExtraCurrencyTransfer": {
"$ref": "#/components/schemas/ExtraCurrencyTransferAction"
},
"InscriptionMint": {
"$ref": "#/components/schemas/InscriptionMintAction"
},
Expand Down Expand Up @@ -1127,6 +1130,7 @@
"type": {
"enum": [
"TonTransfer",
"ExtraCurrencyTransfer",
"JettonTransfer",
"JettonBurn",
"JettonMint",
Expand Down Expand Up @@ -3119,6 +3123,33 @@
],
"type": "object"
},
"EcPreview": {
"properties": {
"decimals": {
"example": 5,
"type": "integer"
},
"image": {
"example": "https://cache.tonapi.io/images/extra.jpg",
"type": "string"
},
"name": {
"example": "FMS",
"type": "string"
},
"symbol": {
"example": "FMS",
"type": "string"
}
},
"required": [
"name",
"symbol",
"decimals",
"image"
],
"type": "object"
},
"ElectionsDepositStakeAction": {
"properties": {
"amount": {
Expand Down Expand Up @@ -3263,6 +3294,42 @@
],
"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"
},
"refund": {
"$ref": "#/components/schemas/Refund"
},
"sender": {
"$ref": "#/components/schemas/AccountAddress"
}
},
"required": [
"sender",
"recipient",
"amount",
"currency"
],
"type": "object"
},
"FoundAccounts": {
"properties": {
"addresses": {
Expand Down
49 changes: 49 additions & 0 deletions api/openapi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5744,6 +5744,7 @@ components:
example: "TonTransfer"
enum:
- TonTransfer
- ExtraCurrencyTransfer
- JettonTransfer
- JettonBurn
- JettonMint
Expand Down Expand Up @@ -5772,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 @@ -5842,6 +5845,52 @@ components:
$ref: '#/components/schemas/EncryptedComment'
refund:
$ref: '#/components/schemas/Refund'
EcPreview:
type: object
required:
- name
- symbol
- decimals
- image
properties:
name:
type: string
example: FMS
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'
refund:
$ref: '#/components/schemas/Refund'
currency:
$ref: '#/components/schemas/EcPreview'
SmartContractAction:
type: object
required:
Expand Down
16 changes: 9 additions & 7 deletions pkg/api/account_converters.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ 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 @@ -60,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", int32(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 @@ -76,15 +78,15 @@ func convertToRawAccount(account *core.Account) (oas.BlockchainRawAccount, error
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: int32(k),
Amount: v.String(),
Decimals: 9, // TODO: or replace with default const
ID: k,
Amount: amount.String(),
Decimals: meta.Decimals,
}
meta, ok := references.ExtraCurrencies[int32(k)]
if ok {
if meta.Name != "" {
cur.Name.SetTo(meta.Name)
cur.Decimals = meta.Decimals
}
res = append(res, cur)
}
Expand Down
34 changes: 34 additions & 0 deletions pkg/api/event_converters.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,38 @@ 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{
Name: meta.Name,
Symbol: meta.Symbol,
Decimals: meta.Decimals,
Image: meta.Image,
},
})
if t.Refund != nil {
action.Value.Refund.SetTo(oas.Refund{
Type: oas.RefundType(t.Refund.Type),
Origin: t.Refund.Origin,
})
}
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 +460,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
11 changes: 11 additions & 0 deletions pkg/bath/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (

const (
TonTransfer ActionType = "TonTransfer"
ExtraCurrencyTransfer ActionType = "ExtraCurrencyTransfer"
SmartContractExec ActionType = "SmartContractExec"
NftItemTransfer ActionType = "NftItemTransfer"
NftPurchase ActionType = "NftPurchase"
Expand Down Expand Up @@ -69,6 +70,7 @@ type (

Action struct {
TonTransfer *TonTransferAction `json:",omitempty"`
ExtraCurrencyTransfer *ExtraCurrencyTransferAction `json:",omitempty"`
SmartContractExec *SmartContractAction `json:",omitempty"`
NftItemTransfer *NftTransferAction `json:",omitempty"`
NftPurchase *NftPurchaseAction `json:",omitempty"`
Expand Down Expand Up @@ -101,6 +103,15 @@ type (
Sender tongo.AccountID
Refund *Refund
}
ExtraCurrencyTransferAction struct {
CurrencyID int32
Amount tlb.VarUInteger32
Comment *string
EncryptedComment *EncryptedComment
Recipient tongo.AccountID
Sender tongo.AccountID
Refund *Refund
}
SmartContractAction struct {
TonAttached int64
Executor tongo.AccountID
Expand Down
2 changes: 2 additions & 0 deletions pkg/bath/bubble.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ func fromTrace(trace *core.Trace) *Bubble {
btx.bounced = msg.Bounced
btx.inputAmount += msg.Value
btx.inputAmount += msg.IhrFee
btx.inputExtraAmount = msg.ValueExtra
btx.opCode = msg.OpCode
btx.decodedBody = msg.DecodedBody
btx.inputFrom = source
Expand All @@ -95,6 +96,7 @@ func fromTrace(trace *core.Trace) *Bubble {
Children: make([]*Bubble, len(trace.Children)),
ValueFlow: &ValueFlow{
Accounts: map[tongo.AccountID]*AccountValueFlow{
// TODO: add extra currency
trace.Account: {
Ton: inputAmount,
},
Expand Down
Loading

0 comments on commit 4a2d6c8

Please sign in to comment.