Skip to content

Task/sean/str 698 #234

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

Merged
merged 26 commits into from
Jul 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
81fa3b5
who did this
Jul 2, 2023
4b3370d
adding erc20 forward logic
Jul 3, 2023
f5afd88
Fixed post process aborting due to trying to get ERC721 data from ERC…
Jul 3, 2023
65a050f
stashing
Jul 5, 2023
d0d8029
coingecko id lookup using address, enable multiple transactions
Jul 7, 2023
948f4e9
improved error message
Jul 7, 2023
3ec8721
sanitize coingecko checksums
Jul 7, 2023
e63222b
cull bunk address data from coingecko
Jul 7, 2023
4591fb0
added mock cost lookup for String USDc, fixed EVM estimation and exec…
Jul 10, 2023
2c7ef8c
stashing
Jul 11, 2023
4a3a5b6
Fix Coingecko Data Caching
Jul 11, 2023
caf8ca8
Added subnet token proxies quick-fix / hack, fixed a bug where coinge…
Jul 12, 2023
04da2ff
Merge branch 'develop' into task/sean/str-675
auroter Jul 12, 2023
5c0a237
Refactor call to repos.Contract.GetForValidation after resolving merg…
Jul 12, 2023
b853272
fix unit21 error from giving it comma delineated txIds
Jul 13, 2023
7e27be0
Removed TX IDs from approvals from all front-facing stuff (receipt, t…
Jul 13, 2023
d16178e
Don't dereference nil in code which isn't doing anything yet
Jul 13, 2023
abed33a
Cull Approve txIds where needed
Jul 13, 2023
4cebd90
Fixed a bug where Confirming an EVM TX would hang on failure!!!! Fixe…
Jul 13, 2023
cd1dcd6
Fix snake_case_variables
Jul 14, 2023
5bfcb30
Merge branch 'develop' into task/sean/str-695
auroter Jul 14, 2023
c1dfcd5
fix issue from merge
Jul 16, 2023
a11adec
Fix another merge issue
Jul 16, 2023
018b5db
Fixed a bug where max cached gas value was overwritten before returni…
Jul 16, 2023
235ee0e
Created internal Gas Rate calculation method + function. Defer to th…
Jul 18, 2023
d6fa1c2
Merge branch 'develop' into task/sean/str-698
auroter Jul 18, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions pkg/service/cost.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,10 +195,22 @@ func (c cost) EstimateTransaction(p EstimationParams, chain Chain) (estimate mod
// transactionCost is for native token transaction cost (tx_value)
transactionCost := costEth * nativeCost

// Query owlracle for gas
ethGasFee, err := c.lookupGas(chain.OwlracleName)
if err != nil {
return estimate, libcommon.StringError(err)
ethGasFee := 0.0
if chain.OwlracleName == "internal" {
// Use the internal gas rate calculator
// Recommended gas rate with 10% boost and 10% tip respectively
gas, tip, err := GetGasRate(chain, 10, 10)
if err != nil {
return estimate, libcommon.StringError(err)
}
gasWithTip := big.NewInt(0).Add(gas, tip)
ethGasFee = float64(gasWithTip.Int64()) / 1e9
} else {
// Query owlracle for gas
ethGasFee, err = c.lookupGas(chain.OwlracleName)
if err != nil {
return estimate, libcommon.StringError(err)
}
}

// Convert it from gwei to eth to USD and apply buffer
Expand Down
63 changes: 53 additions & 10 deletions pkg/service/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ type Executor interface {
GetEventData(txIds []string, eventSignature string) ([]types.Log, error)
ForwardNonFungibleTokens(txIds []string, recipient string) ([]string, []string, error)
ForwardTokens(txIds []string, recipient string) ([]string, []string, []string, error)
GetGasRate(boostPercent int64, tipPercent int64) (*big.Int, *big.Int, error)
}

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

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

// Get handle to function we wish to call
funcEVM, err := w3.NewFunc(call.CxFunc, call.CxReturn)
Expand All @@ -277,8 +280,8 @@ func (e executor) generateTransactionMessage(call ContractCall, incrementNonce u
return w3types.Message{
From: sender,
To: &to,
GasFeeCap: feeCap,
GasTipCap: tipCap,
GasFeeCap: gas,
GasTipCap: tip,
Value: value,
Input: data,
Nonce: nonce + incrementNonce,
Expand All @@ -300,9 +303,11 @@ func (e executor) generateTransactionRequest(call ContractCall, incrementNonce u
return tx, libcommon.StringError(err)
}

// Get dynamic fee tx gas params
tipCap, _ := e.geth.SuggestGasTipCap(context.Background())
feeCap, _ := e.geth.SuggestGasPrice(context.Background())
// TODO: get boost factors from gas analysis engine
gas, tip, err := e.GetGasRate(10, 10)
if err != nil {
return tx, libcommon.StringError(err)
}

// Type conversion for chainId
chainIdBig := new(big.Int).SetUint64(chainId64)
Expand All @@ -314,8 +319,8 @@ func (e executor) generateTransactionRequest(call ContractCall, incrementNonce u
dynamicFeeTx := types.DynamicFeeTx{
ChainID: chainIdBig,
Nonce: msg.Nonce + incrementNonce,
GasTipCap: tipCap,
GasFeeCap: feeCap,
GasTipCap: tip,
GasFeeCap: gas,
Gas: w3.I(call.TxGasLimit).Uint64(),
To: msg.To,
Value: msg.Value,
Expand Down Expand Up @@ -486,3 +491,41 @@ func (e executor) ForwardTokens(txIds []string, recipient string) ([]string, []s

return forwardTxIds, tokens, quantities, nil
}

func (e executor) GetGasRate(boostPercent int64, tipPercent int64) (*big.Int, *big.Int, error) {
gasPrice, err := e.geth.SuggestGasPrice(context.Background())
if err != nil {
return nil, nil, libcommon.StringError(err)
}

gasBoost := big.NewInt(gasPrice.Int64())
gasBoost.Mul(gasBoost, big.NewInt(boostPercent))
gasBoost.Div(gasBoost, big.NewInt(100))
gasPrice.Add(gasBoost, gasPrice)
tip := big.NewInt(gasPrice.Int64())
tip.Mul(tip, big.NewInt(tipPercent))
tip.Div(tip, big.NewInt(100))

return gasPrice, tip, nil
}

func GetGasRate(chain Chain, boostPercent int64, tipPercent int64) (*big.Int, *big.Int, error) {
geth, err := ethclient.Dial(chain.RPC)
if err != nil {
return nil, nil, libcommon.StringError(err)
}
gasPrice, err := geth.SuggestGasPrice(context.Background())
if err != nil {
return nil, nil, libcommon.StringError(err)
}

gasBoost := big.NewInt(gasPrice.Int64())
gasBoost.Mul(gasBoost, big.NewInt(boostPercent))
gasBoost.Div(gasBoost, big.NewInt(100))
gasPrice.Add(gasBoost, gasPrice)
tip := big.NewInt(gasPrice.Int64())
tip.Mul(tip, big.NewInt(tipPercent))
tip.Div(tip, big.NewInt(100))

return gasPrice, tip, nil
}
17 changes: 17 additions & 0 deletions pkg/service/gas_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package service

import (
"fmt"
"testing"

"github.com/stretchr/testify/assert"
)

func TestGetGasPrices(t *testing.T) {
gas, tip, err := GetGasRate(Chain{RPC: "https://api.avax.network/ext/bc/C/rpc"}, 10, 10)
assert.NoError(t, err)
assert.Greater(t, gas.Int64(), int64(0))
assert.Greater(t, tip.Int64(), int64(0))
fmt.Printf("gas: %d, tip: %d", gas.Int64(), tip.Int64())

}