Skip to content

Commit

Permalink
chore(wallet)_: community deployment related types moved to wallet re…
Browse files Browse the repository at this point in the history
…quests package

`DeploymentParameters` and `DeploymentDetails` types moved from `communitytokens` package
to `requests` of the wallet service.
  • Loading branch information
saledjenic committed Dec 23, 2024
1 parent a4e36d4 commit c9a9f66
Show file tree
Hide file tree
Showing 7 changed files with 192 additions and 163 deletions.
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
65 changes: 0 additions & 65 deletions services/communitytokens/api_test.go
Original file line number Diff line number Diff line change
@@ -1,78 +1,13 @@
package communitytokens

import (
"math/big"
"testing"

"github.com/stretchr/testify/require"

"github.com/ethereum/go-ethereum/common"

"github.com/status-im/status-go/services/wallet/bigint"
)

func TestDeploymentParameters(t *testing.T) {
var testCases = []struct {
name string
parameters DeploymentParameters
isError bool
}{
{
name: "emptyName",
parameters: DeploymentParameters{"", "SYMBOL", &bigint.BigInt{Int: big.NewInt(int64(123))}, false, false, false, "", "", "", "", "", nil, "", 0},
isError: true,
},
{
name: "emptySymbol",
parameters: DeploymentParameters{"NAME", "", &bigint.BigInt{Int: big.NewInt(123)}, false, false, false, "", "", "", "", "", nil, "", 0},
isError: true,
},
{
name: "negativeSupply",
parameters: DeploymentParameters{"NAME", "SYM", &bigint.BigInt{Int: big.NewInt(-123)}, false, false, false, "", "", "", "", "", nil, "", 0},
isError: true,
},
{
name: "zeroSupply",
parameters: DeploymentParameters{"NAME", "SYM", &bigint.BigInt{Int: big.NewInt(0)}, false, false, false, "", "", "", "", "", nil, "", 0},
isError: false,
},
{
name: "negativeSupplyAndInfinite",
parameters: DeploymentParameters{"NAME", "SYM", &bigint.BigInt{Int: big.NewInt(-123)}, true, false, false, "", "", "", "", "", nil, "", 0},
isError: false,
},
{
name: "supplyGreaterThanMax",
parameters: DeploymentParameters{"NAME", "SYM", &bigint.BigInt{Int: big.NewInt(maxSupply + 1)}, false, false, false, "", "", "", "", "", nil, "", 0},
isError: true,
},
{
name: "supplyIsMax",
parameters: DeploymentParameters{"NAME", "SYM", &bigint.BigInt{Int: big.NewInt(maxSupply)}, false, false, false, "", "", "", "", "", nil, "", 0},
isError: false,
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
err := tc.parameters.Validate(false)
if tc.isError {
require.Error(t, err)
} else {
require.NoError(t, err)
}
})
}

notInfiniteSupplyParams := DeploymentParameters{"NAME", "SYM", &bigint.BigInt{Int: big.NewInt(123)}, false, false, false, "", "", "", "", "", nil, "", 0}
requiredSupply := big.NewInt(123)
require.Equal(t, notInfiniteSupplyParams.GetSupply(), requiredSupply)
infiniteSupplyParams := DeploymentParameters{"NAME", "SYM", &bigint.BigInt{Int: big.NewInt(123)}, true, false, false, "", "", "", "", "", nil, "", 0}
requiredSupply = infiniteSupplyParams.GetInfiniteSupply()
require.Equal(t, infiniteSupplyParams.GetSupply(), requiredSupply)
}

func TestTypedDataHash(t *testing.T) {
sigHash := common.Hex2Bytes("dd91c30357aafeb2792b5f0facbd83995943c1ea113a906ebbeb58bfeb27dfc2")
domainSep := common.Hex2Bytes("4a672b5a08e88d37f7239165a0c9e03a01196587d52c638c0c99cbee5ba527c8")
Expand Down
5 changes: 3 additions & 2 deletions services/communitytokens/estimations.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/status-im/status-go/logutils"
"github.com/status-im/status-go/protocol/protobuf"
"github.com/status-im/status-go/services/wallet/bigint"
"github.com/status-im/status-go/services/wallet/requests"
"github.com/status-im/status-go/services/wallet/router/fees"
"github.com/status-im/status-go/services/wallet/wallettypes"
)
Expand All @@ -45,7 +46,7 @@ func gweiToWei(val *big.Float) *big.Int {
}

func (s *Service) deployOwnerTokenEstimate(ctx context.Context, chainID uint64, fromAddress string,
ownerTokenParameters DeploymentParameters, masterTokenParameters DeploymentParameters,
ownerTokenParameters requests.DeploymentParameters, masterTokenParameters requests.DeploymentParameters,
communityID string, signerPubKey string) (*CommunityTokenFees, error) {

gasUnits, err := s.deployOwnerTokenGasUnits(ctx, chainID, fromAddress, ownerTokenParameters, masterTokenParameters,
Expand Down Expand Up @@ -157,7 +158,7 @@ func (s *Service) remoteBurnGasUnits(ctx context.Context, chainID uint64, contra
}

func (s *Service) deployOwnerTokenGasUnits(ctx context.Context, chainID uint64, fromAddress string,
ownerTokenParameters DeploymentParameters, masterTokenParameters DeploymentParameters,
ownerTokenParameters requests.DeploymentParameters, masterTokenParameters requests.DeploymentParameters,
communityID string, signerPubKey string) (uint64, error) {
ethClient, err := s.manager.rpcClient.EthClient(chainID)
if err != nil {
Expand Down
Loading

0 comments on commit c9a9f66

Please sign in to comment.