Skip to content

Commit

Permalink
Merge pull request #38 from Gearbox-protocol/HandleaddCollateralToAny
Browse files Browse the repository at this point in the history
fixes: #37
  • Loading branch information
harsh-98 authored Jul 28, 2022
2 parents 2c4ea76 + 1f1cc93 commit 606ece8
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 16 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,4 @@ jobs:
- run: go get github.com/google/go-jsonnet/cmd/jsonnet && ./generate-test.sh
if: steps.cache-packages.outputs.cache-hit != 'true'
- name: Run Test
run: go test ./tests
run: go test ./tests && go test ./services
5 changes: 4 additions & 1 deletion docs/v1_part1_to_part2_update.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,7 @@ Changes btw gearbox v2 part 1(which was created before April) and part2(which wa
- DataCompressor call failed for 0x4 or the 8th datacompressor added to gearbox v2 part 1 gearbox deployment. So, we have to manually calculate the `credit account data`. This has been fixed in the 9th dc.
- In priceOracle, for supporting NFT the convert takes creditAccount as the first param. Third-eye uses convert call in getValueInCurrency(used for collateral value calculation in usd and underlying token) and for getPriceInUSD which has v1 and v2 logic. v1 logic converts token price to USDC to get the USD denominated value. For v2, price is fetched using latestRoundData.
- TokenLiquidationThresholUpdated is not emitted for underlying token as it is set in the FeesUpdated event on creditConfigurator. [This method](https://github.com/Gearbox-protocol/contracts-v2/blob/581000e1948ef6008e8faa5dce3fc2177d17488d/contracts/credit/CreditConfigurator.sol#L433) will has set the liquidation threshold for tokens if the previous lt is more than the underlying token lt. The new value will be equal to underlying token lt.
- In the creditconfigurator `_setParams` is called in the constructor with default values and no event is emitted. There is no feesupdated in current kovan setup. And for underlying token and fees we will have to calc by making call.
- In the creditconfigurator `_setParams` is called in the constructor with default values and no event is emitted. There is no feesupdated in current kovan setup. And for underlying token and fees we will have to calc by making call.
- Another case, where event is not emitted in some cases.
https://github.com/Gearbox-protocol/contracts-v2/blob/main/contracts/credit/CreditConfigurator.sol#L114-L116 while credit facade is updated we haven’t emitted creditfacadeupgraded.
- addcollateral in executeorder can have onbehalOf different than the mainAction user/borrower. handled with issue #37.
30 changes: 27 additions & 3 deletions ds/execute_parser.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package ds

import (
"encoding/hex"

"github.com/Gearbox-protocol/sdk-go/core"
"github.com/ethereum/go-ethereum/common"
)
Expand All @@ -13,10 +15,32 @@ type ExecuteParams struct {
Index uint
BlockNumber int64
}

type GBv2Multicall []struct {
Target common.Address `json:"target"`
CallData []uint8 `json:"callData"`
}
type FuncWithMultiCall struct {
Name string `json:"name"`
MultiCallsLen int `json:"len"`
Name string `json:"name"`
MultiCalls GBv2Multicall `json:"-"`
TestLen int `json:"len"`
}

func (f *FuncWithMultiCall) LenForBorrower(bwr string) (len int) {
// for testing
if f.TestLen != 0 {
return f.TestLen
}
borrower := common.HexToAddress(bwr)
for _, call := range f.MultiCalls {
if hex.EncodeToString(call.CallData[:4]) == "59781034" { // addcollateral(address, address, uint256) call
// if the onbehalf is different then borrower ignore
if common.BytesToAddress(call.CallData[4:36]) != borrower {
continue
}
}
len++
}
return
}

type ExecuteParserI interface {
Expand Down
29 changes: 20 additions & 9 deletions models/credit_manager/v2operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package credit_manager
import (
"fmt"
"math/big"
"sort"
"strings"

"github.com/Gearbox-protocol/sdk-go/core"
Expand All @@ -26,12 +27,13 @@ func (mdl *CreditManager) CMStatsOnOpenAccount(borrowAmount *big.Int) {
func (mdl *CreditManager) multiCallHandler(mainAction *schemas.AccountOperation) {
account := strings.Split(mainAction.SessionId, "_")[0]
txHash := mainAction.TxHash
actionWithMulticallLen := mdl.Repo.GetExecuteParser().GetMainEventLogs(txHash, mdl.GetCreditFacadeAddr())
if len(actionWithMulticallLen) != 1 {
log.Fatal(utils.ToJson(actionWithMulticallLen), utils.ToJson(mainAction))
internalTxWithMulticall := mdl.Repo.GetExecuteParser().GetMainEventLogs(txHash, mdl.GetCreditFacadeAddr())
if len(internalTxWithMulticall) != 1 {
log.Fatal(utils.ToJson(internalTxWithMulticall), utils.ToJson(mainAction))
}
actionWithMulticall := internalTxWithMulticall[0]
var tenderlyEventName string
switch actionWithMulticallLen[0].Name {
switch actionWithMulticall.Name {
case "multicall":
mdl.setUpdateSession(mainAction.SessionId)
tenderlyEventName = "MultiCallStarted(address)"
Expand All @@ -44,13 +46,14 @@ func (mdl *CreditManager) multiCallHandler(mainAction *schemas.AccountOperation)
tenderlyEventName = "CloseCreditAccount(address,address)"
}
if tenderlyEventName != mainAction.Action {
log.Fatalf("Tenderly event %s is different from %s", actionWithMulticallLen[0].Name, mainAction.Action)
log.Fatalf("Tenderly event %s is different from %s", actionWithMulticall.Name, mainAction.Action)
}
events := mdl.multicall.PopMulticallEventsV2()
//
if len(events) != actionWithMulticallLen[0].MultiCallsLen {
borrowerEventLen := actionWithMulticall.LenForBorrower(mainAction.Borrower)
if len(events) != borrowerEventLen {
log.Fatalf("%s expected %d of multi calls, but third-eye detected %d. Events: %s",
actionWithMulticallLen[0].Name, actionWithMulticallLen[0].MultiCallsLen, len(events), utils.ToJson(events))
actionWithMulticall.Name, borrowerEventLen, len(events), utils.ToJson(events))
}
//
executeEvents := []ds.ExecuteParams{}
Expand All @@ -62,8 +65,15 @@ func (mdl *CreditManager) multiCallHandler(mainAction *schemas.AccountOperation)
}

switch event.Action {
case "AddCollateral(address,address,uint256)",
"IncreaseBorrowedAmount(address,uint256)",
case "AddCollateral(address,address,uint256)":
if event.Borrower == mainAction.Borrower {
multicalls = append(multicalls, event)
// add collateral can have different borrower then the mainaction user/borrower.
// related to issue #37.
} else {
mdl.Repo.AddAccountOperation(event)
}
case "IncreaseBorrowedAmount(address,uint256)",
"DecreaseBorrowedAmount(address,uint256)":
multicalls = append(multicalls, event)
case "ExecuteOrder":
Expand All @@ -80,6 +90,7 @@ func (mdl *CreditManager) multiCallHandler(mainAction *schemas.AccountOperation)
}
}
multicalls = append(multicalls, mdl.getProcessedExecuteEvents(txHash, executeEvents)...)
sort.Slice(multicalls, func(i, j int) bool { return multicalls[i].LogId < multicalls[j].LogId })
mainAction.MultiCall = multicalls
// calculate initialAmount on open new credit creditaccount
if mainAction.Action == "OpenCreditAccount(address,address,uint256,uint16)" {
Expand Down
11 changes: 9 additions & 2 deletions services/execute_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,9 +219,16 @@ func getCreditFacadeMainEvent(input string) (*ds.FuncWithMultiCall, error) {
if err != nil {
log.Fatal(err)
}
calls, ok := data["calls"].([]struct {
Target common.Address `json:"target"`
CallData []uint8 `json:"callData"`
})
if !ok {
log.Fatal("calls type is different the creditFacade multicall: ", reflect.TypeOf(data["calls"]))
}
return &ds.FuncWithMultiCall{
Name: method.Name,
MultiCallsLen: reflect.ValueOf(data["calls"]).Len(),
Name: method.Name,
MultiCalls: calls,
}, nil
}

Expand Down
17 changes: 17 additions & 0 deletions services/execute_parser_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package services

import (
"testing"

"github.com/Gearbox-protocol/sdk-go/log"
"github.com/Gearbox-protocol/sdk-go/utils"
"github.com/Gearbox-protocol/third-eye/config"
)

func TestGetMainEventLogs(t *testing.T) {
ep := NewExecuteParser(&config.Config{ChainId: 42})
actionWithMulticall := ep.GetMainEventLogs("0xfbbfbca8d6300adc20c1fd9b2bf2074a14cad0873ad5ed8492ef226861f7c0cc", "0x5aacdab79aa2d30f4242898ecdafda2ed2216db2")
if len(actionWithMulticall) != 1 || actionWithMulticall[0].Name != "openCreditAccountMulticall" || actionWithMulticall[0].LenForBorrower("0xee5998268707e9d57ab1156b3a87cd7476274362") != 1 {
log.Fatal(utils.ToJson(actionWithMulticall))
}
}

0 comments on commit 606ece8

Please sign in to comment.