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 test event into methods #144

Merged
merged 1 commit into from
Jul 18, 2023
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
4 changes: 4 additions & 0 deletions pkg/api/blockchain_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
66 changes: 10 additions & 56 deletions pkg/api/event_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"encoding/base64"
"errors"
"fmt"
"time"

"github.com/tonkeeper/opentonapi/pkg/bath"
"github.com/tonkeeper/opentonapi/pkg/core"
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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,
},
},
},
},
},
}
}
172 changes: 172 additions & 0 deletions pkg/api/test_events.go
Original file line number Diff line number Diff line change
@@ -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
}