diff --git a/pkg/api/blockchain_handlers.go b/pkg/api/blockchain_handlers.go index 5834f107..716a9e6d 100644 --- a/pkg/api/blockchain_handlers.go +++ b/pkg/api/blockchain_handlers.go @@ -33,6 +33,10 @@ func (h Handler) GetTransaction(ctx context.Context, params oas.GetTransactionPa if err != nil { return &oas.BadRequest{Error: err.Error()}, nil } + if hash.Hex() == testEventID { + testTx := getTestTransaction() + return &testTx, nil + } txs, err := h.storage.GetTransaction(ctx, hash) if errors.Is(err, core.ErrEntityNotFound) { return &oas.NotFound{Error: "transaction not found"}, nil diff --git a/pkg/api/event_handlers.go b/pkg/api/event_handlers.go index 74c9d568..ae55db87 100644 --- a/pkg/api/event_handlers.go +++ b/pkg/api/event_handlers.go @@ -5,7 +5,6 @@ import ( "encoding/base64" "errors" "fmt" - "time" "github.com/tonkeeper/opentonapi/pkg/bath" "github.com/tonkeeper/opentonapi/pkg/core" @@ -37,6 +36,10 @@ func (h Handler) GetTrace(ctx context.Context, params oas.GetTraceParams) (r oas if err != nil { return &oas.BadRequest{Error: err.Error()}, nil } + if hash.Hex() == testEventID { + testTrace := getTestTrace() + return &testTrace, nil + } t, err := h.storage.GetTrace(ctx, hash) if errors.Is(err, core.ErrEntityNotFound) { txHash, err2 := h.storage.SearchTransactionByMessageHash(ctx, hash) @@ -57,6 +60,10 @@ func (h Handler) GetEvent(ctx context.Context, params oas.GetEventParams) (oas.G if err != nil { return &oas.BadRequest{Error: err.Error()}, nil } + if traceID.Hex() == testEventID { + testEvent := getTestEvent() + return &testEvent, nil + } trace, err := h.storage.GetTrace(ctx, traceID) if errors.Is(err, core.ErrEntityNotFound) { txHash, err2 := h.storage.SearchTransactionByMessageHash(ctx, traceID) @@ -105,8 +112,8 @@ func (h Handler) GetEventsByAccount(ctx context.Context, params oas.GetEventsByA } lastLT = trace.Lt } - if account.ToRaw() == "0:2cf3b5b8c891e517c9addbda1c0386a09ccacbb0e3faf630b51cfc8152325acb" { - events = slices.Insert(events, 0, generateTestEvent()) + if account.ToRaw() == testEventAccount { + events = slices.Insert(events, 0, getTestAccountEvent()) } for i, j := 0, len(events)-1; i < j; i, j = i+1, j-1 { events[i], events[j] = events[j], events[i] @@ -319,56 +326,3 @@ func emulatedTreeToTrace(tree *txemulator.TxTree, accounts map[tongo.AccountID]t } return t, nil } - -func generateTestEvent() oas.AccountEvent { - return oas.AccountEvent{ - EventID: "a96d84940781cc29d3fb890384d35ba49cdd9d891a123a9f90939ddb57b09fc2", - Account: oas.AccountAddress{ - Address: "0:2cf3b5b8c891e517c9addbda1c0386a09ccacbb0e3faf630b51cfc8152325acb", - IsScam: false, - }, - Timestamp: time.Now().Unix(), - IsScam: false, - Lt: int64(39228825000001), - InProgress: true, - Extra: -5825767, - Actions: []oas.Action{ - { - Type: oas.ActionTypeTonTransfer, - Status: oas.ActionStatusOk, - TonTransfer: oas.OptTonTransferAction{ - Set: true, - Value: oas.TonTransferAction{ - Sender: oas.AccountAddress{ - Address: "0:2cf3b5b8c891e517c9addbda1c0386a09ccacbb0e3faf630b51cfc8152325acb", - IsScam: false, - }, - Recipient: oas.AccountAddress{ - Address: "0:e9a07c65998cd537d6ac2c4c9ddd73a299295527101328c87358508ccbf868fa", - IsScam: false, - }, - Amount: 10_000_000_000, - }, - }, - SimplePreview: oas.ActionSimplePreview{ - Name: "Ton Transfer", - Description: "Transferring 10_000_000_000 TON", - Value: oas.OptString{ - Set: true, - Value: "10_000_000_000 TON", - }, - Accounts: []oas.AccountAddress{ - { - Address: "0:2cf3b5b8c891e517c9addbda1c0386a09ccacbb0e3faf630b51cfc8152325acb", - IsScam: false, - }, - { - Address: "0:e9a07c65998cd537d6ac2c4c9ddd73a299295527101328c87358508ccbf868fa", - IsScam: false, - }, - }, - }, - }, - }, - } -} diff --git a/pkg/api/test_events.go b/pkg/api/test_events.go new file mode 100644 index 00000000..ad88b9cd --- /dev/null +++ b/pkg/api/test_events.go @@ -0,0 +1,172 @@ +package api + +import ( + "time" + + "github.com/tonkeeper/opentonapi/pkg/oas" +) + +// + +const ( + testEventAccount = "0:2cf3b5b8c891e517c9addbda1c0386a09ccacbb0e3faf630b51cfc8152325acb" + testRecipientAccount = "0:e9a07c65998cd537d6ac2c4c9ddd73a299295527101328c87358508ccbf868fa" + testEventID = "a96d84940781cc29d3fb890384d35ba49cdd9d891a123a9f90939ddb57b09fc2" +) + +func getTestActions() []oas.Action { + return []oas.Action{ + { + Type: oas.ActionTypeTonTransfer, + Status: oas.ActionStatusOk, + TonTransfer: oas.OptTonTransferAction{ + Set: true, + Value: oas.TonTransferAction{ + Sender: oas.AccountAddress{ + Address: testEventAccount, + IsScam: false, + }, + Recipient: oas.AccountAddress{ + Address: testRecipientAccount, + IsScam: false, + }, + Amount: 10_000_000, + }, + }, + SimplePreview: oas.ActionSimplePreview{ + Name: "Ton Transfer", + Description: "Transferring 10_000_000 TON", + Value: oas.OptString{ + Set: true, + Value: "10_000_000 TON", + }, + Accounts: []oas.AccountAddress{ + { + Address: testEventAccount, + IsScam: false, + }, + { + Address: testRecipientAccount, + IsScam: false, + }, + }, + }, + }, + } +} + +func getTestAccountEvent() oas.AccountEvent { + return oas.AccountEvent{ + EventID: testEventID, + Account: oas.AccountAddress{ + Address: testEventAccount, + IsScam: false, + }, + Timestamp: time.Now().Unix(), + IsScam: false, + Lt: int64(39228825000001), + InProgress: true, + Extra: -5825767, + Actions: getTestActions(), + } +} + +func getTestEvent() oas.Event { + now := time.Now().Unix() + event := oas.Event{ + EventID: testEventID, + Timestamp: now, + Actions: getTestActions(), + ValueFlow: []oas.ValueFlow{ + { + Account: oas.AccountAddress{ + Address: testEventAccount, + IsScam: false, + }, + Ton: -10_000_000, + Fees: 1, + }, + { + Account: oas.AccountAddress{ + Address: testRecipientAccount, + IsScam: false, + }, + Ton: 10_000_000, + Fees: 1, + }, + }, + IsScam: false, + Lt: int64(39226975000001), + InProgress: true, + } + return event +} + +func getTestTrace() oas.Trace { + trace := oas.Trace{ + Transaction: getTestTransaction(), + Interfaces: []string{"wallet_v4r2", "wallet_v4", "wallet"}, + } + return trace +} + +func getTestTransaction() oas.Transaction { + now := time.Now().Unix() + + tx := oas.Transaction{ + Hash: testEventID, + Lt: int64(39226975000001), + Account: oas.AccountAddress{ + Address: testEventAccount, + IsScam: false, + }, + Success: false, + Utime: now, + OrigStatus: oas.AccountStatusActive, + EndStatus: oas.AccountStatusActive, + TotalFees: 5266796, + TransactionType: oas.TransactionTypeTransOrd, + Aborted: false, + Destroyed: false, + InMsg: oas.OptMessage{ + Set: true, + Value: oas.Message{ + CreatedLt: 39228825000001, + Destination: oas.OptAccountAddress{ + Set: true, + Value: oas.AccountAddress{ + Address: testEventAccount, + IsScam: false, + }, + }, + CreatedAt: now, + RawBody: oas.OptString{Set: true, Value: "b5ee9c720101...0000000000"}, + }, + }, + OutMsgs: []oas.Message{ + { + CreatedLt: 39228825000002, + IhrDisabled: true, + Value: 10_000_000, + FwdFee: 1, + Destination: oas.OptAccountAddress{ + Set: true, + Value: oas.AccountAddress{ + Address: testRecipientAccount, + IsScam: false, + }, + }, + Source: oas.OptAccountAddress{ + Set: true, + Value: oas.AccountAddress{ + Address: testEventAccount, + IsScam: false, + }, + }, + CreatedAt: now, + }, + }, + } + + return tx +}