2023-04-21 11:59:29 +00:00
|
|
|
package transfer
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"fmt"
|
2023-05-11 07:50:07 +00:00
|
|
|
"strings"
|
2023-04-21 11:59:29 +00:00
|
|
|
"testing"
|
|
|
|
|
2023-05-11 07:50:07 +00:00
|
|
|
eth_common "github.com/ethereum/go-ethereum/common"
|
|
|
|
"github.com/status-im/status-go/services/wallet/common"
|
|
|
|
"github.com/status-im/status-go/services/wallet/testutils"
|
2023-04-21 11:59:29 +00:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
type TestTransaction struct {
|
2023-05-11 07:50:07 +00:00
|
|
|
Hash eth_common.Hash
|
|
|
|
ChainID common.ChainID
|
|
|
|
From eth_common.Address // [sender]
|
|
|
|
To eth_common.Address // [address]
|
|
|
|
FromToken string // used to detect type in transfers table
|
|
|
|
ToToken string // only used in multi_transactions table
|
2023-04-21 11:59:29 +00:00
|
|
|
Timestamp int64
|
|
|
|
Value int64
|
|
|
|
BlkNumber int64
|
|
|
|
MultiTransactionID MultiTransactionIDType
|
|
|
|
MultiTransactionType MultiTransactionType
|
|
|
|
}
|
|
|
|
|
|
|
|
func GenerateTestTransactions(t *testing.T, db *sql.DB, firstStartIndex int, count int) (result []TestTransaction) {
|
|
|
|
for i := firstStartIndex; i < (firstStartIndex + count); i++ {
|
|
|
|
tr := TestTransaction{
|
2023-05-11 07:50:07 +00:00
|
|
|
Hash: eth_common.HexToHash(fmt.Sprintf("0x1%d", i)),
|
|
|
|
ChainID: common.ChainID(i),
|
|
|
|
From: eth_common.HexToAddress(fmt.Sprintf("0x2%d", i)),
|
|
|
|
To: eth_common.HexToAddress(fmt.Sprintf("0x3%d", i)),
|
2023-04-21 11:59:29 +00:00
|
|
|
Timestamp: int64(i),
|
|
|
|
Value: int64(i),
|
|
|
|
BlkNumber: int64(i),
|
|
|
|
MultiTransactionID: NoMultiTransactionID,
|
|
|
|
MultiTransactionType: MultiTransactionSend,
|
|
|
|
}
|
|
|
|
result = append(result, tr)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func InsertTestTransfer(t *testing.T, db *sql.DB, tr *TestTransaction) {
|
|
|
|
// Respect `FOREIGN KEY(network_id,address,blk_hash)` of `transfers` table
|
2023-05-11 07:50:07 +00:00
|
|
|
tokenType := "eth"
|
|
|
|
if tr.FromToken != "" && strings.ToUpper(tr.FromToken) != testutils.EthSymbol {
|
|
|
|
tokenType = "erc20"
|
|
|
|
}
|
|
|
|
blkHash := eth_common.HexToHash("4")
|
2023-04-21 11:59:29 +00:00
|
|
|
_, err := db.Exec(`
|
|
|
|
INSERT OR IGNORE INTO blocks(
|
|
|
|
network_id, address, blk_number, blk_hash
|
|
|
|
) VALUES (?, ?, ?, ?);
|
|
|
|
INSERT INTO transfers (network_id, hash, address, blk_hash, tx,
|
|
|
|
sender, receipt, log, type, blk_number, timestamp, loaded,
|
|
|
|
multi_transaction_id, base_gas_fee
|
2023-05-11 07:50:07 +00:00
|
|
|
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, 0, ?, 0)`,
|
2023-04-21 11:59:29 +00:00
|
|
|
tr.ChainID, tr.To, tr.BlkNumber, blkHash,
|
2023-05-11 07:50:07 +00:00
|
|
|
tr.ChainID, tr.Hash, tr.To, blkHash, &JSONBlob{}, tr.From, &JSONBlob{}, &JSONBlob{}, tokenType, tr.BlkNumber, tr.Timestamp, tr.MultiTransactionID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func InsertTestPendingTransaction(t *testing.T, db *sql.DB, tr *TestTransaction) {
|
|
|
|
_, err := db.Exec(`
|
|
|
|
INSERT INTO pending_transactions (network_id, hash, timestamp, from_address, to_address,
|
|
|
|
symbol, gas_price, gas_limit, value, data, type, additional_data, multi_transaction_id
|
|
|
|
) VALUES (?, ?, ?, ?, ?, 'ETH', 0, 0, ?, '', 'test', '', ?)`,
|
|
|
|
tr.ChainID, tr.Hash, tr.Timestamp, tr.From, tr.To, tr.Value, tr.MultiTransactionID)
|
2023-04-21 11:59:29 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func InsertTestMultiTransaction(t *testing.T, db *sql.DB, tr *TestTransaction) MultiTransactionIDType {
|
2023-05-11 07:50:07 +00:00
|
|
|
fromTokenType := tr.FromToken
|
|
|
|
if tr.FromToken == "" {
|
|
|
|
fromTokenType = testutils.EthSymbol
|
|
|
|
}
|
|
|
|
toTokenType := tr.ToToken
|
|
|
|
if tr.ToToken == "" {
|
|
|
|
toTokenType = testutils.EthSymbol
|
|
|
|
}
|
2023-04-21 11:59:29 +00:00
|
|
|
result, err := db.Exec(`
|
|
|
|
INSERT INTO multi_transactions (from_address, from_asset, from_amount, to_address, to_asset, type, timestamp
|
2023-05-11 07:50:07 +00:00
|
|
|
) VALUES (?, ?, 0, ?, ?, ?, ?)`,
|
|
|
|
tr.From, fromTokenType, tr.To, toTokenType, tr.MultiTransactionType, tr.Timestamp)
|
2023-04-21 11:59:29 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
rowID, err := result.LastInsertId()
|
|
|
|
require.NoError(t, err)
|
|
|
|
return MultiTransactionIDType(rowID)
|
|
|
|
}
|