Skip to content

Commit aa7c028

Browse files
authored
Merge pull request #566 from tonkeeper/extract_risk
rewrite extract risk from messages
2 parents 8d750de + bfd41b3 commit aa7c028

File tree

3 files changed

+54
-61
lines changed

3 files changed

+54
-61
lines changed

pkg/api/converters.go

+13-7
Original file line numberDiff line numberDiff line change
@@ -268,15 +268,21 @@ func (h *Handler) convertMultisig(ctx context.Context, item core.Multisig) (*oas
268268
for _, account := range order.Signers {
269269
signers = append(signers, account.ToRaw())
270270
}
271-
messages, err := convertMultisigActionsToRawMessages(order.Actions)
272-
if err != nil {
273-
return nil, err
271+
risk := walletPkg.Risk{
272+
TransferAllRemainingBalance: false,
273+
Jettons: map[tongo.AccountID]big.Int{},
274274
}
275-
risk, err := walletPkg.ExtractRiskFromRawMessages(messages)
276-
if err != nil {
277-
return nil, err
275+
for _, action := range order.Actions {
276+
switch action.SumType {
277+
case "SendMessage":
278+
var err error
279+
risk, err = walletPkg.ExtractRiskFromMessage(action.SendMessage.Field0.Message, risk, action.SendMessage.Field0.Mode)
280+
if err != nil {
281+
return nil, err
282+
}
283+
}
278284
}
279-
oasRisk, err := h.convertRisk(ctx, *risk, item.AccountID)
285+
oasRisk, err := h.convertRisk(ctx, risk, item.AccountID)
280286
if err != nil {
281287
return nil, err
282288
}

pkg/api/multisig_handlers.go

-22
Original file line numberDiff line numberDiff line change
@@ -7,31 +7,9 @@ import (
77

88
"github.com/tonkeeper/opentonapi/pkg/core"
99
"github.com/tonkeeper/opentonapi/pkg/oas"
10-
"github.com/tonkeeper/tongo/abi"
11-
"github.com/tonkeeper/tongo/boc"
12-
"github.com/tonkeeper/tongo/tlb"
1310
"github.com/tonkeeper/tongo/ton"
14-
tongoWallet "github.com/tonkeeper/tongo/wallet"
1511
)
1612

17-
func convertMultisigActionsToRawMessages(actions []abi.MultisigSendMessageAction) ([]tongoWallet.RawMessage, error) {
18-
var messages []tongoWallet.RawMessage
19-
for _, action := range actions {
20-
switch action.SumType {
21-
case "SendMessage":
22-
msg := boc.NewCell()
23-
if err := tlb.Marshal(msg, action.SendMessage.Field0.Message); err != nil {
24-
return nil, err
25-
}
26-
messages = append(messages, tongoWallet.RawMessage{
27-
Message: msg,
28-
Mode: action.SendMessage.Field0.Mode,
29-
})
30-
}
31-
}
32-
return messages, nil
33-
}
34-
3513
func (h *Handler) GetMultisigAccount(ctx context.Context, params oas.GetMultisigAccountParams) (*oas.Multisig, error) {
3614
accountID, err := ton.ParseAccountID(params.AccountID)
3715
if err != nil {

pkg/wallet/risk.go

+41-32
Original file line numberDiff line numberDiff line change
@@ -45,42 +45,51 @@ func ExtractRiskFromRawMessages(rawMessages []tongoWallet.RawMessage) (*Risk, er
4545
if err := tlb.Unmarshal(rawMsg.Message, &m); err != nil {
4646
return nil, err
4747
}
48-
tonValue := uint64(m.MessageInternal.Value.Grams)
49-
destination, err := ton.AccountIDFromTlb(m.MessageInternal.Dest)
48+
var err error
49+
risk, err = ExtractRiskFromMessage(m, risk, rawMsg.Mode)
5050
if err != nil {
5151
return nil, err
5252
}
53-
risk.Ton += tonValue
54-
msgBody := m.MessageInternal.Body.Value.Value
55-
if err != nil {
56-
continue
53+
}
54+
return &risk, nil
55+
}
56+
57+
func ExtractRiskFromMessage(m abi.MessageRelaxed, risk Risk, mode byte) (Risk, error) {
58+
if tongoWallet.IsMessageModeSet(int(mode), tongoWallet.AttachAllRemainingBalance) {
59+
risk.TransferAllRemainingBalance = true
60+
}
61+
tonValue := uint64(m.MessageInternal.Value.Grams)
62+
destination, err := ton.AccountIDFromTlb(m.MessageInternal.Dest)
63+
if err != nil {
64+
return Risk{}, err
65+
}
66+
risk.Ton += tonValue
67+
msgBody := m.MessageInternal.Body.Value.Value
68+
switch x := msgBody.(type) {
69+
case abi.NftTransferMsgBody:
70+
if destination == nil {
71+
return risk, err
5772
}
58-
switch x := msgBody.(type) {
59-
case abi.NftTransferMsgBody:
60-
if destination == nil {
61-
continue
62-
}
63-
// here, destination is an NFT
64-
risk.Nfts = append(risk.Nfts, *destination)
65-
case abi.JettonBurnMsgBody:
66-
if destination == nil {
67-
continue
68-
}
69-
// here, destination is a jetton wallet
70-
amount := big.Int(x.Amount)
71-
currentJettons := risk.Jettons[*destination]
72-
var total big.Int
73-
risk.Jettons[*destination] = *total.Add(&currentJettons, &amount)
74-
case abi.JettonTransferMsgBody:
75-
if destination == nil {
76-
continue
77-
}
78-
// here, destination is a jetton wallet
79-
amount := big.Int(x.Amount)
80-
currentJettons := risk.Jettons[*destination]
81-
var total big.Int
82-
risk.Jettons[*destination] = *total.Add(&currentJettons, &amount)
73+
// here, destination is an NFT
74+
risk.Nfts = append(risk.Nfts, *destination)
75+
case abi.JettonBurnMsgBody:
76+
if destination == nil {
77+
return risk, err
8378
}
79+
// here, destination is a jetton wallet
80+
amount := big.Int(x.Amount)
81+
currentJettons := risk.Jettons[*destination]
82+
var total big.Int
83+
risk.Jettons[*destination] = *total.Add(&currentJettons, &amount)
84+
case abi.JettonTransferMsgBody:
85+
if destination == nil {
86+
return risk, err
87+
}
88+
// here, destination is a jetton wallet
89+
amount := big.Int(x.Amount)
90+
currentJettons := risk.Jettons[*destination]
91+
var total big.Int
92+
risk.Jettons[*destination] = *total.Add(&currentJettons, &amount)
8493
}
85-
return &risk, nil
94+
return risk, err
8695
}

0 commit comments

Comments
 (0)