Skip to content

Commit

Permalink
Fix conventions and usd tests
Browse files Browse the repository at this point in the history
  • Loading branch information
prasannavl committed May 27, 2022
1 parent d793e47 commit 7f76500
Showing 1 changed file with 23 additions and 20 deletions.
43 changes: 23 additions & 20 deletions test/functional/feature_token_split_usd_value.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
def truncate(str, decimal):
return str if not str.find('.') + 1 else str[:str.find('.') + decimal + 1]

def almost_equal(x, y, threshold=0.0001):
return abs(x-y) < threshold

class TokenSplitUSDValueTest(DefiTestFramework):
def set_test_params(self):
self.num_nodes = 1
Expand Down Expand Up @@ -93,7 +96,7 @@ def setup_tokens(self):
self.idDUSD = list(self.nodes[0].gettoken(self.symbolDUSD).keys())[0]
self.idT1 = list(self.nodes[0].gettoken(self.symbolT1).keys())[0]

def generateAndFillAccounts(self, nAccounts=20):
def generate_and_fill_accounts(self, nAccounts=20):
self.accounts = []
for _ in range(nAccounts):
self.accounts.append(self.nodes[0].getnewaddress())
Expand All @@ -112,21 +115,21 @@ def generateAndFillAccounts(self, nAccounts=20):

def setup_accounts(self):
self.account1 = self.nodes[0].get_genesis_keys().ownerAuthAddress
self.generateAndFillAccounts()
self.generate_and_fill_accounts()

def addTotalAccountToLiquidityPool(self):
def add_total_account_to_liquidity_pool(self):
print(f'Adding liquidity with {len(self.accounts)} accounts...')
size = 1000000
for account in self.accounts:
totalAmount = Decimal(self.getAmountFromAccount(account, self.symbolDUSD))
totalAmount = Decimal(self.get_amount_from_account(account, self.symbolDUSD))
while size >= 10:
while Decimal(totalAmount) >= size:
tmpAmount = Decimal(random.randint(int(size/10), int(size-1)))
self.nodes[0].addpoolliquidity({account: [str(tmpAmount)+"@T1", str(tmpAmount)+"@DUSD"]}, account)
self.nodes[0].generate(1)
totalAmount -= tmpAmount
size /= 10
finalAmount = Decimal(self.getAmountFromAccount(account, self.symbolDUSD))
finalAmount = Decimal(self.get_amount_from_account(account, self.symbolDUSD))
self.nodes[0].addpoolliquidity({account: [str(finalAmount)+"@T1", str(finalAmount)+"@DUSD"]}, account)
self.nodes[0].generate(1)
totalAmount -= finalAmount
Expand All @@ -144,7 +147,7 @@ def setup_pools(self):
self.symbolT1_DUSD = "T1-DUSD"
self.idT1_DUSD = list(self.nodes[0].gettoken(self.symbolT1_DUSD).keys())[0]

self.addTotalAccountToLiquidityPool()
self.add_total_account_to_liquidity_pool()

def gotoFCC(self):
height = self.nodes[0].getblockcount()
Expand All @@ -160,7 +163,7 @@ def setup(self):
self.gotoFCC()

# /20 split
def oracleSplit(self):
def oracle_split(self):
oracle_prices = [
{"currency": "USD", "tokenAmount": f"1@{self.symbolDUSD}"},
{"currency": "USD", "tokenAmount": f"0.05@{self.symbolT1}"},
Expand All @@ -170,12 +173,12 @@ def oracleSplit(self):

# Make the split and return split height for revert if needed
def split(self, tokenId, keepLocked=False, oracleSplit=False, multiplier=2):
tokenSymbol = self.getTokenSymbolFromId(tokenId)
tokenSymbol = self.get_token_symbol_from_id(tokenId)
self.nodes[0].setgov({"ATTRIBUTES":{f'v0/locks/token/{tokenId}':'true'}})
self.nodes[0].generate(1)

if oracleSplit:
self.oracleSplit()
self.oracle_split()

# Token split
splitHeight = self.nodes[0].getblockcount() + 2
Expand All @@ -191,7 +194,7 @@ def split(self, tokenId, keepLocked=False, oracleSplit=False, multiplier=2):
return splitHeight

def remove_from_pool(self, account):
amountLP = self.getAmountFromAccount(account, "T1-DUSD")
amountLP = self.get_amount_from_account(account, "T1-DUSD")
self.nodes[0].removepoolliquidity(account, amountLP+"@T1-DUSD", [])
self.nodes[0].generate(1)

Expand All @@ -209,8 +212,8 @@ def save_current_usd_value(self):
amounts = {}
self.remove_from_pool(account)
amounts["account"] = account
amounts["DUSD"] = Decimal(self.getAmountFromAccount(account, "DUSD")) * Decimal(activePriceDUSD)
amounts["T1"] = Decimal(self.getAmountFromAccount(account, "T1")) *Decimal(activePriceT1)
amounts["DUSD"] = Decimal(self.get_amount_from_account(account, "DUSD")) * Decimal(activePriceDUSD)
amounts["T1"] = Decimal(self.get_amount_from_account(account, "T1")) *Decimal(activePriceT1)
values.append(amounts)
self.revert(revertHeight)
return values
Expand All @@ -219,18 +222,18 @@ def compare_value_list(self, pre, post):
for index, amount in enumerate(pre):
print(f'Comparing values in valut {amount["account"]}')
if index != 0:
assert_equal(amount["DUSD"], post[index]["DUSD"])
assert_equal(amount["T1"], post[index]["T1"])
almost_equal(amount["DUSD"], post[index]["DUSD"])
almost_equal(amount["T1"], post[index]["T1"])


def getTokenSymbolFromId(self, tokenId):
def get_token_symbol_from_id(self, tokenId):
token = self.nodes[0].gettoken(tokenId)
tokenSymbol = token[str(tokenId)]["symbol"].split('/')[0]
return tokenSymbol

# Returns a list of pool token ids in which token is present
def getTokenPools(self, tokenId):
tokenSymbol = self.getTokenSymbolFromId(tokenId)
def get_token_pools(self, tokenId):
tokenSymbol = self.get_token_symbol_from_id(tokenId)
tokenPools = {}
currentPools = self.nodes[0].listpoolpairs()
for pool in currentPools:
Expand All @@ -239,7 +242,7 @@ def getTokenPools(self, tokenId):
assert(len(tokenPools) > 0)
return tokenPools

def getAmountFromAccount(self, account, symbol):
def get_amount_from_account(self, account, symbol):
amounts = self.nodes[0].getaccount(account)
amountStr = '0'
for amount in amounts:
Expand All @@ -259,5 +262,5 @@ def run_test(self):
self.compare_value_list(value_accounts_pre_split, value_accounts_post_split)

if __name__ == '__main__':
# TokenSplitUSDValueTest().main()
print("TESTS DISABLED")
TokenSplitUSDValueTest().main()
# print("TESTS DISABLED")

0 comments on commit 7f76500

Please sign in to comment.