2023-03-28 11:06:35 +00:00
|
|
|
package collectibles
|
|
|
|
|
|
|
|
import (
|
2023-06-14 07:47:54 +00:00
|
|
|
"database/sql"
|
2023-03-28 11:06:35 +00:00
|
|
|
"io/ioutil"
|
2023-06-21 11:20:43 +00:00
|
|
|
"math/big"
|
2023-03-28 11:06:35 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/suite"
|
|
|
|
|
|
|
|
"github.com/status-im/status-go/appdatabase"
|
2023-07-07 13:03:37 +00:00
|
|
|
"github.com/status-im/status-go/protocol/communities/token"
|
2023-06-14 07:47:54 +00:00
|
|
|
"github.com/status-im/status-go/protocol/protobuf"
|
2023-03-28 11:06:35 +00:00
|
|
|
"github.com/status-im/status-go/protocol/sqlite"
|
2023-06-21 11:20:43 +00:00
|
|
|
"github.com/status-im/status-go/services/wallet/bigint"
|
2023-03-28 11:06:35 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestDatabaseSuite(t *testing.T) {
|
|
|
|
suite.Run(t, new(DatabaseSuite))
|
|
|
|
}
|
|
|
|
|
|
|
|
type DatabaseSuite struct {
|
|
|
|
suite.Suite
|
|
|
|
|
|
|
|
db *Database
|
|
|
|
}
|
|
|
|
|
2023-07-07 13:03:37 +00:00
|
|
|
func (s *DatabaseSuite) addCommunityToken(db *sql.DB, token *token.CommunityToken) error {
|
2023-06-21 11:20:43 +00:00
|
|
|
_, err := db.Exec(`INSERT INTO community_tokens (community_id, address, type, name, symbol, description, supply_str,
|
2023-06-14 07:47:54 +00:00
|
|
|
infinite_supply, transferable, remote_self_destruct, chain_id, deploy_state, image_base64, decimals)
|
|
|
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`, token.CommunityID, token.Address, token.TokenType, token.Name,
|
2023-06-21 11:20:43 +00:00
|
|
|
token.Symbol, token.Description, token.Supply.String(), token.InfiniteSupply, token.Transferable, token.RemoteSelfDestruct,
|
2023-06-14 07:47:54 +00:00
|
|
|
token.ChainID, token.DeployState, token.Base64Image, token.Decimals)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DatabaseSuite) setupDatabase(db *sql.DB) error {
|
2023-07-07 13:03:37 +00:00
|
|
|
token721 := &token.CommunityToken{
|
2023-06-14 07:47:54 +00:00
|
|
|
CommunityID: "123",
|
|
|
|
TokenType: protobuf.CommunityTokenType_ERC721,
|
|
|
|
Address: "0x123",
|
|
|
|
Name: "StatusToken",
|
|
|
|
Symbol: "STT",
|
|
|
|
Description: "desc",
|
2023-06-21 11:20:43 +00:00
|
|
|
Supply: &bigint.BigInt{Int: big.NewInt(123)},
|
2023-06-14 07:47:54 +00:00
|
|
|
InfiniteSupply: false,
|
|
|
|
Transferable: true,
|
|
|
|
RemoteSelfDestruct: true,
|
|
|
|
ChainID: 1,
|
2023-07-07 13:03:37 +00:00
|
|
|
DeployState: token.InProgress,
|
2023-06-14 07:47:54 +00:00
|
|
|
Base64Image: "ABCD",
|
|
|
|
}
|
|
|
|
|
2023-07-07 13:03:37 +00:00
|
|
|
token20 := &token.CommunityToken{
|
2023-06-14 07:47:54 +00:00
|
|
|
CommunityID: "345",
|
|
|
|
TokenType: protobuf.CommunityTokenType_ERC20,
|
|
|
|
Address: "0x345",
|
|
|
|
Name: "StatusToken",
|
|
|
|
Symbol: "STT",
|
|
|
|
Description: "desc",
|
2023-06-21 11:20:43 +00:00
|
|
|
Supply: &bigint.BigInt{Int: big.NewInt(345)},
|
2023-06-14 07:47:54 +00:00
|
|
|
InfiniteSupply: false,
|
|
|
|
Transferable: true,
|
|
|
|
RemoteSelfDestruct: true,
|
|
|
|
ChainID: 2,
|
2023-07-07 13:03:37 +00:00
|
|
|
DeployState: token.Failed,
|
2023-06-14 07:47:54 +00:00
|
|
|
Base64Image: "QWERTY",
|
|
|
|
Decimals: 21,
|
|
|
|
}
|
|
|
|
|
|
|
|
err := s.addCommunityToken(db, token721)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return s.addCommunityToken(db, token20)
|
|
|
|
}
|
|
|
|
|
2023-03-28 11:06:35 +00:00
|
|
|
func (s *DatabaseSuite) SetupTest() {
|
|
|
|
s.db = nil
|
|
|
|
|
2023-06-14 07:47:54 +00:00
|
|
|
dbPath, err := ioutil.TempFile("", "status-go-community-tokens-db-")
|
2023-03-28 11:06:35 +00:00
|
|
|
s.NoError(err, "creating temp file for db")
|
|
|
|
|
|
|
|
db, err := appdatabase.InitializeDB(dbPath.Name(), "", sqlite.ReducedKDFIterationsNumber)
|
|
|
|
s.NoError(err, "creating sqlite db instance")
|
|
|
|
|
|
|
|
err = sqlite.Migrate(db)
|
|
|
|
s.NoError(err, "protocol migrate")
|
|
|
|
|
|
|
|
s.db = &Database{db: db}
|
|
|
|
|
2023-06-14 07:47:54 +00:00
|
|
|
err = s.setupDatabase(db)
|
|
|
|
s.NoError(err, "setting up database")
|
|
|
|
}
|
2023-03-28 11:06:35 +00:00
|
|
|
|
2023-06-14 07:47:54 +00:00
|
|
|
func (s *DatabaseSuite) TestGetTokenType() {
|
|
|
|
contractType, err := s.db.GetTokenType(1, "0x123")
|
2023-03-28 11:06:35 +00:00
|
|
|
s.Require().NoError(err)
|
2023-06-14 07:47:54 +00:00
|
|
|
s.Equal(contractType, protobuf.CommunityTokenType_ERC721)
|
2023-03-28 11:06:35 +00:00
|
|
|
|
2023-06-14 07:47:54 +00:00
|
|
|
contractType, err = s.db.GetTokenType(2, "0x345")
|
2023-03-28 11:06:35 +00:00
|
|
|
s.Require().NoError(err)
|
2023-06-14 07:47:54 +00:00
|
|
|
s.Equal(contractType, protobuf.CommunityTokenType_ERC20)
|
2023-03-28 11:06:35 +00:00
|
|
|
|
2023-06-14 07:47:54 +00:00
|
|
|
_, err = s.db.GetTokenType(10, "0x777")
|
|
|
|
s.Require().Error(err)
|
2023-03-28 11:06:35 +00:00
|
|
|
}
|