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

rewrite extract risk from messages #566

Merged
merged 3 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
20 changes: 13 additions & 7 deletions pkg/api/converters.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,15 +268,21 @@ func (h *Handler) convertMultisig(ctx context.Context, item core.Multisig) (*oas
for _, account := range order.Signers {
signers = append(signers, account.ToRaw())
}
messages, err := convertMultisigActionsToRawMessages(order.Actions)
if err != nil {
return nil, err
risk := walletPkg.Risk{
TransferAllRemainingBalance: false,
Jettons: map[tongo.AccountID]big.Int{},
}
risk, err := walletPkg.ExtractRiskFromRawMessages(messages)
if err != nil {
return nil, err
for _, action := range order.Actions {
switch action.SumType {
case "SendMessage":
var err error
risk, err = walletPkg.ExtractRiskFromMessage(action.SendMessage.Field0.Message, risk, action.SendMessage.Field0.Mode)
if err != nil {
return nil, err
}
}
}
oasRisk, err := h.convertRisk(ctx, *risk, item.AccountID)
oasRisk, err := h.convertRisk(ctx, risk, item.AccountID)
if err != nil {
return nil, err
}
Expand Down
22 changes: 0 additions & 22 deletions pkg/api/multisig_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,31 +7,9 @@ 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 Down
73 changes: 41 additions & 32 deletions pkg/wallet/risk.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,42 +45,51 @@ func ExtractRiskFromRawMessages(rawMessages []tongoWallet.RawMessage) (*Risk, er
if err := tlb.Unmarshal(rawMsg.Message, &m); err != nil {
return nil, err
}
tonValue := uint64(m.MessageInternal.Value.Grams)
destination, err := ton.AccountIDFromTlb(m.MessageInternal.Dest)
var err error
risk, err = ExtractRiskFromMessage(m, risk, rawMsg.Mode)
if err != nil {
return nil, err
}
risk.Ton += tonValue
msgBody := m.MessageInternal.Body.Value.Value
if err != nil {
continue
}
return &risk, nil
}

func ExtractRiskFromMessage(m abi.MessageRelaxed, risk Risk, mode byte) (Risk, error) {
if tongoWallet.IsMessageModeSet(int(mode), tongoWallet.AttachAllRemainingBalance) {
risk.TransferAllRemainingBalance = true
}
tonValue := uint64(m.MessageInternal.Value.Grams)
destination, err := ton.AccountIDFromTlb(m.MessageInternal.Dest)
if err != nil {
return Risk{}, err
}
risk.Ton += tonValue
msgBody := m.MessageInternal.Body.Value.Value
switch x := msgBody.(type) {
case abi.NftTransferMsgBody:
if destination == nil {
return risk, err
}
switch x := msgBody.(type) {
case abi.NftTransferMsgBody:
if destination == nil {
continue
}
// here, destination is an NFT
risk.Nfts = append(risk.Nfts, *destination)
case abi.JettonBurnMsgBody:
if destination == nil {
continue
}
// here, destination is a jetton wallet
amount := big.Int(x.Amount)
currentJettons := risk.Jettons[*destination]
var total big.Int
risk.Jettons[*destination] = *total.Add(&currentJettons, &amount)
case abi.JettonTransferMsgBody:
if destination == nil {
continue
}
// here, destination is a jetton wallet
amount := big.Int(x.Amount)
currentJettons := risk.Jettons[*destination]
var total big.Int
risk.Jettons[*destination] = *total.Add(&currentJettons, &amount)
// here, destination is an NFT
risk.Nfts = append(risk.Nfts, *destination)
case abi.JettonBurnMsgBody:
if destination == nil {
return risk, err
}
// here, destination is a jetton wallet
amount := big.Int(x.Amount)
currentJettons := risk.Jettons[*destination]
var total big.Int
risk.Jettons[*destination] = *total.Add(&currentJettons, &amount)
case abi.JettonTransferMsgBody:
if destination == nil {
return risk, err
}
// here, destination is a jetton wallet
amount := big.Int(x.Amount)
currentJettons := risk.Jettons[*destination]
var total big.Int
risk.Jettons[*destination] = *total.Add(&currentJettons, &amount)
}
return &risk, nil
return risk, err
}
Loading