Skip to content
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

1185 add generalized dynamic abi types generator #1209

Open
wants to merge 18 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 9 commits
Commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
pragma solidity >=0.8.25 <0.9.0;

/**
* @title PricePredictor
* @title PricePredictorTypes
* @notice Solidity contract that declares the structs for input/output.
*/
contract PricePredictor {
contract PricePredictorTypes {
struct InputData {
uint256 date;
string[] tokens;
Expand Down
247 changes: 247 additions & 0 deletions prophet/handlers/pricepred/generated/pricepred_bindings.go

Large diffs are not rendered by default.

18 changes: 10 additions & 8 deletions prophet/handlers/pricepred/pricepred.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"net/http"
"net/url"
"time"

"github.com/warden-protocol/wardenprotocol/prophet/handlers/pricepred/generated"
)

// PricePredictorSolidity is a handler for the price prediction AI model,
Expand Down Expand Up @@ -109,12 +111,12 @@ func (s PricePredictorSolidity) Execute(ctx context.Context, input []byte) ([]by
return encodedRes, nil
}

func decodeInput(inputData []byte) (PricePredictorInputData, error) {
func decodeInput(inputData []byte) (generated.PricePredictorTypesInputData, error) {
var in struct {
InputData PricePredictorInputData
InputData generated.PricePredictorTypesInputData
}

abi, err := PricePredictorMetaData.GetAbi()
abi, err := generated.PricePredictorTypesMetaData.GetAbi()
if err != nil {
return in.InputData, fmt.Errorf("failed to get ABI: %w", err)
}
Expand All @@ -140,8 +142,8 @@ func decodeInput(inputData []byte) (PricePredictorInputData, error) {
return in.InputData, nil
}

func encodeOutput(outputData PricePredictorOutputData) ([]byte, error) {
abi, err := PricePredictorMetaData.GetAbi()
func encodeOutput(outputData generated.PricePredictorTypesOutputData) ([]byte, error) {
abi, err := generated.PricePredictorTypesMetaData.GetAbi()
if err != nil {
return nil, fmt.Errorf("failed to get ABI: %w", err)
}
Expand All @@ -159,7 +161,7 @@ func encodeOutput(outputData PricePredictorOutputData) ([]byte, error) {
return packed, nil
}

func buildOutputData(inputData PricePredictorInputData, req PredictRequest, res PredictResponse, backtestingRes *BacktestingResponse) (PricePredictorOutputData, error) {
func buildOutputData(inputData generated.PricePredictorTypesInputData, req PredictRequest, res PredictResponse, backtestingRes *BacktestingResponse) (generated.PricePredictorTypesOutputData, error) {
decimals := big.NewFloat(1e16)

tokenPreds := make([]*big.Int, len(req.SolverInput.Tokens))
Expand Down Expand Up @@ -227,13 +229,13 @@ func buildOutputData(inputData PricePredictorInputData, req PredictRequest, res
case P100:
metrics[i][j] = float64ToBigInt(tokenMetrics.Metrics.P100, decimals)
default:
return PricePredictorOutputData{}, fmt.Errorf("invalid requested metric: %d", m)
return generated.PricePredictorTypesOutputData{}, fmt.Errorf("invalid requested metric: %d", m)
}
}
}
}

return PricePredictorOutputData{
return generated.PricePredictorTypesOutputData{
Predictions: tokenPreds,
Metrics: metrics,
}, nil
Expand Down
247 changes: 0 additions & 247 deletions prophet/handlers/pricepred/pricepred_bindings.go

This file was deleted.

15 changes: 8 additions & 7 deletions prophet/handlers/pricepred/pricepred_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,19 @@ import (
"testing"

"github.com/stretchr/testify/require"
"github.com/warden-protocol/wardenprotocol/prophet/handlers/pricepred/generated"
)

func TestDecodeInput(t *testing.T) {
cases := []struct {
name string
input string
expected PricePredictorInputData
expected generated.PricePredictorTypesInputData
}{
{
name: "single element list",
input: "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAZ5unngAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAeAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABkAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAMAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAYAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAOAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAB2JpdGNvaW4AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAZ0ZXRoZXIAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAHdW5pc3dhcAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABY=",
expected: PricePredictorInputData{
expected: generated.PricePredictorTypesInputData{
Date: big.NewInt(1738254238),
Tokens: []string{"bitcoin", "tether", "uniswap"},
Metrics: []*big.Int{big.NewInt(0), big.NewInt(22)},
Expand Down Expand Up @@ -46,15 +47,15 @@ func TestDecodeInput(t *testing.T) {
func TestBuildOutput(t *testing.T) {
cases := []struct {
name string
inputData PricePredictorInputData
inputData generated.PricePredictorTypesInputData
req PredictRequest
res PredictResponse
backtestingRes *BacktestingResponse
expected PricePredictorOutputData
expected generated.PricePredictorTypesOutputData
}{
{
name: "with backtesting",
inputData: PricePredictorInputData{
inputData: generated.PricePredictorTypesInputData{
Metrics: []*big.Int{
big.NewInt(int64(Count)),
big.NewInt(int64(P75)),
Expand Down Expand Up @@ -99,7 +100,7 @@ func TestBuildOutput(t *testing.T) {
},
},
},
expected: PricePredictorOutputData{
expected: generated.PricePredictorTypesOutputData{
Predictions: []*big.Int{
float64ToBigInt(17.435131034851075, big.NewFloat(1e16)),
float64ToBigInt(1.000115715622902, big.NewFloat(1e16)),
Expand Down Expand Up @@ -133,7 +134,7 @@ func TestAllMetricNamesCovered(t *testing.T) {
bmType := reflect.TypeOf(BacktestingMetrics{})
n := bmType.NumField()
for i := range n {
inputData := PricePredictorInputData{Metrics: []*big.Int{big.NewInt(int64(i))}}
inputData := generated.PricePredictorTypesInputData{Metrics: []*big.Int{big.NewInt(int64(i))}}

req := PredictRequest{
SolverInput: RequestSolverInput{
Expand Down
24 changes: 24 additions & 0 deletions prophet/handlers/stoicquote/generated/StoicQuoteTypes.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// SPDX-License-Identifier: GPL-3.0
// Code generated - DO NOT EDIT.
// This file is a generated and any manual changes will be lost.
pragma solidity >=0.8.25 <0.9.0;

/**
* @title StoicQuoteTypes
* @notice Solidity contract that declares structs derived from JSON.
*/
contract StoicQuoteTypes {

struct Data {
string author;
string quote;
}

struct StoicQuote {
Data data;
}

function useAllTypes(StoicQuote memory _stoicquote) external {
// This function doesn't do anything but ensures the top-level struct is referenced.
}
}
234 changes: 234 additions & 0 deletions prophet/handlers/stoicquote/generated/stoicquote_bindings.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading