Skip to content

Commit

Permalink
Emulation: obtain jetton masters for stonfi pool
Browse files Browse the repository at this point in the history
  • Loading branch information
aleksej-paschenko committed Oct 13, 2023
1 parent 2054c3e commit 6f9bf9e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 13 deletions.
9 changes: 6 additions & 3 deletions pkg/api/account_executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,34 @@ import (
"context"
"errors"

"github.com/tonkeeper/opentonapi/pkg/core"
"github.com/tonkeeper/tongo"
"github.com/tonkeeper/tongo/boc"
"github.com/tonkeeper/tongo/tlb"
"github.com/tonkeeper/tongo/tvm"

"github.com/tonkeeper/opentonapi/pkg/core"
)

type shardsAccountExecutor struct {
accounts map[tongo.AccountID]tlb.ShardAccount
configBase64 string
resolver core.LibraryResolver
executor executor
}

func newSharedAccountExecutor(accounts map[tongo.AccountID]tlb.ShardAccount, resolver core.LibraryResolver, configBase64 string) *shardsAccountExecutor {
func newSharedAccountExecutor(accounts map[tongo.AccountID]tlb.ShardAccount, executor executor, resolver core.LibraryResolver, configBase64 string) *shardsAccountExecutor {
return &shardsAccountExecutor{
accounts: accounts,
configBase64: configBase64,
resolver: resolver,
executor: executor,
}
}

func (s shardsAccountExecutor) RunSmcMethodByID(ctx context.Context, accountID tongo.AccountID, methodID int, params tlb.VmStack) (uint32, tlb.VmStack, error) {
account, ok := s.accounts[accountID]
if !ok {
return 0, nil, errors.New("address not found")
return s.executor.RunSmcMethodByID(ctx, accountID, methodID, params)
}
code, data := accountCode(account), accountData(account)
if code == nil || data == nil {
Expand Down
30 changes: 20 additions & 10 deletions pkg/api/event_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/tonkeeper/tongo/abi"
"github.com/tonkeeper/tongo/boc"
"github.com/tonkeeper/tongo/tlb"
"github.com/tonkeeper/tongo/ton"
"github.com/tonkeeper/tongo/txemulator"
tongoWallet "github.com/tonkeeper/tongo/wallet"
"golang.org/x/exp/slices"
Expand Down Expand Up @@ -259,7 +260,7 @@ func (h *Handler) EmulateMessageToAccountEvent(ctx context.Context, request *oas
if err != nil {
return nil, toError(http.StatusInternalServerError, err)
}
trace, err := emulatedTreeToTrace(ctx, h.storage, configBase64, tree, emulator.FinalStates())
trace, err := emulatedTreeToTrace(ctx, h.executor, h.storage, configBase64, tree, emulator.FinalStates())
if err != nil {
return nil, toError(http.StatusInternalServerError, err)
}
Expand Down Expand Up @@ -299,7 +300,7 @@ func (h *Handler) EmulateMessageToEvent(ctx context.Context, request *oas.Emulat
if err != nil {
return nil, toError(http.StatusInternalServerError, err)
}
trace, err := emulatedTreeToTrace(ctx, h.storage, configBase64, tree, emulator.FinalStates())
trace, err := emulatedTreeToTrace(ctx, h.executor, h.storage, configBase64, tree, emulator.FinalStates())
if err != nil {
return nil, toError(http.StatusInternalServerError, err)
}
Expand Down Expand Up @@ -339,7 +340,7 @@ func (h *Handler) EmulateMessageToTrace(ctx context.Context, request *oas.Emulat
if err != nil {
return nil, toError(http.StatusInternalServerError, err)
}
trace, err := emulatedTreeToTrace(ctx, h.storage, configBase64, tree, emulator.FinalStates())
trace, err := emulatedTreeToTrace(ctx, h.executor, h.storage, configBase64, tree, emulator.FinalStates())
if err != nil {
return nil, toError(http.StatusInternalServerError, err)
}
Expand Down Expand Up @@ -408,7 +409,7 @@ func (h *Handler) EmulateMessageToWallet(ctx context.Context, request *oas.Emula
if err != nil {
return nil, toError(http.StatusInternalServerError, err)
}
trace, err := emulatedTreeToTrace(ctx, h.storage, configBase64, tree, emulator.FinalStates())
trace, err := emulatedTreeToTrace(ctx, h.executor, h.storage, configBase64, tree, emulator.FinalStates())
if err != nil {
return nil, toError(http.StatusInternalServerError, err)
}
Expand Down Expand Up @@ -468,7 +469,7 @@ func (h *Handler) addToMempool(bytesBoc []byte, shardAccount map[tongo.AccountID
return shardAccount, err
}
newShardAccount := emulator.FinalStates()
trace, err := emulatedTreeToTrace(ctx, h.storage, config, tree, newShardAccount)
trace, err := emulatedTreeToTrace(ctx, h.executor, h.storage, config, tree, newShardAccount)
if err != nil {
return shardAccount, err
}
Expand Down Expand Up @@ -496,7 +497,7 @@ func (h *Handler) addToMempool(bytesBoc []byte, shardAccount map[tongo.AccountID
return newShardAccount, nil
}

func emulatedTreeToTrace(ctx context.Context, resolver core.LibraryResolver, configBase64 string, tree *txemulator.TxTree, accounts map[tongo.AccountID]tlb.ShardAccount) (*core.Trace, error) {
func emulatedTreeToTrace(ctx context.Context, executor executor, resolver core.LibraryResolver, configBase64 string, tree *txemulator.TxTree, accounts map[tongo.AccountID]tlb.ShardAccount) (*core.Trace, error) {
if !tree.TX.Msgs.InMsg.Exists {
return nil, errors.New("there is no incoming message in emulation result")
}
Expand Down Expand Up @@ -529,7 +530,7 @@ func emulatedTreeToTrace(ctx context.Context, resolver core.LibraryResolver, con
AdditionalInfo: &core.TraceAdditionalInfo{},
}
for i := range tree.Children {
child, err := emulatedTreeToTrace(ctx, resolver, configBase64, tree.Children[i], accounts)
child, err := emulatedTreeToTrace(ctx, executor, resolver, configBase64, tree.Children[i], accounts)
if err != nil {
return nil, err
}
Expand All @@ -544,11 +545,12 @@ func emulatedTreeToTrace(ctx context.Context, resolver core.LibraryResolver, con
if err != nil {
return nil, err
}
executor := newSharedAccountExecutor(accounts, resolver, configBase64)
inspectionResult, err := abi.NewContractInspector().InspectContract(ctx, b, executor, accountID)
sharedExecutor := newSharedAccountExecutor(accounts, executor, resolver, configBase64)
inspectionResult, err := abi.NewContractInspector().InspectContract(ctx, b, sharedExecutor, accountID)
if err != nil {
return nil, err
}
// TODO: for all obtained Jetton Masters confirm that jetton wallets are valid
t.AccountInterfaces = inspectionResult.ContractInterfaces
for _, m := range inspectionResult.GetMethods {
switch data := m.Result.(type) {
Expand Down Expand Up @@ -594,7 +596,15 @@ func emulatedTreeToTrace(ctx context.Context, resolver core.LibraryResolver, con
Token0: *t0,
Token1: *t1,
}
// TODO: find out masters of t0, t1
for _, accountID := range []ton.AccountID{*t0, *t1} {
_, value, err := abi.GetWalletData(ctx, sharedExecutor, accountID)
if err != nil {
return nil, err
}
data := value.(abi.GetWalletDataResult)
master, _ := tongo.AccountIDFromTlb(data.Jetton)
t.AdditionalInfo.SetJettonMaster(accountID, *master)
}
}
}
return t, nil
Expand Down

0 comments on commit 6f9bf9e

Please sign in to comment.