diff --git a/app/core/contracts/PledgingContract.py b/app/core/contracts/PledgingContract.py index 37e2e81a..f0cdac74 100644 --- a/app/core/contracts/PledgingContract.py +++ b/app/core/contracts/PledgingContract.py @@ -40,6 +40,99 @@ def __init__(self, contractaddress=None): ContractMaster.__init__(self, self.template, self.version, contractaddress) + def pledge_tokens(self, callparamsip, repo: FetchRepository): + trxn = [] + callparams = input_to_dict(callparamsip) + cspecs = input_to_dict(self.contractparams['contractspecs']) + custodian_address = cspecs['custodian_address'] + tokens = callparams['tokens'] + signed_wallets = callparams['function_caller'] + lender = callparams["lender"] + function_caller = {} + custodian_present = False + + if len(signed_wallets) != 2: + raise ContractValidationError("Only two signatures are required") + for i in signed_wallets: + if i["wallet_address"] == custodian_address: + custodian_present = True + else: + function_caller = i["wallet_address"] + if not custodian_present: + raise ContractValidationError("Custodian signatures are mandatory") + + for i in tokens: + if i in callparams["value"]: + index_count = 0 + for j in callparams["value"]: + if j["token_code"] == i["token_code"] and j["amount"] == i["amount"]: + del callparams["value"][index_count] + break + index_count += 1 + qparam = {"borrower": function_caller, + "address": self.address, + "lender": lender, + "token_code": i["token_code"] + } + count = repo.select_count().add_table_name("pledge_ledger").where_clause("borrower", function_caller, + 1).and_clause("address", + self.address, 1).and_clause( + "lender", lender, 1).and_clause("token_code", i["token_code"], 1).execute_query_single_result( + qparam) + if i['amount'] < 0: + raise ContractValidationError( + "amount shall be positive for token code %s" % i) + value = function_caller + lender + i["token_code"] + result = hashlib.sha256(value.encode()).hexdigest() + if count[0] == 0: + sc_state_proposal1_data = { + "operation": "save", + "table_name": "pledge_ledger", + "sc_address": self.address, + "data": { + "borrower": function_caller, + "amount": i['amount'], + "time_updated": get_corrected_time_ms(), + "lender": lender, + "token_code": i["token_code"], + "unique_column": result, + "status": 1, + "address": self.address, + } + } + transaction_creator = TransactionCreator() + txtype1 = transaction_creator.transaction_type_8( + sc_state_proposal1_data) + trxn.append(txtype1) + else: + amount = repo.select_Query("id,amount").add_table_name("pledge_ledger").where_clause("borrower", + function_caller, + 1).and_clause( + "address", self.address, 1).and_clause("lender", lender, 1).and_clause("token_code", i[ + "token_code"], 1).execute_query_single_result( + qparam) + if count[0] == 1: + sc_state_proposal1_data = { + "operation": "update", + "table_name": "pledge_ledger", + "sc_address": self.address, + "data": { + "amount": amount[1] + i['amount'], + "time_updated": get_corrected_time_ms() + }, + "unique_column": "id", + "unique_value": amount[0], + } + transaction_creator = TransactionCreator() + txtype1 = transaction_creator.transaction_type_8( + sc_state_proposal1_data) + trxn.append(txtype1) + else: + raise ContractValidationError( + "more than one record found") + return trxn + + def pledge_request(self, callparamsip, repo: FetchRepository): trxn = [] callparams = input_to_dict(callparamsip) @@ -96,7 +189,7 @@ def pledge_request(self, callparamsip, repo: FetchRepository): "lender": lender, "token_code": i["token_code"], "unique_column": result, - "status": 0, + "status": 10, "address": self.address, } } @@ -137,65 +230,65 @@ def pledge_finalise(self, callparamsip, repo: FetchRepository): callparams = input_to_dict(callparamsip) wallet_address = callparams["borrower_wallet"] lender = callparams["lender"] - token_code = callparams["token_code"] signed_wallets = callparams['function_caller'] cspecs = input_to_dict(self.contractparams['contractspecs']) custodian_address = cspecs['custodian_address'] - - - qparam = {"borrower": wallet_address, - "address": self.address, - "lender": lender, - "token_code": token_code - } - count = repo.select_count().add_table_name("pledge_ledger").where_clause("borrower", wallet_address, - 1).and_clause("address", - self.address, 1).and_clause( - "lender", lender, 1).and_clause("token_code", token_code, 1).execute_query_single_result( - qparam) - if count[0] == 0: - raise ContractValidationError( - "No pledge record found for tokencode %s" % token_code) + tokens = callparams["tokens"] + for i in tokens: + token_code = i["token_code"] + qparam = {"borrower": wallet_address, + "address": self.address, + "lender": lender, + "token_code": token_code + } + count = repo.select_count().add_table_name("pledge_ledger").where_clause("borrower", wallet_address, + 1).and_clause("address", + self.address, 1).and_clause( + "lender", lender, 1).and_clause("token_code", token_code, 1).execute_query_single_result( + qparam) + if count[0] == 0: + raise ContractValidationError( + "No pledge record found for tokencode %s" % token_code) - data = repo.select_Query("id,amount,borrower,lender").add_table_name("pledge_ledger").where_clause("borrower", - wallet_address, - 1).and_clause("address", - self.address, 1).and_clause( - "lender", lender, 1).and_clause("token_code", token_code, 1).execute_query_single_result( - qparam) - - borrower_address = data[2] - lender_address = data[3] - - if len(signed_wallets) != 3: - raise ContractValidationError("Total three signatures are required for pledge finalise") - for i in signed_wallets: - if i["wallet_address"] == custodian_address: - custodian_present = True - if i["wallet_address"] == borrower_address: - borrower_present = True - if i["wallet_address"] == lender_address: - lender_present = True + data = repo.select_Query("id,amount,borrower,lender").add_table_name("pledge_ledger").where_clause("borrower", + wallet_address, + 1).and_clause("address", + self.address, 1).and_clause( + "lender", lender, 1).and_clause("token_code", token_code, 1).execute_query_single_result( + qparam) + + borrower_address = data[2] + lender_address = data[3] + + if len(signed_wallets) != 3: + raise ContractValidationError("Total three signatures are required for pledge finalise") + for i in signed_wallets: + if i["wallet_address"] == custodian_address: + custodian_present = True + if i["wallet_address"] == borrower_address: + borrower_present = True + if i["wallet_address"] == lender_address: + lender_present = True - if not borrower_present and lender_present and custodian_present: - raise ContractValidationError("Wrong sigantures provided") + if not borrower_present and lender_present and custodian_present: + raise ContractValidationError("Wrong sigantures provided") - sc_state_proposal1_data = { - "operation": "update", - "table_name": "pledge_ledger", - "sc_address": self.address, - "data": { - "time_updated": get_corrected_time_ms(), - "status": 1 - }, - "unique_column": "id", - "unique_value": data[0] - } - transaction_creator = TransactionCreator() - txtype1 = transaction_creator.transaction_type_8( - sc_state_proposal1_data) - trxn.append(txtype1) + sc_state_proposal1_data = { + "operation": "update", + "table_name": "pledge_ledger", + "sc_address": self.address, + "data": { + "time_updated": get_corrected_time_ms(), + "status": 11 + }, + "unique_column": "id", + "unique_value": data[0] + } + transaction_creator = TransactionCreator() + txtype8 = transaction_creator.transaction_type_8( + sc_state_proposal1_data) + trxn.append(txtype8) return trxn @@ -205,6 +298,8 @@ def unpledge_tokens(self, callparamsip, repo: FetchRepository): wallet_address = callparams["borrower_wallet"] lender = callparams["lender"] tokens = callparams["tokens"] + signed_wallets = callparams['function_caller'] + cspecs = input_to_dict(self.contractparams['contractspecs']) for i in tokens: qparam = {"borrower": wallet_address, @@ -221,7 +316,7 @@ def unpledge_tokens(self, callparamsip, repo: FetchRepository): raise ContractValidationError( "No pledge record found ofr tokencode %s" % i["token_code"]) - data = repo.select_Query("id,amount").add_table_name("pledge_ledger").where_clause("borrower", + data = repo.select_Query("id,amount,status,borrower,lender").add_table_name("pledge_ledger").where_clause("borrower", wallet_address, 1).and_clause("address", self.address, 1).and_clause( @@ -230,6 +325,38 @@ def unpledge_tokens(self, callparamsip, repo: FetchRepository): if i["amount"] > data[1]: raise ContractValidationError( "token amount for token code %s is greater than pledged amount" % i["token_code"]) + + borrower_address = data[3] + lender_address = data[4] + custodian_address = cspecs['custodian_address'] + + # if data[2] == 10: + # #check for borrower sig + # if len(signed_wallets) != 2: + # raise ContractValidationError("Total three signatures are required for pledge finalise") + # for i in signed_wallets: + # if i["wallet_address"] == custodian_address: + # custodian_present = True + # if i["wallet_address"] == borrower_address: + # borrower_present = True + # if not borrower_present and custodian_present: + # raise ContractValidationError("Wrong sigantures provided") + # pass + # elif data[2] == 11: + # #check for borrower, lender sig + # if len(signed_wallets) != 3: + # raise ContractValidationError("Total three signatures are required for pledge finalise") + # for i in signed_wallets: + # if i["wallet_address"] == custodian_address: + # custodian_present = True + # if i["wallet_address"] == borrower_address: + # borrower_present = True + # if i["wallet_address"] == lender_address: + # lender_present = True + # if not borrower_present and lender_present and custodian_present: + # raise ContractValidationError("Wrong sigantures provided") + + transfer_proposal_data = { "transfer_type": 1, "asset1_code": i["token_code"], @@ -250,14 +377,15 @@ def unpledge_tokens(self, callparamsip, repo: FetchRepository): "data": { "amount": int(math.floor(data[1] - i["amount"])), "time_updated": get_corrected_time_ms(), + "status": 2 }, "unique_column": "id", "unique_value": data[0] } transaction_creator = TransactionCreator() - txtype1 = transaction_creator.transaction_type_8( + txtype8 = transaction_creator.transaction_type_8( sc_state_proposal1_data) - trxn.append(txtype1) + trxn.append(txtype8) return trxn def default_tokens(self, callparamsip, repo: FetchRepository): @@ -266,6 +394,9 @@ def default_tokens(self, callparamsip, repo: FetchRepository): wallet_address = callparams["borrower_wallet"] lender = callparams["lender"] token_code = callparams["token_code"] + signed_wallets = callparams['function_caller'] + cspecs = input_to_dict(self.contractparams['contractspecs']) + qparam = {"borrower": wallet_address, "address": self.address, "lender": lender, @@ -280,12 +411,25 @@ def default_tokens(self, callparamsip, repo: FetchRepository): raise ContractValidationError( "No pledge record found ofr tokencode %s" % token_code) - data = repo.select_Query("id,amount").add_table_name("pledge_ledger").where_clause("borrower", + data = repo.select_Query("id,amount,lender").add_table_name("pledge_ledger").where_clause("borrower", wallet_address, 1).and_clause("address", self.address, 1).and_clause( "lender", lender, 1).and_clause("token_code", token_code, 1).execute_query_single_result( qparam) + + # lender_address = data[2] + # custodian_address = cspecs['custodian_address'] + # if len(signed_wallets) != 2: + # raise ContractValidationError("Total two signatures are required for pledge finalise") + # for i in signed_wallets: + # if i["wallet_address"] == custodian_address: + # custodian_present = True + # if i["wallet_address"] == lender_address: + # lender_present = True + # if not lender_present and custodian_present: + # raise ContractValidationError("Wrong sigantures provided") + transfer_proposal_data = { "transfer_type": 1, "asset1_code": token_code, @@ -296,6 +440,8 @@ def default_tokens(self, callparamsip, repo: FetchRepository): "asset2_number": 0, "additional_data": {} } + + transaction_creator = TransactionCreator() trxn.append(transaction_creator.transaction_type_5( transfer_proposal_data)) diff --git a/app/migrations/init_db.py b/app/migrations/init_db.py index bf6f3d4a..a29f1985 100644 --- a/app/migrations/init_db.py +++ b/app/migrations/init_db.py @@ -308,8 +308,7 @@ def init_db(): token_code text not NULL, time_updated TIMESTAMP, status INT, - unique_column text not NULL , - pledge_status text, + unique_column text not NULL ) ''') cur.execute(''' diff --git a/app/migrations/migrations/0011_pledge_update.py b/app/migrations/migrations/0011_pledge_update.py deleted file mode 100644 index 2b4502fd..00000000 --- a/app/migrations/migrations/0011_pledge_update.py +++ /dev/null @@ -1,34 +0,0 @@ -import json -import sqlite3 - -from app.core.helpers.utils import get_person_id_for_wallet_address -from app.config.constants import NEWRL_DB -from app.config.nvalues import CUSTODIAN_DAO_ADDRESS, CUSTODIAN_WALLET_LIST, DAO_MANAGER, ASQI_WALLET, FOUNDATION_WALLET, ASQI_DAO_ADDRESS, \ - ASQI_WALLET_DAO, FOUNDATION_WALLET_DAO, FOUNDATION_DAO_ADDRESS, MEMBER_WALLET_LIST - - -def migrate(): - print("Updating pledge status") - add_pledge_status_main() - - -def add_pledge_status_main(): - """Update Custodian DAO.""" - con = sqlite3.connect(NEWRL_DB) - cur = con.cursor() - - updates_contract_specs = '{"dao_wallet_address":"0xce4b9b89efa5ee6c34655c8198c09494dc3d95bb","max_members":999999999,"max_voting_time":300000,"signatories":{"vote_on_proposal":null,"delete_member":[-1],"create_proposal":null,"add_member":[-1],"invest":[-1],"payout":[-1],"initialize_membership":null},"voting_schemes":[{"function":"add_member","voting_scheme":"voting_scheme_one","params":{"min_yes_votes":50},"veto_available":true,"veto_addresses":["0xf98eafede44ae6db2f6e6ad3762f5419ff1196d9","0x9dd356a2e4aa9a6c182d5f1e3f2e40ffa27bcfd5","0x47538e46a78e729079eb1614e2d6c387119c21fa","0x1342e0ae1664734cbbe522030c7399d6003a07a8","0x495c8153f65cf402bb0af6f93ba1eed4ace9aa7f","0x52c3a0758644133fbbf85377244a35d932443bf0","0x5017b00ced00b8b51d77d4569fa9d611b5b3b77a"]},{"function":"delete_member","voting_scheme":"voting_scheme_one","params":{"min_yes_votes":50},"veto_available":true,"veto_addresses":["0xf98eafede44ae6db2f6e6ad3762f5419ff1196d9","0x9dd356a2e4aa9a6c182d5f1e3f2e40ffa27bcfd5","0x47538e46a78e729079eb1614e2d6c387119c21fa","0x1342e0ae1664734cbbe522030c7399d6003a07a8","0x495c8153f65cf402bb0af6f93ba1eed4ace9aa7f","0x52c3a0758644133fbbf85377244a35d932443bf0","0x5017b00ced00b8b51d77d4569fa9d611b5b3b77a"]}]}' - add_pledge_status(cur, updates_contract_specs, CUSTODIAN_DAO_ADDRESS) - - con.commit() - con.close() - - -def add_pledge_status(cur, contract_specs,address): - - try: - cur.execute(f'''ALTER TABLE pledge_ledger ADD status TEXT''') - except Exception as e: - print('Error adding status to pledge', str(e)) - - pass diff --git a/tests/test_pledge.py b/tests/test_pledge.py index 04ebcf7d..7bd21e24 100644 --- a/tests/test_pledge.py +++ b/tests/test_pledge.py @@ -135,10 +135,11 @@ def test_create_pledge_contract(request): "setup": WALLET['address'], "deploy": WALLET['address'], "create": WALLET['address'], + "pledge_tokens": None, "pledge_request": None, "pledge_finalise": None, - "unpledge_tokens": WALLET['address'], - "default_tokens": WALLET['address'] + "unpledge_tokens": None, + "default_tokens":None }, "contractspecs": { "custodian_address": WALLET['address'] @@ -146,7 +147,7 @@ def test_create_pledge_contract(request): "legalparams": {} } - response = requests.post(NODE_URL+'/add-sc', json= request_create ) + response = requests.post(NODE_URL+'/add-sc', json= request_create ) print(response.text) assert response.status_code == 200 @@ -276,7 +277,7 @@ def test_create_pledge_Req(request): request.config.cache.set('borrower_wallet', borrower_wallet) request.config.cache.set('lender_wallet', lender_wallet) -def test_accept_pledge_Req(request): +def test_finalise_pledge_Req(request): pledge_address = request.config.cache.get('pledge_address', None) borrower_wallet = request.config.cache.get('borrower_wallet', None) lender_wallet = request.config.cache.get('lender_wallet', None) @@ -293,7 +294,10 @@ def test_accept_pledge_Req(request): "params": { "borrower_wallet": borrower_wallet['address'], "token_code":"NWRL", - "lender": lender_wallet['address'] + "lender": lender_wallet['address'], + "tokens":[{ + "token_code":"NWRL" + }] } } response = requests.post(NODE_URL+'/call-sc', json=req_json) @@ -362,281 +366,157 @@ def test_accept_pledge_Req(request): response_val = response.json() assert response_val["data"] is not None assert len(response_val["data"]) > 0 + assert response_val["data"][7] == 11 - request.config.cache.set('borrower_wallet', borrower_wallet) - request.config.cache.set('lender_wallet', lender_wallet) +def test_unpledge(request): + pledge_address = request.config.cache.get('pledge_address', None) + borrower_wallet = request.config.cache.get('borrower_wallet', None) + lender_wallet = request.config.cache.get('lender_wallet', None) + + + req_json = { + "sc_address": pledge_address, + "function_called": "unpledge_tokens", + "signers": [ + WALLET['address'] + ], + "params": { + "borrower_wallet": borrower_wallet['address'], + "token_code":"NWRL", + "lender": lender_wallet['address'], + "tokens":[{ + "token_code":"NWRL", + "amount":1000000 + }] + } + } + response = requests.post(NODE_URL+'/call-sc', json=req_json) + + assert response.status_code == 200 + unsigned_transaction = response.json() + assert unsigned_transaction['transaction'] + assert len(unsigned_transaction['signatures']) == 0 + unsigned_transaction['transaction']['fee'] = 1000000 + + signed_transaction_response = requests.post(NODE_URL+'/sign-transaction', json={ + "wallet_data":WALLET , + "transaction_data": unsigned_transaction + }) + + assert signed_transaction_response.status_code == 200 + signed_transaction = signed_transaction_response.json() + assert signed_transaction['transaction'] + assert signed_transaction['signatures'] + assert len(signed_transaction['signatures']) == 1 + + # req_sign = { + # "wallet_data":lender_wallet, + # "transaction_data": signed_transaction + # } + # signed_transaction_response = requests.post(NODE_URL+'/sign-transaction', json= req_sign) + + # assert signed_transaction_response.status_code == 200 + # signed_transaction = signed_transaction_response.json() + # assert signed_transaction['transaction'] + # assert signed_transaction['signatures'] + # assert len(signed_transaction['signatures']) == 2 + + response = requests.post( + NODE_URL+'/validate-transaction', json=signed_transaction) + assert response.status_code == 200 + assert response.json()['response']['valid'] + if TEST_ENV == 'local': + response = requests.post( + NODE_URL + '/run-updater?add_to_chain_before_consensus=true') + else: + print('Waiting to mine block') + time.sleep(BLOCK_WAIT_TIME) + + + params = { + 'table_name': "pledge_ledger", + 'contract_address': pledge_address, + 'unique_column': 'borrower', + 'unique_value': borrower_wallet['address'] + } + response = requests.get(NODE_URL+"/sc-state", params=params) + assert response.status_code == 200 + response_val = response.json() + assert response_val["data"] is not None + assert len(response_val["data"]) > 0 + assert response_val["data"][7] == 2 + +def test_default(request): + pledge_address = request.config.cache.get('pledge_address', None) + borrower_wallet = request.config.cache.get('borrower_wallet', None) + lender_wallet = request.config.cache.get('lender_wallet', None) + + + req_json = { + "sc_address": pledge_address, + "function_called": "default_tokens", + "signers": [ + WALLET['address'] + ], + "params": { + "borrower_wallet": borrower_wallet['address'], + "token_code":"NWRL", + "lender": lender_wallet['address'] + } + } + response = requests.post(NODE_URL+'/call-sc', json=req_json) + + assert response.status_code == 200 + unsigned_transaction = response.json() + assert unsigned_transaction['transaction'] + assert len(unsigned_transaction['signatures']) == 0 + unsigned_transaction['transaction']['fee'] = 1000000 -# def test_add_alias_duplicate_fail(request): -# alias_address = request.config.cache.get('alias_address', None) -# wallet = create_wallet() -# fund_wallet_nwrl(WALLET,wallet['address'],9000000) -# alias = "alias_"+ str(random.randrange(111111, 999999, 5)) -# req_json = { -# "sc_address": alias_address, -# "function_called": "add_alias", -# "signers": [ -# wallet['address'] -# ], -# "params": { -# "alias": alias -# } -# } -# response = requests.post(NODE_URL+'/call-sc', json=req_json) - -# assert response.status_code == 200 -# unsigned_transaction = response.json() -# assert unsigned_transaction['transaction'] -# assert len(unsigned_transaction['signatures']) == 0 -# unsigned_transaction['transaction']['fee'] = 1000000 - -# response = requests.post(NODE_URL+'/sign-transaction', json={ -# "wallet_data": wallet, -# "transaction_data": unsigned_transaction -# }) - -# assert response.status_code == 200 -# signed_transaction = response.json() -# assert signed_transaction['transaction'] -# assert signed_transaction['signatures'] -# assert len(signed_transaction['signatures']) == 1 - -# response = requests.post( -# NODE_URL+'/validate-transaction', json=signed_transaction) -# assert response.status_code == 200 -# assert response.json()['response']['valid'] -# if TEST_ENV == 'local': -# response = requests.post( -# NODE_URL + '/run-updater?add_to_chain_before_consensus=true') -# else: -# print('Waiting to mine block') -# time.sleep(BLOCK_WAIT_TIME) - - -# params = { -# 'table_name': "alias", -# 'contract_address': alias_address, -# } -# response = requests.get(NODE_URL+"/sc-states", params=params) -# assert response.status_code == 200 -# response_val = response.json() -# assert response_val["data"] is not None -# assert len(response_val["data"]) > 0 -# assert response_val["data"][1][0] == alias -# time.sleep(2) - -# #duplicate entry -# req_json = { -# "sc_address": alias_address, -# "function_called": "add_alias", -# "signers": [ -# wallet['address'] -# ], -# "params": { -# "alias": alias -# } -# } -# response = requests.post(NODE_URL+'/call-sc', json=req_json) - -# assert response.status_code == 200 -# unsigned_transaction = response.json() -# assert unsigned_transaction['transaction'] -# assert len(unsigned_transaction['signatures']) == 0 -# unsigned_transaction['transaction']['fee'] = 1000000 - -# response = requests.post(NODE_URL+'/sign-transaction', json={ -# "wallet_data": wallet, -# "transaction_data": unsigned_transaction -# }) - -# assert response.status_code == 200 -# signed_transaction = response.json() -# assert signed_transaction['transaction'] -# assert signed_transaction['signatures'] -# assert len(signed_transaction['signatures']) == 1 - -# response = requests.post( -# NODE_URL+'/validate-transaction', json=signed_transaction) -# assert response.status_code == 200 -# assert not response.json()['response']['valid'] - -# def test_update_alias(request): -# alias_address = request.config.cache.get('alias_address', None) -# wallet = request.config.cache.get('wallet1', None) -# alias1 = request.config.cache.get('alias1', None) -# alias_updated = alias1+"_updated" -# req_json = { -# "sc_address": alias_address, -# "function_called": "update_alias", -# "signers": [ -# wallet['address'] -# ], -# "params": { -# "alias": alias_updated -# } -# } -# response = requests.post(NODE_URL+'/call-sc', json=req_json) - -# assert response.status_code == 200 -# unsigned_transaction = response.json() -# assert unsigned_transaction['transaction'] -# assert len(unsigned_transaction['signatures']) == 0 -# unsigned_transaction['transaction']['fee'] = 1000000 - -# response = requests.post(NODE_URL+'/sign-transaction', json={ -# "wallet_data": wallet, -# "transaction_data": unsigned_transaction -# }) - -# assert response.status_code == 200 -# signed_transaction = response.json() -# assert signed_transaction['transaction'] -# assert signed_transaction['signatures'] -# assert len(signed_transaction['signatures']) == 1 - -# response = requests.post( -# NODE_URL+'/validate-transaction', json=signed_transaction) -# assert response.status_code == 200 -# assert response.json()['response']['valid'] -# if TEST_ENV == 'local': -# response = requests.post( -# NODE_URL + '/run-updater?add_to_chain_before_consensus=true') -# else: -# print('Waiting to mine block') -# time.sleep(BLOCK_WAIT_TIME) - - -# params = { -# 'table_name': "alias", -# 'contract_address': alias_address, -# } -# response = requests.get(NODE_URL+"/sc-states", params=params) -# assert response.status_code == 200 -# response_val = response.json() -# assert response_val["data"] is not None -# assert len(response_val["data"]) > 0 -# assert response_val["data"][0][0] == alias_updated -# request.config.cache.set('alias1', alias_updated) - - -# def test_update_alias_fail(request): -# alias_address = request.config.cache.get('alias_address', None) -# wallet = request.config.cache.get('wallet1', None) -# alias1 = request.config.cache.get('alias1', None) -# wallet2 = create_wallet() -# fund_wallet_nwrl(WALLET,wallet2['address'], 5000000) -# req_json = { -# "sc_address": alias_address, -# "function_called": "update_alias", -# "signers": [ -# wallet2['address'] -# ], -# "params": { -# "alias": alias1 -# } -# } -# response = requests.post(NODE_URL+'/call-sc', json=req_json) - -# assert response.status_code == 200 -# unsigned_transaction = response.json() -# assert unsigned_transaction['transaction'] -# assert len(unsigned_transaction['signatures']) == 0 -# unsigned_transaction['transaction']['fee'] = 1000000 - -# response = requests.post(NODE_URL+'/sign-transaction', json={ -# "wallet_data": wallet2, -# "transaction_data": unsigned_transaction -# }) - -# assert response.status_code == 200 -# signed_transaction = response.json() -# assert signed_transaction['transaction'] -# assert signed_transaction['signatures'] -# assert len(signed_transaction['signatures']) == 1 - -# response = requests.post( -# NODE_URL+'/validate-transaction', json=signed_transaction) -# assert response.status_code == 200 -# assert not response.json()['response']['valid'] - -# def test_update_alias_fail(request): -# alias_address = request.config.cache.get('alias_address', None) -# wallet = request.config.cache.get('wallet1', None) -# alias1 = request.config.cache.get('alias1', None) -# wallet2 = create_wallet() -# fund_wallet_nwrl(WALLET,wallet2['address'], 5000000) -# req_json = { -# "sc_address": alias_address, -# "function_called": "update_alias", -# "signers": [ -# wallet2['address'] -# ], -# "params": { -# "alias": alias1 -# } -# } -# response = requests.post(NODE_URL+'/call-sc', json=req_json) - -# assert response.status_code == 200 -# unsigned_transaction = response.json() -# assert unsigned_transaction['transaction'] -# assert len(unsigned_transaction['signatures']) == 0 -# unsigned_transaction['transaction']['fee'] = 1000000 - -# response = requests.post(NODE_URL+'/sign-transaction', json={ -# "wallet_data": wallet2, -# "transaction_data": unsigned_transaction -# }) - -# assert response.status_code == 200 -# signed_transaction = response.json() -# assert signed_transaction['transaction'] -# assert signed_transaction['signatures'] -# assert len(signed_transaction['signatures']) == 1 - -# response = requests.post( -# NODE_URL+'/validate-transaction', json=signed_transaction) -# assert response.status_code == 200 -# assert not response.json()['response']['valid'] - - - -# def test_update_alias_duplicate_fail(request): -# alias_address = request.config.cache.get('alias_address', None) -# wallet = request.config.cache.get('wallet1', None) -# alias1 = request.config.cache.get('alias1', None) - -# #duplicate entry -# req_json = { -# "sc_address": alias_address, -# "function_called": "update_alias", -# "signers": [ -# wallet['address'] -# ], -# "params": { -# "alias": alias1 -# } -# } -# response = requests.post(NODE_URL+'/call-sc', json=req_json) - -# assert response.status_code == 200 -# unsigned_transaction = response.json() -# assert unsigned_transaction['transaction'] -# assert len(unsigned_transaction['signatures']) == 0 -# unsigned_transaction['transaction']['fee'] = 1000000 - -# response = requests.post(NODE_URL+'/sign-transaction', json={ -# "wallet_data": wallet, -# "transaction_data": unsigned_transaction -# }) - -# assert response.status_code == 200 -# signed_transaction = response.json() -# assert signed_transaction['transaction'] -# assert signed_transaction['signatures'] -# assert len(signed_transaction['signatures']) == 1 - -# response = requests.post( -# NODE_URL+'/validate-transaction', json=signed_transaction) -# assert response.status_code == 200 -# assert not response.json()['response']['valid'] + signed_transaction_response = requests.post(NODE_URL+'/sign-transaction', json={ + "wallet_data":WALLET , + "transaction_data": unsigned_transaction + }) + + assert signed_transaction_response.status_code == 200 + signed_transaction = signed_transaction_response.json() + assert signed_transaction['transaction'] + assert signed_transaction['signatures'] + assert len(signed_transaction['signatures']) == 1 + + # req_sign = { + # "wallet_data":lender_wallet, + # "transaction_data": signed_transaction + # } + # signed_transaction_response = requests.post(NODE_URL+'/sign-transaction', json= req_sign) + + # assert signed_transaction_response.status_code == 200 + # signed_transaction = signed_transaction_response.json() + # assert signed_transaction['transaction'] + # assert signed_transaction['signatures'] + # assert len(signed_transaction['signatures']) == 2 + + response = requests.post( + NODE_URL+'/validate-transaction', json=signed_transaction) + assert response.status_code == 200 + assert response.json()['response']['valid'] + if TEST_ENV == 'local': + response = requests.post( + NODE_URL + '/run-updater?add_to_chain_before_consensus=true') + else: + print('Waiting to mine block') + time.sleep(BLOCK_WAIT_TIME) + + + params = { + 'table_name': "pledge_ledger", + 'contract_address': pledge_address, + 'unique_column': 'borrower', + 'unique_value': borrower_wallet['address'] + } + response = requests.get(NODE_URL+"/sc-state", params=params) + assert response.status_code == 200 + response_val = response.json() + assert response_val["data"] is not None + assert len(response_val["data"]) > 0 + assert response_val["data"][7] == -1