Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
260 changes: 203 additions & 57 deletions app/core/contracts/PledgingContract.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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,
}
}
Expand Down Expand Up @@ -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

Expand All @@ -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,
Expand All @@ -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(
Expand All @@ -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"],
Expand All @@ -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):
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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))
Expand Down
3 changes: 1 addition & 2 deletions app/migrations/init_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -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('''
Expand Down
Loading