-
Notifications
You must be signed in to change notification settings - Fork 18
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
Changes from 14 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
9ef9821
Balancer flash loan builder
cbrit 1128d03
Pendle builder
cbrit c9a75ff
Gofmt
cbrit e52b91a
Comments and validation
cbrit ee44898
Initial adaptor call builders unit tests
cbrit 9d8b48c
Initial integration tests + move examples to builder directory
cbrit a026c3b
Fmt
cbrit 6f46df3
Compiler errors
cbrit 541b2a4
move steward_proto_go to go folder
cbrit 702e629
More examples in the go builder
cbrit 15fe4a7
Tidying up
cbrit c301715
Some builder/simulation encoding integration tests
cbrit 4404c8b
Only run builder simulation integration tests when in integration env…
cbrit ef8b18a
Go mod tidy
cbrit 6702f61
Update API docs
cbrit 37671ee
Improve examples and tests
cbrit 01100f5
Concurrent broadcast example
cbrit File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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 | ||
} |
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,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) | ||
} |
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
23 changes: 23 additions & 0 deletions
23
go/builder/adaptors/aave_v2_enable_asset_as_collateral_test.go
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,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) | ||
} | ||
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,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) | ||
} |
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,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) | ||
|
||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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]) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.
should be updated to a valid address.