From d5533ae2325a34212be628139a440157ae94eae7 Mon Sep 17 00:00:00 2001 From: "aleksej.paschenko" Date: Wed, 11 Oct 2023 17:30:37 +0300 Subject: [PATCH] Bugfix: Stonfi swap didnt contain out jetton master on failure --- pkg/api/event_handlers.go | 3 ++- pkg/bath/bath_test.go | 2 +- pkg/bath/jettons.go | 9 ++++++--- pkg/bath/megatonfi.go | 9 ++++++--- pkg/bath/stonfi.go | 10 ++++++++-- pkg/bath/straws.go | 9 ++++++--- pkg/bath/testdata/stonfi-failed-swap.json | 2 +- pkg/core/trace.go | 22 ++++++++++++++++++++-- 8 files changed, 50 insertions(+), 16 deletions(-) diff --git a/pkg/api/event_handlers.go b/pkg/api/event_handlers.go index b75d0cf2..d6dc4091 100644 --- a/pkg/api/event_handlers.go +++ b/pkg/api/event_handlers.go @@ -546,7 +546,8 @@ func emulatedTreeToTrace(ctx context.Context, resolver core.LibraryResolver, con for _, m := range inspectionResult.GetMethods { switch data := m.Result.(type) { case abi.GetWalletDataResult: - t.AdditionalInfo.JettonMaster, _ = tongo.AccountIDFromTlb(data.Jetton) + master, _ := tongo.AccountIDFromTlb(data.Jetton) + t.AdditionalInfo.SetJettonMaster(k, *master) case abi.GetSaleData_GetgemsResult: price := big.Int(data.FullPrice) owner, err := tongo.AccountIDFromTlb(data.Owner) diff --git a/pkg/bath/bath_test.go b/pkg/bath/bath_test.go index f52752af..b2dc2457 100644 --- a/pkg/bath/bath_test.go +++ b/pkg/bath/bath_test.go @@ -137,7 +137,7 @@ func TestFindActions(t *testing.T) { tongo.MustParseBlockID("(0,8000000000000000,38499308)"), // liquid withdraw tongo.MustParseBlockID("(0,8000000000000000,38912382)"), - //telemint deploy + // telemint deploy tongo.MustParseBlockID("(0,8000000000000000,38603492)"), }), ) diff --git a/pkg/bath/jettons.go b/pkg/bath/jettons.go index 16a7123b..edc56f97 100644 --- a/pkg/bath/jettons.go +++ b/pkg/bath/jettons.go @@ -2,6 +2,7 @@ package bath import ( "fmt" + "github.com/tonkeeper/opentonapi/internal/g" "github.com/tonkeeper/tongo" "github.com/tonkeeper/tongo/abi" @@ -138,9 +139,11 @@ var JettonBurnStraw = Straw[BubbleJettonBurn]{ if tx.inputFrom != nil { newAction.sender = *tx.inputFrom } - if tx.additionalInfo.JettonMaster != nil { //todo: find why it doesn't set sometimes - // maybe it already fixed but this commit where i left comment - newAction.master = *tx.additionalInfo.JettonMaster + if tx.additionalInfo != nil { + if master, ok := tx.additionalInfo.JettonMaster(tx.account.Address); ok { + //todo: find why it doesn't set sometimes (maybe it already fixed but who knows?) + newAction.master = master + } } newAction.senderWallet = tx.account.Address newAction.success = tx.success diff --git a/pkg/bath/megatonfi.go b/pkg/bath/megatonfi.go index 150acddd..7fb38a25 100644 --- a/pkg/bath/megatonfi.go +++ b/pkg/bath/megatonfi.go @@ -1,8 +1,9 @@ package bath import ( - "github.com/tonkeeper/tongo/abi" "math/big" + + "github.com/tonkeeper/tongo/abi" ) // MegatonFiJettonSwap creates a BubbleJettonSwap if there is a jetton swap in a trace. @@ -58,8 +59,10 @@ var WtonMintStraw = Straw[BubbleJettonMint]{ tx := bubble.Info.(BubbleTx) body := tx.decodedBody.Value.(abi.JettonInternalTransferMsgBody) newAction.amount = body.Amount - if tx.additionalInfo != nil && tx.additionalInfo.JettonMaster != nil { - newAction.master = *tx.additionalInfo.JettonMaster + if tx.additionalInfo != nil { + if master, ok := tx.additionalInfo.JettonMaster(tx.account.Address); ok { + newAction.master = master + } } newAction.recipientWallet = tx.account.Address newAction.success = tx.success diff --git a/pkg/bath/stonfi.go b/pkg/bath/stonfi.go index 5691473f..6433c0e3 100644 --- a/pkg/bath/stonfi.go +++ b/pkg/bath/stonfi.go @@ -1,9 +1,10 @@ package bath import ( + "math/big" + "github.com/tonkeeper/tongo" "github.com/tonkeeper/tongo/abi" - "math/big" ) type Dex string @@ -65,6 +66,10 @@ var StonfiSwapStraw = Straw[BubbleJettonSwap]{ } newAction.In.JettonWallet = a newAction.Out.JettonWallet = b + if tx.additionalInfo != nil { + newAction.In.JettonMaster, _ = tx.additionalInfo.JettonMaster(a) + newAction.Out.JettonMaster, _ = tx.additionalInfo.JettonMaster(b) + } return nil }, SingleChild: &Straw[BubbleJettonSwap]{ @@ -79,9 +84,10 @@ var StonfiSwapStraw = Straw[BubbleJettonSwap]{ Builder: func(newAction *BubbleJettonSwap, bubble *Bubble) error { jettonTx := bubble.Info.(BubbleJettonTransfer) if jettonTx.senderWallet != newAction.Out.JettonWallet { + // operation has failed, + // stonfi's sent jettons back to the user return nil } - newAction.Out.JettonMaster = jettonTx.master newAction.Out.Amount = big.Int(jettonTx.amount) newAction.Out.IsTon = jettonTx.isWrappedTon newAction.Success = true diff --git a/pkg/bath/straws.go b/pkg/bath/straws.go index e3486678..73e1ebe0 100644 --- a/pkg/bath/straws.go +++ b/pkg/bath/straws.go @@ -2,9 +2,10 @@ package bath import ( "fmt" - "github.com/tonkeeper/opentonapi/internal/g" "math/big" + "github.com/tonkeeper/opentonapi/internal/g" + "github.com/tonkeeper/tongo" "github.com/tonkeeper/tongo/abi" ) @@ -157,8 +158,10 @@ func FindJettonTransfer(bubble *Bubble) bool { }, payload: intention.ForwardPayload.Value, } - if transferBubbleInfo.additionalInfo != nil && transferBubbleInfo.additionalInfo.JettonMaster != nil { - transfer.master = *transferBubbleInfo.additionalInfo.JettonMaster + if transferBubbleInfo.additionalInfo != nil { + if master, ok := transferBubbleInfo.additionalInfo.JettonMaster(transferBubbleInfo.account.Address); ok { + transfer.master = master + } } newBubble := Bubble{ Children: bubble.Children, diff --git a/pkg/bath/testdata/stonfi-failed-swap.json b/pkg/bath/testdata/stonfi-failed-swap.json index 4c7f2c30..81e13268 100644 --- a/pkg/bath/testdata/stonfi-failed-swap.json +++ b/pkg/bath/testdata/stonfi-failed-swap.json @@ -14,7 +14,7 @@ "Out": { "Amount": 1717458000, "IsTon": false, - "JettonMaster": "0:0000000000000000000000000000000000000000000000000000000000000000", + "JettonMaster": "0:729c13b6df2c07cbf0a06ab63d34af454f3d320ec1bcd8fb5c6d24d0806a17c2", "JettonWallet": "0:14ac072c56291232d7cd93ddec120235c5e5cf5e2027f49bbc5aa276e5d224d8" } }, diff --git a/pkg/core/trace.go b/pkg/core/trace.go index 2103e399..bbadfed5 100644 --- a/pkg/core/trace.go +++ b/pkg/core/trace.go @@ -19,7 +19,8 @@ type Trace struct { // TraceAdditionalInfo holds information about a trace // but not directly extracted from it or a corresponding transaction. type TraceAdditionalInfo struct { - JettonMaster *tongo.AccountID + // JettonMasters maps jetton wallets to their masters. + JettonMasters map[tongo.AccountID]tongo.AccountID // NftSaleContract is set, if a transaction's account implements "get_sale_data" method. NftSaleContract *NftSaleContract // STONfiPool is set, if a transaction's account implements "get_pool_data" method and abi.StonfiPool interface. @@ -137,7 +138,7 @@ func CollectAdditionalInfo(ctx context.Context, infoSource InformationSource, tr trace.AdditionalInfo = &TraceAdditionalInfo{} if isDestinationJettonWallet(trace.InMsg) { if master, ok := masters[*trace.InMsg.Destination]; ok { - trace.AdditionalInfo.JettonMaster = &master + trace.AdditionalInfo.SetJettonMaster(*trace.InMsg.Destination, master) } } if hasInterface(trace.AccountInterfaces, abi.NftSaleV2) { @@ -153,8 +154,25 @@ func CollectAdditionalInfo(ctx context.Context, infoSource InformationSource, tr if hasInterface(trace.AccountInterfaces, abi.StonfiPool) { if pool, ok := stonfiPools[trace.Account]; ok { trace.AdditionalInfo.STONfiPool = &pool + trace.AdditionalInfo.SetJettonMaster(pool.Token0, masters[pool.Token0]) + trace.AdditionalInfo.SetJettonMaster(pool.Token1, masters[pool.Token1]) } } }) return nil } + +func (info *TraceAdditionalInfo) JettonMaster(jettonWallet tongo.AccountID) (tongo.AccountID, bool) { + if info.JettonMasters == nil { + return tongo.AccountID{}, false + } + master, ok := info.JettonMasters[jettonWallet] + return master, ok +} + +func (info *TraceAdditionalInfo) SetJettonMaster(jettonWallet tongo.AccountID, jettonMaster tongo.AccountID) { + if info.JettonMasters == nil { + info.JettonMasters = make(map[tongo.AccountID]tongo.AccountID) + } + info.JettonMasters[jettonWallet] = jettonMaster +}