2023-08-25 15:36:39 +00:00
|
|
|
package communitytokens
|
2023-03-10 14:09:34 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
2023-06-14 07:47:54 +00:00
|
|
|
"fmt"
|
|
|
|
|
2023-08-14 08:27:46 +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-10 14:09:34 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Database struct {
|
|
|
|
db *sql.DB
|
|
|
|
}
|
|
|
|
|
2023-06-14 07:47:54 +00:00
|
|
|
func NewCommunityTokensDatabase(db *sql.DB) *Database {
|
2023-03-10 14:09:34 +00:00
|
|
|
return &Database{db: db}
|
|
|
|
}
|
|
|
|
|
2023-06-14 07:47:54 +00:00
|
|
|
func (db *Database) GetTokenType(chainID uint64, contractAddress string) (protobuf.CommunityTokenType, error) {
|
|
|
|
var result = protobuf.CommunityTokenType_UNKNOWN_TOKEN_TYPE
|
|
|
|
rows, err := db.db.Query(`SELECT type FROM community_tokens WHERE chain_id=? AND address=? LIMIT 1`, chainID, contractAddress)
|
2023-03-10 14:09:34 +00:00
|
|
|
if err != nil {
|
2023-06-14 07:47:54 +00:00
|
|
|
return result, err
|
2023-03-10 14:09:34 +00:00
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
|
2023-06-14 07:47:54 +00:00
|
|
|
if rows.Next() {
|
|
|
|
err := rows.Scan(&result)
|
|
|
|
return result, err
|
2023-03-10 14:09:34 +00:00
|
|
|
}
|
2023-06-14 07:47:54 +00:00
|
|
|
return result, fmt.Errorf("can't find token: chainId %v, contractAddress %v", chainID, contractAddress)
|
2023-03-10 14:09:34 +00:00
|
|
|
}
|
2023-08-14 08:27:46 +00:00
|
|
|
|
|
|
|
func (db *Database) GetTokenPrivilegesLevel(chainID uint64, contractAddress string) (token.PrivilegesLevel, error) {
|
|
|
|
var result = token.CommunityLevel
|
|
|
|
rows, err := db.db.Query(`SELECT privileges_level FROM community_tokens WHERE chain_id=? AND address=? LIMIT 1`, chainID, contractAddress)
|
|
|
|
if err != nil {
|
|
|
|
return result, err
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
|
|
|
|
if rows.Next() {
|
|
|
|
err := rows.Scan(&result)
|
|
|
|
return result, err
|
|
|
|
}
|
|
|
|
return result, fmt.Errorf("can't find privileges level: chainId %v, contractAddress %v", chainID, contractAddress)
|
|
|
|
}
|
2023-12-12 07:37:57 +00:00
|
|
|
|
|
|
|
func (db *Database) GetCommunityERC20Metadata() ([]*token.CommunityToken, error) {
|
2023-12-21 15:05:29 +00:00
|
|
|
rows, err := db.db.Query(`SELECT community_id, address, name, symbol, chain_id FROM community_tokens WHERE type = ?`, protobuf.CommunityTokenType_ERC20)
|
2023-12-12 07:37:57 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
|
|
|
|
var result []*token.CommunityToken
|
|
|
|
for rows.Next() {
|
|
|
|
token := token.CommunityToken{}
|
2023-12-21 15:05:29 +00:00
|
|
|
err := rows.Scan(&token.CommunityID, &token.Address, &token.Name, &token.Symbol, &token.ChainID)
|
2023-12-12 07:37:57 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
result = append(result, &token)
|
|
|
|
}
|
|
|
|
return result, rows.Err()
|
|
|
|
}
|