forked from ethereum-optimism/op-geth
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Backend wrapper for Celo functionality
- Loading branch information
Showing
12 changed files
with
228 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
package celoapi | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"math/big" | ||
|
||
"github.com/ethereum/go-ethereum/common" | ||
"github.com/ethereum/go-ethereum/common/exchange" | ||
"github.com/ethereum/go-ethereum/common/lru" | ||
"github.com/ethereum/go-ethereum/contracts" | ||
"github.com/ethereum/go-ethereum/internal/ethapi" | ||
"github.com/ethereum/go-ethereum/rpc" | ||
) | ||
|
||
func NewCeloAPIBackend(b ethapi.Backend) *CeloAPIBackend { | ||
return &CeloAPIBackend{ | ||
Backend: b, | ||
exchangeRatesCache: lru.NewCache[common.Hash, common.ExchangeRates](128), | ||
} | ||
} | ||
|
||
// CeloAPIBackend is a wrapper for the ethapi.Backend, that provides additional Celo specific | ||
// functionality. CeloAPIBackend is mainly passed to the JSON RPC services and provides | ||
// an easy way to make extra functionality available in the service internal methods without | ||
// having to change their call signature significantly. | ||
// CeloAPIBackend keeps a threadsafe LRU cache of block-hash to exchange rates for that block. | ||
// Cache invalidation is only a problem when an already existing blocks' hash | ||
// doesn't change, but the rates change. That shouldn't be possible, since changing the rates | ||
// requires different transaction hashes / state and thus a different block hash. | ||
// If the previous rates change during a reorg, the previous block hash should also change | ||
// and with it the new block's hash. | ||
// Stale branches cache values will get evicted eventually. | ||
type CeloAPIBackend struct { | ||
ethapi.Backend | ||
|
||
exchangeRatesCache *lru.Cache[common.Hash, common.ExchangeRates] | ||
} | ||
|
||
func (b *CeloAPIBackend) getContractCaller(ctx context.Context, atBlock common.Hash) (*contracts.CeloBackend, error) { | ||
state, _, err := b.Backend.StateAndHeaderByNumberOrHash( | ||
ctx, | ||
rpc.BlockNumberOrHashWithHash(atBlock, false), | ||
) | ||
if err != nil { | ||
return nil, fmt.Errorf("retrieve state for block hash %s: %w", atBlock.String(), err) | ||
} | ||
return &contracts.CeloBackend{ | ||
ChainConfig: b.Backend.ChainConfig(), | ||
State: state, | ||
}, nil | ||
} | ||
|
||
func (b *CeloAPIBackend) GetFeeBalance(ctx context.Context, atBlock common.Hash, account common.Address, feeCurrency *common.Address) (*big.Int, error) { | ||
cb, err := b.getContractCaller(ctx, atBlock) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return contracts.GetFeeBalance(cb, account, feeCurrency), nil | ||
} | ||
|
||
func (b *CeloAPIBackend) GetExchangeRates(ctx context.Context, atBlock common.Hash) (common.ExchangeRates, error) { | ||
cachedRates, ok := b.exchangeRatesCache.Get(atBlock) | ||
if ok { | ||
return cachedRates, nil | ||
} | ||
cb, err := b.getContractCaller(ctx, atBlock) | ||
if err != nil { | ||
return nil, err | ||
} | ||
er, err := contracts.GetExchangeRates(cb) | ||
if err != nil { | ||
return nil, fmt.Errorf("retrieve exchange rates from current state: %w", err) | ||
} | ||
b.exchangeRatesCache.Add(atBlock, er) | ||
return er, nil | ||
} | ||
|
||
func (b *CeloAPIBackend) ConvertToCurrency(ctx context.Context, atBlock common.Hash, value *big.Int, fromFeeCurrency *common.Address) (*big.Int, error) { | ||
er, err := b.GetExchangeRates(ctx, atBlock) | ||
if err != nil { | ||
return nil, err | ||
} | ||
currencyValue, err := exchange.ConvertGoldToCurrency(er, fromFeeCurrency, value) | ||
if err != nil { | ||
return nil, fmt.Errorf("could not convert to native from fee currency (%s): %w ", fromFeeCurrency, err) | ||
} | ||
return currencyValue, nil | ||
} | ||
|
||
func (b *CeloAPIBackend) ConvertToGold(ctx context.Context, atBlock common.Hash, value *big.Int, toFeeCurrency *common.Address) (*big.Int, error) { | ||
er, err := b.GetExchangeRates(ctx, atBlock) | ||
if err != nil { | ||
return nil, err | ||
} | ||
goldValue, err := exchange.ConvertCurrencyToGold(er, value, toFeeCurrency) | ||
if err != nil { | ||
return nil, fmt.Errorf("could not convert from native to fee currency (%s): %w ", toFeeCurrency, err) | ||
} | ||
return goldValue, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.