From 103f9aa69cd7b3850ba737b369ffc7a1aa8845d3 Mon Sep 17 00:00:00 2001 From: Prasanna Loganathar Date: Fri, 27 May 2022 11:12:14 +0530 Subject: [PATCH] Fix conventions and usd tests --- .../feature_token_split_usd_value.py | 43 ++++++++++--------- 1 file changed, 23 insertions(+), 20 deletions(-) diff --git a/test/functional/feature_token_split_usd_value.py b/test/functional/feature_token_split_usd_value.py index 88cfda21d6e..3c857ec3d7f 100755 --- a/test/functional/feature_token_split_usd_value.py +++ b/test/functional/feature_token_split_usd_value.py @@ -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 @@ -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()) @@ -112,13 +115,13 @@ 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))) @@ -126,7 +129,7 @@ def addTotalAccountToLiquidityPool(self): 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 @@ -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() @@ -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}"}, @@ -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 @@ -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) @@ -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 @@ -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: @@ -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: @@ -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")