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

Go Request Builder Library #265

Merged
merged 17 commits into from
Jul 25, 2024
Merged
Show file tree
Hide file tree
Changes from 14 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
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ replace github.com/gogo/protobuf => github.com/regen-network/protobuf v1.3.3-alp

replace github.com/99designs/keyring => github.com/cosmos/keyring v1.1.7-0.20210622111912-ef00f8ac3d76

replace github.com/peggyjv/steward/steward_proto_go/steward_proto => ./steward_proto_go/steward_proto
replace github.com/peggyjv/steward/steward_proto_go/steward_proto => ./go/steward_proto_go/steward_proto

// use cometbft
replace github.com/tendermint/tendermint => github.com/cometbft/cometbft v0.34.28
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
49 changes: 49 additions & 0 deletions go/builder/adaptors/aave_v2_a_token_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,53 @@ func TestNewAaveATokenAdaptorCall(t *testing.T) {
assert.IsType(t, &steward_proto.AaveATokenAdaptorV2_RevokeApproval{}, builder.calls[0].Function)
assert.IsType(t, &steward_proto.AaveATokenAdaptorV2_DepositToAave_{}, builder.calls[1].Function)
assert.IsType(t, &steward_proto.AaveATokenAdaptorV2_WithdrawFromAave_{}, builder.calls[2].Function)

result := builder.Build()
assert.Equal(t, adaptor.Hex(), result.Adaptor)
assert.IsType(t, &steward_proto.AdaptorCall_AaveATokenV2Calls{}, result.CallData)
}

func TestEmptyBuilder(t *testing.T) {
adaptor := common.HexToAddress("0x1234567890123456789012345678901234567890")
builder := NewAaveV2ATokenAdaptorV2CallBuilder(adaptor)

assert.Equal(t, 0, len(builder.calls))

result := builder.Build()
assert.Equal(t, adaptor.Hex(), result.Adaptor)
assert.IsType(t, &steward_proto.AdaptorCall_AaveATokenV2Calls{}, result.CallData)
}

func TestRevokeApproval(t *testing.T) {
adaptor := common.HexToAddress("0x1234567890123456789012345678901234567890")
builder := NewAaveV2ATokenAdaptorV2CallBuilder(adaptor)

asset := common.HexToAddress("0x00000000000000000000000000000000000000000")
spender := common.HexToAddress("0x11111111111111111111111111111111111111111")
builder.RevokeApproval(asset, spender)

assert.Equal(t, 1, len(builder.calls))
assert.IsType(t, &steward_proto.AaveATokenAdaptorV2_RevokeApproval{}, builder.calls[0].Function)
}

func TestDepositToAave(t *testing.T) {
adaptor := common.HexToAddress("0x1234567890123456789012345678901234567890")
builder := NewAaveV2ATokenAdaptorV2CallBuilder(adaptor)

asset := common.HexToAddress("0x00000000000000000000000000000000000000000")
builder.DepositToAave(asset, big.NewInt(100))

assert.Equal(t, 1, len(builder.calls))
assert.IsType(t, &steward_proto.AaveATokenAdaptorV2_DepositToAave_{}, builder.calls[0].Function)
}

func TestWithdrawFromAave(t *testing.T) {
adaptor := common.HexToAddress("0x1234567890123456789012345678901234567890")
builder := NewAaveV2ATokenAdaptorV2CallBuilder(adaptor)

asset := common.HexToAddress("0x00000000000000000000000000000000000000000")
builder.WithdrawFromAave(asset, big.NewInt(100))

assert.Equal(t, 1, len(builder.calls))
assert.IsType(t, &steward_proto.AaveATokenAdaptorV2_WithdrawFromAave_{}, builder.calls[0].Function)
}
72 changes: 72 additions & 0 deletions go/builder/adaptors/aave_v2_debt_token.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package adaptors

import (
"math/big"

"github.com/ethereum/go-ethereum/common"
"github.com/peggyjv/steward/steward_proto_go/steward_proto"
)

// AaveV2DebtTokenAdaptorV2CallBuilder is a builder for AaveV2DebtTokenAdaptorV2 calls.
// Contract: https://github.com/PeggyJV/cellar-contracts/blob/main/src/modules/adaptors/Aave/AaveDebtTokenAdaptor.sol
type AaveV2DebtTokenAdaptorV2CallBuilder struct {
adaptor common.Address
calls []*steward_proto.AaveDebtTokenAdaptorV2
}

func NewAaveV2DebtTokenAdaptorV2CallBuilder(adaptor common.Address) *AaveV2DebtTokenAdaptorV2CallBuilder {
return &AaveV2DebtTokenAdaptorV2CallBuilder{
adaptor: adaptor,
calls: make([]*steward_proto.AaveDebtTokenAdaptorV2, 0),
}
}

func (b *AaveV2DebtTokenAdaptorV2CallBuilder) Build() *steward_proto.AdaptorCall {
return &steward_proto.AdaptorCall{
Adaptor: b.adaptor.Hex(),
CallData: &steward_proto.AdaptorCall_AaveDebtTokenV2Calls{
AaveDebtTokenV2Calls: &steward_proto.AaveDebtTokenAdaptorV2Calls{
Calls: b.calls,
},
},
}
}

func (b *AaveV2DebtTokenAdaptorV2CallBuilder) RevokeApproval(asset common.Address, spender common.Address) *AaveV2DebtTokenAdaptorV2CallBuilder {
b.calls = append(b.calls, &steward_proto.AaveDebtTokenAdaptorV2{
Function: &steward_proto.AaveDebtTokenAdaptorV2_RevokeApproval{
RevokeApproval: &steward_proto.RevokeApproval{
Asset: asset.Hex(),
Spender: spender.Hex(),
},
},
})

return b
}

func (b *AaveV2DebtTokenAdaptorV2CallBuilder) BorrowFromAave(token common.Address, amount *big.Int) *AaveV2DebtTokenAdaptorV2CallBuilder {
b.calls = append(b.calls, &steward_proto.AaveDebtTokenAdaptorV2{
Function: &steward_proto.AaveDebtTokenAdaptorV2_BorrowFromAave_{
BorrowFromAave: &steward_proto.AaveDebtTokenAdaptorV2_BorrowFromAave{
Token: token.Hex(),
Amount: amount.String(),
},
},
})

return b
}

func (b *AaveV2DebtTokenAdaptorV2CallBuilder) RepayAaveDebt(token common.Address, amount *big.Int) *AaveV2DebtTokenAdaptorV2CallBuilder {
b.calls = append(b.calls, &steward_proto.AaveDebtTokenAdaptorV2{
Function: &steward_proto.AaveDebtTokenAdaptorV2_RepayAaveDebt_{
RepayAaveDebt: &steward_proto.AaveDebtTokenAdaptorV2_RepayAaveDebt{
Token: token.Hex(),
Amount: amount.String(),
},
},
})

return b
}
83 changes: 83 additions & 0 deletions go/builder/adaptors/aave_v2_debt_token_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package adaptors

import (
"math/big"
"testing"

"github.com/ethereum/go-ethereum/common"
"github.com/peggyjv/steward/steward_proto_go/steward_proto"
"github.com/stretchr/testify/assert"
)

// Test the AaveV2DebtTokenAdaptorCallBuilder constructor
func TestNewAaveDebtTokenAdaptorCall(t *testing.T) {
// Create a new AaveV2DebtTokenAdaptorCallBuilder
adaptor := common.HexToAddress("0x1234567890123456789012345678901234567890")
builder := NewAaveV2DebtTokenAdaptorV2CallBuilder(adaptor)

// Check the builder
assert.Equal(t, adaptor.Hex(), builder.adaptor.Hex())
assert.Equal(t, 0, len(builder.calls))

asset := common.HexToAddress("0x00000000000000000000000000000000000000000")
spender := common.HexToAddress("0x11111111111111111111111111111111111111111")
builder.RevokeApproval(asset, spender)
builder.BorrowFromAave(asset, big.NewInt(100))
builder.RepayAaveDebt(asset, big.NewInt(100))

// Check the builder
assert.Equal(t, adaptor.Hex(), builder.adaptor.Hex())
assert.Equal(t, 3, len(builder.calls))
assert.IsType(t, &steward_proto.AaveDebtTokenAdaptorV2_RevokeApproval{}, builder.calls[0].Function)
assert.IsType(t, &steward_proto.AaveDebtTokenAdaptorV2_BorrowFromAave_{}, builder.calls[1].Function)
assert.IsType(t, &steward_proto.AaveDebtTokenAdaptorV2_RepayAaveDebt_{}, builder.calls[2].Function)

result := builder.Build()
assert.Equal(t, adaptor.Hex(), result.Adaptor)
assert.IsType(t, &steward_proto.AdaptorCall_AaveDebtTokenV2Calls{}, result.CallData)
}

func TestEmptyAaveV2DebtTokenAdaptorBuilder(t *testing.T) {
adaptor := common.HexToAddress("0x1234567890123456789012345678901234567890")
builder := NewAaveV2DebtTokenAdaptorV2CallBuilder(adaptor)

assert.Equal(t, 0, len(builder.calls))

result := builder.Build()
assert.Equal(t, adaptor.Hex(), result.Adaptor)
assert.IsType(t, &steward_proto.AdaptorCall_AaveDebtTokenV2Calls{}, result.CallData)
}

func TestAaveV2DebtTokenAdaptorRevokeApproval(t *testing.T) {
adaptor := common.HexToAddress("0x1234567890123456789012345678901234567890")
builder := NewAaveV2DebtTokenAdaptorV2CallBuilder(adaptor)

asset := common.HexToAddress("0x00000000000000000000000000000000000000000")
spender := common.HexToAddress("0x11111111111111111111111111111111111111111")
builder.RevokeApproval(asset, spender)

assert.Equal(t, 1, len(builder.calls))
assert.IsType(t, &steward_proto.AaveDebtTokenAdaptorV2_RevokeApproval{}, builder.calls[0].Function)
}

func TestBorrowFromAave(t *testing.T) {
adaptor := common.HexToAddress("0x1234567890123456789012345678901234567890")
builder := NewAaveV2DebtTokenAdaptorV2CallBuilder(adaptor)

asset := common.HexToAddress("0x00000000000000000000000000000000000000000")
builder.BorrowFromAave(asset, big.NewInt(100))

assert.Equal(t, 1, len(builder.calls))
assert.IsType(t, &steward_proto.AaveDebtTokenAdaptorV2_BorrowFromAave_{}, builder.calls[0].Function)
}

func TestRepayAaveDebt(t *testing.T) {
adaptor := common.HexToAddress("0x1234567890123456789012345678901234567890")
builder := NewAaveV2DebtTokenAdaptorV2CallBuilder(adaptor)

asset := common.HexToAddress("0x00000000000000000000000000000000000000000")
builder.RepayAaveDebt(asset, big.NewInt(100))

assert.Equal(t, 1, len(builder.calls))
assert.IsType(t, &steward_proto.AaveDebtTokenAdaptorV2_RepayAaveDebt_{}, builder.calls[0].Function)
}
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
23 changes: 23 additions & 0 deletions go/builder/adaptors/aave_v2_enable_asset_as_collateral_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package adaptors

import (
"testing"

"github.com/ethereum/go-ethereum/common"
"github.com/peggyjv/steward/steward_proto_go/steward_proto"
"github.com/stretchr/testify/assert"
)

// Test enabling an asset as collateral
func TestEnableAssetAsCollateral(t *testing.T) {
adaptor := common.HexToAddress("0x1234567890123456789012345678901234567890")
builder := NewAaveV2EnableAssetAsCollateralAdaptorV1CallBuilder(adaptor)

// Test enabling
asset := common.HexToAddress("0x00000000000000000000000000000000000000000")
builder.SetUserUseReserveAsCollateral(asset, true)

// Check the builder
assert.Equal(t, 1, len(builder.calls))
assert.IsType(t, &steward_proto.AaveV2EnableAssetAsCollateralAdaptorV1_SetUserUseReserveAsCollateral_{}, builder.calls[0].Function)
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Update the asset address to a valid one.

The asset address used in the test is invalid.

asset := common.HexToAddress("0x00000000000000000000000000000000000000000")

should be updated to a valid address.

asset := common.HexToAddress("0x0000000000000000000000000000000000000000")

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
44 changes: 44 additions & 0 deletions go/builder/adaptors/aave_v3_a_token_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package adaptors

import (
"math/big"
"testing"

"github.com/ethereum/go-ethereum/common"
"github.com/peggyjv/steward/steward_proto_go/steward_proto"
"github.com/stretchr/testify/assert"
)

// Test the AaveV3ATokenAdaptorCallBuilder constructor
func TestNewAaveV3ATokenAdaptorCall(t *testing.T) {
adaptor := common.HexToAddress("0x1234567890123456789012345678901234567890")
builder := NewAaveV3ATokenAdaptorV1CallBuilder(adaptor)

// Check the builder
assert.Equal(t, adaptor.Hex(), builder.adaptor.Hex())
assert.Equal(t, 0, len(builder.calls))
}

// Test DepositToAave function
func TestDepositToAaveV3(t *testing.T) {
adaptor := common.HexToAddress("0x1234567890123456789012345678901234567890")
builder := NewAaveV3ATokenAdaptorV1CallBuilder(adaptor)

asset := common.HexToAddress("0x00000000000000000000000000000000000000000")
builder.DepositToAave(asset, big.NewInt(100))

assert.Equal(t, 1, len(builder.calls))
assert.IsType(t, &steward_proto.AaveV3ATokenAdaptorV1_DepositToAave_{}, builder.calls[0].Function)
}

// Test WithdrawFromAave function
func TestWithdrawFromAaveV3(t *testing.T) {
adaptor := common.HexToAddress("0x1234567890123456789012345678901234567890")
builder := NewAaveV3ATokenAdaptorV1CallBuilder(adaptor)

asset := common.HexToAddress("0x00000000000000000000000000000000000000000")
builder.WithdrawFromAave(asset, big.NewInt(100))

assert.Equal(t, 1, len(builder.calls))
assert.IsType(t, &steward_proto.AaveV3ATokenAdaptorV1_WithdrawFromAave_{}, builder.calls[0].Function)
}
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
5 changes: 3 additions & 2 deletions go/builder/adaptors/aave_v3_debt_token_flash_loan.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@ 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
}

// Constructor
func NewAaveV3DebtTokenFlashLoanAdaptorV1Call(adaptor common.Address) *AaveV3DebtTokenFlashLoanAdaptorV1CallBuilder {
func NewAaveV3DebtTokenFlashLoanAdaptorV1CallBuilder(adaptor common.Address) *AaveV3DebtTokenFlashLoanAdaptorV1CallBuilder {
return &AaveV3DebtTokenFlashLoanAdaptorV1CallBuilder{
adaptor: adaptor,
}
Expand Down
37 changes: 37 additions & 0 deletions go/builder/adaptors/aave_v3_debt_token_flash_loan_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package adaptors

import (
"math/big"
"testing"

"github.com/ethereum/go-ethereum/common"
"github.com/peggyjv/steward/steward_proto_go/steward_proto"
"github.com/stretchr/testify/assert"
)

// Test the AaveV3DebtTokenFlashLoanAdaptorCallBuilder constructor
func TestNewAaveV3DebtTokenFlashLoanAdaptorCall(t *testing.T) {
adaptor := common.HexToAddress("0x1234567890123456789012345678901234567890")
builder := NewAaveV3DebtTokenFlashLoanAdaptorV1CallBuilder(adaptor)

// Check the builder
assert.Equal(t, adaptor.Hex(), builder.adaptor.Hex())
assert.Equal(t, 0, len(builder.calls))
}

// Test RequestFlashLoan function
func TestRequestFlashLoan(t *testing.T) {
adaptor := common.HexToAddress("0x1234567890123456789012345678901234567890")
builder := NewAaveV3DebtTokenFlashLoanAdaptorV1CallBuilder(adaptor)

asset := common.HexToAddress("0x00000000000000000000000000000000000000000")

// AaveV2 adaptor call builder
call := NewAaveV2ATokenAdaptorV2CallBuilder(common.HexToAddress("0x1")).DepositToAave(common.HexToAddress("0x2"), big.NewInt(100)).Build()

builder.FlashLoan([]common.Address{asset}, []*big.Int{big.NewInt(100)}, []*steward_proto.AdaptorCall{call})

assert.Equal(t, 1, len(builder.calls))
assert.IsType(t, &steward_proto.AaveV3DebtTokenAdaptorV1FlashLoan_FlashLoan{}, builder.calls[0].FlashLoan)

}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider adding more assertions to verify the correctness of the flash loan request.

Adding more assertions can help in verifying the correctness of the flash loan request.

assert.Equal(t, asset.Hex(), builder.calls[0].FlashLoan.Assets[0].Hex())
assert.Equal(t, big.NewInt(100), builder.calls[0].FlashLoan.Amounts[0])
assert.Equal(t, call, builder.calls[0].FlashLoan.AdaptorCalls[0])

Loading
Loading