Skip to content

Commit 3b7ed9e

Browse files
authored
Task/sean/str 698 (#234)
* who did this * adding erc20 forward logic * Fixed post process aborting due to trying to get ERC721 data from ERC20 transfer events * stashing * coingecko id lookup using address, enable multiple transactions * improved error message * sanitize coingecko checksums * cull bunk address data from coingecko * added mock cost lookup for String USDc, fixed EVM estimation and execution of multiple calls, added calls for NFT and Token Forwarding (not complete yet) * stashing * Fix Coingecko Data Caching * Added subnet token proxies quick-fix / hack, fixed a bug where coingecko always appeared to be down, fixed an issue with updating the timestamp of a cached token value even when failing to retrieve a new value * Refactor call to repos.Contract.GetForValidation after resolving merge conflicts * fix unit21 error from giving it comma delineated txIds * Removed TX IDs from approvals from all front-facing stuff (receipt, transact return body) and unit21 * Don't dereference nil in code which isn't doing anything yet * Cull Approve txIds where needed * Fixed a bug where Confirming an EVM TX would hang on failure!!!! Fixed a bug where finding 0 tokens to forward for ERC721 or ERC20 reported an error, fixed a bug where forwards failed because they were using safeTransferFrom without an approve, added true max gas caching for each transaction request, fixed a bug where forwarding would be attempted when there was nothing to forward, added true gas summing including forwarding * Fix snake_case_variables * fix issue from merge * Fix another merge issue * Fixed a bug where max cached gas value was overwritten before returning quote * Created internal Gas Rate calculation method + function. Defer to this during Cost estimation if network oracle name is set to "internal" in the db. Also use this value when generating the real evm transaction request.
1 parent 9d7e465 commit 3b7ed9e

3 files changed

Lines changed: 86 additions & 14 deletions

File tree

pkg/service/cost.go

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -195,10 +195,22 @@ func (c cost) EstimateTransaction(p EstimationParams, chain Chain) (estimate mod
195195
// transactionCost is for native token transaction cost (tx_value)
196196
transactionCost := costEth * nativeCost
197197

198-
// Query owlracle for gas
199-
ethGasFee, err := c.lookupGas(chain.OwlracleName)
200-
if err != nil {
201-
return estimate, libcommon.StringError(err)
198+
ethGasFee := 0.0
199+
if chain.OwlracleName == "internal" {
200+
// Use the internal gas rate calculator
201+
// Recommended gas rate with 10% boost and 10% tip respectively
202+
gas, tip, err := GetGasRate(chain, 10, 10)
203+
if err != nil {
204+
return estimate, libcommon.StringError(err)
205+
}
206+
gasWithTip := big.NewInt(0).Add(gas, tip)
207+
ethGasFee = float64(gasWithTip.Int64()) / 1e9
208+
} else {
209+
// Query owlracle for gas
210+
ethGasFee, err = c.lookupGas(chain.OwlracleName)
211+
if err != nil {
212+
return estimate, libcommon.StringError(err)
213+
}
202214
}
203215

204216
// Convert it from gwei to eth to USD and apply buffer

pkg/service/executor.go

Lines changed: 53 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ type Executor interface {
4848
GetEventData(txIds []string, eventSignature string) ([]types.Log, error)
4949
ForwardNonFungibleTokens(txIds []string, recipient string) ([]string, []string, error)
5050
ForwardTokens(txIds []string, recipient string) ([]string, []string, []string, error)
51+
GetGasRate(boostPercent int64, tipPercent int64) (*big.Int, *big.Int, error)
5152
}
5253

5354
type executor struct {
@@ -257,9 +258,11 @@ func (e executor) generateTransactionMessage(call ContractCall, incrementNonce u
257258
return w3types.Message{}, libcommon.StringError(err)
258259
}
259260

260-
// Get dynamic fee tx gas params
261-
tipCap, _ := e.geth.SuggestGasTipCap(context.Background())
262-
feeCap, _ := e.geth.SuggestGasPrice(context.Background())
261+
// TODO: get boost factors from gas analysis engine
262+
gas, tip, err := e.GetGasRate(10, 10)
263+
if err != nil {
264+
return w3types.Message{}, libcommon.StringError(err)
265+
}
263266

264267
// Get handle to function we wish to call
265268
funcEVM, err := w3.NewFunc(call.CxFunc, call.CxReturn)
@@ -277,8 +280,8 @@ func (e executor) generateTransactionMessage(call ContractCall, incrementNonce u
277280
return w3types.Message{
278281
From: sender,
279282
To: &to,
280-
GasFeeCap: feeCap,
281-
GasTipCap: tipCap,
283+
GasFeeCap: gas,
284+
GasTipCap: tip,
282285
Value: value,
283286
Input: data,
284287
Nonce: nonce + incrementNonce,
@@ -300,9 +303,11 @@ func (e executor) generateTransactionRequest(call ContractCall, incrementNonce u
300303
return tx, libcommon.StringError(err)
301304
}
302305

303-
// Get dynamic fee tx gas params
304-
tipCap, _ := e.geth.SuggestGasTipCap(context.Background())
305-
feeCap, _ := e.geth.SuggestGasPrice(context.Background())
306+
// TODO: get boost factors from gas analysis engine
307+
gas, tip, err := e.GetGasRate(10, 10)
308+
if err != nil {
309+
return tx, libcommon.StringError(err)
310+
}
306311

307312
// Type conversion for chainId
308313
chainIdBig := new(big.Int).SetUint64(chainId64)
@@ -314,8 +319,8 @@ func (e executor) generateTransactionRequest(call ContractCall, incrementNonce u
314319
dynamicFeeTx := types.DynamicFeeTx{
315320
ChainID: chainIdBig,
316321
Nonce: msg.Nonce + incrementNonce,
317-
GasTipCap: tipCap,
318-
GasFeeCap: feeCap,
322+
GasTipCap: tip,
323+
GasFeeCap: gas,
319324
Gas: w3.I(call.TxGasLimit).Uint64(),
320325
To: msg.To,
321326
Value: msg.Value,
@@ -486,3 +491,41 @@ func (e executor) ForwardTokens(txIds []string, recipient string) ([]string, []s
486491

487492
return forwardTxIds, tokens, quantities, nil
488493
}
494+
495+
func (e executor) GetGasRate(boostPercent int64, tipPercent int64) (*big.Int, *big.Int, error) {
496+
gasPrice, err := e.geth.SuggestGasPrice(context.Background())
497+
if err != nil {
498+
return nil, nil, libcommon.StringError(err)
499+
}
500+
501+
gasBoost := big.NewInt(gasPrice.Int64())
502+
gasBoost.Mul(gasBoost, big.NewInt(boostPercent))
503+
gasBoost.Div(gasBoost, big.NewInt(100))
504+
gasPrice.Add(gasBoost, gasPrice)
505+
tip := big.NewInt(gasPrice.Int64())
506+
tip.Mul(tip, big.NewInt(tipPercent))
507+
tip.Div(tip, big.NewInt(100))
508+
509+
return gasPrice, tip, nil
510+
}
511+
512+
func GetGasRate(chain Chain, boostPercent int64, tipPercent int64) (*big.Int, *big.Int, error) {
513+
geth, err := ethclient.Dial(chain.RPC)
514+
if err != nil {
515+
return nil, nil, libcommon.StringError(err)
516+
}
517+
gasPrice, err := geth.SuggestGasPrice(context.Background())
518+
if err != nil {
519+
return nil, nil, libcommon.StringError(err)
520+
}
521+
522+
gasBoost := big.NewInt(gasPrice.Int64())
523+
gasBoost.Mul(gasBoost, big.NewInt(boostPercent))
524+
gasBoost.Div(gasBoost, big.NewInt(100))
525+
gasPrice.Add(gasBoost, gasPrice)
526+
tip := big.NewInt(gasPrice.Int64())
527+
tip.Mul(tip, big.NewInt(tipPercent))
528+
tip.Div(tip, big.NewInt(100))
529+
530+
return gasPrice, tip, nil
531+
}

pkg/service/gas_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package service
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
func TestGetGasPrices(t *testing.T) {
11+
gas, tip, err := GetGasRate(Chain{RPC: "https://api.avax.network/ext/bc/C/rpc"}, 10, 10)
12+
assert.NoError(t, err)
13+
assert.Greater(t, gas.Int64(), int64(0))
14+
assert.Greater(t, tip.Int64(), int64(0))
15+
fmt.Printf("gas: %d, tip: %d", gas.Int64(), tip.Int64())
16+
17+
}

0 commit comments

Comments
 (0)