Skip to content

Commit

Permalink
u2
Browse files Browse the repository at this point in the history
  • Loading branch information
saledjenic committed Dec 27, 2024
1 parent 5d02bdd commit 2c5b3f4
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 38 deletions.
33 changes: 19 additions & 14 deletions services/wallet/requests/router_input_community_params.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,16 @@ var (
ErrEmptyCollectibleSymbol = &errors.ErrorResponse{Code: errors.ErrorCode("WRRC-002"), Details: "empty collectible symbol"}
ErrWrongSupplyValue = &errors.ErrorResponse{Code: errors.ErrorCode("WRRC-003"), Details: "wrong supply value: %v"}
ErrWalletAddressesEmpty = &errors.ErrorResponse{Code: errors.ErrorCode("WRRC-004"), Details: "wallet addresses list is empty"}
ErrTokenAmountMustBePositive = &errors.ErrorResponse{Code: errors.ErrorCode("WRRC-005"), Details: "token amount must be positive"}
ErrNoCommunityIdProvided = &errors.ErrorResponse{Code: errors.ErrorCode("WRRC-006"), Details: "community id is required for community related transfers"}
ErrNoCommunitySignerPubKey = &errors.ErrorResponse{Code: errors.ErrorCode("WRRC-007"), Details: "signer pub key is required"}
ErrNoCommunityTokenDeploymentSignature = &errors.ErrorResponse{Code: errors.ErrorCode("WRRC-008"), Details: "signature is required"}
ErrNoCommunityOwnerTokenParameters = &errors.ErrorResponse{Code: errors.ErrorCode("WRRC-009"), Details: "owner token parameters are required"}
ErrNoCommunityMasterTokenParameters = &errors.ErrorResponse{Code: errors.ErrorCode("WRRC-010"), Details: "master token parameters are required"}
ErrNoCommunityDeploymentParameters = &errors.ErrorResponse{Code: errors.ErrorCode("WRRC-011"), Details: "deployment parameters are required"}
ErrNoCommunityContractAddress = &errors.ErrorResponse{Code: errors.ErrorCode("WRRC-012"), Details: "contract address is required"}
ErrNoCommunityBurnAmount = &errors.ErrorResponse{Code: errors.ErrorCode("WRRC-013"), Details: "burn amount is required"}
ErrCommunityBurnAmountMustBePositive = &errors.ErrorResponse{Code: errors.ErrorCode("WRRC-014"), Details: "burn amount must be positive"}
ErrCommunityTokenIdsListEmpty = &errors.ErrorResponse{Code: errors.ErrorCode("WRRC-015"), Details: "token list is empty"}
ErrNoCommunityAmount = &errors.ErrorResponse{Code: errors.ErrorCode("WRRC-005"), Details: "amount is required"}
ErrCommunityAmountMustBePositive = &errors.ErrorResponse{Code: errors.ErrorCode("WRRC-006"), Details: "amount must be positive"}
ErrNoCommunityIdProvided = &errors.ErrorResponse{Code: errors.ErrorCode("WRRC-007"), Details: "community id is required for community related transfers"}
ErrNoCommunitySignerPubKey = &errors.ErrorResponse{Code: errors.ErrorCode("WRRC-008"), Details: "signer pub key is required"}
ErrNoCommunityTokenDeploymentSignature = &errors.ErrorResponse{Code: errors.ErrorCode("WRRC-009"), Details: "signature is required"}
ErrNoCommunityOwnerTokenParameters = &errors.ErrorResponse{Code: errors.ErrorCode("WRRC-010"), Details: "owner token parameters are required"}
ErrNoCommunityMasterTokenParameters = &errors.ErrorResponse{Code: errors.ErrorCode("WRRC-011"), Details: "master token parameters are required"}
ErrNoCommunityDeploymentParameters = &errors.ErrorResponse{Code: errors.ErrorCode("WRRC-012"), Details: "deployment parameters are required"}
ErrNoCommunityContractAddress = &errors.ErrorResponse{Code: errors.ErrorCode("WRRC-013"), Details: "contract address is required"}
ErrCommunityTokenIdsListEmpty = &errors.ErrorResponse{Code: errors.ErrorCode("WRRC-014"), Details: "token list is empty"}
)

type CommunityRouteInputParams struct {
Expand Down Expand Up @@ -111,10 +110,10 @@ func (c *CommunityRouteInputParams) validateCommunityRelatedInputs(sendType send
return ErrNoCommunityContractAddress
}
if c.Amount == nil {
return ErrNoCommunityBurnAmount
return ErrNoCommunityAmount
}
if c.Amount.ToInt().Cmp(big.NewInt(0)) <= 0 {
return ErrCommunityBurnAmountMustBePositive
return ErrCommunityAmountMustBePositive
}
}

Expand Down Expand Up @@ -157,8 +156,14 @@ func (c *CommunityRouteInputParams) validateCommunityRelatedInputs(sendType send
if len(c.WalletAddresses) == 0 {
return ErrWalletAddressesEmpty
}
if c.TokenContractAddress == "" {
return ErrNoCommunityContractAddress
}
if c.Amount == nil {
return ErrNoCommunityAmount
}
if c.Amount.ToInt().Cmp(big.NewInt(0)) <= 0 {
return ErrTokenAmountMustBePositive
return ErrCommunityAmountMustBePositive
}
}

Expand Down
13 changes: 13 additions & 0 deletions services/wallet/requests/router_input_params.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ var (
ErrLockedAmountExcludesAllSupported = &errors.ErrorResponse{Code: errors.ErrorCode("WRR-018"), Details: "all supported chains are excluded, routing impossible"}
ErrCannotCheckLockedAmounts = &errors.ErrorResponse{Code: errors.ErrorCode("WRR-019"), Details: "cannot check locked amounts"}
ErrNoCommunityParametersProvided = &errors.ErrorResponse{Code: errors.ErrorCode("WRR-020"), Details: "no community parameters provided"}
ErrNoFromChainProvided = &errors.ErrorResponse{Code: errors.ErrorCode("WRR-021"), Details: "from chain not provided"}
ErrNoToChainProvided = &errors.ErrorResponse{Code: errors.ErrorCode("WRR-022"), Details: "to chain not provided"}
ErrFromAndToChainMustBeTheSame = &errors.ErrorResponse{Code: errors.ErrorCode("WRR-023"), Details: "from and to chain IDs must be the same"}
)

type RouteInputParams struct {
Expand Down Expand Up @@ -146,6 +149,16 @@ func (i *RouteInputParams) Validate() error {
}

if i.SendType.IsCommunityRelatedTransfer() {
if i.DisabledFromChainIDs == nil || len(i.DisabledFromChainIDs) == 0 {
return ErrNoFromChainProvided
}
if i.DisabledToChainIDs == nil || len(i.DisabledToChainIDs) == 0 {
return ErrNoToChainProvided
}
if len(i.DisabledFromChainIDs) != 1 || len(i.DisabledToChainIDs) != 1 ||
i.DisabledFromChainIDs[0] != i.DisabledToChainIDs[0] {
return ErrFromAndToChainMustBeTheSame
}
if i.CommunityRouteInputParams == nil {
return ErrNoCommunityParametersProvided
}
Expand Down
57 changes: 33 additions & 24 deletions services/wallet/router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -617,14 +617,16 @@ func (r *Router) CreateProcessorInputParams(input *requests.RouteInputParams, fr
processorInputParams.CommunityAmount = input.CommunityRouteInputParams.Amount.ToInt()
}

processorInputParams.CommunityTokenType, err = r.tokenManager.GetCommunityTokenType(fromNetwork.ChainID, input.CommunityRouteInputParams.TokenContractAddress)
if err != nil {
return processorInputParams, err
}
if fromNetwork != nil {
processorInputParams.CommunityTokenType, err = r.tokenManager.GetCommunityTokenType(fromNetwork.ChainID, input.CommunityRouteInputParams.TokenContractAddress)
if err != nil {
return processorInputParams, err
}

processorInputParams.CommunityPrivilegeLevel, err = r.tokenManager.GetCommunityTokenPrivilegesLevel(fromNetwork.ChainID, input.CommunityRouteInputParams.TokenContractAddress)
if err != nil {
return processorInputParams, err
processorInputParams.CommunityPrivilegeLevel, err = r.tokenManager.GetCommunityTokenPrivilegesLevel(fromNetwork.ChainID, input.CommunityRouteInputParams.TokenContractAddress)
if err != nil {
return processorInputParams, err
}
}
}

Expand All @@ -639,9 +641,11 @@ func (r *Router) CreateProcessorInputParams(input *requests.RouteInputParams, fr
processorInputParams.CommunityAmount = input.CommunityRouteInputParams.Amount.ToInt()
}

processorInputParams.CommunityTokenType, err = r.tokenManager.GetCommunityTokenType(fromNetwork.ChainID, input.CommunityRouteInputParams.TokenContractAddress)
if err != nil {
return processorInputParams, err
if fromNetwork != nil {
processorInputParams.CommunityTokenType, err = r.tokenManager.GetCommunityTokenType(fromNetwork.ChainID, input.CommunityRouteInputParams.TokenContractAddress)
if err != nil {
return processorInputParams, err
}
}

processorInputParams.CommunityWalletAddresses = input.CommunityRouteInputParams.WalletAddresses
Expand Down Expand Up @@ -688,6 +692,22 @@ func (r *Router) CreateProcessorInputParams(input *requests.RouteInputParams, fr
return processorInputParams, err
}

func (r *Router) findFromAndToTokens(testsMode bool, input *requests.RouteInputParams, network *params.Network) (fromToken *walletToken.Token, toToken *walletToken.Token) {
if testsMode {
fromToken = input.TestParams.TokenFrom
} else {
fromToken = findToken(input.SendType, r.tokenManager, r.collectiblesService, input.AddrFrom, network, input.TokenID)
}
if fromToken == nil {
return
}

if input.SendType == sendtype.Swap {
toToken = findToken(input.SendType, r.tokenManager, r.collectiblesService, common.Address{}, network, input.ToTokenID)
}
return
}

func (r *Router) resolveCandidates(ctx context.Context, input *requests.RouteInputParams, selectedFromChains []*params.Network,
selectedToChains []*params.Network) (candidates routes.Route, processorErrors []*ProcessorError, err error) {
var (
Expand Down Expand Up @@ -730,24 +750,11 @@ func (r *Router) resolveCandidates(ctx context.Context, input *requests.RouteInp
continue
}

var (
token *walletToken.Token
toToken *walletToken.Token
)

if testsMode {
token = input.TestParams.TokenFrom
} else {
token = findToken(input.SendType, r.tokenManager, r.collectiblesService, input.AddrFrom, network, input.TokenID)
}
token, toToken := r.findFromAndToTokens(testsMode, input, network)
if token == nil {
continue
}

if input.SendType == sendtype.Swap {
toToken = findToken(input.SendType, r.tokenManager, r.collectiblesService, common.Address{}, network, input.ToTokenID)
}

var fetchedFees *fees.SuggestedFees
if testsMode {
fetchedFees = input.TestParams.SuggestedFees
Expand Down Expand Up @@ -889,6 +896,8 @@ func (r *Router) resolveCandidates(ctx context.Context, input *requests.RouteInp

SubtractFees: amountOption.subtractFees,
}
path.SetCommunityTokenType(processorInputParams.CommunityTokenType)
path.SetCommunityPrivilegeLevel(processorInputParams.CommunityPrivilegeLevel)

err = r.cacluateFees(ctx, path, fetchedFees, processorInputParams.TestsMode, processorInputParams.TestApprovalL1Fee)
if err != nil {
Expand Down
25 changes: 25 additions & 0 deletions services/wallet/router/routes/router_path.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/status-im/status-go/params"
communitiestoken "github.com/status-im/status-go/protocol/communities/token"
"github.com/status-im/status-go/protocol/protobuf"
"github.com/status-im/status-go/services/wallet/router/fees"
walletToken "github.com/status-im/status-go/services/wallet/token"
)
Expand Down Expand Up @@ -51,12 +53,32 @@ type Path struct {
RequiredTokenBalance *big.Int // (in selected token)
RequiredNativeBalance *big.Int // (in ETH WEI)
SubtractFees bool

// used internally
communityTokenType protobuf.CommunityTokenType
communityPrivilegeLevel communitiestoken.PrivilegesLevel
}

func (p *Path) Equal(o *Path) bool {
return p.FromChain.ChainID == o.FromChain.ChainID && p.ToChain.ChainID == o.ToChain.ChainID
}

func (p *Path) SetCommunityTokenType(tokenType protobuf.CommunityTokenType) {
p.communityTokenType = tokenType
}

func (p *Path) GetCommunityTokenType() protobuf.CommunityTokenType {
return p.communityTokenType
}

func (p *Path) SetCommunityPrivilegeLevel(privilegeLevel communitiestoken.PrivilegesLevel) {
p.communityPrivilegeLevel = privilegeLevel
}

func (p *Path) GetCommunityPrivilegeLevel() communitiestoken.PrivilegesLevel {
return p.communityPrivilegeLevel
}

func (p *Path) Copy() *Path {
newPath := &Path{
ProcessorName: p.ProcessorName,
Expand Down Expand Up @@ -174,6 +196,9 @@ func (p *Path) Copy() *Path {
newPath.RequiredNativeBalance = big.NewInt(0).Set(p.RequiredNativeBalance)
}

newPath.communityTokenType = p.communityTokenType
newPath.communityPrivilegeLevel = p.communityPrivilegeLevel

return newPath
}

Expand Down
3 changes: 3 additions & 0 deletions services/wallet/transfer/transaction_manager_route.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,9 @@ func buildTxForPath(path *routes.Path, pathProcessors map[string]pathprocessor.P
processorInputParams.ToToken = path.ToToken
processorInputParams.AmountIn = path.AmountIn.ToInt()
processorInputParams.AmountOut = path.AmountOut.ToInt()
// update porcessor input community related params
processorInputParams.CommunityTokenType = path.GetCommunityTokenType()
processorInputParams.CommunityPrivilegeLevel = path.GetCommunityPrivilegeLevel()

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

0 comments on commit 2c5b3f4

Please sign in to comment.