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

Add stonfi v2 swap straw #509

Merged
merged 4 commits into from
Oct 9, 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
24 changes: 22 additions & 2 deletions pkg/bath/bath_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import (
"context"
"encoding/json"
"fmt"
"github.com/tonkeeper/opentonapi/internal/g"
"os"
"sort"
"testing"

"github.com/stretchr/testify/require"
"github.com/tonkeeper/opentonapi/internal/g"
"github.com/tonkeeper/opentonapi/pkg/core"
"github.com/tonkeeper/tongo"
"github.com/tonkeeper/tongo/liteapi"
Expand Down Expand Up @@ -57,7 +57,7 @@ func (m *mockInfoSource) JettonMastersForWallets(ctx context.Context, wallets []
return m.OnJettonMastersForWallets(ctx, wallets)
}

func (m *mockInfoSource) STONfiPools(ctx context.Context, pools []tongo.AccountID) (map[tongo.AccountID]core.STONfiPool, error) {
func (m *mockInfoSource) STONfiPools(ctx context.Context, pools []core.STONfiPoolID) (map[tongo.AccountID]core.STONfiPool, error) {
return map[tongo.AccountID]core.STONfiPool{}, nil
}

Expand Down Expand Up @@ -166,6 +166,16 @@ func TestFindActions(t *testing.T) {
tongo.MustParseBlockID("(0,8000000000000000,33600829)"),
// failed dedust swap
tongo.MustParseBlockID("(0,7000000000000000,45592983)"),
// stonfi v2 swap simple
tongo.MustParseBlockID("(0,6000000000000000,46034062)"),
tongo.MustParseBlockID("(0,e000000000000000,46027828)"),
tongo.MustParseBlockID("(0,9000000000000000,45998794)"),
tongo.MustParseBlockID("(0,6000000000000000,46034070)"),
tongo.MustParseBlockID("(0,6000000000000000,46034067)"),
// stonfi v2 swap with ref
tongo.MustParseBlockID("(0,2000000000000000,46145069)"),
tongo.MustParseBlockID("(0,6000000000000000,46151880)"),
tongo.MustParseBlockID("(0,2000000000000000,46145074)"),
}),
)

Expand Down Expand Up @@ -411,6 +421,16 @@ func TestFindActions(t *testing.T) {
hash: "887c7763f41ca4a4b9de28900ab514caabc0c27ed5b41d9918d60f5e7f4a9d96",
filenamePrefix: "failed-dedust-swap",
},
{
name: "stonfi v2 swap simple",
hash: "3fa256638e5f6cd356afa70eb37c89de80846973dea0c9c46adf4df5cca39a68",
filenamePrefix: "stonfi-v2-swap-simple",
},
{
name: "stonfi v2 swap with ref",
hash: "d70fddb4786c04932669bf589ee73c16293115a1927dfbee5b719304232e2e1b",
filenamePrefix: "stonfi-v2-swap-ref",
},
} {
t.Run(c.name, func(t *testing.T) {
trace, err := storage.GetTrace(context.Background(), tongo.MustParseHash(c.hash))
Expand Down
116 changes: 116 additions & 0 deletions pkg/bath/stonfi.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package bath

import (
"github.com/tonkeeper/tongo/tlb"
"github.com/tonkeeper/tongo/ton"
"math/big"

Expand Down Expand Up @@ -122,6 +123,121 @@ var StonfiSwapStraw = Straw[BubbleJettonSwap]{
},
}

var StonfiV2PTONStraw = Straw[BubbleJettonTransfer]{
CheckFuncs: []bubbleCheck{IsTx, HasInterface(abi.JettonWallet), HasOperation(abi.PtonTonTransferMsgOp)},
Builder: func(newAction *BubbleJettonTransfer, bubble *Bubble) error {
tx := bubble.Info.(BubbleTx)
newAction.master, _ = tx.additionalInfo.JettonMaster(tx.account.Address)
newAction.senderWallet = tx.account.Address
newAction.sender = tx.inputFrom
body := tx.decodedBody.Value.(abi.PtonTonTransferMsgBody)
newAction.amount = tlb.VarUInteger16(*big.NewInt(int64(body.TonAmount)))
newAction.isWrappedTon = true
recipient, err := ton.AccountIDFromTlb(body.RefundAddress)
if err == nil {
newAction.recipient = &Account{Address: *recipient}
}
return nil
},
SingleChild: &Straw[BubbleJettonTransfer]{
CheckFuncs: []bubbleCheck{IsTx, HasOperation(abi.JettonNotifyMsgOp)},
Builder: func(newAction *BubbleJettonTransfer, bubble *Bubble) error {
tx := bubble.Info.(BubbleTx)
newAction.success = true
body := tx.decodedBody.Value.(abi.JettonNotifyMsgBody)
newAction.amount = body.Amount
newAction.payload = body.ForwardPayload.Value
newAction.recipient = &tx.account
return nil
},
},
}

var StonfiSwapV2Straw = Straw[BubbleJettonSwap]{
CheckFuncs: []bubbleCheck{func(bubble *Bubble) bool {
jettonTx, ok := bubble.Info.(BubbleJettonTransfer)
if !ok {
return false
}
if jettonTx.sender == nil {
return false
}
if jettonTx.payload.SumType != abi.StonfiSwapV2JettonOp {
return false
}
swap, ok := jettonTx.payload.Value.(abi.StonfiSwapV2JettonPayload)
if !ok {
return false
}
to, err := ton.AccountIDFromTlb(swap.CrossSwapBody.Receiver)
if err != nil || to == nil {
return false
}
if jettonTx.sender.Address != *to { //protection against invalid swaps
return false
}
return true
}},
Builder: func(newAction *BubbleJettonSwap, bubble *Bubble) error {
newAction.Dex = Stonfi
jettonTx := bubble.Info.(BubbleJettonTransfer)
newAction.UserWallet = jettonTx.sender.Address
newAction.In.Amount = big.Int(jettonTx.amount)
newAction.In.IsTon = jettonTx.isWrappedTon
newAction.In.JettonMaster = jettonTx.master
return nil
},
SingleChild: &Straw[BubbleJettonSwap]{
CheckFuncs: []bubbleCheck{IsTx, HasOperation(abi.StonfiSwapV2MsgOp), HasInterface(abi.StonfiPoolV2)},
Builder: func(newAction *BubbleJettonSwap, bubble *Bubble) error {
tx := bubble.Info.(BubbleTx)
a, b := tx.additionalInfo.STONfiPool.Token0, tx.additionalInfo.STONfiPool.Token1
body := tx.decodedBody.Value.(abi.StonfiSwapV2MsgBody)
if body.QueryId > 0 && a.IsZero() && b.IsZero() {
return nil
}
s, err := tongo.AccountIDFromTlb(body.DexPayload.TokenWallet1)
if err != nil {
return err
}
if s != nil && *s == a {
a, b = b, a
}
newAction.In.Amount = big.Int(body.RightAmount)
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]{
CheckFuncs: []bubbleCheck{IsTx, HasOperation(abi.StonfiPayToV2MsgOp)},
Builder: func(newAction *BubbleJettonSwap, bubble *Bubble) error {
tx := bubble.Info.(BubbleTx)
newAction.Router = tx.account.Address
return nil
},
SingleChild: &Straw[BubbleJettonSwap]{
CheckFuncs: []bubbleCheck{Is(BubbleJettonTransfer{})},
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.Amount = big.Int(jettonTx.amount)
newAction.Out.IsTon = jettonTx.isWrappedTon
newAction.Success = true
return nil
},
},
},
},
}

// https://dev.tonviewer.com/transaction/e19381edd8f05922eeba3c31f4b8b4b737478b4ca7b37130bdbbfd7bfa773227
// todo: add liquidity (mint lp tokens)
var StonfiMintStraw = Straw[BubbleJettonMint]{}
2 changes: 2 additions & 0 deletions pkg/bath/straws.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,14 @@ var DefaultStraws = []Merger{
NftTransferStraw,
NftTransferNotifyStraw,
JettonTransferPTONStraw,
StonfiV2PTONStraw,
JettonTransferClassicStraw,
JettonTransferMinimalStraw,
JettonBurnStraw,
WtonMintStraw,
NftPurchaseStraw,
StonfiSwapStraw,
StonfiSwapV2Straw,
DedustSwapJettonsStraw,
DedustSwapToTONStraw,
DedustSwapFromTONStraw,
Expand Down
131 changes: 131 additions & 0 deletions pkg/bath/testdata/stonfi-v2-swap-ref.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
{
"Actions": [
{
"JettonSwap": {
"Dex": "stonfi",
"UserWallet": "0:1c764efbd8cbe157c5e67d4787d9401dffba81496934bd7a78cc52e65fcdde80",
"Router": "0:429752403644e932927d37795992d3cca69d9bb517b6bd5c07a27d256bc89b46",
"In": {
"Amount": 793509017,
"IsTon": true,
"JettonMaster": "0:671963027f7f85659ab55b821671688601cdcf1ee674fc7fbbb1a776a18d34a3",
"JettonWallet": "0:433b0d3c9cb130afd4d35f25c388fb81a201d933fd51938d8ab3c87e15090fdf"
},
"Out": {
"Amount": 4165614,
"IsTon": false,
"JettonMaster": "0:b113a994b5024a16719f69139328eb759596c38a25f59028b146fecdc3621dfe",
"JettonWallet": "0:40a0fe4e243dc71295bb6ea73491a3a020594c814ce2937219fd1a6fb308a4b5"
}
},
"Success": true,
"Type": "JettonSwap",
"BaseTransactions": [
"d5f3ba7bdec7c81c39f6944208ad4059990dea6279830b6add7e078789f0fa88",
"135f4d0e0ae8522e90d53f9df8704e10d5c52fcdc59c3b3d0f29d58c4d18c4c8",
"6a264b138e5aa033fa4da2434d10aa18ce1029c82e2bb364c37ee78142890f9a",
"2e29dd132181ab545786cf46b7a11f0a1ce6181e83d38cd7e3e4e286663a539e",
"44fb8b15c09564851dfa7518c0a0151e840fce72d25139afbfc720b1ad0302bd",
"f12140b774c67599c6c8f67880d05c9d4177df5af7ad5275a450be926e9c0993",
"3cb721a7b14724d8acc5fa82e0a735da50cf54bba25e32b4d0f2c142c782ce1e"
]
},
{
"SmartContractExec": {
"TonAttached": 36574051,
"Executor": "0:4e7714a51a18f18d5615c22e9b947c5f4b5b56b0e5deb0cac0d67a67ba259e38",
"Contract": "0:429752403644e932927d37795992d3cca69d9bb517b6bd5c07a27d256bc89b46",
"Operation": "StonfiPayVaultV2",
"Payload": "AdditionalInfo:\n Amount0Out: \"4174\"\n Amount1Out: \"0\"\n Token0Address: 0:40a0fe4e243dc71295bb6ea73491a3a020594c814ce2937219fd1a6fb308a4b5\n Token1Address: 0:433b0d3c9cb130afd4d35f25c388fb81a201d933fd51938d8ab3c87e15090fdf\nExcessesAddress: 0:1c764efbd8cbe157c5e67d4787d9401dffba81496934bd7a78cc52e65fcdde80\nOwner: 0:b089166b7d44a530cd1dcfe1e6e0a5b522a685cf53f0c60dfeb748d00eddfaa8\nQueryId: 24196457155416\n"
},
"Success": true,
"Type": "SmartContractExec",
"BaseTransactions": [
"e7a1470824486dad13292d6f8dd495f1a041c5a5ca04e4e746af79b95df96f2e"
]
},
{
"SmartContractExec": {
"TonAttached": 29176851,
"Executor": "0:429752403644e932927d37795992d3cca69d9bb517b6bd5c07a27d256bc89b46",
"Contract": "0:65eb5db66adc1d34c70e482fa0f41f079a5da255479af2b378d1e7fb219db831",
"Operation": "StonfiDepositRefFeeV2",
"Payload": "ExcessesAddress: 0:1c764efbd8cbe157c5e67d4787d9401dffba81496934bd7a78cc52e65fcdde80\nJettonAmount: \"4174\"\nQueryId: 24196457155416\n"
},
"Success": true,
"Type": "SmartContractExec",
"BaseTransactions": [
"be717a41e4e2c7870fe0c9104c4789766b382a62ab95dc403012e98609ed48d2"
]
},
{
"TonTransfer": {
"Amount": 26388041,
"Comment": null,
"EncryptedComment": null,
"Recipient": "0:1c764efbd8cbe157c5e67d4787d9401dffba81496934bd7a78cc52e65fcdde80",
"Sender": "0:65eb5db66adc1d34c70e482fa0f41f079a5da255479af2b378d1e7fb219db831",
"Refund": null
},
"Success": true,
"Type": "TonTransfer",
"BaseTransactions": [
"066e82c2a58b9dacea02d83e6017f002bcd6818f2609e54c3bc319ddd5959a0b"
]
}
],
"Accounts": [
{
"Account": "0:1c764efbd8cbe157c5e67d4787d9401dffba81496934bd7a78cc52e65fcdde80",
"Ton": -840494007,
"Fee": 4830408,
"Jettons": [
{
"Address": "0:b113a994b5024a16719f69139328eb759596c38a25f59028b146fecdc3621dfe",
"Quantity": 4165614
}
]
},
{
"Account": "0:40a0fe4e243dc71295bb6ea73491a3a020594c814ce2937219fd1a6fb308a4b5",
"Ton": -2,
"Fee": 1549602,
"Jettons": null
},
{
"Account": "0:429752403644e932927d37795992d3cca69d9bb517b6bd5c07a27d256bc89b46",
"Ton": 0,
"Fee": 22854803,
"Jettons": [
{
"Address": "0:b113a994b5024a16719f69139328eb759596c38a25f59028b146fecdc3621dfe",
"Quantity": -4165614
}
]
},
{
"Account": "0:433b0d3c9cb130afd4d35f25c388fb81a201d933fd51938d8ab3c87e15090fdf",
"Ton": 793509017,
"Fee": 4381202,
"Jettons": null
},
{
"Account": "0:4e7714a51a18f18d5615c22e9b947c5f4b5b56b0e5deb0cac0d67a67ba259e38",
"Ton": 0,
"Fee": 9723203,
"Jettons": null
},
{
"Account": "0:506e1af93741d3a8147ba27ed2bb43e48734e9b1dd56d5a8b5991919138c7dfe",
"Ton": 1170,
"Fee": 855794,
"Jettons": null
},
{
"Account": "0:65eb5db66adc1d34c70e482fa0f41f079a5da255479af2b378d1e7fb219db831",
"Ton": 0,
"Fee": 2788810,
"Jettons": null
}
]
}
Loading
Loading