Skip to content

Commit

Permalink
Merge branch 'tilen/get_feeds' into 'main'
Browse files Browse the repository at this point in the history
Updating get feeds functionality

See merge request flarenetwork/ftso/fast-updates!33
  • Loading branch information
tilenflare committed Dec 17, 2024
2 parents 0ca9daa + 0fdd4d6 commit 832ebe7
Show file tree
Hide file tree
Showing 111 changed files with 2,622 additions and 218 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,7 @@ nul
# other files
go-client/sig.out
go-client/keys.out

# contracts
*.bin
*.abi
4 changes: 0 additions & 4 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,4 +0,0 @@
[submodule "flare-smart-contracts-v2"]
path = flare-smart-contracts-v2
url = [email protected]:flare-foundation/flare-smart-contracts-v2.git
branch = main
1 change: 0 additions & 1 deletion flare-smart-contracts-v2
Submodule flare-smart-contracts-v2 deleted from 70eaa2
16 changes: 12 additions & 4 deletions go-client/Makefile
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
FLARE_SMART_CONTRACTS_V2 = ../flare-smart-contracts-v2
GO_CLIENT_RELATIVE = ../go-client
FLARE_SMART_CONTRACTS_V2 = ../../flare-smart-contracts-v2
GO_CLIENT_RELATIVE = ../fast-updates/go-client
SOLC_OPTIONS = @openzeppelin/=node_modules/@openzeppelin/ flare-smart-contracts/=node_modules/flare-smart-contracts/ --evm-version london --optimize --optimize-runs 200
CONTRACTS_FOLDER = contracts-interface

compile: compile-solc compile-abigen

compile-solc: compile-solc-base compile-solc-mock
compile-solc-base: compile-solc-fast_updater-abi compile-solc-fast_updater-bin compile-solc-fast_updates-configuration-abi compile-solc-fast_updates-configuration-bin compile-solc-fast_update-incentive-abi compile-solc-fast_update-incentive-bin compile-solc-submission-abi
compile-solc-base: compile-solc-fast_updater-abi compile-solc-fast_updater-bin compile-solc-fast_updates-configuration-abi compile-solc-fast_updates-configuration-bin compile-solc-fast_update-incentive-abi compile-solc-fast_update-incentive-bin compile-solc-submission-abi compile-solc-fee_calculator-abi compile-solc-fee_calculator-bin
compile-solc-mock: compile-solc-mock-abi compile-solc-mock-bin

compile-solc-fast_updater-abi:
Expand Down Expand Up @@ -36,8 +36,13 @@ compile-solc-mock-abi:
compile-solc-mock-bin:
cd $(FLARE_SMART_CONTRACTS_V2); solc $(SOLC_OPTIONS) --bin contracts/fastUpdates/mock/FlareSystemMock.sol -o $(GO_CLIENT_RELATIVE)/$(CONTRACTS_FOLDER) --overwrite

compile-solc-fee_calculator-abi:
cd $(FLARE_SMART_CONTRACTS_V2); solc $(SOLC_OPTIONS) --abi contracts/fastUpdates/implementation/FeeCalculator.sol -o $(GO_CLIENT_RELATIVE)/$(CONTRACTS_FOLDER) --overwrite

compile-abigen: compile-abigen-flare_system_manager compile-abigen-flare_mock compile-abigen-voter_registry compile-abigen-fast_updater compile-abigen-fast_update-configuration compile-abigen-fast_update-incentive compile-abigen-submission
compile-solc-fee_calculator-bin:
cd $(FLARE_SMART_CONTRACTS_V2); solc $(SOLC_OPTIONS) --bin contracts/fastUpdates/implementation/FeeCalculator.sol -o $(GO_CLIENT_RELATIVE)/$(CONTRACTS_FOLDER) --overwrite

compile-abigen: compile-abigen-flare_system_manager compile-abigen-flare_mock compile-abigen-voter_registry compile-abigen-fast_updater compile-abigen-fast_update-configuration compile-abigen-fast_update-incentive compile-abigen-submission compile-abigen-fee_calculator

compile-abigen-flare_system_manager:
cd $(CONTRACTS_FOLDER); abigen --abi=IFlareSystemsManager.abi --pkg=system_manager --out=system_manager/FlareSystemManager.go
Expand All @@ -59,3 +64,6 @@ compile-abigen-fast_update-incentive:

compile-abigen-submission:
cd $(CONTRACTS_FOLDER); abigen --abi=Submission.abi --pkg=submission --out=submission/Submission.go

compile-abigen-fee_calculator:
cd $(CONTRACTS_FOLDER); abigen --bin=FeeCalculator.bin --abi=FeeCalculator.abi --pkg=fee_calculator --out=fee_calculator/FeeCalculator.go
12 changes: 8 additions & 4 deletions go-client/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,13 @@ flare_system_manager = "0x919b4b4B561C72c990DC868F751328eF127c45F4"
incentive_manager_address = "0x919b4b4B561C72c990DC868F751328eF127c45F4"
# parameter defining when a fast update can be submitted
submission_window = 10
# url of the off-chain data provider
# url of the off-chain data provider
value_provider_url = "http://127.0.0.1:3101/feed-values/0"
# (optional) when (off-chain) fetching feeds values, an address with sufficient
# balance should be provided, even though the transactions never happen;
# the below address is the default one, which on flare chains has enough balance
fetch_current_feeds_address = "0x000000000000000000000000000000000000dEaD"
fetch_current_feeds_value = "1000000000000000000000000"

[transactions]
gas_limit = 8000000
Expand Down Expand Up @@ -156,6 +161,7 @@ where the key value needs to be replaced by the generated private key and the ad
to be replaced by the actual _entity_ address.

In case you forgot your public key, but saved the private one, you can run the following for outputting the key:

```bash
go run keygen/keygen.go --key 0x1512de600a10a0aac01580dbfc080965b89ed2329a7b2bf538f4c7e09e34aa1
```
Expand Down Expand Up @@ -231,10 +237,8 @@ in the protocol.

The Fast Updates Go client uses an interface to the contracts that was compiled using `solc` compiler and `abigen` tool. In the case
that the contracts are changed, the interface needs to be changed as well. Use the provided
`Makefile` to compile the new interfaces with
`Makefile` to compile the new interfaces. In the makefile set the destination of the `flare-smart-contracts-v2` repo and run

```bash
make compile
```

assuming the submodule repository `flare-smart-contracts-v2` is up to date.
53 changes: 36 additions & 17 deletions go-client/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"math/big"
"time"

"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/ethclient"
Expand All @@ -23,23 +24,26 @@ import (
)

type FastUpdatesClient struct {
params config.FastUpdateClientConfig
chainClient *ethclient.Client
valuesProvider provider.ValuesProvider
signingAccount *Account
transactionAccounts []*Account
fastUpdater *fast_updater.FastUpdater
fastUpdatesConfig *fast_updates_configuration.FastUpdatesConfiguration
submission *submission.Submission
flareSystemMock *mock.Mock
flareSystemManager *system_manager.SystemManager
IncentiveManager *incentive.Incentive
key *sortition.Key
registeredEpochs map[int64]bool
transactionQueue *TransactionQueue
allFeeds []provider.FeedId
loggingParams config.LoggerConfig
Stats UpdatesStats
params config.FastUpdateClientConfig
chainClient *ethclient.Client
valuesProvider provider.ValuesProvider
signingAccount *Account
transactionAccounts []*Account
fastUpdater *fast_updater.FastUpdater
fastUpdaterABI *abi.ABI
fastUpdatesConfig *fast_updates_configuration.FastUpdatesConfiguration
submission *submission.Submission
flareSystemMock *mock.Mock
flareSystemManager *system_manager.SystemManager
IncentiveManager *incentive.Incentive
key *sortition.Key
registeredEpochs map[int64]bool
transactionQueue *TransactionQueue
allFeeds []provider.FeedId
loggingParams config.LoggerConfig
Stats UpdatesStats
FetchCurrentFeedsValue *big.Int
FetchCurrentFeedsAddress common.Address
}

type Account struct {
Expand Down Expand Up @@ -113,6 +117,10 @@ func CreateFastUpdatesClient(cfg *config.Config, valuesProvider provider.ValuesP
return nil, fmt.Errorf("CreateFastUpdatesClient: NewFastUpdater: %w", err)
}
}
fastUpdatesClient.fastUpdaterABI, err = fast_updater.FastUpdaterMetaData.GetAbi()
if err != nil {
return nil, fmt.Errorf("CreateFastUpdatesClient: GetAbi: %w", err)
}

fastUpdatesClient.IncentiveManager, err = incentive.NewIncentive(
common.HexToAddress(cfg.Client.IncentiveManagerAddress), fastUpdatesClient.chainClient,
Expand Down Expand Up @@ -177,6 +185,17 @@ func CreateFastUpdatesClient(cfg *config.Config, valuesProvider provider.ValuesP
if fastUpdatesClient.params.MaxWeight == 0 {
fastUpdatesClient.params.MaxWeight = 4096
}
if cfg.Client.FetchCurrentFeedsAddress != "" {
fastUpdatesClient.FetchCurrentFeedsAddress = common.HexToAddress(cfg.Client.FetchCurrentFeedsAddress)
var check bool
fastUpdatesClient.FetchCurrentFeedsValue, check = new(big.Int).SetString(cfg.Client.FetchCurrentFeedsValue, 10)
if !check {
return nil, fmt.Errorf("CreateFastUpdatesClient: failed reading fetch_current_feeds_value: %s", cfg.Client.FetchCurrentFeedsValue)
}
} else {
fastUpdatesClient.FetchCurrentFeedsAddress = common.HexToAddress("0x000000000000000000000000000000000000dEaD")
fastUpdatesClient.FetchCurrentFeedsValue, _ = new(big.Int).SetString("1000000000000000000000000", 10)
}

return &fastUpdatesClient, nil
}
Expand Down
49 changes: 44 additions & 5 deletions go-client/client/client_requests.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"strings"
"time"

"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
Expand All @@ -20,6 +22,8 @@ import (
"fast-updates-client/updates"
)

const fetchCurrentFeedsMethod = "fetchCurrentFeeds"

func (client *FastUpdatesClient) CurrentBlockNumber() (uint64, error) {
ctx, cancelFunc := context.WithTimeout(context.Background(), time.Duration(config.CallTimeoutMillisDefault)*time.Millisecond)
currentBlockNum, err := client.chainClient.BlockNumber(ctx)
Expand Down Expand Up @@ -97,19 +101,54 @@ func (client *FastUpdatesClient) GetMyWeight() (uint64, error) {
return weight.Uint64(), nil
}

func (client *FastUpdatesClient) GetFeeds(feedIndexes []int) ([]float64, uint64, error) {
ctx, cancelFunc := context.WithTimeout(context.Background(), time.Duration(config.CallTimeoutMillisDefault)*time.Millisecond)
ops := &bind.CallOpts{Context: ctx}
func (client *FastUpdatesClient) callContractFetchCurrentFeeds(ctx context.Context, feedIndexes []*big.Int) (*provider.ValuesDecimals, error) {
fastUpdatesAddress := common.HexToAddress(client.params.FastUpdaterAddress)

input, err := client.fastUpdaterABI.Pack(fetchCurrentFeedsMethod, feedIndexes)
if err != nil {
return nil, err
}

msg := ethereum.CallMsg{
To: &fastUpdatesAddress,
Data: input,
Value: client.FetchCurrentFeedsValue,
From: client.FetchCurrentFeedsAddress,
}
outputBytes, err := client.chainClient.CallContract(ctx, msg, nil)
if err != nil {
return nil, err
}

res, err := client.fastUpdaterABI.Unpack(fetchCurrentFeedsMethod, outputBytes)
if err != nil {
return nil, err
}
if len(res) != 3 {
return nil, fmt.Errorf("ABI unpack error, length of response %d", len(res))
}

return &provider.ValuesDecimals{
Feeds: *abi.ConvertType(res[0], new([]*big.Int)).(*[]*big.Int),
Decimals: *abi.ConvertType(res[1], new([]int8)).(*[]int8),
Timestamp: *abi.ConvertType(res[2], new(uint64)).(*uint64),
}, nil
}

func (client *FastUpdatesClient) GetFeeds(feedIndexes []int) ([]float64, uint64, error) {
feedIndexesBigInt := make([]*big.Int, len(feedIndexes))
for i, index := range feedIndexes {
feedIndexesBigInt[i] = big.NewInt(int64(index))
}

feedValues, err := client.fastUpdater.FetchCurrentFeeds(ops, feedIndexesBigInt)
ctx, cancelFunc := context.WithTimeout(context.Background(), time.Duration(config.CallTimeoutMillisDefault)*time.Millisecond)
feedValues, err := client.callContractFetchCurrentFeeds(ctx, feedIndexesBigInt)
cancelFunc()
if err != nil {
return nil, 0, err
}

floatValues := RawChainValuesToFloats(feedValues)
floatValues := RawChainValuesToFloats(*feedValues)
return floatValues, feedValues.Timestamp, err
}

Expand Down
10 changes: 6 additions & 4 deletions go-client/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,12 @@ func TestClient(t *testing.T) {

// set configuration parameters
cfgClient := config.FastUpdateClientConfig{
SigningPrivateKey: "0xd49743deccbccc5dc7baa8e69e5be03298da8688a15dd202e20f15d5e0e9a9fb",
SortitionPrivateKey: "0xd49743deccbccc5dc7baa8e69e5be03298da8688a15dd202e20f15d5e0e9a9fb",
SubmissionWindow: 4,
MaxWeight: 1024,
SigningPrivateKey: "0xd49743deccbccc5dc7baa8e69e5be03298da8688a15dd202e20f15d5e0e9a9fb",
SortitionPrivateKey: "0xd49743deccbccc5dc7baa8e69e5be03298da8688a15dd202e20f15d5e0e9a9fb",
SubmissionWindow: 4,
MaxWeight: 1024,
FetchCurrentFeedsAddress: "0xeAD9C93b79Ae7C1591b1FB5323BD777E86e150d4",
FetchCurrentFeedsValue: "10000",
}
cfgTransactions := config.TransactionsConfig{
Accounts: []string{"0xd49743deccbccc5dc7baa8e69e5be03298da8688a15dd202e20f15d5e0e9a9fb",
Expand Down
2 changes: 2 additions & 0 deletions go-client/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ type FastUpdateClientConfig struct {
SubmissionWindow int `toml:"submission_window"`
MaxWeight int `toml:"max_weight"`
ValueProviderUrl string `toml:"value_provider_url"`
FetchCurrentFeedsValue string `toml:"fetch_current_feeds_value"`
FetchCurrentFeedsAddress string `toml:"fetch_current_feeds_address"`
}

type TransactionsConfig struct {
Expand Down
1 change: 0 additions & 1 deletion go-client/contracts-interface/AddressUpdatable.abi

This file was deleted.

Empty file.
1 change: 0 additions & 1 deletion go-client/contracts-interface/Bn256.abi

This file was deleted.

1 change: 0 additions & 1 deletion go-client/contracts-interface/Bn256.bin

This file was deleted.

1 change: 0 additions & 1 deletion go-client/contracts-interface/CircularListManager.abi

This file was deleted.

Empty file.
1 change: 0 additions & 1 deletion go-client/contracts-interface/ECDSA.abi

This file was deleted.

1 change: 0 additions & 1 deletion go-client/contracts-interface/ECDSA.bin

This file was deleted.

Loading

0 comments on commit 832ebe7

Please sign in to comment.