Skip to content

Commit

Permalink
Comments and validation
Browse files Browse the repository at this point in the history
  • Loading branch information
cbrit committed Jul 4, 2024
1 parent c9a75ff commit e52b91a
Show file tree
Hide file tree
Showing 11 changed files with 57 additions and 6 deletions.
2 changes: 2 additions & 0 deletions go/builder/adaptors/aave_v2_a_token.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"github.com/peggyjv/steward/steward_proto_go/steward_proto"
)

// AaveV2ATokenAdaptorV2CallBuilder is a builder for AaveV2ATokenAdaptorV2 calls.
// Contract: https://github.com/PeggyJV/cellar-contracts/blob/main/src/modules/adaptors/Aave/AaveATokenAdaptor.sol
type AaveV2ATokenAdaptorV2CallBuilder struct {
adaptor common.Address
calls []*steward_proto.AaveATokenAdaptorV2
Expand Down
2 changes: 2 additions & 0 deletions go/builder/adaptors/aave_v2_enable_asset_as_collateral.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"github.com/peggyjv/steward/steward_proto_go/steward_proto"
)

// AaveV2EnableAssetAsCollateralAdaptorV1CallBuilder builds calls for the AaveV2EnableAssetAsCollateralAdaptorV1 adaptor
// Contract: https://github.com/PeggyJV/cellar-contracts/blob/main/src/modules/adaptors/Aave/AaveV2EnableAssetAsCollateralAdaptor.sol
type AaveV2EnableAssetAsCollateralAdaptorV1CallBuilder struct {
adaptor common.Address
calls []*steward_proto.AaveV2EnableAssetAsCollateralAdaptorV1
Expand Down
2 changes: 2 additions & 0 deletions go/builder/adaptors/aave_v3_a_token.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"github.com/peggyjv/steward/steward_proto_go/steward_proto"
)

// Builder for AaveV3ATokenAdaptorV1 adaptor
// Contract: https://github.com/PeggyJV/cellar-contracts/blob/main/src/modules/adaptors/Aave/V3/AaveV3ATokenAdaptor.sol
type AaveV3ATokenAdaptorV1CallBuilder struct {
adaptor common.Address
calls []*steward_proto.AaveV3ATokenAdaptorV1
Expand Down
2 changes: 2 additions & 0 deletions go/builder/adaptors/aave_v3_debt_token.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"github.com/peggyjv/steward/steward_proto_go/steward_proto"
)

// AaveV3DebtTokenAdaptorV1CallBuilder is a builder for AaveV3DebtTokenAdaptorV1 calls
// Contract: https://github.com/PeggyJV/cellar-contracts/blob/main/src/modules/adaptors/Aave/V3/AaveV3DebtTokenAdaptor.sol
type AaveV3DebtTokenAdaptorV1CallBuilder struct {
adaptor common.Address
calls []*steward_proto.AaveV3DebtTokenAdaptorV1
Expand Down
3 changes: 2 additions & 1 deletion go/builder/adaptors/aave_v3_debt_token_flash_loan.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import (
"github.com/peggyjv/steward/steward_proto_go/steward_proto"
)

// Builder
// Builder for using flash loans with the AaveV3DebtTokenAdaptorV1 adaptor
// Contract: https://github.com/PeggyJV/cellar-contracts/blob/main/src/modules/adaptors/Aave/V3/AaveV3DebtTokenAdaptor.sol
type AaveV3DebtTokenFlashLoanAdaptorV1CallBuilder struct {
adaptor common.Address
calls []*steward_proto.AaveV3DebtTokenAdaptorV1FlashLoan
Expand Down
2 changes: 2 additions & 0 deletions go/builder/adaptors/balancer_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ const (
BalancerSwapKindGivenOut
)

// BalancerPoolAdaptorV1CallBuilder is a builder for BalancerPoolAdaptorV1 calls
// Contract: https://github.com/PeggyJV/cellar-contracts/blob/main/src/modules/adaptors/Balancer/BalancerPoolAdaptor.sol
type BalancerPoolAdaptorV1CallBuilder struct {
adaptor common.Address
calls []*steward_proto.BalancerPoolAdaptorV1
Expand Down
3 changes: 2 additions & 1 deletion go/builder/adaptors/balancer_pool_flash_loan.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import (
"github.com/peggyjv/steward/steward_proto_go/steward_proto"
)

// Builder
// BalancerPoolAdaptorV1CallBuilder is a builder for using flash loans with the BalancerPoolAdaptorV1 adaptor.
// Contract: https://github.com/PeggyJV/cellar-contracts/blob/main/src/modules/adaptors/Balancer/BalancerPoolAdaptor.sol
type BalancerPoolFlashLoanAdaptorV1CallBuilder struct {
adaptor common.Address
calls []*steward_proto.BalancerPoolAdaptorV1FlashLoan
Expand Down
3 changes: 2 additions & 1 deletion go/builder/adaptors/pendle.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import (
"github.com/peggyjv/steward/steward_proto_go/steward_proto"
)

// Builder
// PendleAdaptorCallBuilder is a builder for PendleAdaptorV1 calls
// Contract: https://github.com/PeggyJV/cellar-contracts/blob/main/src/modules/adaptors/Pendle/PendleAdaptor.sol
type PendleAdaptorCallBuilder struct {
adaptor common.Address
calls []*steward_proto.PendleAdaptorV1
Expand Down
4 changes: 2 additions & 2 deletions go/builder/adaptors/swap_with_uniswap.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import (
"github.com/peggyjv/steward/steward_proto_go/steward_proto"
)

// Builder for SwapWithUniswapAdaptorV1
// SwapWithUniswapAdaptorV1CallBuilder is a builder for SwapWithUniswapAdaptorV1 calls
// Contract: https://github.com/PeggyJV/cellar-contracts/blob/main/src/modules/adaptors/Uniswap/SwapWithUniswapAdaptor.sol
type SwapWithUniswapAdaptorV1CallBuilder struct {
adaptor common.Address
calls []*steward_proto.SwapWithUniswapAdaptorV1
Expand All @@ -21,7 +22,6 @@ func NewSwapWithUniswapAdaptorV1CallBuilder(adaptor common.Address) *SwapWithUni
}
}

// Build builds the AdaptorCall
func (b *SwapWithUniswapAdaptorV1CallBuilder) Build() *steward_proto.AdaptorCall {
return &steward_proto.AdaptorCall{
Adaptor: b.adaptor.Hex(),
Expand Down
3 changes: 3 additions & 0 deletions go/builder/cellar_call.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,19 @@ import (
"github.com/peggyjv/steward/steward_proto_go/steward_proto"
)

// CellarCallBuilder is the builder for cellar call data. It can be used to string together multiple function calls to one Cellar. Function calls will execute in the order they are added to the builder.
type CellarCallBuilder struct {
functionCalls []*steward_proto.CellarV2_5_FunctionCall
}

// NewCallDataBuilder creates a new CallDataBuilder
func NewCallDataBuilder() *CellarCallBuilder {
return &CellarCallBuilder{
functionCalls: make([]*steward_proto.CellarV2_5_FunctionCall, 0),
}
}

// Build returns the CallData struct with the function calls added to the builder
func (cdb *CellarCallBuilder) Build() (*steward_proto.CellarV2_5, error) {
if len(cdb.functionCalls) == 0 {
return nil, fmt.Errorf("no function calls added to CallDataBuilder")
Expand Down
37 changes: 36 additions & 1 deletion go/builder/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/peggyjv/steward/steward_proto_go/steward_proto"
)

// ScheduleRequestBuilder is the top-level builder for sending requests to Steward. It is used to build a ScheduleRequest.
type ScheduleRequestBuilder struct {
cellarID string
chainID int
Expand All @@ -15,10 +16,14 @@ type ScheduleRequestBuilder struct {
deadline int
}

// NewScheduleRequestBuilder creates a new ScheduleRequestBuilder.
func NewScheduleRequestBuilder() *ScheduleRequestBuilder {
return &ScheduleRequestBuilder{}
return &ScheduleRequestBuilder{
callData: &steward_proto.CellarV2_5{},
}
}

// Build tries to build the ScheduleRequest from the builder. It will return an error if basic validation fails.
func (b *ScheduleRequestBuilder) Build() (*steward_proto.ScheduleRequest, error) {
if err := b.Validate(); err != nil {
return nil, err
Expand All @@ -35,6 +40,7 @@ func (b *ScheduleRequestBuilder) Build() (*steward_proto.ScheduleRequest, error)
}, nil
}

// Validate checks if the builder has all the required fields set with legal values.
func (b *ScheduleRequestBuilder) Validate() error {
if b.chainID == 0 {
return fmt.Errorf("chain ID cannot be zero")
Expand All @@ -46,29 +52,58 @@ func (b *ScheduleRequestBuilder) Validate() error {
return fmt.Errorf("deadline must be zero or positive")
}

return validateCallData(b.callData)
}

// Sanity check for call data builder
func validateCallData(callData *steward_proto.CellarV2_5) error {
if callData == nil {
return fmt.Errorf("call data cannot be nil")
}

switch callData.GetCallType().(type) {
case *steward_proto.CellarV2_5_FunctionCall_:
if callData.GetFunctionCall() == nil {
return fmt.Errorf("function call cannot be nil")
}
case *steward_proto.CellarV2_5_Multicall_:
if callData.GetMulticall() == nil {
return fmt.Errorf("multi call cannot be nil")
}

if len(callData.GetMulticall().FunctionCalls) == 0 {
return fmt.Errorf("multi call cannot be empty")
}
}

return nil
}

// WithCellarID sets the cellar ID for the request.
func (b *ScheduleRequestBuilder) WithCellarID(cellarID common.Address) *ScheduleRequestBuilder {
b.cellarID = cellarID.Hex()
return b
}

// WithChainID sets the chain ID for the request.
func (b *ScheduleRequestBuilder) WithChainID(chainID int) *ScheduleRequestBuilder {
b.chainID = chainID
return b
}

// WithBlockHeight sets the Sommelier block height for the request at which the chain will tally votes for the cellar call.
func (b *ScheduleRequestBuilder) WithBlockHeight(blockHeight int) *ScheduleRequestBuilder {
b.blockHeight = blockHeight
return b
}

// WithCallData sets the call data for the request.
func (b *ScheduleRequestBuilder) WithCallData(callData *steward_proto.CellarV2_5) *ScheduleRequestBuilder {
b.callData = callData
return b
}

// WithDeadline sets the deadline for the request. Only applies to non-Ethereum chains.
func (b *ScheduleRequestBuilder) WithDeadline(deadline int) *ScheduleRequestBuilder {
b.deadline = deadline
return b
Expand Down

0 comments on commit e52b91a

Please sign in to comment.