feat(CommunityTokens): Keep community token details in database

New table for community tokens.
API for get,add, update community token.

Issue #9233
This commit is contained in:
Michal Iskierko 2023-01-27 14:27:24 +01:00 committed by Michał Iskierko
parent e543fda4b5
commit d0cc036d48
9 changed files with 437 additions and 312 deletions

View File

@ -1 +1 @@
0.132.3 0.133.0

View File

@ -381,6 +381,37 @@ func (o *Community) initialize() {
} }
} }
type TokenType uint8
const (
ERC721 TokenType = iota
ERC20
)
type DeployState uint8
const (
Failed DeployState = iota
InProgress
Deployed
)
type CommunityToken struct {
TokenType TokenType `json:"tokenType"`
CommunityID string `json:"communityId"`
Address string `json:"address"`
Name string `json:"name"`
Symbol string `json:"symbol"`
Description string `json:"description"`
Supply int `json:"supply"`
InfiniteSupply bool `json:"infiniteSupply"`
Transferable bool `json:"transferable"`
RemoteSelfDestruct bool `json:"remoteSelfDestruct"`
ChainID int `json:"chainId"`
DeployState DeployState `json:"deployState"`
Base64Image string `json:"image"`
}
type CommunitySettings struct { type CommunitySettings struct {
CommunityID string `json:"communityId"` CommunityID string `json:"communityId"`
HistoryArchiveSupportEnabled bool `json:"historyArchiveSupportEnabled"` HistoryArchiveSupportEnabled bool `json:"historyArchiveSupportEnabled"`

View File

@ -2565,3 +2565,15 @@ func findIndexFile(files []*torrent.File) (index int, ok bool) {
} }
return 0, false return 0, false
} }
func (m *Manager) GetCommunityTokens(communityID string, chainID int) ([]*CommunityToken, error) {
return m.persistence.GetCommunityTokens(communityID, chainID)
}
func (m *Manager) AddCommunityToken(token *CommunityToken) error {
return m.persistence.AddCommunityToken(token)
}
func (m *Manager) UpdateCommunityTokenState(contractAddress string, deployState DeployState) error {
return m.persistence.UpdateCommunityTokenState(contractAddress, deployState)
}

View File

@ -827,3 +827,41 @@ func (p *Persistence) GetCommunityChatIDs(communityID types.HexBytes) ([]string,
} }
return ids, nil return ids, nil
} }
func (p *Persistence) GetCommunityTokens(communityID string, chainID int) ([]*CommunityToken, error) {
rows, err := p.db.Query(`SELECT community_id, address, type, name, symbol, description, supply,
infinite_supply, transferable, remote_self_destruct, chain_id, deploy_state, image_base64
FROM community_tokens WHERE community_id = ? AND chain_id = ?`, communityID, chainID)
if err != nil {
return nil, err
}
defer rows.Close()
tokens := []*CommunityToken{}
for rows.Next() {
token := CommunityToken{}
err := rows.Scan(&token.CommunityID, &token.Address, &token.TokenType, &token.Name,
&token.Symbol, &token.Description, &token.Supply, &token.InfiniteSupply, &token.Transferable,
&token.RemoteSelfDestruct, &token.ChainID, &token.DeployState, &token.Base64Image)
if err != nil {
return nil, err
}
tokens = append(tokens, &token)
}
return tokens, nil
}
func (p *Persistence) AddCommunityToken(token *CommunityToken) error {
_, err := p.db.Exec(`INSERT INTO community_tokens (community_id, address, type, name, symbol, description, supply,
infinite_supply, transferable, remote_self_destruct, chain_id, deploy_state, image_base64)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`, token.CommunityID, token.Address, token.TokenType, token.Name,
token.Symbol, token.Description, token.Supply, token.InfiniteSupply, token.Transferable, token.RemoteSelfDestruct,
token.ChainID, token.DeployState, token.Base64Image)
return err
}
func (p *Persistence) UpdateCommunityTokenState(contractAddress string, deployState DeployState) error {
_, err := p.db.Exec(`UPDATE community_tokens SET deploy_state = ? WHERE address = ?`, deployState, contractAddress)
return err
}

View File

@ -369,3 +369,58 @@ func (s *PersistenceSuite) TestUpdateCommunitySettings() {
s.NoError(err) s.NoError(err)
s.Equal(settings, rst) s.Equal(settings, rst)
} }
func (s *PersistenceSuite) TestGetCommunityTokens() {
tokens, err := s.db.GetCommunityTokens("123", 1)
s.Require().NoError(err)
s.Require().Len(tokens, 0)
token := CommunityToken{
CommunityID: "123",
TokenType: ERC721,
Address: "0x123",
Name: "StatusToken",
Symbol: "STT",
Description: "desc",
Supply: 123,
InfiniteSupply: false,
Transferable: true,
RemoteSelfDestruct: true,
ChainID: 1,
DeployState: InProgress,
Base64Image: "ABCD",
}
token2 := CommunityToken{
CommunityID: "345",
TokenType: ERC721,
Address: "0x345",
Name: "StatusToken",
Symbol: "STT",
Description: "desc",
Supply: 345,
InfiniteSupply: false,
Transferable: true,
RemoteSelfDestruct: true,
ChainID: 2,
DeployState: Failed,
Base64Image: "QWERTY",
}
err = s.db.AddCommunityToken(&token)
s.Require().NoError(err)
err = s.db.AddCommunityToken(&token2)
s.Require().NoError(err)
tokens, err = s.db.GetCommunityTokens("123", 1)
s.Require().NoError(err)
s.Require().Len(tokens, 1)
s.Require().Equal(token, *tokens[0])
err = s.db.UpdateCommunityTokenState("0x123", Deployed)
s.Require().NoError(err)
tokens, err = s.db.GetCommunityTokens("123", 1)
s.Require().NoError(err)
s.Require().Len(tokens, 1)
s.Require().Equal(Deployed, tokens[0].DeployState)
}

View File

@ -3283,3 +3283,15 @@ func (m *Messenger) chatMessagesToWakuMessages(chatMessages []*common.Message, c
return wakuMessages, nil return wakuMessages, nil
} }
func (m *Messenger) GetCommunityTokens(communityID string, chainID int) ([]*communities.CommunityToken, error) {
return m.communitiesManager.GetCommunityTokens(communityID, chainID)
}
func (m *Messenger) AddCommunityToken(token *communities.CommunityToken) error {
return m.communitiesManager.AddCommunityToken(token)
}
func (m *Messenger) UpdateCommunityTokenState(contractAddress string, deployState communities.DeployState) error {
return m.communitiesManager.UpdateCommunityTokenState(contractAddress, deployState)
}

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,16 @@
CREATE TABLE IF NOT EXISTS community_tokens (
community_id TEXT NOT NULL,
address TEXT NOT NULL,
type INT NOT NULL,
name TEXT NOT NULL,
symbol TEXT NOT NULL,
description TEXT NOT NULL,
supply INT NOT NULL DEFAULT 0,
infinite_supply BOOLEAN NOT NULL DEFAULT FALSE,
transferable BOOLEAN NOT NULL DEFAULT FALSE,
remote_self_destruct BOOLEAN NOT NULL DEFAULT FALSE,
chain_id INT NOT NULL,
deploy_state INT NOT NULL,
image_base64 TEXT NOT NULL DEFAULT "",
PRIMARY KEY(community_id, address, chain_id)
);

View File

@ -1270,6 +1270,18 @@ func (api *PublicAPI) BuildContact(publicKey string) (*protocol.Contact, error)
return api.service.messenger.BuildContact(publicKey) return api.service.messenger.BuildContact(publicKey)
} }
func (api *PublicAPI) GetCommunityTokens(communityID string, chainID int) ([]*communities.CommunityToken, error) {
return api.service.messenger.GetCommunityTokens(communityID, chainID)
}
func (api *PublicAPI) AddCommunityToken(token *communities.CommunityToken) error {
return api.service.messenger.AddCommunityToken(token)
}
func (api *PublicAPI) UpdateCommunityTokenState(contractAddress string, deployState communities.DeployState) error {
return api.service.messenger.UpdateCommunityTokenState(contractAddress, deployState)
}
// ----- // -----
// HELPER // HELPER
// ----- // -----