From fb4b93e8be3cbe35ac9c6bdb49c141862343dad6 Mon Sep 17 00:00:00 2001 From: Michal Iskierko Date: Thu, 25 May 2023 11:59:44 +0200 Subject: [PATCH] feat(EstimateMintTo): Add EstimateMintTo to collectibles API Issue #10536 --- VERSION | 2 +- services/collectibles/api.go | 50 ++++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 1 deletion(-) diff --git a/VERSION b/VERSION index 188d0b2f9..b97a9dda0 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -0.155.1 \ No newline at end of file +0.156.0 diff --git a/services/collectibles/api.go b/services/collectibles/api.go index 18d50f360..0bfb88804 100644 --- a/services/collectibles/api.go +++ b/services/collectibles/api.go @@ -160,6 +160,56 @@ func (api *API) MintTo(ctx context.Context, chainID uint64, contractAddress stri return tx.Hash().Hex(), nil } +func (api *API) EstimateMintTo(ctx context.Context, chainID uint64, contractAddress string, walletAddresses []string, amount int) (uint64, error) { + err := api.validateWalletsAndAmounts(walletAddresses, amount) + if err != nil { + return 0, err + } + + ethClient, err := api.RPCClient.EthClient(chainID) + if err != nil { + log.Error(err.Error()) + return 0, err + } + + collectiblesABI, err := abi.JSON(strings.NewReader(collectibles.CollectiblesABI)) + if err != nil { + return 0, err + } + + totalAddresses := api.multiplyWalletAddresses(amount, walletAddresses) + + var usersAddresses = []common.Address{} + for _, k := range totalAddresses { + usersAddresses = append(usersAddresses, common.HexToAddress(k)) + } + + data, err := collectiblesABI.Pack("mintTo", usersAddresses) + if err != nil { + return 0, err + } + + ownerAddr, err := api.ContractOwner(ctx, chainID, contractAddress) + if err != nil { + return 0, err + } + toAddr := common.HexToAddress(contractAddress) + fromAddr := common.HexToAddress(ownerAddr) + + callMsg := ethereum.CallMsg{ + From: fromAddr, + To: &toAddr, + Value: big.NewInt(0), + Data: data, + } + + estimate, err := ethClient.EstimateGas(ctx, callMsg) + if err != nil { + return 0, err + } + return estimate + uint64(float32(estimate)*0.1), nil +} + func (api *API) RemoteBurn(ctx context.Context, chainID uint64, contractAddress string, txArgs transactions.SendTxArgs, password string, tokenIds []*bigint.BigInt) (string, error) { if len(tokenIds) == 0 { return "", errors.New("tokenIds list is empty")