2023-08-25 15:36:39 +00:00
|
|
|
package communitytokens
|
2023-03-28 11:06:35 +00:00
|
|
|
|
|
|
|
import (
|
2023-06-14 07:47:54 +00:00
|
|
|
"database/sql"
|
2023-06-21 11:20:43 +00:00
|
|
|
"math/big"
|
2023-03-28 11:06:35 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/suite"
|
|
|
|
|
2023-09-21 11:16:05 +00:00
|
|
|
"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-08-11 11:28:45 +00:00
|
|
|
"github.com/status-im/status-go/t/helpers"
|
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-07-18 08:33:45 +00:00
|
|
|
infinite_supply, transferable, remote_self_destruct, chain_id, deploy_state, image_base64, decimals, deployer, privileges_level)
|
|
|
|
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-07-18 08:33:45 +00:00
|
|
|
token.ChainID, token.DeployState, token.Base64Image, token.Decimals, token.Deployer, token.PrivilegesLevel)
|
2023-06-14 07:47:54 +00:00
|
|
|
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-18 08:33:45 +00:00
|
|
|
Deployer: "0xDEP1",
|
|
|
|
PrivilegesLevel: token.OwnerLevel,
|
2023-06-14 07:47:54 +00:00
|
|
|
}
|
|
|
|
|
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,
|
2023-07-18 08:33:45 +00:00
|
|
|
Deployer: "0xDEP2",
|
|
|
|
PrivilegesLevel: token.CommunityLevel,
|
2023-06-14 07:47:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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-09-21 11:16:05 +00:00
|
|
|
db, err := helpers.SetupTestMemorySQLDB(appdatabase.DbInitializer{})
|
2023-03-28 11:06:35 +00:00
|
|
|
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
|
|
|
}
|
2023-08-14 08:27:46 +00:00
|
|
|
|
|
|
|
func (s *DatabaseSuite) TestGetPrivilegesLevel() {
|
|
|
|
privLevel, err := s.db.GetTokenPrivilegesLevel(1, "0x123")
|
|
|
|
s.Require().NoError(err)
|
|
|
|
s.Equal(privLevel, token.OwnerLevel)
|
|
|
|
|
|
|
|
privLevel, err = s.db.GetTokenPrivilegesLevel(2, "0x345")
|
|
|
|
s.Require().NoError(err)
|
|
|
|
s.Equal(privLevel, token.CommunityLevel)
|
|
|
|
|
|
|
|
_, err = s.db.GetTokenType(10, "0x777")
|
|
|
|
s.Require().Error(err)
|
|
|
|
}
|