2023-04-21 11:59:29 +00:00
|
|
|
package transfer
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"fmt"
|
2023-06-16 10:28:16 +00:00
|
|
|
"math/big"
|
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"
|
2023-06-16 10:28:16 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common/hexutil"
|
2023-10-11 05:10:08 +00:00
|
|
|
"github.com/ethereum/go-ethereum/core/types"
|
2023-09-21 06:58:36 +00:00
|
|
|
|
2024-01-31 19:06:03 +00:00
|
|
|
"github.com/status-im/status-go/services/wallet/bigint"
|
2023-05-11 07:50:07 +00:00
|
|
|
"github.com/status-im/status-go/services/wallet/common"
|
|
|
|
"github.com/status-im/status-go/services/wallet/testutils"
|
2023-06-13 09:25:23 +00:00
|
|
|
"github.com/status-im/status-go/services/wallet/token"
|
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
|
2023-08-24 12:23:40 +00:00
|
|
|
Nonce uint64
|
|
|
|
Contract eth_common.Address
|
2024-03-12 09:15:30 +00:00
|
|
|
MultiTransactionID common.MultiTransactionIDType
|
2023-06-14 16:10:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type TestTransfer struct {
|
|
|
|
TestTransaction
|
|
|
|
To eth_common.Address // [address]
|
|
|
|
Value int64
|
2023-06-13 09:25:23 +00:00
|
|
|
Token *token.Token
|
2023-06-14 16:10:20 +00:00
|
|
|
}
|
|
|
|
|
2024-05-26 08:31:13 +00:00
|
|
|
type TestCollectibleTransfer struct {
|
|
|
|
TestTransfer
|
|
|
|
TestCollectible
|
|
|
|
}
|
|
|
|
|
2023-06-13 09:25:23 +00:00
|
|
|
func SeedToToken(seed int) *token.Token {
|
|
|
|
tokenIndex := seed % len(TestTokens)
|
|
|
|
return TestTokens[tokenIndex]
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTrToToken(t *testing.T, tt *TestTransaction) (token *token.Token, isNative bool) {
|
|
|
|
// Sanity check that none of the markers changed and they should be equal to seed
|
|
|
|
require.Equal(t, tt.Timestamp, tt.BlkNumber)
|
|
|
|
|
|
|
|
tokenIndex := int(tt.Timestamp) % len(TestTokens)
|
|
|
|
isNative = testutils.SliceContains(NativeTokenIndices, tokenIndex)
|
|
|
|
|
|
|
|
return TestTokens[tokenIndex], isNative
|
|
|
|
}
|
|
|
|
|
2023-06-14 16:10:20 +00:00
|
|
|
func generateTestTransaction(seed int) TestTransaction {
|
2023-06-13 09:25:23 +00:00
|
|
|
token := SeedToToken(seed)
|
2023-06-14 16:10:20 +00:00
|
|
|
return TestTransaction{
|
2023-09-14 21:50:51 +00:00
|
|
|
Hash: eth_common.HexToHash(fmt.Sprintf("0x1%d", seed)),
|
|
|
|
ChainID: common.ChainID(token.ChainID),
|
|
|
|
From: eth_common.HexToAddress(fmt.Sprintf("0x2%d", seed)),
|
|
|
|
Timestamp: int64(seed),
|
|
|
|
BlkNumber: int64(seed),
|
|
|
|
Success: true,
|
|
|
|
Nonce: uint64(seed),
|
|
|
|
// In practice this is last20Bytes(Keccak256(RLP(From, nonce)))
|
|
|
|
Contract: eth_common.HexToAddress(fmt.Sprintf("0x4%d", seed)),
|
2024-03-12 09:15:30 +00:00
|
|
|
MultiTransactionID: common.NoMultiTransactionID,
|
2023-06-14 16:10:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func generateTestTransfer(seed int) TestTransfer {
|
2023-06-13 09:25:23 +00:00
|
|
|
tokenIndex := seed % len(TestTokens)
|
|
|
|
token := TestTokens[tokenIndex]
|
2023-06-14 16:10:20 +00:00
|
|
|
return TestTransfer{
|
|
|
|
TestTransaction: generateTestTransaction(seed),
|
|
|
|
To: eth_common.HexToAddress(fmt.Sprintf("0x3%d", seed)),
|
|
|
|
Value: int64(seed),
|
2023-06-13 09:25:23 +00:00
|
|
|
Token: token,
|
2023-06-14 16:10:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-26 08:31:13 +00:00
|
|
|
// Will be used in tests to generate a collectible transfer
|
|
|
|
// nolint:unused
|
|
|
|
func generateTestCollectibleTransfer(seed int) TestCollectibleTransfer {
|
|
|
|
collectibleIndex := seed % len(TestCollectibles)
|
|
|
|
collectible := TestCollectibles[collectibleIndex]
|
|
|
|
tr := TestCollectibleTransfer{
|
|
|
|
TestTransfer: TestTransfer{
|
|
|
|
TestTransaction: generateTestTransaction(seed),
|
|
|
|
To: eth_common.HexToAddress(fmt.Sprintf("0x3%d", seed)),
|
|
|
|
Value: int64(seed),
|
|
|
|
Token: &token.Token{
|
|
|
|
Address: collectible.TokenAddress,
|
|
|
|
Name: "Collectible",
|
|
|
|
ChainID: uint64(collectible.ChainID),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
TestCollectible: collectible,
|
|
|
|
}
|
|
|
|
tr.TestTransaction.ChainID = collectible.ChainID
|
|
|
|
return tr
|
|
|
|
}
|
|
|
|
|
2024-05-03 10:46:46 +00:00
|
|
|
func GenerateTestSendMultiTransaction(tr TestTransfer) MultiTransaction {
|
|
|
|
return MultiTransaction{
|
2024-05-23 16:22:57 +00:00
|
|
|
ID: multiTransactionIDGenerator(),
|
2024-05-03 10:46:46 +00:00
|
|
|
Type: MultiTransactionSend,
|
|
|
|
FromAddress: tr.From,
|
|
|
|
ToAddress: tr.To,
|
|
|
|
FromAsset: tr.Token.Symbol,
|
|
|
|
ToAsset: tr.Token.Symbol,
|
|
|
|
FromAmount: (*hexutil.Big)(big.NewInt(tr.Value)),
|
|
|
|
ToAmount: (*hexutil.Big)(big.NewInt(0)),
|
|
|
|
Timestamp: uint64(tr.Timestamp),
|
2023-06-14 16:10:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-03 10:46:46 +00:00
|
|
|
func GenerateTestSwapMultiTransaction(tr TestTransfer, toToken string, toAmount int64) MultiTransaction {
|
|
|
|
return MultiTransaction{
|
2024-05-23 16:22:57 +00:00
|
|
|
ID: multiTransactionIDGenerator(),
|
2024-05-03 10:46:46 +00:00
|
|
|
Type: MultiTransactionSwap,
|
|
|
|
FromAddress: tr.From,
|
|
|
|
ToAddress: tr.To,
|
|
|
|
FromAsset: tr.Token.Symbol,
|
|
|
|
ToAsset: toToken,
|
|
|
|
FromAmount: (*hexutil.Big)(big.NewInt(tr.Value)),
|
|
|
|
ToAmount: (*hexutil.Big)(big.NewInt(toAmount)),
|
|
|
|
Timestamp: uint64(tr.Timestamp),
|
2023-06-14 16:10:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-03 10:46:46 +00:00
|
|
|
func GenerateTestBridgeMultiTransaction(fromTr, toTr TestTransfer) MultiTransaction {
|
|
|
|
return MultiTransaction{
|
2024-05-23 16:22:57 +00:00
|
|
|
ID: multiTransactionIDGenerator(),
|
2024-05-03 10:46:46 +00:00
|
|
|
Type: MultiTransactionBridge,
|
|
|
|
FromAddress: fromTr.From,
|
|
|
|
ToAddress: toTr.To,
|
|
|
|
FromAsset: fromTr.Token.Symbol,
|
|
|
|
ToAsset: toTr.Token.Symbol,
|
|
|
|
FromAmount: (*hexutil.Big)(big.NewInt(fromTr.Value)),
|
|
|
|
ToAmount: (*hexutil.Big)(big.NewInt(toTr.Value)),
|
|
|
|
Timestamp: uint64(fromTr.Timestamp),
|
2023-06-14 16:10:20 +00:00
|
|
|
}
|
2023-04-21 11:59:29 +00:00
|
|
|
}
|
|
|
|
|
2024-06-05 03:35:43 +00:00
|
|
|
func GenerateTestApproveMultiTransaction(tr TestTransfer) MultiTransaction {
|
|
|
|
return MultiTransaction{
|
|
|
|
ID: multiTransactionIDGenerator(),
|
|
|
|
Type: MultiTransactionApprove,
|
|
|
|
FromAddress: tr.From,
|
|
|
|
ToAddress: tr.To,
|
|
|
|
FromAsset: tr.Token.Symbol,
|
|
|
|
ToAsset: tr.Token.Symbol,
|
|
|
|
FromAmount: (*hexutil.Big)(big.NewInt(tr.Value)),
|
|
|
|
ToAmount: (*hexutil.Big)(big.NewInt(0)),
|
|
|
|
Timestamp: uint64(tr.Timestamp),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-13 09:25:23 +00:00
|
|
|
// GenerateTestTransfers will generate transaction based on the TestTokens index and roll over if there are more than
|
|
|
|
// len(TestTokens) transactions
|
2023-07-10 15:00:35 +00:00
|
|
|
func GenerateTestTransfers(tb testing.TB, 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-09-21 06:58:36 +00:00
|
|
|
type TestCollectible struct {
|
|
|
|
TokenAddress eth_common.Address
|
|
|
|
TokenID *big.Int
|
|
|
|
ChainID common.ChainID
|
|
|
|
}
|
|
|
|
|
|
|
|
var TestCollectibles = []TestCollectible{
|
|
|
|
TestCollectible{
|
|
|
|
TokenAddress: eth_common.HexToAddress("0x97a04fda4d97c6e3547d66b572e29f4a4ff40392"),
|
|
|
|
TokenID: big.NewInt(1),
|
|
|
|
ChainID: 1,
|
|
|
|
},
|
|
|
|
TestCollectible{ // Same token ID as above but different address
|
|
|
|
TokenAddress: eth_common.HexToAddress("0x2cec8879915cdbd80c88d8b1416aa9413a24ddfa"),
|
|
|
|
TokenID: big.NewInt(1),
|
|
|
|
ChainID: 1,
|
|
|
|
},
|
2024-02-22 18:46:21 +00:00
|
|
|
TestCollectible{ // TokenID (big.Int) value 0 might be problematic if not handled properly
|
|
|
|
TokenAddress: eth_common.HexToAddress("0x97a04fda4d97c6e3547d66b572e29f4a4ff4ABCD"),
|
|
|
|
TokenID: big.NewInt(0),
|
|
|
|
ChainID: 420,
|
|
|
|
},
|
2023-09-21 06:58:36 +00:00
|
|
|
TestCollectible{
|
|
|
|
TokenAddress: eth_common.HexToAddress("0x1dea7a3e04849840c0eb15fd26a55f6c40c4a69b"),
|
|
|
|
TokenID: big.NewInt(11),
|
|
|
|
ChainID: 5,
|
|
|
|
},
|
|
|
|
TestCollectible{ // Same address as above but different token ID
|
|
|
|
TokenAddress: eth_common.HexToAddress("0x1dea7a3e04849840c0eb15fd26a55f6c40c4a69b"),
|
|
|
|
TokenID: big.NewInt(12),
|
|
|
|
ChainID: 5,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2023-06-13 09:25:23 +00:00
|
|
|
var EthMainnet = token.Token{
|
|
|
|
Address: eth_common.HexToAddress("0x"),
|
|
|
|
Name: "Ether",
|
|
|
|
Symbol: "ETH",
|
|
|
|
ChainID: 1,
|
|
|
|
}
|
|
|
|
|
2024-10-15 07:29:21 +00:00
|
|
|
var EthSepolia = token.Token{
|
2023-06-13 09:25:23 +00:00
|
|
|
Address: eth_common.HexToAddress("0x"),
|
|
|
|
Name: "Ether",
|
|
|
|
Symbol: "ETH",
|
2024-10-15 07:29:21 +00:00
|
|
|
ChainID: 11155111,
|
2023-06-13 09:25:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var EthOptimism = token.Token{
|
|
|
|
Address: eth_common.HexToAddress("0x"),
|
|
|
|
Name: "Ether",
|
|
|
|
Symbol: "ETH",
|
|
|
|
ChainID: 10,
|
|
|
|
}
|
|
|
|
|
|
|
|
var UsdcMainnet = token.Token{
|
|
|
|
Address: eth_common.HexToAddress("0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48"),
|
|
|
|
Name: "USD Coin",
|
|
|
|
Symbol: "USDC",
|
|
|
|
ChainID: 1,
|
|
|
|
}
|
|
|
|
|
2024-10-15 07:29:21 +00:00
|
|
|
var UsdcSepolia = token.Token{
|
|
|
|
Address: eth_common.HexToAddress("0x1c7D4B196Cb0C7B01d743Fbc6116a902379C7238"),
|
|
|
|
Name: "USD Coin",
|
|
|
|
Symbol: "USDC",
|
|
|
|
Decimals: 6,
|
|
|
|
ChainID: 11155111,
|
2023-06-13 09:25:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var UsdcOptimism = token.Token{
|
|
|
|
Address: eth_common.HexToAddress("0x7f5c764cbc14f9669b88837ca1490cca17c31607"),
|
|
|
|
Name: "USD Coin",
|
|
|
|
Symbol: "USDC",
|
|
|
|
ChainID: 10,
|
|
|
|
}
|
|
|
|
|
|
|
|
var SntMainnet = token.Token{
|
|
|
|
Address: eth_common.HexToAddress("0x744d70fdbe2ba4cf95131626614a1763df805b9e"),
|
|
|
|
Name: "Status Network Token",
|
|
|
|
Symbol: "SNT",
|
|
|
|
ChainID: 1,
|
|
|
|
}
|
|
|
|
|
|
|
|
var DaiMainnet = token.Token{
|
|
|
|
Address: eth_common.HexToAddress("0xf2edF1c091f683E3fb452497d9a98A49cBA84666"),
|
|
|
|
Name: "DAI Stablecoin",
|
|
|
|
Symbol: "DAI",
|
|
|
|
ChainID: 5,
|
|
|
|
}
|
|
|
|
|
2024-10-15 07:29:21 +00:00
|
|
|
var DaiSepolia = token.Token{
|
|
|
|
Address: eth_common.HexToAddress("0x3e622317f8c93f7328350cf0b56d9ed4c620c5d6"),
|
|
|
|
Name: "DAI Stablecoin",
|
|
|
|
Symbol: "DAI",
|
|
|
|
Decimals: 18,
|
|
|
|
ChainID: 11155111,
|
2023-06-13 09:25:23 +00:00
|
|
|
}
|
|
|
|
|
2024-10-15 07:29:21 +00:00
|
|
|
// TestTokens contains ETH/Mainnet, ETH/Sepolia, ETH/Optimism, USDC/Mainnet, USDC/Sepolia, USDC/Optimism, SNT/Mainnet, DAI/Mainnet, DAI/Sepolia
|
2023-06-13 09:25:23 +00:00
|
|
|
var TestTokens = []*token.Token{
|
2024-10-15 07:29:21 +00:00
|
|
|
&EthMainnet, &EthSepolia, &EthOptimism, &UsdcMainnet, &UsdcSepolia, &UsdcOptimism, &SntMainnet, &DaiMainnet, &DaiSepolia,
|
2023-06-13 09:25:23 +00:00
|
|
|
}
|
|
|
|
|
2024-01-26 04:31:18 +00:00
|
|
|
func LookupTokenIdentity(chainID uint64, address eth_common.Address, native bool) *token.Token {
|
|
|
|
for _, token := range TestTokens {
|
|
|
|
if token.ChainID == chainID && token.Address == address && token.IsNative() == native {
|
|
|
|
return token
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-06-13 09:25:23 +00:00
|
|
|
var NativeTokenIndices = []int{0, 1, 2}
|
|
|
|
|
2023-07-10 15:00:35 +00:00
|
|
|
func InsertTestTransfer(tb testing.TB, db *sql.DB, address eth_common.Address, tr *TestTransfer) {
|
2023-06-13 09:25:23 +00:00
|
|
|
token := TestTokens[int(tr.Timestamp)%len(TestTokens)]
|
2023-07-10 15:00:35 +00:00
|
|
|
InsertTestTransferWithOptions(tb, db, address, tr, &TestTransferOptions{
|
2023-06-27 22:49:02 +00:00
|
|
|
TokenAddress: token.Address,
|
|
|
|
})
|
2023-06-13 09:25:23 +00:00
|
|
|
}
|
|
|
|
|
2023-06-27 22:49:02 +00:00
|
|
|
type TestTransferOptions struct {
|
|
|
|
TokenAddress eth_common.Address
|
2023-08-11 17:28:46 +00:00
|
|
|
TokenID *big.Int
|
2023-06-27 22:49:02 +00:00
|
|
|
NullifyAddresses []eth_common.Address
|
2023-10-11 05:10:08 +00:00
|
|
|
Tx *types.Transaction
|
2023-10-12 10:21:03 +00:00
|
|
|
Receipt *types.Receipt
|
2023-10-11 05:10:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func GenerateTxField(data []byte) *types.Transaction {
|
|
|
|
return types.NewTx(&types.DynamicFeeTx{
|
|
|
|
Data: data,
|
|
|
|
})
|
2023-06-27 22:49:02 +00:00
|
|
|
}
|
|
|
|
|
2023-07-10 15:00:35 +00:00
|
|
|
func InsertTestTransferWithOptions(tb testing.TB, db *sql.DB, address eth_common.Address, tr *TestTransfer, opt *TestTransferOptions) {
|
2023-06-20 02:50:49 +00:00
|
|
|
var (
|
|
|
|
tx *sql.Tx
|
|
|
|
)
|
|
|
|
tx, err := db.Begin()
|
2023-07-10 15:00:35 +00:00
|
|
|
require.NoError(tb, err)
|
2023-06-20 02:50:49 +00:00
|
|
|
defer func() {
|
|
|
|
if err == nil {
|
|
|
|
err = tx.Commit()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
_ = tx.Rollback()
|
|
|
|
}()
|
|
|
|
|
|
|
|
blkHash := eth_common.HexToHash("4")
|
|
|
|
|
|
|
|
block := blockDBFields{
|
|
|
|
chainID: uint64(tr.ChainID),
|
2023-09-11 09:54:37 +00:00
|
|
|
account: address,
|
2023-06-20 02:50:49 +00:00
|
|
|
blockNumber: big.NewInt(tr.BlkNumber),
|
|
|
|
blockHash: blkHash,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Respect `FOREIGN KEY(network_id,address,blk_hash)` of `transfers` table
|
|
|
|
err = insertBlockDBFields(tx, block)
|
2023-07-10 15:00:35 +00:00
|
|
|
require.NoError(tb, err)
|
2023-06-20 02:50:49 +00:00
|
|
|
|
|
|
|
receiptStatus := uint64(0)
|
|
|
|
if tr.Success {
|
|
|
|
receiptStatus = 1
|
|
|
|
}
|
|
|
|
|
2023-05-11 07:50:07 +00:00
|
|
|
tokenType := "eth"
|
2023-06-27 22:49:02 +00:00
|
|
|
if (opt.TokenAddress != eth_common.Address{}) {
|
2023-08-11 17:28:46 +00:00
|
|
|
if opt.TokenID == nil {
|
|
|
|
tokenType = "erc20"
|
|
|
|
} else {
|
|
|
|
tokenType = "erc721"
|
|
|
|
}
|
2023-05-11 07:50:07 +00:00
|
|
|
}
|
2023-06-13 09:25:23 +00:00
|
|
|
|
2023-06-27 22:49:02 +00:00
|
|
|
// Workaround to simulate writing of NULL values for addresses
|
|
|
|
txTo := &tr.To
|
|
|
|
txFrom := &tr.From
|
|
|
|
for i := 0; i < len(opt.NullifyAddresses); i++ {
|
|
|
|
if opt.NullifyAddresses[i] == tr.To {
|
|
|
|
txTo = nil
|
|
|
|
}
|
|
|
|
if opt.NullifyAddresses[i] == tr.From {
|
|
|
|
txFrom = nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-20 02:50:49 +00:00
|
|
|
transfer := transferDBFields{
|
|
|
|
chainID: uint64(tr.ChainID),
|
|
|
|
id: tr.Hash,
|
2023-11-30 11:37:32 +00:00
|
|
|
txHash: &tr.Hash,
|
2023-06-20 02:50:49 +00:00
|
|
|
address: address,
|
|
|
|
blockHash: blkHash,
|
|
|
|
blockNumber: big.NewInt(tr.BlkNumber),
|
|
|
|
sender: tr.From,
|
2023-11-27 10:08:17 +00:00
|
|
|
transferType: common.Type(tokenType),
|
2023-06-20 02:50:49 +00:00
|
|
|
timestamp: uint64(tr.Timestamp),
|
|
|
|
multiTransactionID: tr.MultiTransactionID,
|
|
|
|
baseGasFees: "0x0",
|
|
|
|
receiptStatus: &receiptStatus,
|
|
|
|
txValue: big.NewInt(tr.Value),
|
2023-06-27 22:49:02 +00:00
|
|
|
txFrom: txFrom,
|
|
|
|
txTo: txTo,
|
2023-08-24 12:23:40 +00:00
|
|
|
txNonce: &tr.Nonce,
|
2023-06-27 22:49:02 +00:00
|
|
|
tokenAddress: &opt.TokenAddress,
|
2023-08-24 12:23:40 +00:00
|
|
|
contractAddress: &tr.Contract,
|
2023-08-11 17:28:46 +00:00
|
|
|
tokenID: opt.TokenID,
|
2023-10-11 05:10:08 +00:00
|
|
|
transaction: opt.Tx,
|
2023-10-12 10:21:03 +00:00
|
|
|
receipt: opt.Receipt,
|
2023-06-20 02:50:49 +00:00
|
|
|
}
|
|
|
|
err = updateOrInsertTransfersDBFields(tx, []transferDBFields{transfer})
|
2023-07-10 15:00:35 +00:00
|
|
|
require.NoError(tb, err)
|
2023-05-11 07:50:07 +00:00
|
|
|
}
|
|
|
|
|
2023-07-10 15:00:35 +00:00
|
|
|
func InsertTestPendingTransaction(tb testing.TB, 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
|
2023-07-18 15:57:44 +00:00
|
|
|
) VALUES (?, ?, ?, ?, ?, 'ETH', 0, 0, ?, '', 'eth', '', ?)`,
|
2024-01-31 19:06:03 +00:00
|
|
|
tr.ChainID, tr.Hash, tr.Timestamp, tr.From, tr.To, (*bigint.SQLBigIntBytes)(big.NewInt(tr.Value)), tr.MultiTransactionID)
|
2023-07-10 15:00:35 +00:00
|
|
|
require.NoError(tb, err)
|
2023-04-21 11:59:29 +00:00
|
|
|
}
|
|
|
|
|
2024-05-03 10:46:46 +00:00
|
|
|
func InsertTestMultiTransaction(tb testing.TB, db *sql.DB, tr *MultiTransaction) common.MultiTransactionIDType {
|
|
|
|
if tr.FromAsset == "" {
|
|
|
|
tr.FromAsset = testutils.EthSymbol
|
2023-05-11 07:50:07 +00:00
|
|
|
}
|
2024-05-03 10:46:46 +00:00
|
|
|
if tr.ToAsset == "" {
|
|
|
|
tr.ToAsset = testutils.EthSymbol
|
2023-05-11 07:50:07 +00:00
|
|
|
}
|
2023-06-16 10:28:16 +00:00
|
|
|
|
2024-05-03 10:46:46 +00:00
|
|
|
tr.ID = multiTransactionIDGenerator()
|
2024-05-23 16:22:57 +00:00
|
|
|
multiTxDB := NewMultiTransactionDB(db)
|
|
|
|
err := multiTxDB.CreateMultiTransaction(tr)
|
2023-07-10 15:00:35 +00:00
|
|
|
require.NoError(tb, err)
|
2024-05-03 10:46:46 +00:00
|
|
|
return tr.ID
|
2023-04-21 11:59:29 +00:00
|
|
|
}
|
2023-11-27 10:08:17 +00:00
|
|
|
|
|
|
|
// For using in tests only outside the package
|
|
|
|
func SaveTransfersMarkBlocksLoaded(database *Database, chainID uint64, address eth_common.Address, transfers []Transfer, blocks []*big.Int) error {
|
|
|
|
return saveTransfersMarkBlocksLoaded(database.client, chainID, address, transfers, blocks)
|
|
|
|
}
|
2024-05-03 10:46:46 +00:00
|
|
|
|
|
|
|
func SetMultiTransactionIDGenerator(f func() common.MultiTransactionIDType) {
|
|
|
|
multiTransactionIDGenerator = f
|
|
|
|
}
|
|
|
|
|
|
|
|
func StaticIDCounter() (f func() common.MultiTransactionIDType) {
|
|
|
|
var i int
|
|
|
|
f = func() common.MultiTransactionIDType {
|
|
|
|
i++
|
|
|
|
return common.MultiTransactionIDType(i)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2024-05-23 16:22:57 +00:00
|
|
|
|
|
|
|
type InMemMultiTransactionStorage struct {
|
|
|
|
storage map[common.MultiTransactionIDType]*MultiTransaction
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewInMemMultiTransactionStorage() *InMemMultiTransactionStorage {
|
|
|
|
return &InMemMultiTransactionStorage{
|
|
|
|
storage: make(map[common.MultiTransactionIDType]*MultiTransaction),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *InMemMultiTransactionStorage) CreateMultiTransaction(multiTx *MultiTransaction) error {
|
|
|
|
s.storage[multiTx.ID] = multiTx
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *InMemMultiTransactionStorage) GetMultiTransaction(id common.MultiTransactionIDType) (*MultiTransaction, error) {
|
|
|
|
multiTx, ok := s.storage[id]
|
|
|
|
if !ok {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
return multiTx, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *InMemMultiTransactionStorage) UpdateMultiTransaction(multiTx *MultiTransaction) error {
|
|
|
|
s.storage[multiTx.ID] = multiTx
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *InMemMultiTransactionStorage) DeleteMultiTransaction(id common.MultiTransactionIDType) error {
|
|
|
|
delete(s.storage, id)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-05-26 08:31:13 +00:00
|
|
|
func (s *InMemMultiTransactionStorage) ReadMultiTransactions(details *MultiTxDetails) ([]*MultiTransaction, error) {
|
2024-05-23 16:22:57 +00:00
|
|
|
var multiTxs []*MultiTransaction
|
2024-05-26 08:31:13 +00:00
|
|
|
for _, multiTx := range s.storage {
|
|
|
|
if len(details.IDs) > 0 && !testutils.SliceContains(details.IDs, multiTx.ID) {
|
2024-05-23 16:22:57 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if (details.AnyAddress != eth_common.Address{}) &&
|
|
|
|
(multiTx.FromAddress != details.AnyAddress && multiTx.ToAddress != details.AnyAddress) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if (details.FromAddress != eth_common.Address{}) && multiTx.FromAddress != details.FromAddress {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if (details.ToAddress != eth_common.Address{}) && multiTx.ToAddress != details.ToAddress {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if details.ToChainID != 0 && multiTx.ToNetworkID != details.ToChainID {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2024-05-26 08:31:13 +00:00
|
|
|
if details.Type != MultiTransactionDBTypeInvalid && multiTx.Type != mtDBTypeToMTType(details.Type) {
|
2024-05-23 16:22:57 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if details.CrossTxID != "" && multiTx.CrossTxID != details.CrossTxID {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
multiTxs = append(multiTxs, multiTx)
|
|
|
|
}
|
|
|
|
return multiTxs, nil
|
|
|
|
}
|