Skip to content

Commit

Permalink
Add standalone affiliates deployment
Browse files Browse the repository at this point in the history
  • Loading branch information
cwsnt committed Apr 21, 2021
1 parent 3047d41 commit 12a0a9b
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 4 deletions.
50 changes: 50 additions & 0 deletions scripts/deployment/deploy_affiliates.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
from brownie import *
from brownie.network.contract import InterfaceContainer
from brownie.network.state import _add_contract, _remove_contract
from scripts.deployment.deploy_loanToken import deployLoanTokens
import shared
import json
from munch import Munch

'''
script to deploy the loan tokens. can be used to deploy loan tokens separately, but is also used by deploy_everything
if deploying separetly, the addresses of the existing contracts need to be set.
'''
def main():
thisNetwork = network.show_active()
##Affiliates
# == Load config =======================================================================================================================
if thisNetwork == "development":
acct = accounts[0]
configFile = open('./scripts/contractInteraction/testnet_contracts.json')
elif thisNetwork == "testnet":
acct = accounts.load("rskdeployer")
configFile = open('./scripts/contractInteraction/testnet_contracts.json')
elif thisNetwork == "rsk-mainnet":
acct = accounts.load("rskdeployer")
configFile = open('./scripts/contractInteraction/mainnet_contracts.json')
else:
raise Exception("network not supported")

# TODO check CSOV addresses in config files
# load deployed contracts addresses
contracts = json.load(configFile)
protocolAddress = contracts['sovrynProtocol']

tokens = Munch()
if thisNetwork == "development":
wrbtcAddress = '0x602C71e4DAC47a042Ee7f46E0aee17F94A3bA0B6'
susdAddress = '0x3194cBDC3dbcd3E11a07892e7bA5c3394048Cc87'
protocolAddress = '0x2c15A315610Bfa5248E4CbCbd693320e9D8E03Cc'
tokens.wrbtc = Contract.from_abi("TestWrbtc", address = wrbtcAddress, abi = TestWrbtc.abi, owner = acct)
tokens.susd = Contract.from_abi("TestToken", address = susdAddress, abi = TestToken.abi, owner = acct)

print("Deploying Affiliates.")
affiliates = acct.deploy(Affiliates)
print("Calling replaceContract.")
sovryn = Contract.from_abi("sovryn", address=protocolAddress, abi=interface.ISovrynBrownie.abi, owner=acct)
sovryn.replaceContract(affiliates.address)

# Test integration
if thisNetwork == "development":
(loanTokenSUSD, loanTokenWRBTC, loanTokenSettingsSUSD, loanTokenSettingsWRBTC) = deployLoanTokens(acct, sovryn, tokens)
9 changes: 5 additions & 4 deletions scripts/deployment/deploy_loanToken.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,6 @@ def testDeployment(acct, sovryn, loanTokenAddress, underlyingToken, collateralTo
test the affiliate margin trade by entering and closing a trade position
'''
def testAffiliatesIntegration(acct, sovryn, loanTokenAddress, underlyingToken, collateralToken, loanTokenSent, value):

print('\n TESTING THE AFFILIATES INTEGRATION')
loanToken = Contract.from_abi("loanToken", address=loanTokenAddress, abi=LoanTokenLogicStandard.abi, owner=acct)

Expand All @@ -213,6 +212,8 @@ def testAffiliatesIntegration(acct, sovryn, loanTokenAddress, underlyingToken, c

referrerAddress = accounts[9]
print('\n Referrer address : ', referrerAddress)
previousAffiliatesRewardBalanceOnChain = sovryn.affiliatesReferrerBalances(referrerAddress, underlyingToken)
previousBalanceInTokenContract = underlyingToken.balanceOf(referrerAddress)

tx = loanToken.marginTradeAffiliate(
"0", #loanId (0 for new loans)
Expand Down Expand Up @@ -243,7 +244,7 @@ def testAffiliatesIntegration(acct, sovryn, loanTokenAddress, underlyingToken, c
print("\n--- CHECK AFFILIATE REWARD BALANCE ---")
submittedAffiliatesReward = tx.events['PayTradingFeeToAffiliate']['fee']
affiliatesRewardBalanceOnChain = sovryn.affiliatesReferrerBalances(referrerAddress, underlyingToken)
if affiliatesRewardBalanceOnChain != submittedAffiliatesReward:
if affiliatesRewardBalanceOnChain - previousAffiliatesRewardBalanceOnChain != submittedAffiliatesReward:
raise Exception("Affiliates reward is invalid")
print("Check affiliate reward balance -- Passed with value (" , submittedAffiliatesReward , " & " , affiliatesRewardBalanceOnChain , ")\n")

Expand All @@ -267,11 +268,11 @@ def testAffiliatesIntegration(acct, sovryn, loanTokenAddress, underlyingToken, c
print("Rest Balance", restAffiliateBalance)
if latestAffiliateBalanceBeforeWithdrawal - affiliatesRewardBalanceOnChain != restAffiliateBalance:
raise Exception("Rest token amount after withdrawal in affiliate contract is not matched")

print("\n--- CHECK TOKEN BALANCE IN TOKEN CONTRACT ---")
balanceInTokenContract = underlyingToken.balanceOf(referrerAddress)
print("\nPrevious balance in token contract: ", previousBalanceInTokenContract)
print("Withdrawn token: ", affiliatesRewardBalanceOnChain)
print("Latest balance in token contract: ", balanceInTokenContract)
if balanceInTokenContract != affiliatesRewardBalanceOnChain:
if balanceInTokenContract - previousBalanceInTokenContract != affiliatesRewardBalanceOnChain:
raise Exception("Withdrawn token amount in token contract is not matched")

0 comments on commit 12a0a9b

Please sign in to comment.