Skip to content

Commit

Permalink
Merge pull request #220 from tonkeeper/fix-stonfi
Browse files Browse the repository at this point in the history
Bugfix: Stonfi swap didnt contain out jetton master on failure
  • Loading branch information
mr-tron authored Oct 11, 2023
2 parents 67323c9 + d5533ae commit 5e82c62
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 16 deletions.
3 changes: 2 additions & 1 deletion pkg/api/event_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -553,7 +553,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)
Expand Down
2 changes: 1 addition & 1 deletion pkg/bath/bath_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)"),
}),
)
Expand Down
9 changes: 6 additions & 3 deletions pkg/bath/jettons.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package bath

import (
"fmt"

"github.com/tonkeeper/opentonapi/internal/g"
"github.com/tonkeeper/tongo"
"github.com/tonkeeper/tongo/abi"
Expand Down Expand Up @@ -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
Expand Down
9 changes: 6 additions & 3 deletions pkg/bath/megatonfi.go
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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
Expand Down
10 changes: 8 additions & 2 deletions pkg/bath/stonfi.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package bath

import (
"math/big"

"github.com/tonkeeper/tongo"
"github.com/tonkeeper/tongo/abi"
"math/big"
)

type Dex string
Expand Down Expand Up @@ -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]{
Expand All @@ -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
Expand Down
9 changes: 6 additions & 3 deletions pkg/bath/straws.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion pkg/bath/testdata/stonfi-failed-swap.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"Out": {
"Amount": 1717458000,
"IsTon": false,
"JettonMaster": "0:0000000000000000000000000000000000000000000000000000000000000000",
"JettonMaster": "0:729c13b6df2c07cbf0a06ab63d34af454f3d320ec1bcd8fb5c6d24d0806a17c2",
"JettonWallet": "0:14ac072c56291232d7cd93ddec120235c5e5cf5e2027f49bbc5aa276e5d224d8"
}
},
Expand Down
22 changes: 20 additions & 2 deletions pkg/core/trace.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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) {
Expand All @@ -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
}

0 comments on commit 5e82c62

Please sign in to comment.