-
Notifications
You must be signed in to change notification settings - Fork 247
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
feat(wallet)!: allowing client to set custom values for fees, nonce, gas #6124
base: develop
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package requests | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/ethereum/go-ethereum/common/hexutil" | ||
"github.com/status-im/status-go/errors" | ||
"github.com/status-im/status-go/services/wallet/router/fees" | ||
) | ||
|
||
var ( | ||
ErrMaxFeesPerGasRequired = &errors.ErrorResponse{Code: errors.ErrorCode("WRC-001"), Details: "maxFeesPerGas is required"} | ||
ErrPriorityFeeRequired = &errors.ErrorResponse{Code: errors.ErrorCode("WRC-002"), Details: "priorityFee is required"} | ||
) | ||
|
||
type PathTxCustomParams struct { | ||
GasFeeMode fees.GasFeeMode `json:"gasFeeMode" validate:"required"` | ||
Nonce uint64 `json:"nonce"` | ||
GasAmount uint64 `json:"gasAmount"` | ||
MaxFeesPerGas *hexutil.Big `json:"maxFeesPerGas"` | ||
PriorityFee *hexutil.Big `json:"priorityFee"` | ||
} | ||
|
||
type PathTxIdentity struct { | ||
RouterInputParamsUuid string `json:"routerInputParamsUuid" validate:"required"` | ||
PathName string `json:"pathName" validate:"required"` | ||
ChainID uint64 `json:"chainID" validate:"required"` | ||
IsApprovalTx bool `json:"isApprovalTx"` | ||
} | ||
|
||
func (p *PathTxIdentity) PathIdentity() string { | ||
return fmt.Sprintf("%s-%s-%d", p.RouterInputParamsUuid, p.PathName, p.ChainID) | ||
} | ||
|
||
func (p *PathTxIdentity) TxIdentityKey() string { | ||
return fmt.Sprintf("%s-%v", p.PathIdentity(), p.IsApprovalTx) | ||
} | ||
|
||
func (p *PathTxCustomParams) Validate() error { | ||
if p.GasFeeMode != fees.GasFeeCustom { | ||
return nil | ||
} | ||
if p.MaxFeesPerGas == nil { | ||
return ErrMaxFeesPerGasRequired | ||
} | ||
if p.PriorityFee == nil { | ||
return ErrPriorityFeeRequired | ||
} | ||
return nil | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,7 @@ import ( | |
"github.com/ethereum/go-ethereum/consensus/misc" | ||
"github.com/ethereum/go-ethereum/params" | ||
gaspriceoracle "github.com/status-im/status-go/contracts/gas-price-oracle" | ||
"github.com/status-im/status-go/errors" | ||
"github.com/status-im/status-go/rpc" | ||
"github.com/status-im/status-go/rpc/chain" | ||
"github.com/status-im/status-go/services/wallet/common" | ||
|
@@ -23,6 +24,11 @@ const ( | |
GasFeeLow GasFeeMode = iota | ||
GasFeeMedium | ||
GasFeeHigh | ||
GasFeeCustom | ||
) | ||
|
||
var ( | ||
ErrCustomFeeModeNotAvailableInSuggestedFees = &errors.ErrorResponse{Code: errors.ErrorCode("WRF-001"), Details: "custom fee mode is not available in suggested fees"} | ||
) | ||
|
||
type MaxFeesLevels struct { | ||
|
@@ -50,23 +56,28 @@ type SuggestedFeesGwei struct { | |
MaxFeePerGasLow *big.Float `json:"maxFeePerGasLow"` | ||
MaxFeePerGasMedium *big.Float `json:"maxFeePerGasMedium"` | ||
MaxFeePerGasHigh *big.Float `json:"maxFeePerGasHigh"` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. shall we adjust our various fees level? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How do you mean this? To have a new formula for each? This is what we have now: {
Low: (*hexutil.Big)(new(big.Int).Add(baseFee, maxPriorityFeePerGas)),
Medium: (*hexutil.Big)(new(big.Int).Add(new(big.Int).Mul(baseFee, big.NewInt(2)), maxPriorityFeePerGas)),
High: (*hexutil.Big)(new(big.Int).Add(new(big.Int).Mul(baseFee, big.NewInt(3)), maxPriorityFeePerGas)),
} Let me know if you want to change the equation and to what? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would suggest: high to be 2x base fee and medium to be 1.2 base fee There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
MaxFeePerGasCustom *big.Float `json:"maxFeePerGasCustom"` | ||
L1GasFee *big.Float `json:"l1GasFee,omitempty"` | ||
EIP1559Enabled bool `json:"eip1559Enabled"` | ||
} | ||
|
||
func (m *MaxFeesLevels) FeeFor(mode GasFeeMode) *big.Int { | ||
func (m *MaxFeesLevels) FeeFor(mode GasFeeMode) (*big.Int, error) { | ||
if mode == GasFeeCustom { | ||
return nil, ErrCustomFeeModeNotAvailableInSuggestedFees | ||
} | ||
|
||
if mode == GasFeeLow { | ||
return m.Low.ToInt() | ||
return m.Low.ToInt(), nil | ||
} | ||
|
||
if mode == GasFeeHigh { | ||
return m.High.ToInt() | ||
return m.High.ToInt(), nil | ||
} | ||
|
||
return m.Medium.ToInt() | ||
return m.Medium.ToInt(), nil | ||
} | ||
|
||
func (s *SuggestedFees) FeeFor(mode GasFeeMode) *big.Int { | ||
func (s *SuggestedFees) FeeFor(mode GasFeeMode) (*big.Int, error) { | ||
return s.MaxFeesLevels.FeeFor(mode) | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -64,6 +64,7 @@ type SuggestedRoutes struct { | |
|
||
type Router struct { | ||
rpcClient *rpc.Client | ||
transactor *transactions.Transactor | ||
tokenManager *token.Manager | ||
marketManager *market.Manager | ||
collectiblesService *collectibles.Service | ||
|
@@ -92,6 +93,7 @@ func NewRouter(rpcClient *rpc.Client, transactor *transactions.Transactor, token | |
|
||
return &Router{ | ||
rpcClient: rpcClient, | ||
transactor: transactor, | ||
tokenManager: tokenManager, | ||
marketManager: marketManager, | ||
collectiblesService: collectibles, | ||
|
@@ -140,6 +142,57 @@ func (r *Router) SetTestBalanceMap(balanceMap map[string]*big.Int) { | |
} | ||
} | ||
|
||
func (r *Router) setCustomTxDetails(pathTxIdentity *requests.PathTxIdentity, pathTxCustomParams *requests.PathTxCustomParams) error { | ||
if pathTxIdentity == nil { | ||
return ErrTxIdentityNotProvided | ||
} | ||
if pathTxCustomParams == nil { | ||
return ErrTxCustomParamsNotProvided | ||
} | ||
err := pathTxCustomParams.Validate() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
r.activeRoutesMutex.Lock() | ||
defer r.activeRoutesMutex.Unlock() | ||
if r.activeRoutes == nil || len(r.activeRoutes.Best) == 0 { | ||
return ErrCannotCustomizeIfNoRoute | ||
} | ||
|
||
for _, path := range r.activeRoutes.Best { | ||
if path.PathIdentity() != pathTxIdentity.PathIdentity() { | ||
continue | ||
} | ||
|
||
r.lastInputParamsMutex.Lock() | ||
if r.lastInputParams.PathTxCustomParams == nil { | ||
r.lastInputParams.PathTxCustomParams = make(map[string]*requests.PathTxCustomParams) | ||
} | ||
r.lastInputParams.PathTxCustomParams[pathTxIdentity.TxIdentityKey()] = pathTxCustomParams | ||
r.lastInputParamsMutex.Unlock() | ||
|
||
return nil | ||
} | ||
|
||
return ErrCannotFindPathForProvidedIdentity | ||
} | ||
|
||
func (r *Router) SetFeeMode(ctx context.Context, pathTxIdentity *requests.PathTxIdentity, feeMode fees.GasFeeMode) error { | ||
if feeMode == fees.GasFeeCustom { | ||
return ErrCustomFeeModeCannotBeSetThisWay | ||
} | ||
|
||
return r.setCustomTxDetails(pathTxIdentity, &requests.PathTxCustomParams{GasFeeMode: feeMode}) | ||
} | ||
|
||
func (r *Router) SetCustomTxDetails(ctx context.Context, pathTxIdentity *requests.PathTxIdentity, pathTxCustomParams *requests.PathTxCustomParams) error { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could we find a better name for the private function? IMO a bit unclear There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am open to suggestions, but it's not private, it's exposed via API. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes this one is not private but there is also setCustomTxDetails |
||
if pathTxCustomParams != nil && pathTxCustomParams.GasFeeMode != fees.GasFeeCustom { | ||
return ErrOnlyCustomFeeModeCanBeSetThisWay | ||
} | ||
return r.setCustomTxDetails(pathTxIdentity, pathTxCustomParams) | ||
} | ||
|
||
func newSuggestedRoutes( | ||
input *requests.RouteInputParams, | ||
candidates routes.Route, | ||
|
@@ -585,7 +638,11 @@ func (r *Router) resolveCandidates(ctx context.Context, input *requests.RouteInp | |
var ( | ||
testsMode = input.TestsMode && input.TestParams != nil | ||
group = async.NewAtomicGroup(ctx) | ||
mu sync.Mutex | ||
|
||
candidatesMu sync.Mutex | ||
|
||
usedNonces = make(map[uint64]uint64) | ||
usedNoncesMu sync.Mutex | ||
) | ||
|
||
crossChainAmountOptions, err := r.findOptionsForSendingAmount(input, selectedFromChains) | ||
|
@@ -601,17 +658,17 @@ func (r *Router) resolveCandidates(ctx context.Context, input *requests.RouteInp | |
zap.Uint64("toChainId", toChainID), | ||
zap.Stringer("amount", amount), | ||
zap.Error(err)) | ||
mu.Lock() | ||
defer mu.Unlock() | ||
candidatesMu.Lock() | ||
defer candidatesMu.Unlock() | ||
processorErrors = append(processorErrors, &ProcessorError{ | ||
ProcessorName: processorName, | ||
Error: err, | ||
}) | ||
} | ||
|
||
appendPathFn := func(path *routes.Path) { | ||
mu.Lock() | ||
defer mu.Unlock() | ||
candidatesMu.Lock() | ||
defer candidatesMu.Unlock() | ||
candidates = append(candidates, path) | ||
} | ||
|
||
|
@@ -765,22 +822,16 @@ func (r *Router) resolveCandidates(ctx context.Context, input *requests.RouteInp | |
continue | ||
} | ||
|
||
maxFeesPerGas := fetchedFees.FeeFor(input.GasFeeMode) | ||
|
||
estimatedTime := r.feesManager.TransactionEstimatedTime(ctx, network.ChainID, maxFeesPerGas) | ||
if approvalRequired && estimatedTime < fees.MoreThanFiveMinutes { | ||
estimatedTime += 1 | ||
} | ||
|
||
path := &routes.Path{ | ||
ProcessorName: pProcessor.Name(), | ||
FromChain: network, | ||
ToChain: dest, | ||
FromToken: token, | ||
ToToken: toToken, | ||
AmountIn: (*hexutil.Big)(amountOption.amount), | ||
AmountInLocked: amountOption.locked, | ||
AmountOut: (*hexutil.Big)(amountOut), | ||
RouterInputParamsUuid: input.Uuid, | ||
ProcessorName: pProcessor.Name(), | ||
FromChain: network, | ||
ToChain: dest, | ||
FromToken: token, | ||
ToToken: toToken, | ||
AmountIn: (*hexutil.Big)(amountOption.amount), | ||
AmountInLocked: amountOption.locked, | ||
AmountOut: (*hexutil.Big)(amountOut), | ||
|
||
// set params that we don't want to be recalculated with every new block creation | ||
TxGasAmount: gasLimit, | ||
|
@@ -792,12 +843,12 @@ func (r *Router) resolveCandidates(ctx context.Context, input *requests.RouteInp | |
ApprovalContractAddress: &approvalContractAddress, | ||
ApprovalGasAmount: approvalGasLimit, | ||
|
||
EstimatedTime: estimatedTime, | ||
|
||
SubtractFees: amountOption.subtractFees, | ||
} | ||
|
||
err = r.cacluateFees(ctx, path, fetchedFees, processorInputParams.TestsMode, processorInputParams.TestApprovalL1Fee) | ||
usedNoncesMu.Lock() | ||
err = r.evaluateAndUpdatePathDetails(ctx, path, fetchedFees, usedNonces, processorInputParams.TestsMode, processorInputParams.TestApprovalL1Fee) | ||
usedNoncesMu.Unlock() | ||
if err != nil { | ||
appendProcessorErrorFn(pProcessor.Name(), input.SendType, processorInputParams.FromChain.ChainID, processorInputParams.ToChain.ChainID, processorInputParams.AmountIn, err) | ||
continue | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if used internally it could be lower case?
that way it won't even be exported in json
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It cannot cause it's a different package. So either this or to have a function that will set this property, but more or less the same thing.