Skip to content

Commit 73e3715

Browse files
committed
test: for credit_manager add collateral on opencreditaccount v2 event
1 parent d7c4427 commit 73e3715

File tree

6 files changed

+300
-9
lines changed

6 files changed

+300
-9
lines changed

.github/workflows/go.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,4 @@ jobs:
2626
- run: go get github.com/google/go-jsonnet/cmd/jsonnet && ./generate-test.sh
2727
if: steps.cache-packages.outputs.cache-hit != 'true'
2828
- name: Run Test
29-
run: go test ./tests && go test ./services && go test ./ds
29+
run: go test ./tests && go test ./services && go test ./ds && go test ./models/credit_manager

ds/repo_dummy.go

Lines changed: 228 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,228 @@
1+
package ds
2+
3+
import (
4+
"math/big"
5+
6+
"github.com/Gearbox-protocol/sdk-go/artifacts/dataCompressor/mainnet"
7+
"github.com/Gearbox-protocol/sdk-go/core"
8+
"github.com/Gearbox-protocol/sdk-go/core/schemas"
9+
"github.com/Gearbox-protocol/third-eye/ds/dc_wrapper"
10+
)
11+
12+
type DummyRepo struct {
13+
}
14+
15+
// sync adapters
16+
func (DummyRepo) GetKit() *AdapterKit {
17+
return nil
18+
}
19+
func (DummyRepo) AddSyncAdapter(adapterI SyncAdapterI) {
20+
}
21+
func (DummyRepo) InitChecks() {
22+
}
23+
func (DummyRepo) GetChainId() uint {
24+
return 0
25+
}
26+
27+
// saving to the db
28+
func (DummyRepo) Flush() error {
29+
return nil
30+
}
31+
32+
// adding block/timestamp
33+
func (DummyRepo) SetBlock(blockNum int64) {
34+
}
35+
func (DummyRepo) SetAndGetBlock(blockNum int64) *schemas.Block {
36+
return nil
37+
}
38+
func (DummyRepo) GetBlocks() map[int64]*schemas.Block {
39+
return nil
40+
}
41+
func (DummyRepo) GetTokenOracles() map[int16]map[string]*schemas.TokenOracle {
42+
return nil
43+
}
44+
func (DummyRepo) GetDisabledTokens() []*schemas.AllowedToken {
45+
return nil
46+
}
47+
func (DummyRepo) LoadBlocks(from, to int64) {
48+
}
49+
50+
// credit account operations
51+
func (DummyRepo) AddAccountOperation(accountOperation *schemas.AccountOperation) {
52+
}
53+
54+
// for getting executeparser
55+
func (DummyRepo) GetExecuteParser() ExecuteParserI {
56+
return nil
57+
}
58+
59+
// price feed/oracle funcs
60+
func (DummyRepo) DirectlyAddTokenOracle(tokenOracle *schemas.TokenOracle) {
61+
}
62+
func (DummyRepo) AddPriceFeed(pf *schemas.PriceFeed) {
63+
}
64+
65+
// token funcs
66+
func (DummyRepo) AddAllowedProtocol(logID uint, txHash, creditFilter string, p *schemas.Protocol) {
67+
}
68+
func (DummyRepo) DisableProtocol(blockNum int64, logID uint, txHash, cm, creditFilter, protocol string) {
69+
}
70+
func (DummyRepo) AddAllowedToken(logID uint, txHash, creditFilter string, atoken *schemas.AllowedToken) {
71+
}
72+
func (DummyRepo) DisableAllowedToken(blockNum int64, logID uint, txHash string, creditManager, creditFilter, token string) {
73+
}
74+
75+
// v2
76+
func (DummyRepo) AddAllowedTokenV2(logID uint, txHash, creditFilter string, atoken *schemas.AllowedToken) {
77+
}
78+
func (DummyRepo) UpdateLimits(logID uint, txHash, creditConfigurator string, params *schemas.Parameters) {
79+
}
80+
func (DummyRepo) UpdateFees(logID uint, txHash, creditConfigurator string, params *schemas.Parameters) {
81+
}
82+
func (DummyRepo) TransferAccountAllowed(*schemas.TransferAccountAllowed) {
83+
}
84+
func (DummyRepo) GetPricesInUSD(blockNum int64, tokenAddrs []string) core.JsonFloatMap {
85+
return nil
86+
}
87+
88+
//
89+
func (DummyRepo) GetToken(addr string) *schemas.Token {
90+
return nil
91+
}
92+
func (DummyRepo) GetTokens() []string {
93+
return nil
94+
}
95+
func (DummyRepo) ConvertToBalanceWithMask(balances []mainnet.DataTypesTokenBalance, mask *big.Int) (*core.JsonBalance, error) {
96+
return nil, nil
97+
}
98+
99+
// credit session funcs
100+
func (DummyRepo) AddCreditSession(session *schemas.CreditSession, loadedFromDB bool, txHash string, logID uint) {
101+
}
102+
func (DummyRepo) GetCreditSession(sessionId string) *schemas.CreditSession {
103+
return nil
104+
}
105+
func (DummyRepo) UpdateCreditSession(sessionId string, values map[string]interface{}) *schemas.CreditSession {
106+
return nil
107+
}
108+
func (DummyRepo) GetSessions() map[string]*schemas.CreditSession {
109+
return nil
110+
}
111+
func (DummyRepo) GetValueInCurrency(blockNum int64, version int16, token, currency string, amount *big.Int) *big.Int {
112+
return nil
113+
}
114+
func (DummyRepo) AddDieselToken(dieselToken, underlyingToken, pool string) {
115+
}
116+
func (DummyRepo) GetDieselTokens() map[string]*schemas.UTokenAndPool {
117+
return nil
118+
}
119+
120+
// credit session snapshots funcs
121+
func (DummyRepo) AddCreditSessionSnapshot(css *schemas.CreditSessionSnapshot) {
122+
}
123+
124+
// dc
125+
func (DummyRepo) GetDCWrapper() *dc_wrapper.DataCompressorWrapper {
126+
return nil
127+
}
128+
129+
// pools
130+
func (DummyRepo) AddPoolStat(ps *schemas.PoolStat) {
131+
}
132+
func (DummyRepo) AddPoolLedger(pl *schemas.PoolLedger) {
133+
}
134+
func (DummyRepo) GetPoolUniqueUserLen(pool string) int {
135+
return 0
136+
}
137+
func (DummyRepo) IsDieselToken(token string) bool {
138+
return false
139+
}
140+
func (DummyRepo) GetWETHAddr() string {
141+
return ""
142+
}
143+
func (DummyRepo) GetUSDCAddr() string {
144+
return ""
145+
}
146+
func (DummyRepo) GetGearTokenAddr() string {
147+
return ""
148+
}
149+
150+
// credit manager
151+
func (DummyRepo) AddAccountTokenTransfer(tt *schemas.TokenTransfer) {
152+
}
153+
func (DummyRepo) AddCreditManagerToFilter(cmAddr, cfAddr string) {
154+
}
155+
func (DummyRepo) GetMask(blockNum int64, cmAddr, accountAddr string, version int16) *big.Int {
156+
return nil
157+
}
158+
func (DummyRepo) AddCreditManagerStats(cms *schemas.CreditManagerStat) {
159+
}
160+
func (DummyRepo) GetCMState(cmAddr string) *schemas.CreditManagerState {
161+
return nil
162+
}
163+
func (DummyRepo) GetUnderlyingDecimal(cmAddr string) int8 {
164+
return 0
165+
}
166+
func (DummyRepo) AddRepayOnCM(cm string, pnl schemas.PnlOnRepay) {
167+
}
168+
func (DummyRepo) AddParameters(logID uint, txHash string, params *schemas.Parameters, token string) {
169+
}
170+
func (DummyRepo) AddFastCheckParams(logID uint, txHash, cm, creditFilter string, fcParams *schemas.FastCheckParams) {
171+
}
172+
func (DummyRepo) AfterSync(blockNum int64) {
173+
}
174+
func (DummyRepo) GetAccountManager() *DirectTransferManager {
175+
return nil
176+
}
177+
func (DummyRepo) AddAccountAddr(account string) {
178+
}
179+
180+
// dao
181+
func (DummyRepo) AddDAOOperation(operation *schemas.DAOOperation) {
182+
}
183+
func (DummyRepo) CalCurrentTreasuryValue(syncTill int64) {
184+
}
185+
func (DummyRepo) AddTreasuryTransfer(blockNum int64, logID uint, token string, amount *big.Int) {
186+
}
187+
func (DummyRepo) RecentEventMsg(blockNum int64, msg string, args ...interface{}) {
188+
}
189+
190+
//
191+
// oracle and uni
192+
func (DummyRepo) AddUniswapPrices(prices *schemas.UniPoolPrices) {
193+
}
194+
func (DummyRepo) GetYearnFeedAddrs() []string {
195+
return nil
196+
}
197+
198+
// has mutex lock
199+
func (DummyRepo) AddNewPriceOracleEvent(*schemas.TokenOracle) {
200+
}
201+
202+
//
203+
func (DummyRepo) LoadLastDebtSync() int64 {
204+
return 0
205+
}
206+
func (DummyRepo) LoadLastAdapterSync() int64 {
207+
return 0
208+
}
209+
func (DummyRepo) Clear() {
210+
}
211+
212+
// multicall
213+
func (DummyRepo) GetUniPricesByToken(token string) []*schemas.UniPoolPrices {
214+
return nil
215+
}
216+
func (DummyRepo) AddUniPoolsForToken(blockNum int64, token string) {
217+
}
218+
func (DummyRepo) AddUniPriceAndChainlinkRelation(relation *schemas.UniPriceAndChainlink) {
219+
}
220+
func (DummyRepo) AddLastSyncForToken(token string, lastSync int64) {
221+
}
222+
223+
// for testing
224+
func (DummyRepo) AddTokenObj(token *schemas.Token) {
225+
}
226+
func (DummyRepo) PrepareSyncAdapter(adapter *SyncAdapter) SyncAdapterI {
227+
return nil
228+
}

models/credit_manager/v1operation.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414
)
1515

1616
func (mdl *CreditManager) onOpenCreditAccount(txLog *types.Log, onBehalfOf, account string,
17-
amount,
17+
amount, // collateral/user added funds
1818
borrowAmount,
1919
referralCode *big.Int) error {
2020
// manager state

models/credit_manager/v2.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ func (mdl *CreditManager) processRemainingMultiCalls() {
162162
if mainAction != nil && mdl.multicall.lenOfMultiCalls() == 0 {
163163
mdl.setUpdateSession(mainAction.SessionId)
164164
mdl.Repo.AddAccountOperation(mainAction)
165-
mdl.openCreditAccountInitialAmount(mainAction.BlockNumber, mainAction)
165+
mdl.addCollteralForOpenCreditAccount(mainAction.BlockNumber, mainAction)
166166
}
167167
// for multicalls
168168
mainAction = mdl.multicall.OpenEvent
@@ -215,7 +215,7 @@ func (mdl *CreditManager) processNonMultiCalls() {
215215
}
216216

217217
// TO CHECK
218-
func (mdl *CreditManager) getInitialAmount(blockNum int64, mainAction *schemas.AccountOperation) *big.Int {
218+
func (mdl *CreditManager) getCollateralAmount(blockNum int64, mainAction *schemas.AccountOperation) *big.Int {
219219
balances := map[string]*big.Int{}
220220
for _, event := range mainAction.MultiCall {
221221
if event.Action == "AddCollateral(address,address,uint256)" {
@@ -249,8 +249,8 @@ func (mdl *CreditManager) getInitialAmount(blockNum int64, mainAction *schemas.A
249249
return initialAmount
250250
}
251251

252-
func (mdl *CreditManager) openCreditAccountInitialAmount(blockNum int64, mainAction *schemas.AccountOperation) {
253-
initialAmount := mdl.getInitialAmount(blockNum, mainAction)
254-
(*mainAction.Args)["amount"] = initialAmount.String()
255-
mdl.Repo.UpdateCreditSession(mainAction.SessionId, map[string]interface{}{"InitialAmount": initialAmount})
252+
func (mdl *CreditManager) addCollteralForOpenCreditAccount(blockNum int64, mainAction *schemas.AccountOperation) {
253+
collateral := mdl.getCollateralAmount(blockNum, mainAction)
254+
(*mainAction.Args)["amount"] = collateral.String()
255+
mdl.Repo.UpdateCreditSession(mainAction.SessionId, map[string]interface{}{"InitialAmount": collateral})
256256
}

models/credit_manager/v2_test.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package credit_manager
2+
3+
import (
4+
"testing"
5+
6+
"github.com/Gearbox-protocol/sdk-go/core"
7+
"github.com/Gearbox-protocol/sdk-go/core/schemas"
8+
"github.com/Gearbox-protocol/sdk-go/utils"
9+
"github.com/Gearbox-protocol/third-eye/ds"
10+
)
11+
12+
type RepoWrapper struct {
13+
*ds.DummyRepo
14+
prices core.JsonFloatMap
15+
tokens map[string]*schemas.Token
16+
}
17+
18+
func (repo RepoWrapper) GetToken(addr string) *schemas.Token {
19+
return repo.tokens[addr]
20+
}
21+
func (repo RepoWrapper) GetPricesInUSD(blockNum int64, tokenAddrs []string) core.JsonFloatMap {
22+
return repo.prices
23+
}
24+
func TestGetCollateralAmountOnOpen(t *testing.T) {
25+
usdc, weth := utils.RandomAddr(), utils.RandomAddr()
26+
repo := RepoWrapper{
27+
DummyRepo: &ds.DummyRepo{},
28+
prices: core.JsonFloatMap{
29+
weth: 1800,
30+
usdc: 1,
31+
},
32+
tokens: map[string]*schemas.Token{
33+
weth: {Symbol: "WETH", Decimals: 18},
34+
usdc: {Symbol: "USDC", Decimals: 6},
35+
},
36+
}
37+
cm := CreditManager{
38+
SyncAdapter: &ds.SyncAdapter{Repo: repo},
39+
State: &schemas.CreditManagerState{
40+
UnderlyingToken: weth,
41+
},
42+
}
43+
// account has weth as underlying
44+
collateral := cm.getCollateralAmount(5, &schemas.AccountOperation{
45+
MultiCall: []*schemas.AccountOperation{
46+
{
47+
Action: "AddCollateral(address,address,uint256)",
48+
Transfers: &core.Transfers{
49+
usdc: utils.GetExpInt(6 + 3), // 1000 usdc
50+
},
51+
},
52+
{
53+
Action: "AddCollateral(address,address,uint256)",
54+
Transfers: &core.Transfers{
55+
weth: utils.GetExpInt(18), // 1 weth
56+
},
57+
},
58+
},
59+
})
60+
if collateral.String() != "1555555555555555555" { // 1+ 1000/1800
61+
t.Fatalf("Collateral %d is different.", collateral)
62+
}
63+
}

models/credit_manager/v2operation.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ func (mdl *CreditManager) multiCallHandler(mainEvent *schemas.AccountOperation)
111111
mainEvent.MultiCall = multicalls
112112
// calculate initialAmount on open new credit creditaccount
113113
if mainEvent.Action == "OpenCreditAccount(address,address,uint256,uint16)" {
114-
mdl.openCreditAccountInitialAmount(mainEvent.BlockNumber, mainEvent)
114+
mdl.addCollteralForOpenCreditAccount(mainEvent.BlockNumber, mainEvent)
115115
}
116116
mdl.Repo.AddAccountOperation(mainEvent)
117117
}

0 commit comments

Comments
 (0)