Skip to content

Commit

Permalink
/v2/multisig/{account} to return risk for every order
Browse files Browse the repository at this point in the history
  • Loading branch information
aleksej-paschenko committed Aug 8, 2024
1 parent 9acd89c commit 754fa86
Show file tree
Hide file tree
Showing 11 changed files with 107 additions and 14 deletions.
6 changes: 5 additions & 1 deletion api/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -4310,6 +4310,9 @@
"format": "int64",
"type": "integer"
},
"risk": {
"$ref": "#/components/schemas/Risk"
},
"sent_for_execution": {
"example": false,
"type": "boolean"
Expand All @@ -4334,7 +4337,8 @@
"sent_for_execution",
"signers",
"approvals_num",
"expiration_date"
"expiration_date",
"risk"
],
"type": "object"
},
Expand Down
3 changes: 3 additions & 0 deletions api/openapi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5482,6 +5482,7 @@ components:
- signers
- approvals_num
- expiration_date
- risk
properties:
address:
type: string
Expand Down Expand Up @@ -5509,6 +5510,8 @@ components:
expiration_date:
type: integer
format: int64
risk:
$ref: '#/components/schemas/Risk'
Refund:
type: object
required:
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -272,8 +272,6 @@ github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsT
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/tonkeeper/scam_backoffice_rules v0.0.0-20240606040945-382d2b4edb98 h1:lpzXxMZktaBS5YouFNGIv6haEbsYtDA/gaCPAspWKLI=
github.com/tonkeeper/scam_backoffice_rules v0.0.0-20240606040945-382d2b4edb98/go.mod h1:SqZXYO9vbID8ku+xnnaKXeNGmehxigODGrk5V1KqbRA=
github.com/tonkeeper/tongo v1.9.1-0.20240710061111-60ccf2c10f84 h1:tHmuFcQ0zUuGULQcpzBXHNW9hrLFiUJjio8o0XGUoN4=
github.com/tonkeeper/tongo v1.9.1-0.20240710061111-60ccf2c10f84/go.mod h1:MjgIgAytFarjCoVjMLjYEtpZNN1f2G/pnZhKjr28cWs=
github.com/tonkeeper/tongo v1.9.1 h1:RaTHi7zvAhclv9EU9/9HLdGUZwq9iUc8EO9xtpuPwus=
github.com/tonkeeper/tongo v1.9.1/go.mod h1:MjgIgAytFarjCoVjMLjYEtpZNN1f2G/pnZhKjr28cWs=
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
Expand Down
6 changes: 5 additions & 1 deletion pkg/api/account_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -510,7 +510,11 @@ func (h *Handler) GetAccountMultisigs(ctx context.Context, params oas.GetAccount
}
var converted []oas.Multisig
for _, multisig := range multisigs {
converted = append(converted, convertMultisig(multisig))
oasMultisig, err := h.convertMultisig(ctx, multisig)
if err != nil {
return nil, toError(http.StatusInternalServerError, err)
}
converted = append(converted, *oasMultisig)
}
return &oas.Multisigs{Multisigs: converted}, nil
}
Expand Down
19 changes: 17 additions & 2 deletions pkg/api/converters.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package api

import (
"context"
"encoding/json"
"fmt"
"math/big"
Expand All @@ -11,6 +12,7 @@ import (

"github.com/tonkeeper/opentonapi/pkg/core"
imgGenerator "github.com/tonkeeper/opentonapi/pkg/image"
walletPkg "github.com/tonkeeper/opentonapi/pkg/wallet"
"github.com/tonkeeper/tongo/boc"
"github.com/tonkeeper/tongo/tlb"

Expand Down Expand Up @@ -210,7 +212,7 @@ func stringToTVMStackRecord(s string) (tlb.VmStackValue, error) {
return tlb.VmStackValue{SumType: "VmStkCell", VmStkCell: tlb.Ref[boc.Cell]{Value: *c}}, nil
}

func convertMultisig(item core.Multisig) oas.Multisig {
func (h *Handler) convertMultisig(ctx context.Context, item core.Multisig) (*oas.Multisig, error) {
converted := oas.Multisig{
Address: item.AccountID.ToRaw(),
Seqno: item.Seqno,
Expand All @@ -227,6 +229,18 @@ func convertMultisig(item core.Multisig) oas.Multisig {
for _, account := range order.Signers {
signers = append(signers, account.ToRaw())
}
messages, err := convertMultisigActionsToRawMessages(order.Actions)
if err != nil {
return nil, err
}
risk, err := walletPkg.ExtractRiskFromRawMessages(messages)
if err != nil {
return nil, err
}
oasRisk, err := h.convertRisk(ctx, *risk, item.AccountID)
if err != nil {
return nil, err
}
converted.Orders = append(converted.Orders, oas.MultisigOrder{
Address: order.AccountID.ToRaw(),
OrderSeqno: order.OrderSeqno,
Expand All @@ -235,7 +249,8 @@ func convertMultisig(item core.Multisig) oas.Multisig {
Signers: signers,
ApprovalsNum: order.ApprovalsNum,
ExpirationDate: order.ExpirationDate,
Risk: oasRisk,
})
}
return converted
return &converted, nil
}
29 changes: 27 additions & 2 deletions pkg/api/multisig_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,31 @@ import (

"github.com/tonkeeper/opentonapi/pkg/core"
"github.com/tonkeeper/opentonapi/pkg/oas"
"github.com/tonkeeper/tongo/abi"
"github.com/tonkeeper/tongo/boc"
"github.com/tonkeeper/tongo/tlb"
"github.com/tonkeeper/tongo/ton"
tongoWallet "github.com/tonkeeper/tongo/wallet"
)

func convertMultisigActionsToRawMessages(actions []abi.MultisigSendMessageAction) ([]tongoWallet.RawMessage, error) {
var messages []tongoWallet.RawMessage
for _, action := range actions {
switch action.SumType {
case "SendMessage":
msg := boc.NewCell()
if err := tlb.Marshal(msg, action.SendMessage.Field0.Message); err != nil {
return nil, err
}
messages = append(messages, tongoWallet.RawMessage{
Message: msg,
Mode: action.SendMessage.Field0.Mode,
})
}
}
return messages, nil
}

func (h *Handler) GetMultisigAccount(ctx context.Context, params oas.GetMultisigAccountParams) (*oas.Multisig, error) {
accountID, err := ton.ParseAccountID(params.AccountID)
if err != nil {
Expand All @@ -22,6 +44,9 @@ func (h *Handler) GetMultisigAccount(ctx context.Context, params oas.GetMultisig
if err != nil {
return nil, toError(http.StatusInternalServerError, err)
}
converted := convertMultisig(*multisig)
return &converted, nil
converted, err := h.convertMultisig(ctx, *multisig)
if err != nil {
return nil, toError(http.StatusInternalServerError, err)
}
return converted, nil
}
2 changes: 2 additions & 0 deletions pkg/core/multisig.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package core

import (
"github.com/tonkeeper/tongo/abi"
"github.com/tonkeeper/tongo/ton"
)

Expand All @@ -22,4 +23,5 @@ type MultisigOrder struct {
ApprovalsMask []byte
ApprovalsNum int32
ExpirationDate int64
Actions []abi.MultisigSendMessageAction
}
19 changes: 17 additions & 2 deletions pkg/oas/oas_json_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions pkg/oas/oas_schemas_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions pkg/oas/oas_validators_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 9 additions & 4 deletions pkg/wallet/risk.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/tonkeeper/tongo/boc"
"github.com/tonkeeper/tongo/tlb"
"github.com/tonkeeper/tongo/ton"
walletTongo "github.com/tonkeeper/tongo/wallet"
tongoWallet "github.com/tonkeeper/tongo/wallet"
)

// Risk specifies assets that could be lost
Expand All @@ -23,23 +23,28 @@ type Risk struct {
Nfts []tongo.AccountID
}

func ExtractRisk(version walletTongo.Version, msg *boc.Cell) (*Risk, error) {
rawMessages, err := walletTongo.ExtractRawMessages(version, msg)
func ExtractRisk(version tongoWallet.Version, msg *boc.Cell) (*Risk, error) {
rawMessages, err := tongoWallet.ExtractRawMessages(version, msg)
if err != nil {
return nil, err
}
return ExtractRiskFromRawMessages(rawMessages)
}

func ExtractRiskFromRawMessages(rawMessages []tongoWallet.RawMessage) (*Risk, error) {
risk := Risk{
TransferAllRemainingBalance: false,
Jettons: map[tongo.AccountID]big.Int{},
}
for _, rawMsg := range rawMessages {
if walletTongo.IsMessageModeSet(int(rawMsg.Mode), walletTongo.AttachAllRemainingBalance) {
if tongoWallet.IsMessageModeSet(int(rawMsg.Mode), tongoWallet.AttachAllRemainingBalance) {
risk.TransferAllRemainingBalance = true
}
var m tlb.Message
if err := tlb.Unmarshal(rawMsg.Message, &m); err != nil {
return nil, err
}
var err error
var tonValue uint64
var destination *tongo.AccountID
if m.Info.IntMsgInfo != nil {
Expand Down

0 comments on commit 754fa86

Please sign in to comment.