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-06-14 16:10:20 +00:00
|
|
|
Hash eth_common.Hash
|
|
|
|
ChainID common.ChainID
|
|
|
|
From eth_common.Address // [sender]
|
|
|
|
Timestamp int64
|
|
|
|
BlkNumber int64
|
|
|
|
Success bool
|
|
|
|
MultiTransactionID MultiTransactionIDType
|
|
|
|
}
|
|
|
|
|
|
|
|
type TestTransfer struct {
|
|
|
|
TestTransaction
|
|
|
|
To eth_common.Address // [address]
|
|
|
|
Token string // used to detect type in transfers table
|
|
|
|
Value int64
|
|
|
|
}
|
|
|
|
|
|
|
|
type TestMultiTransaction struct {
|
2023-04-21 11:59:29 +00:00
|
|
|
MultiTransactionID MultiTransactionIDType
|
|
|
|
MultiTransactionType MultiTransactionType
|
2023-06-14 16:10:20 +00:00
|
|
|
FromAddress eth_common.Address
|
|
|
|
ToAddress eth_common.Address
|
|
|
|
FromToken string
|
|
|
|
ToToken string
|
|
|
|
FromAmount int64
|
|
|
|
ToAmount int64
|
|
|
|
Timestamp int64
|
|
|
|
}
|
|
|
|
|
|
|
|
func generateTestTransaction(seed int) TestTransaction {
|
|
|
|
return TestTransaction{
|
|
|
|
Hash: eth_common.HexToHash(fmt.Sprintf("0x1%d", seed)),
|
|
|
|
ChainID: common.ChainID(seed),
|
|
|
|
From: eth_common.HexToAddress(fmt.Sprintf("0x2%d", seed)),
|
|
|
|
Timestamp: int64(seed),
|
|
|
|
BlkNumber: int64(seed),
|
|
|
|
Success: true,
|
|
|
|
MultiTransactionID: NoMultiTransactionID,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func generateTestTransfer(seed int) TestTransfer {
|
|
|
|
return TestTransfer{
|
|
|
|
TestTransaction: generateTestTransaction(seed),
|
|
|
|
Token: "",
|
|
|
|
To: eth_common.HexToAddress(fmt.Sprintf("0x3%d", seed)),
|
|
|
|
Value: int64(seed),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func GenerateTestSendMultiTransaction(tr TestTransfer) TestMultiTransaction {
|
|
|
|
return TestMultiTransaction{
|
|
|
|
MultiTransactionType: MultiTransactionSend,
|
|
|
|
FromAddress: tr.From,
|
|
|
|
ToAddress: tr.To,
|
|
|
|
FromToken: tr.Token,
|
|
|
|
ToToken: tr.Token,
|
|
|
|
FromAmount: tr.Value,
|
|
|
|
Timestamp: tr.Timestamp,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func GenerateTestSwapMultiTransaction(tr TestTransfer, toToken string, toAmount int64) TestMultiTransaction {
|
|
|
|
return TestMultiTransaction{
|
|
|
|
MultiTransactionType: MultiTransactionSwap,
|
|
|
|
FromAddress: tr.From,
|
|
|
|
ToAddress: tr.To,
|
|
|
|
FromToken: tr.Token,
|
|
|
|
ToToken: toToken,
|
|
|
|
FromAmount: tr.Value,
|
|
|
|
ToAmount: toAmount,
|
|
|
|
Timestamp: tr.Timestamp,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func GenerateTestBridgeMultiTransaction(fromTr, toTr TestTransfer) TestMultiTransaction {
|
|
|
|
return TestMultiTransaction{
|
|
|
|
MultiTransactionType: MultiTransactionBridge,
|
|
|
|
FromAddress: fromTr.From,
|
|
|
|
ToAddress: toTr.To,
|
|
|
|
FromToken: fromTr.Token,
|
|
|
|
ToToken: toTr.Token,
|
|
|
|
FromAmount: fromTr.Value,
|
|
|
|
ToAmount: toTr.Value,
|
|
|
|
Timestamp: fromTr.Timestamp,
|
|
|
|
}
|
2023-04-21 11:59:29 +00:00
|
|
|
}
|
|
|
|
|
2023-06-14 16:10:20 +00:00
|
|
|
func GenerateTestTransfers(t *testing.T, db *sql.DB, firstStartIndex int, count int) (result []TestTransfer, fromAddresses, toAddresses []eth_common.Address) {
|
2023-04-21 11:59:29 +00:00
|
|
|
for i := firstStartIndex; i < (firstStartIndex + count); i++ {
|
2023-06-14 16:10:20 +00:00
|
|
|
tr := generateTestTransfer(i)
|
2023-05-28 10:40:50 +00:00
|
|
|
fromAddresses = append(fromAddresses, tr.From)
|
|
|
|
toAddresses = append(toAddresses, tr.To)
|
2023-04-21 11:59:29 +00:00
|
|
|
result = append(result, tr)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-06-14 16:10:20 +00:00
|
|
|
func InsertTestTransfer(t *testing.T, db *sql.DB, tr *TestTransfer) {
|
2023-04-21 11:59:29 +00:00
|
|
|
// Respect `FOREIGN KEY(network_id,address,blk_hash)` of `transfers` table
|
2023-05-11 07:50:07 +00:00
|
|
|
tokenType := "eth"
|
2023-06-14 16:10:20 +00:00
|
|
|
if tr.Token != "" && strings.ToUpper(tr.Token) != testutils.EthSymbol {
|
2023-05-11 07:50:07 +00:00
|
|
|
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,
|
2023-05-28 10:40:50 +00:00
|
|
|
multi_transaction_id, base_gas_fee, status
|
|
|
|
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, 0, ?, 0, ?)`,
|
2023-04-21 11:59:29 +00:00
|
|
|
tr.ChainID, tr.To, tr.BlkNumber, blkHash,
|
2023-05-28 10:40:50 +00:00
|
|
|
tr.ChainID, tr.Hash, tr.To, blkHash, &JSONBlob{}, tr.From, &JSONBlob{}, &JSONBlob{}, tokenType, tr.BlkNumber, tr.Timestamp, tr.MultiTransactionID, tr.Success)
|
2023-05-11 07:50:07 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
2023-06-14 16:10:20 +00:00
|
|
|
func InsertTestPendingTransaction(t *testing.T, db *sql.DB, tr *TestTransfer) {
|
2023-05-11 07:50:07 +00:00
|
|
|
_, 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)
|
|
|
|
}
|
|
|
|
|
2023-06-14 16:10:20 +00:00
|
|
|
func InsertTestMultiTransaction(t *testing.T, db *sql.DB, tr *TestMultiTransaction) 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(`
|
2023-06-14 16:10:20 +00:00
|
|
|
INSERT INTO multi_transactions (from_address, from_asset, from_amount, to_address, to_asset, to_amount, type, timestamp
|
|
|
|
) VALUES (?, ?, ?, ?, ?, ?, ?, ?)`,
|
|
|
|
tr.FromAddress, fromTokenType, tr.FromAmount, tr.ToAddress, toTokenType, tr.ToAmount, tr.MultiTransactionType, tr.Timestamp)
|
2023-04-21 11:59:29 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
rowID, err := result.LastInsertId()
|
|
|
|
require.NoError(t, err)
|
2023-06-14 16:10:20 +00:00
|
|
|
tr.MultiTransactionID = MultiTransactionIDType(rowID)
|
|
|
|
return tr.MultiTransactionID
|
2023-04-21 11:59:29 +00:00
|
|
|
}
|