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

Integration test scripts after redeployments #345

Merged
merged 5 commits into from
Jul 30, 2021
Merged
Changes from 2 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
169 changes: 160 additions & 9 deletions scripts/contractInteraction/loan_tokens.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ def lendToPool(loanTokenAddress, tokenAddress, amount):
loanToken = Contract.from_abi("loanToken", address=loanTokenAddress, abi=LoanTokenLogicStandard.abi, owner=conf.acct)
if(token.allowance(conf.acct, loanToken.address) < amount):
token.approve(loanToken.address, amount)
loanToken.mint(conf.acct, amount)
tx = loanToken.mint(conf.acct, amount)
tx.info()

def lendToPoolWithMS(loanTokenAddress, tokenAddress, amount):
token = Contract.from_abi("TestToken", address = tokenAddress, abi = TestToken.abi, owner = conf.acct)
Expand All @@ -24,7 +25,9 @@ def lendToPoolWithMS(loanTokenAddress, tokenAddress, amount):

def removeFromPool(loanTokenAddress, amount):
loanToken = Contract.from_abi("loanToken", address = loanTokenAddress, abi=LoanTokenLogicStandard.abi, owner=conf.acct)
loanToken.burn(conf.acct, amount)
tx = loanToken.burn(conf.acct, amount)
tx.info()
return tx

def readLoanTokenState(loanTokenAddress):
loanToken = Contract.from_abi("loanToken", address=loanTokenAddress, abi=LoanTokenLogicStandard.abi, owner=conf.acct)
Expand Down Expand Up @@ -83,6 +86,7 @@ def testTradeOpeningAndClosing(protocolAddress, loanTokenAddress, underlyingToke
0, # no collateral token sent
collateralTokenAddress, # collateralTokenAddress
conf.acct, # trader,
0, # slippage
b'', # loanDataBytes (only required with ether)
{'value': sendValue}
)
Expand All @@ -96,13 +100,16 @@ def testTradeOpeningAndClosing(protocolAddress, loanTokenAddress, underlyingToke
print(loan)
if(testClose):
tx = sovryn.closeWithSwap(loanId, conf.acct, collateral, True, b'')
tx.info()



def testTradeOpeningAndClosingWithCollateral(protocolAddress, loanTokenAddress, underlyingTokenAddress, collateralTokenAddress, collateralTokenSent, leverage, testClose, sendValue):
loanToken = Contract.from_abi("loanToken", address=loanTokenAddress, abi=LoanTokenLogicStandard.abi, owner=conf.acct)
testToken = Contract.from_abi("TestToken", address = underlyingTokenAddress, abi = TestToken.abi, owner = conf.acct)
testToken = Contract.from_abi("TestToken", address = collateralTokenAddress, abi = TestToken.abi, owner = conf.acct)
sovryn = Contract.from_abi("sovryn", address=protocolAddress, abi=interface.ISovrynBrownie.abi, owner=conf.acct)
#if(sendValue == 0 and testToken.allowance(conf.acct, loanTokenAddress) < loanTokenSent):
# testToken.approve(loanToken, loanTokenSent)
if(sendValue == 0 and testToken.allowance(conf.acct, loanTokenAddress) < collateralTokenSent):
testToken.approve(loanToken, collateralTokenSent)
print('going to trade')
tx = loanToken.marginTrade(
"0", # loanId (0 for new loans)
Expand All @@ -111,8 +118,9 @@ def testTradeOpeningAndClosingWithCollateral(protocolAddress, loanTokenAddress,
collateralTokenSent, # no collateral token sent
collateralTokenAddress, # collateralTokenAddress
conf.acct, # trader,
0, # slippage
b'', # loanDataBytes (only required with ether)
{'value': sendValue}
{'value': sendValue, "allow_revert": True}
)
tx.info()
loanId = tx.events['Trade']['loanId']
Expand All @@ -124,10 +132,11 @@ def testTradeOpeningAndClosingWithCollateral(protocolAddress, loanTokenAddress,
print(loan)
if(testClose):
tx = sovryn.closeWithSwap(loanId, conf.acct, collateral, True, b'')
tx.info()



def testBorrow(protocolAddress, loanTokenAddress, underlyingTokenAddress, collateralTokenAddress):
def testBorrow(protocolAddress, loanTokenAddress, underlyingTokenAddress, collateralTokenAddress, amount):
#read contract abis
sovryn = Contract.from_abi("sovryn", address=protocolAddress, abi=interface.ISovrynBrownie.abi, owner=conf.acct)
loanToken = Contract.from_abi("loanToken", address=loanTokenAddress, abi=LoanTokenLogicStandard.abi, owner=conf.acct)
Expand All @@ -136,7 +145,7 @@ def testBorrow(protocolAddress, loanTokenAddress, underlyingTokenAddress, collat
# determine borrowing parameter
withdrawAmount = 0.000010e18 #i want to borrow 10 USD
# compute the required collateral. params: address loanToken, address collateralToken, uint256 newPrincipal,uint256 marginAmount, bool isTorqueLoan
collateralTokenSent = 2* sovryn.getRequiredCollateral(underlyingTokenAddress,collateralTokenAddress,withdrawAmount,50e18, True)
collateralTokenSent = 2* sovryn.getRequiredCollateral(underlyingTokenAddress,collateralTokenAddress,withdrawAmount, amount, True)
print("collateral needed", collateralTokenSent/1e18)
durationInSeconds = 60*60*24*10 #10 days

Expand Down Expand Up @@ -339,4 +348,146 @@ def readLiquidity():

tokenContract = Contract.from_abi("Token", address=conf.contracts['WRBTC'], abi=TestToken.abi, owner=conf.acct)
bal = tokenContract.balanceOf(conf.contracts['ConverterUSDT'])
print("supply of rBTC on swap", bal/1e18)
print("supply of rBTC on swap", bal/1e18)

def testSwapsExternal(underlyingTokenAddress, collateralTokenAddress, amount):
sovryn = Contract.from_abi("sovryn", address=conf.contracts['sovrynProtocol'], abi=interface.ISovrynBrownie.abi, owner=conf.acct)
underlyingToken = Contract.from_abi("TestToken", address=underlyingTokenAddress, abi=ERC20.abi, owner=conf.acct)

receiver = conf.acct
tx = underlyingToken.approve(conf.contracts['sovrynProtocol'], amount)
tx.info()

tx = sovryn.swapExternal(
underlyingTokenAddress,
collateralTokenAddress,
receiver,
receiver,
amount,
0,
0,
b'',
{"value": 0, "allow_revert": True})
tx.info()

# Notes: This function will do:
# 1. tradeOpenAndClosingWithoutCollateral (using amountUnderlying that is sent in the arguments)
# 2. lendToPool (using amountUnderlying that is sent in the arguments)
# 3. removeFromPool (50% of the lending)
# 4. tradeOpenAndClosingWithCollateral (using amountCollateral that is sent in the arguments)
# 5. Test borrow (using amountCollateral that is sent in the arguments)
# 6. SwapsExternal (using amountUnderlying that is sent in the arguments)
#
# WARN:
# 1. make sure you have 3 times balance of underlyingTokenAddress
# 2. make sure you have 2 times balance of amountCollateral
def wrappedIntegrationTest(loanTokenAddress, underlyingTokenAddress, collateralTokenAddress, amountUnderlying, amountCollateral):
tjcloa marked this conversation as resolved.
Show resolved Hide resolved
sovryn = Contract.from_abi("sovryn", address=conf.contracts['sovrynProtocol'], abi=interface.ISovrynBrownie.abi, owner=conf.acct)
loanToken = Contract.from_abi("loanToken", address=loanTokenAddress, abi=LoanTokenLogicStandard.abi, owner=conf.acct)

underlyingToken = Contract.from_abi("TestToken", address=underlyingTokenAddress, abi=ERC20.abi, owner=conf.acct)
collateralToken = Contract.from_abi("TestToken", address=collateralTokenAddress, abi=ERC20.abi, owner=conf.acct)

prevUnderlyingBalance = underlyingToken.balanceOf(conf.acct)
prevCollateralBalance = collateralToken.balanceOf(conf.acct)

# ------------------------------------------------ Test Trade Open & Close without collateral ------------------------------------------------------
print("Test Trade open and closing without collateral")
testTradeOpeningAndClosing(sovryn.address, loanToken.address, underlyingTokenAddress, collateralTokenAddress, amountUnderlying, 2e18, True,0)

print("=============================== PREVIOUS BALANCE ====================================")
print("Underlying balance: ", prevUnderlyingBalance)
print("Collateral balance: ", prevCollateralBalance)

print("=============================== UPDATED BALANCE =====================================")
updatedUnderlyingBalance = underlyingToken.balanceOf(conf.acct)
updatedCollateralBalance = collateralToken.balanceOf(conf.acct)
print("Underlying balance: ", updatedUnderlyingBalance)
print("Collateral balance: ", updatedCollateralBalance)

# Verify
if prevUnderlyingBalance - updatedUnderlyingBalance != amountUnderlying:
raise Exception("Updated underyling balance is not matched with the amount that was traded")

prevUnderlyingBalance = updatedUnderlyingBalance
prevCollateralBalance = updatedCollateralBalance


# ------------------------------------------------ Test Lend to Pool -------------------------------------------------------------------------------
lendToPool(loanToken.address, underlyingTokenAddress, amountUnderlying)
print("=============================== PREVIOUS BALANCE ====================================")
print("Underlying balance: ", prevUnderlyingBalance)
print("Collateral balance: ", prevCollateralBalance)

print("=============================== UPDATED BALANCE =====================================")
updatedUnderlyingBalance = underlyingToken.balanceOf(conf.acct)
updatedCollateralBalance = collateralToken.balanceOf(conf.acct)
print("Underlying balance: ", updatedUnderlyingBalance)
print("Collateral balance: ", updatedCollateralBalance)

# Verify
if prevUnderlyingBalance - updatedUnderlyingBalance != amountUnderlying:
raise Exception("Updated underyling balance is not matched with the amount that was lent")

prevUnderlyingBalance = updatedUnderlyingBalance
prevCollateralBalance = updatedCollateralBalance

# ------------------------------------------------ Test Remove from Pool -------------------------------------------------------------------------------
removeFromPool(loanToken.address, 0.5*(amountUnderlying))
print("=============================== PREVIOUS BALANCE ====================================")
print("Underlying balance: ", prevUnderlyingBalance)
print("Collateral balance: ", prevCollateralBalance)

print("=============================== UPDATED BALANCE =====================================")
updatedUnderlyingBalance = underlyingToken.balanceOf(conf.acct)
updatedCollateralBalance = collateralToken.balanceOf(conf.acct)
print("Underlying balance: ", updatedUnderlyingBalance)
print("Collateral balance: ", updatedCollateralBalance)

prevUnderlyingBalance = updatedUnderlyingBalance
prevCollateralBalance = updatedCollateralBalance

# ------------------------------------------------ Test Trade Open & Close with collateral -------------------------------------------------------------------------------
testTradeOpeningAndClosingWithCollateral(sovryn.address, loanToken.address, underlyingTokenAddress, collateralTokenAddress, amountCollateral, 2e18, True, 0)

print("=============================== PREVIOUS BALANCE ====================================")
print("Underlying balance: ", prevUnderlyingBalance)
print("Collateral balance: ", prevCollateralBalance)

print("=============================== UPDATED BALANCE =====================================")
updatedUnderlyingBalance = underlyingToken.balanceOf(conf.acct)
updatedCollateralBalance = collateralToken.balanceOf(conf.acct)
print("Underlying balance: ", updatedUnderlyingBalance)
print("Collateral balance: ", updatedCollateralBalance)


prevUnderlyingBalance = updatedUnderlyingBalance
prevCollateralBalance = updatedCollateralBalance


# ------------------------------------------------ Test Borrow -------------------------------------------------------------------------------
testBorrow(sovryn.address, loanToken.address, underlyingTokenAddress, collateralTokenAddress, amountCollateral)

print("=============================== PREVIOUS BALANCE ====================================")
print("Underlying balance: ", prevUnderlyingBalance)
print("Collateral balance: ", prevCollateralBalance)

print("=============================== UPDATED BALANCE =====================================")
updatedUnderlyingBalance = underlyingToken.balanceOf(conf.acct)
updatedCollateralBalance = collateralToken.balanceOf(conf.acct)
print("Underlying balance: ", updatedUnderlyingBalance)
print("Collateral balance: ", updatedCollateralBalance)


# ------------------------------------------------ Test External Swap -------------------------------------------------------------------------------
testSwapsExternal(underlyingTokenAddress, collateralTokenAddress, amountUnderlying)

print("=============================== PREVIOUS BALANCE ====================================")
print("Underlying balance: ", prevUnderlyingBalance)
print("Collateral balance: ", prevCollateralBalance)

print("=============================== UPDATED BALANCE =====================================")
updatedUnderlyingBalance = underlyingToken.balanceOf(conf.acct)
updatedCollateralBalance = collateralToken.balanceOf(conf.acct)
print("Underlying balance: ", updatedUnderlyingBalance)
print("Collateral balance: ", updatedCollateralBalance)