fix(AddTokenOwners): Move AddTokenOwners from MintTo

Issue #10161
This commit is contained in:
Michal Iskierko 2023-04-04 15:53:15 +02:00 committed by Michał Iskierko
parent 5fd9e93e9c
commit 4e3ba2941c
2 changed files with 28 additions and 9 deletions

View File

@ -118,6 +118,8 @@ func (api *API) newCollectiblesInstance(chainID uint64, contractAddress string)
return collectibles.NewCollectibles(common.HexToAddress(contractAddress), backend)
}
// if we want to mint 2 tokens to addresses ["a", "b"] we need to mint
// twice to every address - we need to send to smart contract table ["a", "a", "b", "b"]
func (api *API) multiplyWalletAddresses(amount int, contractAddresses []string) []string {
var totalAddresses []string
for i := 1; i <= amount; i++ {
@ -126,9 +128,10 @@ func (api *API) multiplyWalletAddresses(amount int, contractAddresses []string)
return totalAddresses
}
func (api *API) MintTo(ctx context.Context, chainID uint64, contractAddress string, txArgs transactions.SendTxArgs, password string, users []string, amount int) (string, error) {
if len(users) == 0 {
return "", errors.New("users list is empty")
func (api *API) MintTo(ctx context.Context, chainID uint64, contractAddress string, txArgs transactions.SendTxArgs, password string, walletAddresses []string, amount int) (string, error) {
err := api.validateWalletsAndAmounts(walletAddresses, amount)
if err != nil {
return "", err
}
contractInst, err := api.newCollectiblesInstance(chainID, contractAddress)
@ -136,9 +139,7 @@ func (api *API) MintTo(ctx context.Context, chainID uint64, contractAddress stri
return "", err
}
// if we want to mint 2 tokens to addresses ["a", "b"] we need to mint
// twice to every address - we need to send to smart contract table ["a", "a", "b", "b"]
totalAddresses := api.multiplyWalletAddresses(amount, users)
totalAddresses := api.multiplyWalletAddresses(amount, walletAddresses)
var usersAddresses = []common.Address{}
for _, k := range totalAddresses {
@ -152,9 +153,6 @@ func (api *API) MintTo(ctx context.Context, chainID uint64, contractAddress stri
return "", err
}
//save to db
_ = api.db.AddTokenOwners(chainID, contractAddress, totalAddresses)
return tx.Hash().Hex(), nil
}
@ -170,3 +168,23 @@ func (api *API) ContractOwner(ctx context.Context, chainID uint64, contractAddre
}
return owner.String(), nil
}
func (api *API) AddTokenOwners(ctx context.Context, chainID uint64, contractAddress string, walletAddresses []string, amount int) error {
err := api.validateWalletsAndAmounts(walletAddresses, amount)
if err != nil {
return err
}
totalAddresses := api.multiplyWalletAddresses(amount, walletAddresses)
return api.db.AddTokenOwners(chainID, contractAddress, totalAddresses)
}
func (api *API) validateWalletsAndAmounts(walletAddresses []string, amount int) error {
if len(walletAddresses) == 0 {
return errors.New("wallet addresses list is empty")
}
if amount <= 0 {
return errors.New("amount is <= 0")
}
return nil
}

View File

@ -75,6 +75,7 @@ const (
BuyStickerPack PendingTrxType = "BuyStickerPack"
WalletTransfer PendingTrxType = "WalletTransfer"
CollectibleDeployment PendingTrxType = "CollectibleDeployment"
CollectibleAirdrop PendingTrxType = "CollectibleAirdrop"
)
type PendingTransaction struct {