Skip to content

Commit

Permalink
u1
Browse files Browse the repository at this point in the history
  • Loading branch information
saledjenic committed Dec 12, 2024
1 parent 2ab05bf commit 7bc48bf
Show file tree
Hide file tree
Showing 10 changed files with 64 additions and 69 deletions.
9 changes: 4 additions & 5 deletions services/wallet/requests/router_input_community_params.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,9 @@ type CommunityRouteInputParams struct {
CommunityID string `json:"communityID"`
TokenContractAddress string `json:"tokenContractAddress"`
SignerPubKey string `json:"signerPubKey"`
BurnAmount *hexutil.Big `json:"burnAmount"`
Amount *hexutil.Big `json:"amount"`
TokenIds []*hexutil.Big `json:"tokenIds"`
WalletAddresses []common.Address `json:"walletAddresses"`
TokenAmount *hexutil.Big `json:"collectibleAmount"`
TokenDeploymentSignature string `json:"tokenDeploymentSignature"`
OwnerTokenParameters *DeploymentParameters `json:"ownerTokenParameters"`
MasterTokenParameters *DeploymentParameters `json:"masterTokenParameters"`
Expand Down Expand Up @@ -110,10 +109,10 @@ func (c *CommunityRouteInputParams) validateCommunityRelatedInputs(sendType send
if c.TokenContractAddress == "" {
return ErrNoCommunityContractAddress
}
if c.BurnAmount == nil {
if c.Amount == nil {
return ErrNoCommunityBurnAmount
}
if c.BurnAmount.ToInt().Cmp(big.NewInt(0)) <= 0 {
if c.Amount.ToInt().Cmp(big.NewInt(0)) <= 0 {
return ErrCommunityBurnAmountMustBePositive
}
}
Expand Down Expand Up @@ -157,7 +156,7 @@ func (c *CommunityRouteInputParams) validateCommunityRelatedInputs(sendType send
if len(c.WalletAddresses) == 0 {
return ErrWalletAddressesEmpty
}
if c.TokenAmount.ToInt().Cmp(big.NewInt(0)) <= 0 {
if c.Amount.ToInt().Cmp(big.NewInt(0)) <= 0 {
return ErrTokenAmountMustBePositive
}
}
Expand Down
4 changes: 4 additions & 0 deletions services/wallet/requests/router_input_params.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ var (
ErrNoCommunityBurnAmount = &errors.ErrorResponse{Code: errors.ErrorCode("WRR-027"), Details: "burn amount is required"}
ErrCommunityBurnAmountMustBePositive = &errors.ErrorResponse{Code: errors.ErrorCode("WRR-028"), Details: "burn amount must be positive"}
ErrCommunityTokenIdsListEmpty = &errors.ErrorResponse{Code: errors.ErrorCode("WRR-029"), Details: "token list is empty"}
ErrNoCommunityParametersProvided = &errors.ErrorResponse{Code: errors.ErrorCode("WRR-030"), Details: "no community parameters provided"}
)

type RouteInputParams struct {
Expand Down Expand Up @@ -155,6 +156,9 @@ func (i *RouteInputParams) Validate() error {
}

if i.SendType.IsCommunityRelatedTransfer() {
if i.CommunityRouteInputParams == nil {
return ErrNoCommunityParametersProvided
}
return i.CommunityRouteInputParams.validateCommunityRelatedInputs(i.SendType)
}

Expand Down
17 changes: 9 additions & 8 deletions services/wallet/routeexecution/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/status-im/status-go/services/wallet/responses"
"github.com/status-im/status-go/services/wallet/routeexecution/storage"
"github.com/status-im/status-go/services/wallet/router"
"github.com/status-im/status-go/services/wallet/router/pathprocessor"
pathProcessorCommon "github.com/status-im/status-go/services/wallet/router/pathprocessor/common"
"github.com/status-im/status-go/services/wallet/router/sendtype"
"github.com/status-im/status-go/services/wallet/transfer"
Expand Down Expand Up @@ -86,6 +87,13 @@ func (m *Manager) BuildTransactionsFromRoute(ctx context.Context, buildInputPara
return
}

// re-use path processor input params structure to pass extra params to transaction manager
var extraParams pathprocessor.ProcessorInputParams
extraParams, err = m.router.CreateProcessorInputParams(&routeInputParams, nil, nil, nil, nil, nil, buildInputParams.SlippagePercentage)
if err != nil {
return
}

m.buildInputParams = buildInputParams

response.SendDetails.UpdateFields(routeInputParams)
Expand All @@ -96,14 +104,7 @@ func (m *Manager) BuildTransactionsFromRoute(ctx context.Context, buildInputPara
response.SigningDetails, err = m.transactionManager.BuildTransactionsFromRoute(
route,
m.router.GetPathProcessors(),
transfer.BuildRouteExtraParams{
AddressFrom: routeInputParams.AddrFrom,
AddressTo: routeInputParams.AddrTo,
Username: routeInputParams.Username,
PublicKey: routeInputParams.PublicKey,
PackID: routeInputParams.PackID.ToInt(),
SlippagePercentage: buildInputParams.SlippagePercentage,
},
&extraParams,
)
}()
}
Expand Down
13 changes: 7 additions & 6 deletions services/wallet/router/pathprocessor/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ type ProcessorCommunityTokenParams struct {
OwnerTokenAddress string
MasterTokenAddress string
}

type ProcessorInputParams struct {
FromChain *params.Network
ToChain *params.Network
Expand All @@ -67,19 +68,19 @@ type ProcessorInputParams struct {
AmountOut *big.Int

// extra params
BonderFee *big.Int
Username string
PublicKey string
PackID *big.Int
BonderFee *big.Int
Username string
PublicKey string
PackID *big.Int
SlippagePercentage float32

// community related params
CommunityID string
CommunityTokenContractAddress common.Address
CommunitySignerPubKey string
CommunityBurnAmount *big.Int
CommunityAmount *big.Int
CommunityTokenIds []*big.Int
CommunityWalletAddresses []common.Address
CommunityTokenAmount *big.Int
CommunityTokenDeploymentSignature string
CommunityTokenType protobuf.CommunityTokenType
CommunityPrivilegeLevel communitiestoken.PrivilegesLevel
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func (s *CommunityBurnProcessor) validateBurnAmount(ctx context.Context, params
if err != nil {
return err
}
if params.CommunityBurnAmount.Cmp(remainingSupply) > 1 {
if params.CommunityAmount.Cmp(remainingSupply) > 1 {
return ErrBurnAmountTooHigh
}
return nil
Expand Down Expand Up @@ -160,7 +160,7 @@ func (s *CommunityBurnProcessor) PackTxInputData(params ProcessorInputParams) ([
return []byte{}, createCommunityBurnErrorResponse(err)
}
newMaxSupply := big.NewInt(0)
newMaxSupply.Sub(maxSupply, params.CommunityBurnAmount)
newMaxSupply.Sub(maxSupply, params.CommunityAmount)

switch params.CommunityTokenType {
case protobuf.CommunityTokenType_ERC721:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,14 @@ func prepareMintAssetsData(amount *big.Int, walletAddresses []common.Address) (r
func (s *CommunityMintTokensProcessor) PackTxInputData(params ProcessorInputParams) ([]byte, error) {
switch params.CommunityTokenType {
case protobuf.CommunityTokenType_ERC721:
destinationAddresses := multiplyWalletAddresses(params.CommunityTokenAmount, params.CommunityWalletAddresses)
destinationAddresses := multiplyWalletAddresses(params.CommunityAmount, params.CommunityWalletAddresses)
collectiblesABI, err := abi.JSON(strings.NewReader(collectibles.CollectiblesABI))
if err != nil {
return []byte{}, err
}
return collectiblesABI.Pack("mintTo", destinationAddresses)
case protobuf.CommunityTokenType_ERC20:
destinationAmounts := prepareMintAssetsData(params.CommunityTokenAmount, params.CommunityWalletAddresses)
destinationAmounts := prepareMintAssetsData(params.CommunityAmount, params.CommunityWalletAddresses)
assetsABI, err := abi.JSON(strings.NewReader(assets.AssetsABI))
if err != nil {
return []byte{}, err
Expand Down
29 changes: 15 additions & 14 deletions services/wallet/router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -580,16 +580,18 @@ func (r *Router) getSelectedChains(input *requests.RouteInputParams) (selectedFr
return selectedFromChains, selectedToChains, nil
}

func (r *Router) mapToProcessorInputParams(input *requests.RouteInputParams, fromNetwork *params.Network, toNetwork *params.Network, fromToken *token.Token, toToken *token.Token, amountIn *big.Int) (pathprocessor.ProcessorInputParams, error) {
func (r *Router) CreateProcessorInputParams(input *requests.RouteInputParams, fromNetwork *params.Network, toNetwork *params.Network,
fromToken *token.Token, toToken *token.Token, amountIn *big.Int, slippagePercentage float32) (pathprocessor.ProcessorInputParams, error) {
var err error
processorInputParams := pathprocessor.ProcessorInputParams{
FromChain: fromNetwork,
ToChain: toNetwork,
FromToken: fromToken,
ToToken: toToken,
ToAddr: input.AddrTo,
FromAddr: input.AddrFrom,
AmountIn: amountIn,
FromChain: fromNetwork,
ToChain: toNetwork,
FromToken: fromToken,
ToToken: toToken,
ToAddr: input.AddrTo,
FromAddr: input.AddrFrom,
AmountIn: amountIn,
SlippagePercentage: slippagePercentage,

Username: input.Username,
PublicKey: input.PublicKey,
Expand All @@ -608,12 +610,11 @@ func (r *Router) mapToProcessorInputParams(input *requests.RouteInputParams, fro
processorInputParams.CommunityID = input.CommunityRouteInputParams.CommunityID
processorInputParams.CommunityTokenContractAddress = common.HexToAddress(input.CommunityRouteInputParams.TokenContractAddress)
processorInputParams.CommunitySignerPubKey = input.CommunityRouteInputParams.SignerPubKey
processorInputParams.CommunityBurnAmount = input.CommunityRouteInputParams.BurnAmount.ToInt()
processorInputParams.CommunityTokenDeploymentSignature = input.CommunityRouteInputParams.TokenDeploymentSignature

if input.SendType == sendtype.CommunityBurn {
if input.CommunityRouteInputParams.BurnAmount != nil {
processorInputParams.CommunityBurnAmount = input.CommunityRouteInputParams.BurnAmount.ToInt()
if input.CommunityRouteInputParams.Amount != nil {
processorInputParams.CommunityAmount = input.CommunityRouteInputParams.Amount.ToInt()
}

processorInputParams.CommunityTokenType, err = r.tokenManager.GetCommunityTokenType(fromNetwork.ChainID, input.CommunityRouteInputParams.TokenContractAddress)
Expand All @@ -634,8 +635,8 @@ func (r *Router) mapToProcessorInputParams(input *requests.RouteInputParams, fro
}

if input.SendType == sendtype.CommunityMintTokens {
if input.CommunityRouteInputParams.TokenAmount != nil {
processorInputParams.CommunityTokenAmount = input.CommunityRouteInputParams.TokenAmount.ToInt()
if input.CommunityRouteInputParams.Amount != nil {
processorInputParams.CommunityAmount = input.CommunityRouteInputParams.Amount.ToInt()
}

processorInputParams.CommunityWalletAddresses = input.CommunityRouteInputParams.WalletAddresses
Expand Down Expand Up @@ -794,7 +795,7 @@ func (r *Router) resolveCandidates(ctx context.Context, input *requests.RouteInp
continue
}

processorInputParams, err := r.mapToProcessorInputParams(input, network, dest, token, toToken, amountOption.amount)
processorInputParams, err := r.CreateProcessorInputParams(input, network, dest, token, toToken, amountOption.amount, 0)
if err != nil {
appendProcessorErrorFn(pProcessor.Name(), input.SendType, network.ChainID, dest.ChainID, amountOption.amount, err)
continue
Expand Down
4 changes: 4 additions & 0 deletions services/wallet/router/sendtype/send_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,5 +139,9 @@ func (s SendType) IsAvailableFor(network *params.Network) bool {
return network.ChainID == walletCommon.EthereumMainnet || network.ChainID == walletCommon.EthereumSepolia
}

if s.IsCommunityRelatedTransfer() {
return true
}

return false
}
1 change: 1 addition & 0 deletions services/wallet/token/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -715,6 +715,7 @@ func (tm *Manager) DeleteCustom(chainID uint64, address common.Address) error {
return err
}

// TODO: remove this method after migration to new token store
func (tm *Manager) SignalCommunityTokenReceived(address common.Address, txHash common.Hash, value *big.Int, t *Token, isFirst bool) {
defer gocommon.LogOnPanic()
if tm.walletFeed == nil || t == nil || t.CommunityData == nil {
Expand Down
48 changes: 16 additions & 32 deletions services/wallet/transfer/transaction_manager_route.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,6 @@ import (
"github.com/status-im/status-go/transactions"
)

type BuildRouteExtraParams struct {
AddressFrom common.Address
AddressTo common.Address
Username string
PublicKey string
PackID *big.Int
SlippagePercentage float32
}

func (tm *TransactionManager) ClearLocalRouterTransactionsData() {
tm.routerTransactions = nil
}
Expand Down Expand Up @@ -126,38 +117,31 @@ func buildApprovalTxForPath(transactor transactions.TransactorIface, path *route
}

func buildTxForPath(transactor transactions.TransactorIface, path *routes.Path, pathProcessors map[string]pathprocessor.PathProcessor,
usedNonces map[uint64]int64, signer ethTypes.Signer, params BuildRouteExtraParams) (*wallettypes.TransactionData, error) {
usedNonces map[uint64]int64, signer ethTypes.Signer, processorInputParams *pathprocessor.ProcessorInputParams) (*wallettypes.TransactionData, error) {
lastUsedNonce := int64(-1)
if nonce, ok := usedNonces[path.FromChain.ChainID]; ok {
lastUsedNonce = nonce
}

processorInputParams := pathprocessor.ProcessorInputParams{
FromAddr: params.AddressFrom,
ToAddr: params.AddressTo,
FromChain: path.FromChain,
ToChain: path.ToChain,
FromToken: path.FromToken,
ToToken: path.ToToken,
AmountIn: path.AmountIn.ToInt(),
AmountOut: path.AmountOut.ToInt(),

Username: params.Username,
PublicKey: params.PublicKey,
PackID: params.PackID,
}
// update processor input params for the current path
processorInputParams.FromChain = path.FromChain
processorInputParams.ToChain = path.ToChain
processorInputParams.FromToken = path.FromToken
processorInputParams.ToToken = path.ToToken
processorInputParams.AmountIn = path.AmountIn.ToInt()
processorInputParams.AmountOut = path.AmountOut.ToInt()

data, err := pathProcessors[path.ProcessorName].PackTxInputData(processorInputParams)
data, err := pathProcessors[path.ProcessorName].PackTxInputData(*processorInputParams)
if err != nil {
return nil, err
}

addrTo := types.Address(params.AddressTo)
addrTo := types.Address(processorInputParams.ToAddr)
sendArgs := &wallettypes.SendTxArgs{
Version: wallettypes.SendTxArgsVersion1,

// tx fields
From: types.Address(params.AddressFrom),
From: types.Address(processorInputParams.FromAddr),
To: &addrTo,
Value: path.AmountIn,
Data: data,
Expand All @@ -170,7 +154,7 @@ func buildTxForPath(transactor transactions.TransactorIface, path *routes.Path,
ValueOut: path.AmountOut,
FromChainID: path.FromChain.ChainID,
ToChainID: path.ToChain.ChainID,
SlippagePercentage: params.SlippagePercentage,
SlippagePercentage: processorInputParams.SlippagePercentage,
}
if path.FromToken != nil {
sendArgs.FromTokenID = path.FromToken.Symbol
Expand Down Expand Up @@ -214,12 +198,12 @@ func buildTxForPath(transactor transactions.TransactorIface, path *routes.Path,
}

func (tm *TransactionManager) BuildTransactionsFromRoute(route routes.Route, pathProcessors map[string]pathprocessor.PathProcessor,
params BuildRouteExtraParams) (*responses.SigningDetails, error) {
processorInputParams *pathprocessor.ProcessorInputParams) (*responses.SigningDetails, error) {
if len(route) == 0 {
return nil, ErrNoRoute
}

accFrom, err := tm.accountsDB.GetAccountByAddress(types.Address(params.AddressFrom))
accFrom, err := tm.accountsDB.GetAccountByAddress(types.Address(processorInputParams.FromAddr))
if err != nil {
return nil, err
}
Expand All @@ -244,7 +228,7 @@ func (tm *TransactionManager) BuildTransactionsFromRoute(route routes.Route, pat

// always check for approval tx first for the path and build it if needed
if path.ApprovalRequired && !tm.ApprovalPlacedForPath(path.ProcessorName) {
txDetails.ApprovalTxData, err = buildApprovalTxForPath(tm.transactor, path, params.AddressFrom, usedNonces, signer)
txDetails.ApprovalTxData, err = buildApprovalTxForPath(tm.transactor, path, processorInputParams.FromAddr, usedNonces, signer)
if err != nil {
return nil, err
}
Expand All @@ -257,7 +241,7 @@ func (tm *TransactionManager) BuildTransactionsFromRoute(route routes.Route, pat
}

// build tx for the path
txDetails.TxData, err = buildTxForPath(tm.transactor, path, pathProcessors, usedNonces, signer, params)
txDetails.TxData, err = buildTxForPath(tm.transactor, path, pathProcessors, usedNonces, signer, processorInputParams)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 7bc48bf

Please sign in to comment.