!refactor: introduce `SaveCommunityToken()` and change `AddCommunityToken()`
**This is a breaking change!** Prior to this commit we had `AddCommunityToken(token *communities, croppedImage CroppedImage)` that we used to 1. add a `CommunityToken` to the user's database and 2. to create a `CommunityTokenMetadata` from it which is then added to the community's `CommunityDescription` and published to its members However, I've then discovered that we need to separate these two things, such that we can deploy a community token, then add it to the database only for tracking purposes, **then** add it to the community description (and propagate to members) once we know that the deploy tx indeed went through. To implement this, this commit introduces a new API `SaveCommunityToken(token *communities.CommunityToken, croppedImage CroppedImage)` which adds the token to the database only and doesn't touch the community description. The `AddCommunityToken` API is then changed that it's exclusively used for adding an already saved `CommunityToken` to the community description so it can be published to members. Hence, the signature is now `AddCommunityToken(communityID string, chainID int, address string)`, which makes this a breaking change. Clients that used `AddCommunityToken()` before now need to ensure that they first call `SaveCommunityToken()` as `AddCommunityToken()` will fail otherwise.
This commit is contained in:
parent
c77878bbfb
commit
e8bac916ec
|
@ -3878,16 +3878,7 @@ func (m *Manager) ImageToBase64(uri string) string {
|
|||
return base64img
|
||||
}
|
||||
|
||||
func (m *Manager) AddCommunityToken(token *CommunityToken, croppedImage *images.CroppedImage) (*CommunityToken, error) {
|
||||
|
||||
community, err := m.GetByIDString(token.CommunityID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if community == nil {
|
||||
return nil, ErrOrgNotFound
|
||||
}
|
||||
|
||||
func (m *Manager) SaveCommunityToken(token *CommunityToken, croppedImage *images.CroppedImage) (*CommunityToken, error) {
|
||||
if croppedImage != nil && croppedImage.ImagePath != "" {
|
||||
bytes, err := images.OpenAndAdjustImage(*croppedImage, true)
|
||||
if err != nil {
|
||||
|
@ -3903,6 +3894,24 @@ func (m *Manager) AddCommunityToken(token *CommunityToken, croppedImage *images.
|
|||
token.Base64Image = m.ImageToBase64(token.Base64Image)
|
||||
}
|
||||
|
||||
return token, m.persistence.AddCommunityToken(token)
|
||||
}
|
||||
|
||||
func (m *Manager) AddCommunityToken(communityID string, chainID int, address string) error {
|
||||
|
||||
community, err := m.GetByIDString(communityID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if community == nil {
|
||||
return ErrOrgNotFound
|
||||
}
|
||||
|
||||
token, err := m.persistence.GetCommunityToken(communityID, chainID, address)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
tokenMetadata := &protobuf.CommunityTokenMetadata{
|
||||
ContractAddresses: map[uint64]string{uint64(token.ChainID): token.Address},
|
||||
Description: token.Description,
|
||||
|
@ -3914,15 +3923,10 @@ func (m *Manager) AddCommunityToken(token *CommunityToken, croppedImage *images.
|
|||
}
|
||||
_, err = community.AddCommunityTokensMetadata(tokenMetadata)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return err
|
||||
}
|
||||
|
||||
err = m.saveAndPublish(community)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return token, m.persistence.AddCommunityToken(token)
|
||||
return m.saveAndPublish(community)
|
||||
}
|
||||
|
||||
func (m *Manager) UpdateCommunityTokenState(chainID int, contractAddress string, deployState DeployState) error {
|
||||
|
|
|
@ -1187,6 +1187,26 @@ func (p *Persistence) GetCommunityTokens(communityID string) ([]*CommunityToken,
|
|||
return p.getCommunityTokensInternal(rows)
|
||||
}
|
||||
|
||||
func (p *Persistence) GetCommunityToken(communityID string, chainID int, address string) (*CommunityToken, error) {
|
||||
token := CommunityToken{}
|
||||
var supplyStr string
|
||||
err := p.db.QueryRow(`SELECT community_id, address, type, name, symbol, description, supply_str, infinite_supply, transferable, remote_self_destruct, chain_id, deploy_state, image_base64, decimals FROM community_tokens WHERE community_id = ? AND chain_id = ? AND address = ?`, communityID, chainID, address).Scan(&token.CommunityID, &token.Address, &token.TokenType, &token.Name,
|
||||
&token.Symbol, &token.Description, &supplyStr, &token.InfiniteSupply, &token.Transferable,
|
||||
&token.RemoteSelfDestruct, &token.ChainID, &token.DeployState, &token.Base64Image, &token.Decimals)
|
||||
if err == sql.ErrNoRows {
|
||||
return nil, nil
|
||||
} else if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
supplyBigInt, ok := new(big.Int).SetString(supplyStr, 10)
|
||||
if ok {
|
||||
token.Supply = &bigint.BigInt{Int: supplyBigInt}
|
||||
} else {
|
||||
token.Supply = &bigint.BigInt{Int: big.NewInt(0)}
|
||||
}
|
||||
return &token, nil
|
||||
}
|
||||
|
||||
func (p *Persistence) getCommunityTokensInternal(rows *sql.Rows) ([]*CommunityToken, error) {
|
||||
tokens := []*CommunityToken{}
|
||||
|
||||
|
|
|
@ -373,6 +373,35 @@ func (s *PersistenceSuite) TestUpdateCommunitySettings() {
|
|||
s.Equal(settings, rst)
|
||||
}
|
||||
|
||||
func (s *PersistenceSuite) TestGetCommunityToken() {
|
||||
tokens, err := s.db.GetCommunityTokens("123")
|
||||
s.Require().NoError(err)
|
||||
s.Require().Len(tokens, 0)
|
||||
|
||||
tokenERC721 := CommunityToken{
|
||||
CommunityID: "123",
|
||||
TokenType: protobuf.CommunityTokenType_ERC721,
|
||||
Address: "0x123",
|
||||
Name: "StatusToken",
|
||||
Symbol: "STT",
|
||||
Description: "desc",
|
||||
Supply: &bigint.BigInt{Int: big.NewInt(123)},
|
||||
InfiniteSupply: false,
|
||||
Transferable: true,
|
||||
RemoteSelfDestruct: true,
|
||||
ChainID: 1,
|
||||
DeployState: InProgress,
|
||||
Base64Image: "ABCD",
|
||||
}
|
||||
|
||||
err = s.db.AddCommunityToken(&tokenERC721)
|
||||
s.Require().NoError(err)
|
||||
|
||||
token, err := s.db.GetCommunityToken("123", 1, "0x123")
|
||||
s.Require().NoError(err)
|
||||
s.Require().Equal(&tokenERC721, token)
|
||||
}
|
||||
|
||||
func (s *PersistenceSuite) TestGetCommunityTokens() {
|
||||
tokens, err := s.db.GetCommunityTokens("123")
|
||||
s.Require().NoError(err)
|
||||
|
|
|
@ -4062,8 +4062,12 @@ func (m *Messenger) GetAllCommunityTokens() ([]*communities.CommunityToken, erro
|
|||
return m.communitiesManager.GetAllCommunityTokens()
|
||||
}
|
||||
|
||||
func (m *Messenger) AddCommunityToken(token *communities.CommunityToken, croppedImage *images.CroppedImage) (*communities.CommunityToken, error) {
|
||||
return m.communitiesManager.AddCommunityToken(token, croppedImage)
|
||||
func (m *Messenger) SaveCommunityToken(token *communities.CommunityToken, croppedImage *images.CroppedImage) (*communities.CommunityToken, error) {
|
||||
return m.communitiesManager.SaveCommunityToken(token, croppedImage)
|
||||
}
|
||||
|
||||
func (m *Messenger) AddCommunityToken(communityID string, chainID int, address string) error {
|
||||
return m.communitiesManager.AddCommunityToken(communityID, chainID, address)
|
||||
}
|
||||
|
||||
func (m *Messenger) UpdateCommunityTokenState(chainID int, contractAddress string, deployState communities.DeployState) error {
|
||||
|
|
|
@ -1325,8 +1325,12 @@ func (api *PublicAPI) GetAllCommunityTokens() ([]*communities.CommunityToken, er
|
|||
return api.service.messenger.GetAllCommunityTokens()
|
||||
}
|
||||
|
||||
func (api *PublicAPI) AddCommunityToken(token *communities.CommunityToken, croppedImage *images.CroppedImage) (*communities.CommunityToken, error) {
|
||||
return api.service.messenger.AddCommunityToken(token, croppedImage)
|
||||
func (api *PublicAPI) SaveCommunityToken(token *communities.CommunityToken, croppedImage *images.CroppedImage) (*communities.CommunityToken, error) {
|
||||
return api.service.messenger.SaveCommunityToken(token, croppedImage)
|
||||
}
|
||||
|
||||
func (api *PublicAPI) AddCommunityToken(communityID string, chainID int, address string) error {
|
||||
return api.service.messenger.AddCommunityToken(communityID, chainID, address)
|
||||
}
|
||||
|
||||
func (api *PublicAPI) UpdateCommunityTokenState(chainID int, contractAddress string, deployState communities.DeployState) error {
|
||||
|
|
Loading…
Reference in New Issue