Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(wallet-community)_: Move community minting airdrop to router #6196

Draft
wants to merge 4 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 78 additions & 0 deletions contracts/community-tokens/contracts.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package communitytokens

import (
"errors"

"github.com/ethereum/go-ethereum/common"
"github.com/status-im/status-go/contracts/community-tokens/assets"
"github.com/status-im/status-go/contracts/community-tokens/collectibles"
communitytokendeployer "github.com/status-im/status-go/contracts/community-tokens/deployer"
"github.com/status-im/status-go/contracts/community-tokens/mastertoken"
"github.com/status-im/status-go/contracts/community-tokens/ownertoken"
communityownertokenregistry "github.com/status-im/status-go/contracts/community-tokens/registry"
"github.com/status-im/status-go/rpc"
)

type CommunityTokensContractMaker struct {
RPCClient rpc.ClientInterface
}

func NewCommunityTokensContractMakerMaker(client rpc.ClientInterface) (*CommunityTokensContractMaker, error) {
if client == nil {
return nil, errors.New("could not initialize CommunityTokensContractMaker with an rpc client")
}
return &CommunityTokensContractMaker{RPCClient: client}, nil
}

func (c *CommunityTokensContractMaker) NewOwnerTokenInstance(chainID uint64, contractAddress common.Address,
) (*ownertoken.OwnerToken, error) {
backend, err := c.RPCClient.EthClient(chainID)
if err != nil {
return nil, err
}
return ownertoken.NewOwnerToken(contractAddress, backend)
}

func (c *CommunityTokensContractMaker) NewMasterTokenInstance(chainID uint64, contractAddress common.Address,
) (*mastertoken.MasterToken, error) {
backend, err := c.RPCClient.EthClient(chainID)
if err != nil {
return nil, err
}
return mastertoken.NewMasterToken(contractAddress, backend)
}

func (c *CommunityTokensContractMaker) NewCollectiblesInstance(chainID uint64, contractAddress common.Address,
) (*collectibles.Collectibles, error) {
backend, err := c.RPCClient.EthClient(chainID)
if err != nil {
return nil, err
}
return collectibles.NewCollectibles(contractAddress, backend)
}

func (c *CommunityTokensContractMaker) NewAssetsInstance(chainID uint64, contractAddress common.Address,
) (*assets.Assets, error) {
backend, err := c.RPCClient.EthClient(chainID)
if err != nil {
return nil, err
}
return assets.NewAssets(contractAddress, backend)
}

func (c *CommunityTokensContractMaker) NewCommunityTokenDeployerInstance(chainID uint64, contractAddress common.Address,
) (*communitytokendeployer.CommunityTokenDeployer, error) {
backend, err := c.RPCClient.EthClient(chainID)
if err != nil {
return nil, err
}
return communitytokendeployer.NewCommunityTokenDeployer(contractAddress, backend)
}

func (c *CommunityTokensContractMaker) NewCommunityOwnerTokenRegistryInstance(chainID uint64, contractAddress common.Address) (*communityownertokenregistry.CommunityOwnerTokenRegistry, error) {
backend, err := c.RPCClient.EthClient(chainID)
if err != nil {
return nil, err
}
return communityownertokenregistry.NewCommunityOwnerTokenRegistry(contractAddress, backend)
}
122 changes: 29 additions & 93 deletions services/communitytokens/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"fmt"
"math/big"

"github.com/pkg/errors"
"go.uber.org/zap"

"github.com/ethereum/go-ethereum/accounts/abi/bind"
Expand All @@ -18,13 +17,13 @@ import (
communityownertokenregistry "github.com/status-im/status-go/contracts/community-tokens/registry"
"github.com/status-im/status-go/eth-node/crypto"
"github.com/status-im/status-go/eth-node/types"
"github.com/status-im/status-go/images"
"github.com/status-im/status-go/logutils"
"github.com/status-im/status-go/protocol/communities/token"
"github.com/status-im/status-go/protocol/protobuf"
"github.com/status-im/status-go/services/utils"
"github.com/status-im/status-go/services/wallet/bigint"
wcommon "github.com/status-im/status-go/services/wallet/common"
"github.com/status-im/status-go/services/wallet/requests"
"github.com/status-im/status-go/services/wallet/wallettypes"
"github.com/status-im/status-go/transactions"
)
Expand All @@ -39,80 +38,17 @@ type API struct {
s *Service
}

type DeploymentDetails struct {
ContractAddress string `json:"contractAddress"`
TransactionHash string `json:"transactionHash"`
CommunityToken *token.CommunityToken `json:"communityToken"`
OwnerToken *token.CommunityToken `json:"ownerToken"`
MasterToken *token.CommunityToken `json:"masterToken"`
}

const maxSupply = 999999999

type DeploymentParameters struct {
Name string `json:"name"`
Symbol string `json:"symbol"`
Supply *bigint.BigInt `json:"supply"`
InfiniteSupply bool `json:"infiniteSupply"`
Transferable bool `json:"transferable"`
RemoteSelfDestruct bool `json:"remoteSelfDestruct"`
TokenURI string `json:"tokenUri"`
OwnerTokenAddress string `json:"ownerTokenAddress"`
MasterTokenAddress string `json:"masterTokenAddress"`
CommunityID string `json:"communityId"`
Description string `json:"description"`
CroppedImage *images.CroppedImage `json:"croppedImage,omitempty"` // for community tokens
Base64Image string `json:"base64image"` // for owner & master tokens
Decimals int `json:"decimals"`
}

func (d *DeploymentParameters) GetSupply() *big.Int {
if d.InfiniteSupply {
return d.GetInfiniteSupply()
}
return d.Supply.Int
}

// infinite supply for ERC721 is 2^256-1
func (d *DeploymentParameters) GetInfiniteSupply() *big.Int {
return GetInfiniteSupply()
}

func GetInfiniteSupply() *big.Int {
max := new(big.Int).Exp(big.NewInt(2), big.NewInt(256), nil)
max.Sub(max, big.NewInt(1))
return max
}

func (d *DeploymentParameters) Validate(isAsset bool) error {
if len(d.Name) <= 0 {
return errors.New("empty collectible name")
}
if len(d.Symbol) <= 0 {
return errors.New("empty collectible symbol")
}
var maxForType = big.NewInt(maxSupply)
if isAsset {
assetMultiplier, _ := big.NewInt(0).SetString("1000000000000000000", 10)
maxForType = maxForType.Mul(maxForType, assetMultiplier)
}
if !d.InfiniteSupply && (d.Supply.Cmp(big.NewInt(0)) < 0 || d.Supply.Cmp(maxForType) > 0) {
return fmt.Errorf("wrong supply value: %v", d.Supply)
}
return nil
}

func (api *API) DeployCollectibles(ctx context.Context, chainID uint64, deploymentParameters DeploymentParameters, txArgs wallettypes.SendTxArgs, password string) (DeploymentDetails, error) {
func (api *API) DeployCollectibles(ctx context.Context, chainID uint64, deploymentParameters requests.DeploymentParameters, txArgs wallettypes.SendTxArgs, password string) (requests.DeploymentDetails, error) {
err := deploymentParameters.Validate(false)
if err != nil {
return DeploymentDetails{}, err
return requests.DeploymentDetails{}, err
}
transactOpts := txArgs.ToTransactOpts(utils.VerifyPasswordAndGetSigner(chainID, api.s.accountsManager, api.s.config.KeyStoreDir, txArgs.From, password))

ethClient, err := api.s.manager.rpcClient.EthClient(chainID)
if err != nil {
logutils.ZapLogger().Error(err.Error())
return DeploymentDetails{}, err
return requests.DeploymentDetails{}, err
}
address, tx, _, err := collectibles.DeployCollectibles(transactOpts, ethClient, deploymentParameters.Name,
deploymentParameters.Symbol, deploymentParameters.GetSupply(),
Expand All @@ -121,7 +57,7 @@ func (api *API) DeployCollectibles(ctx context.Context, chainID uint64, deployme
common.HexToAddress(deploymentParameters.MasterTokenAddress))
if err != nil {
logutils.ZapLogger().Error(err.Error())
return DeploymentDetails{}, err
return requests.DeploymentDetails{}, err
}

err = api.s.pendingTracker.TrackPendingTransaction(
Expand All @@ -135,16 +71,16 @@ func (api *API) DeployCollectibles(ctx context.Context, chainID uint64, deployme
)
if err != nil {
logutils.ZapLogger().Error("TrackPendingTransaction error", zap.Error(err))
return DeploymentDetails{}, err
return requests.DeploymentDetails{}, err
}

savedCommunityToken, err := api.s.CreateCommunityTokenAndSave(int(chainID), deploymentParameters, txArgs.From.Hex(), address.Hex(),
protobuf.CommunityTokenType_ERC721, token.CommunityLevel, tx.Hash().Hex())
if err != nil {
return DeploymentDetails{}, err
return requests.DeploymentDetails{}, err
}

return DeploymentDetails{
return requests.DeploymentDetails{
ContractAddress: address.Hex(),
TransactionHash: tx.Hash().Hex(),
CommunityToken: savedCommunityToken}, nil
Expand Down Expand Up @@ -180,27 +116,27 @@ func prepareDeploymentSignatureStruct(signature string, communityID string, addr
}

func (api *API) DeployOwnerToken(ctx context.Context, chainID uint64,
ownerTokenParameters DeploymentParameters, masterTokenParameters DeploymentParameters,
signerPubKey string, txArgs wallettypes.SendTxArgs, password string) (DeploymentDetails, error) {
ownerTokenParameters requests.DeploymentParameters, masterTokenParameters requests.DeploymentParameters,
signerPubKey string, txArgs wallettypes.SendTxArgs, password string) (requests.DeploymentDetails, error) {
err := ownerTokenParameters.Validate(false)
if err != nil {
return DeploymentDetails{}, err
return requests.DeploymentDetails{}, err
}

if len(signerPubKey) <= 0 {
return DeploymentDetails{}, fmt.Errorf("signerPubKey is empty")
return requests.DeploymentDetails{}, fmt.Errorf("signerPubKey is empty")
}

err = masterTokenParameters.Validate(false)
if err != nil {
return DeploymentDetails{}, err
return requests.DeploymentDetails{}, err
}

transactOpts := txArgs.ToTransactOpts(utils.VerifyPasswordAndGetSigner(chainID, api.s.accountsManager, api.s.config.KeyStoreDir, txArgs.From, password))

deployerContractInst, err := api.NewCommunityTokenDeployerInstance(chainID)
if err != nil {
return DeploymentDetails{}, err
return requests.DeploymentDetails{}, err
}

ownerTokenConfig := communitytokendeployer.CommunityTokenDeployerTokenConfig{
Expand All @@ -217,12 +153,12 @@ func (api *API) DeployOwnerToken(ctx context.Context, chainID uint64,

signature, err := api.s.Messenger.CreateCommunityTokenDeploymentSignature(context.Background(), chainID, txArgs.From.Hex(), ownerTokenParameters.CommunityID)
if err != nil {
return DeploymentDetails{}, err
return requests.DeploymentDetails{}, err
}

communitySignature, err := prepareDeploymentSignatureStruct(types.HexBytes(signature).String(), ownerTokenParameters.CommunityID, common.Address(txArgs.From))
if err != nil {
return DeploymentDetails{}, err
return requests.DeploymentDetails{}, err
}

logutils.ZapLogger().Debug("Prepare deployment", zap.Any("signature", communitySignature))
Expand All @@ -231,7 +167,7 @@ func (api *API) DeployOwnerToken(ctx context.Context, chainID uint64,

if err != nil {
logutils.ZapLogger().Error(err.Error())
return DeploymentDetails{}, err
return requests.DeploymentDetails{}, err
}

logutils.ZapLogger().Debug("Contract deployed", zap.Stringer("hash", tx.Hash()))
Expand All @@ -247,21 +183,21 @@ func (api *API) DeployOwnerToken(ctx context.Context, chainID uint64,
)
if err != nil {
logutils.ZapLogger().Error("TrackPendingTransaction error", zap.Error(err))
return DeploymentDetails{}, err
return requests.DeploymentDetails{}, err
}

savedOwnerToken, err := api.s.CreateCommunityTokenAndSave(int(chainID), ownerTokenParameters, txArgs.From.Hex(),
api.s.TemporaryOwnerContractAddress(tx.Hash().Hex()), protobuf.CommunityTokenType_ERC721, token.OwnerLevel, tx.Hash().Hex())
if err != nil {
return DeploymentDetails{}, err
return requests.DeploymentDetails{}, err
}
savedMasterToken, err := api.s.CreateCommunityTokenAndSave(int(chainID), masterTokenParameters, txArgs.From.Hex(),
api.s.TemporaryMasterContractAddress(tx.Hash().Hex()), protobuf.CommunityTokenType_ERC721, token.MasterLevel, tx.Hash().Hex())
if err != nil {
return DeploymentDetails{}, err
return requests.DeploymentDetails{}, err
}

return DeploymentDetails{
return requests.DeploymentDetails{
ContractAddress: "",
TransactionHash: tx.Hash().Hex(),
OwnerToken: savedOwnerToken,
Expand All @@ -273,19 +209,19 @@ func (api *API) ReTrackOwnerTokenDeploymentTransaction(ctx context.Context, chai
return api.s.ReTrackOwnerTokenDeploymentTransaction(ctx, chainID, contractAddress)
}

func (api *API) DeployAssets(ctx context.Context, chainID uint64, deploymentParameters DeploymentParameters, txArgs wallettypes.SendTxArgs, password string) (DeploymentDetails, error) {
func (api *API) DeployAssets(ctx context.Context, chainID uint64, deploymentParameters requests.DeploymentParameters, txArgs wallettypes.SendTxArgs, password string) (requests.DeploymentDetails, error) {

err := deploymentParameters.Validate(true)
if err != nil {
return DeploymentDetails{}, err
return requests.DeploymentDetails{}, err
}

transactOpts := txArgs.ToTransactOpts(utils.VerifyPasswordAndGetSigner(chainID, api.s.accountsManager, api.s.config.KeyStoreDir, txArgs.From, password))

ethClient, err := api.s.manager.rpcClient.EthClient(chainID)
if err != nil {
logutils.ZapLogger().Error(err.Error())
return DeploymentDetails{}, err
return requests.DeploymentDetails{}, err
}

const decimals = 18
Expand All @@ -296,7 +232,7 @@ func (api *API) DeployAssets(ctx context.Context, chainID uint64, deploymentPara
common.HexToAddress(deploymentParameters.MasterTokenAddress))
if err != nil {
logutils.ZapLogger().Error(err.Error())
return DeploymentDetails{}, err
return requests.DeploymentDetails{}, err
}

err = api.s.pendingTracker.TrackPendingTransaction(
Expand All @@ -310,16 +246,16 @@ func (api *API) DeployAssets(ctx context.Context, chainID uint64, deploymentPara
)
if err != nil {
logutils.ZapLogger().Error("TrackPendingTransaction error", zap.Error(err))
return DeploymentDetails{}, err
return requests.DeploymentDetails{}, err
}

savedCommunityToken, err := api.s.CreateCommunityTokenAndSave(int(chainID), deploymentParameters, txArgs.From.Hex(), address.Hex(),
protobuf.CommunityTokenType_ERC20, token.CommunityLevel, tx.Hash().Hex())
if err != nil {
return DeploymentDetails{}, err
return requests.DeploymentDetails{}, err
}

return DeploymentDetails{
return requests.DeploymentDetails{
ContractAddress: address.Hex(),
TransactionHash: tx.Hash().Hex(),
CommunityToken: savedCommunityToken}, nil
Expand All @@ -334,7 +270,7 @@ func (api *API) DeployAssetsEstimate(ctx context.Context, chainID uint64, fromAd
}

func (api *API) DeployOwnerTokenEstimate(ctx context.Context, chainID uint64, fromAddress string,
ownerTokenParameters DeploymentParameters, masterTokenParameters DeploymentParameters,
ownerTokenParameters requests.DeploymentParameters, masterTokenParameters requests.DeploymentParameters,
communityID string, signerPubKey string) (*CommunityTokenFees, error) {
return api.s.deployOwnerTokenEstimate(ctx, chainID, fromAddress, ownerTokenParameters, masterTokenParameters, communityID, signerPubKey)
}
Expand Down
Loading
Loading