Skip to content

Commit

Permalink
Merge pull request #180 from mysteriumnetwork/eip-1559
Browse files Browse the repository at this point in the history
EIP-1559 london hardfork support
  • Loading branch information
tomasmik authored Feb 3, 2022
2 parents 22e569d + 7ce28a9 commit b7ac3c4
Show file tree
Hide file tree
Showing 11 changed files with 332 additions and 503 deletions.
457 changes: 185 additions & 272 deletions client/client.go

Large diffs are not rendered by default.

35 changes: 31 additions & 4 deletions transaction/delivery.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,14 @@ type Delivery struct {
UniqueID string
Sender common.Address

Nonce uint64
ChainID int64
Nonce uint64
ChainID int64

GasPrice *big.Int

GasTip *big.Int
BaseFee *big.Int

Type DeliverableType
State DeliveryState

Expand Down Expand Up @@ -73,13 +77,22 @@ type SignFunc func(common.Address, *types.Transaction) (*types.Transaction, erro

// ToWriteRequest will convert a Delivery to a typical write request used by `client` package.
func (t *Delivery) ToWriteRequest(signer SignFunc, gasLimit uint64) client.WriteRequest {
return client.WriteRequest{
wr := client.WriteRequest{
Identity: t.Sender,
Signer: bind.SignerFn(signer),
GasLimit: gasLimit,
GasPrice: t.GasPrice,
Nonce: new(big.Int).SetUint64(t.Nonce),
}

// Support pre EIP-1559 transactions.
if t.GasPrice != nil && t.GasPrice.Cmp(big.NewInt(0)) > 0 {
wr.GasPrice = t.GasPrice
return wr
}

wr.GasTip = t.GasTip
wr.BaseFee = t.BaseFee
return wr
}

func (t *Delivery) GetLastTransaction() (*types.Transaction, error) {
Expand All @@ -91,6 +104,18 @@ func (t *Delivery) GetLastTransaction() (*types.Transaction, error) {
return tx, tx.UnmarshalJSON(t.SentTransaction)
}

func (d *Delivery) applyFees(f *fees) *Delivery {
dd := *d
if d.GasPrice != nil && d.GasPrice.Cmp(big.NewInt(0)) > 0 {
dd.GasPrice = new(big.Int).Add(f.Base, f.Tip)
} else {
dd.GasTip = f.Tip
dd.BaseFee = f.Base
}

return &dd
}

type DeliveryRequest struct {
ChainID int64
Sender common.Address
Expand Down Expand Up @@ -120,6 +145,8 @@ func (t *DeliveryRequest) toDelivery(nonce uint64) (Delivery, error) {
Nonce: nonce,
ChainID: t.ChainID,
GasPrice: new(big.Int).SetInt64(0),
GasTip: new(big.Int).SetInt64(0),
BaseFee: new(big.Int).SetInt64(0),

Type: t.Type,
State: DeliveryStateWaiting,
Expand Down
8 changes: 4 additions & 4 deletions transaction/depot.go
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ func (d *Depot) sendOutTransaction(td Delivery) (Delivery, error) {
}

func (d *Depot) calculateNewGasPrice(td Delivery) (Delivery, error) {
newPrice := big.NewInt(0)
newPrice := &fees{}
switch td.State {
case DeliveryStatePacking, DeliveryStateWaiting:
gasPrice, err := d.gasStation.ReceiveInitialGas(td.ChainID)
Expand Down Expand Up @@ -365,7 +365,7 @@ func (d *Depot) calculateNewGasPrice(td Delivery) (Delivery, error) {
return Delivery{}, fmt.Errorf("impossible to handle state: %v", td.State)
}

if newPrice.Cmp(big.NewInt(0)) == 0 {
if newPrice == nil && newPrice.Tip.Cmp(big.NewInt(0)) == 0 {
return Delivery{}, errors.New("ended up with 0 gas price, cannot continue")
}

Expand All @@ -376,8 +376,8 @@ func (d *Depot) calculateNewGasPrice(td Delivery) (Delivery, error) {
return newDelivery, nil
}

func (d *Depot) deliveryUpdateGasPrice(td Delivery, newGas *big.Int) (Delivery, error) {
td.GasPrice = newGas
func (d *Depot) deliveryUpdateGasPrice(td Delivery, newGas *fees) (Delivery, error) {
td = *td.applyFees(newGas)
td.UpdateUTC = time.Now().UTC()
if err := d.storage.UpsertDeliveryRequest(td); err != nil {
return td, fmt.Errorf("failed to update delivery gas price: %w", err)
Expand Down
171 changes: 0 additions & 171 deletions transaction/gas/eth.go

This file was deleted.

15 changes: 11 additions & 4 deletions transaction/gas/etherscan.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,16 @@ func (esa *EtherscanStation) GetGasPrices() (*GasPrices, error) {
if err != nil {
return nil, err
}
base, err := strconv.ParseFloat(res.Result.SuggestBaseFee, 64)
if err != nil {
return nil, err
}
prices := GasPrices{
SafeLow: esa.result(safeLow),
Average: esa.result(average),
Fast: esa.result(fast),

BaseFee: units.FloatGweiToBigIntWei(base),
}
return &prices, nil
}
Expand Down Expand Up @@ -117,10 +123,11 @@ type etherscanGasPriceResponseFail struct {

// gasPriceResult the gas prices for the last block.
type gasPriceResult struct {
LastBlock string `json:"LastBlock"`
SafeGasPrice string `json:"SafeGasPrice"`
ProposeGasPrice string `json:"ProposeGasPrice"`
LastBlock string `json:"LastBlock"`
GasUsedRatio string `json:"gasUsedRatio"`

FastGasPrice string `json:"FastGasPrice"`
ProposeGasPrice string `json:"ProposeGasPrice"`
SafeGasPrice string `json:"SafeGasPrice"`
SuggestBaseFee string `json:"suggestBaseFee"`
GasUsedRatio string `json:"gasUsedRatio"`
}
6 changes: 3 additions & 3 deletions transaction/gas/multichain_station_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func TestMultichain(t *testing.T) {
t.Run("green path", func(t *testing.T) {
defaultPrice := big.NewInt(10)
mq := MultichainStation{
1: []Station{NewStaticStation(defaultPrice), NewStaticStation(big.NewInt(600))},
1: []Station{NewStaticStation(defaultPrice, big.NewInt(1)), NewStaticStation(big.NewInt(600), big.NewInt(1))},
}

prices, err := mq.GetGasPrices(1)
Expand All @@ -25,7 +25,7 @@ func TestMultichain(t *testing.T) {
t.Run("fallback", func(t *testing.T) {
defaultPrice := big.NewInt(10)
mq := MultichainStation{
1: []Station{NewFailingStationMock(), NewStaticStation(defaultPrice)},
1: []Station{NewFailingStationMock(), NewStaticStation(defaultPrice, big.NewInt(1))},
}

prices, err := mq.GetGasPrices(1)
Expand All @@ -37,7 +37,7 @@ func TestMultichain(t *testing.T) {
t.Run("chain does not exist", func(t *testing.T) {
defaultPrice := big.NewInt(10)
mq := MultichainStation{
1: []Station{NewFailingStationMock(), NewStaticStation(defaultPrice)},
1: []Station{NewFailingStationMock(), NewStaticStation(defaultPrice, big.NewInt(1))},
}
prices, err := mq.GetGasPrices(2)

Expand Down
31 changes: 21 additions & 10 deletions transaction/gas/matic.go → transaction/gas/polygon_official.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (

// DefaultMaticStationURI is the default gas station URL that can be used in matic gas station.
// Default URL is for mainnet of matic gas station service.
const DefaultMaticStationURI = "https://gasstation-mainnet.matic.network"
const DefaultMaticStationURI = "https://gasstation-mainnet.matic.network/v2"

// MaticStation represents matic gas station api.
type MaticStation struct {
Expand All @@ -22,12 +22,21 @@ type MaticStation struct {
}

type maticGasPriceResp struct {
SafeLow float64 `json:"safeLow"`
Standard float64 `json:"standard"`
Fast float64 `json:"fast"`
Fastest float64 `json:"fastest"`
BlockTime int `json:"blockTime"`
BlockNumber int `json:"blockNumber"`
BlockNumber int64 `json:"blockNumber"`
BlockTime int64 `json:"blockTime"`
EstimatedBaseFee float64 `json:"estimatedBaseFee"`
Fast struct {
MaxFee float64 `json:"maxFee"`
MaxPriorityFee float64 `json:"maxPriorityFee"`
} `json:"fast"`
SafeLow struct {
MaxFee float64 `json:"maxFee"`
MaxPriorityFee float64 `json:"maxPriorityFee"`
} `json:"safeLow"`
Standard struct {
MaxFee float64 `json:"maxFee"`
MaxPriorityFee float64 `json:"maxPriorityFee"`
} `json:"standard"`
}

// NewMaticStation returns a new instance of matic gas station which can be used for gas price checks.
Expand All @@ -47,9 +56,11 @@ func (m *MaticStation) GetGasPrices() (*GasPrices, error) {
return nil, err
}
prices := GasPrices{
SafeLow: m.result(resp.SafeLow),
Average: m.result(resp.Standard),
Fast: m.result(resp.Fast),
SafeLow: m.result(resp.SafeLow.MaxPriorityFee),
Average: m.result(resp.Standard.MaxPriorityFee),
Fast: m.result(resp.Fast.MaxPriorityFee),

BaseFee: units.FloatGweiToBigIntWei(resp.EstimatedBaseFee),
}
return &prices, nil
}
Expand Down
Loading

0 comments on commit b7ac3c4

Please sign in to comment.