Checkpoint : completed logic for smart contract transfers
This commit is contained in:
parent
2e94fc7a51
commit
44134365e5
@ -585,6 +585,7 @@ def processTransaction(transaction_data, parsed_data):
|
|||||||
# todo Rule 46 - If the transfer type is smart contract, then call the function transferToken to do sanity checks & lock the balance
|
# todo Rule 46 - If the transfer type is smart contract, then call the function transferToken to do sanity checks & lock the balance
|
||||||
elif parsed_data['transferType'] == 'smartContract':
|
elif parsed_data['transferType'] == 'smartContract':
|
||||||
if os.path.isfile(f"./smartContracts/{parsed_data['contractName']}-{parsed_data['contractAddress']}.db"):
|
if os.path.isfile(f"./smartContracts/{parsed_data['contractName']}-{parsed_data['contractAddress']}.db"):
|
||||||
|
|
||||||
# Check if the transaction hash already exists in the contract db (Safety check)
|
# Check if the transaction hash already exists in the contract db (Safety check)
|
||||||
engine = create_engine(
|
engine = create_engine(
|
||||||
'sqlite:///smartContracts/{}-{}.db'.format(parsed_data['contractName'], outputlist[0]), echo=True)
|
'sqlite:///smartContracts/{}-{}.db'.format(parsed_data['contractName'], outputlist[0]), echo=True)
|
||||||
@ -600,7 +601,7 @@ def processTransaction(transaction_data, parsed_data):
|
|||||||
f"Error | Transaction {transaction_data['txid']} rejected as it already exists in the Smart Contract db. This is unusual, please check your code" )
|
f"Error | Transaction {transaction_data['txid']} rejected as it already exists in the Smart Contract db. This is unusual, please check your code" )
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
#if contractAddress was passed check if it matches the output address of this contract
|
# if contractAddress was passed, then check if it matches the output address of this contract
|
||||||
if 'contractAddress' in parsed_data:
|
if 'contractAddress' in parsed_data:
|
||||||
if parsed_data['contractAddress'] != outputlist[0]:
|
if parsed_data['contractAddress'] != outputlist[0]:
|
||||||
logger.info(
|
logger.info(
|
||||||
@ -638,7 +639,6 @@ def processTransaction(transaction_data, parsed_data):
|
|||||||
pushData_SSEapi('Error| Mismatch in contract address specified in flodata and the output address of the transaction {}'.format(transaction_data['txid']))
|
pushData_SSEapi('Error| Mismatch in contract address specified in flodata and the output address of the transaction {}'.format(transaction_data['txid']))
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
|
|
||||||
# check the status of the contract
|
# check the status of the contract
|
||||||
engine = create_engine('sqlite:///system.db', echo=True)
|
engine = create_engine('sqlite:///system.db', echo=True)
|
||||||
connection = engine.connect()
|
connection = engine.connect()
|
||||||
@ -677,7 +677,6 @@ def processTransaction(transaction_data, parsed_data):
|
|||||||
'message': f"Error | Transaction {parsed_data['txid']} closed as Smart contract {parsed_data['contractName']} at the {outputlist[0]} is closed"},
|
'message': f"Error | Transaction {parsed_data['txid']} closed as Smart contract {parsed_data['contractName']} at the {outputlist[0]} is closed"},
|
||||||
headers=headers)
|
headers=headers)
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
else:
|
else:
|
||||||
engine = create_engine(
|
engine = create_engine(
|
||||||
'sqlite:///smartContracts/{}-{}.db'.format(parsed_data['contractName'], outputlist[0]),
|
'sqlite:///smartContracts/{}-{}.db'.format(parsed_data['contractName'], outputlist[0]),
|
||||||
@ -721,210 +720,431 @@ def processTransaction(transaction_data, parsed_data):
|
|||||||
session.commit()
|
session.commit()
|
||||||
session.close()
|
session.close()
|
||||||
pushData_SSEapi(
|
pushData_SSEapi(
|
||||||
f"Error| Transaction {parsed_data['txid']} rejected as Smart contract {parsed_data['contractName']}-{outputlist[0]} has expired and will not accept any user participation"
|
f"Error| Transaction {parsed_data['txid']} rejected as Smart contract {parsed_data['contractName']}-{outputlist[0]} has expired and will not accept any user participation")
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
|
# pull out the contract structure into a dictionary
|
||||||
# Check if contractAmount is part of the contract structure, and enforce it if it is
|
|
||||||
engine = create_engine('sqlite:///smartContracts/{}-{}.db'.format(parsed_data['contractName'], outputlist[0]), echo=True)
|
engine = create_engine('sqlite:///smartContracts/{}-{}.db'.format(parsed_data['contractName'], outputlist[0]), echo=True)
|
||||||
connection = engine.connect()
|
connection = engine.connect()
|
||||||
contractAmount = connection.execute(
|
attributevaluepair = connection.execute("select attribute, value from contractstructure where attribute != 'contractName' and attribute != 'flodata' and attribute != 'contractAddress'").fetchall()
|
||||||
'select value from contractstructure where attribute=="contractAmount"').fetchall()
|
contractStructure = {}
|
||||||
connection.close()
|
conditionDict = {}
|
||||||
|
counter = 0
|
||||||
if len(contractAmount) != 0:
|
for item in attributevaluepair:
|
||||||
if float(contractAmount[0][0]) != float(parsed_data['tokenAmount']):
|
if list(item)[0] == 'exitconditions':
|
||||||
logger.info(
|
conditionDict[counter] = list(item)[1]
|
||||||
f"Transaction {parsed_data['txid']} rejected as contractAmount being transferred is not part of the structure of Smart Contract named {parsed_data['contractName']} at the address {outputlist[0]}")
|
counter = counter + 1
|
||||||
# Store transfer as part of RejectedContractTransactionHistory
|
|
||||||
engine = create_engine(
|
|
||||||
f"sqlite:///smartContracts/{parsed_data['contractName']}-{outputlist[0]}.db",
|
|
||||||
echo=True)
|
|
||||||
ContractBase.metadata.create_all(bind=engine)
|
|
||||||
session = sessionmaker(bind=engine)()
|
|
||||||
blockchainReference = neturl + 'tx/' + transaction_data['txid']
|
|
||||||
session.add(
|
|
||||||
RejectedContractTransactionHistory(transactionType='participation',
|
|
||||||
sourceFloAddress=inputadd,
|
|
||||||
destFloAddress=outputlist[0],
|
|
||||||
transferAmount=None,
|
|
||||||
blockNumber=transaction_data['blockheight'],
|
|
||||||
blockHash=transaction_data['blockhash'],
|
|
||||||
time=transaction_data['blocktime'],
|
|
||||||
transactionHash=transaction_data['txid'],
|
|
||||||
blockchainReference=blockchainReference,
|
|
||||||
jsonData=json.dumps(transaction_data),
|
|
||||||
rejectComment=f"Transaction {parsed_data['txid']} rejected as contractAmount being transferred is not part of the structure of Smart Contract named {parsed_data['contractName']} at the address {outputlist[0]}"))
|
|
||||||
session.commit()
|
|
||||||
session.close()
|
|
||||||
pushData_SSEapi(
|
|
||||||
f"Error| Transaction {parsed_data['txid']} rejected as contractAmount being transferred is not part of the structure of Smart Contract named {parsed_data['contractName']} at the address {outputlist[0]}")
|
|
||||||
return 0
|
|
||||||
|
|
||||||
|
|
||||||
# Check if exitcondition exists as part of contractstructure and is given in right format
|
|
||||||
engine = create_engine('sqlite:///smartContracts/{}-{}.db'.format(parsed_data['contractName'], outputlist[0]), echo=True)
|
|
||||||
connection = engine.connect()
|
|
||||||
|
|
||||||
contractAttributes = connection.execute('select attribute, value from contractstructure').fetchall()
|
|
||||||
contractAttributes_T = list(zip(*contractAttributes))
|
|
||||||
|
|
||||||
if 'exitconditions' in contractAttributes_T[0]:
|
|
||||||
exitconditions = connection.execute('select id,value from contractstructure where attribute=="exitconditions"').fetchall()
|
|
||||||
exitconditions_T = list(zip(*exitconditions))
|
|
||||||
if parsed_data['userChoice'] not in list(exitconditions_T[1]):
|
|
||||||
logger.info(f"Transaction {parsed_data['txid']} rejected as wrong userchoice entered for the Smart Contract named {parsed_data['contractName']} at the address {outputlist[0]}")
|
|
||||||
# Store transfer as part of RejectedContractTransactionHistory
|
|
||||||
engine = create_engine(
|
|
||||||
f"sqlite:///smartContracts/{parsed_data['contractName']}-{outputlist[0]}.db",
|
|
||||||
echo=True)
|
|
||||||
ContractBase.metadata.create_all(bind=engine)
|
|
||||||
session = sessionmaker(bind=engine)()
|
|
||||||
blockchainReference = neturl + 'tx/' + transaction_data['txid']
|
|
||||||
session.add(
|
|
||||||
RejectedContractTransactionHistory(transactionType='participation',
|
|
||||||
sourceFloAddress=inputadd,
|
|
||||||
destFloAddress=outputlist[0],
|
|
||||||
transferAmount=None,
|
|
||||||
blockNumber=transaction_data['blockheight'],
|
|
||||||
blockHash=transaction_data['blockhash'],
|
|
||||||
time=transaction_data['blocktime'],
|
|
||||||
transactionHash=transaction_data['txid'],
|
|
||||||
blockchainReference=blockchainReference,
|
|
||||||
jsonData=json.dumps(transaction_data),
|
|
||||||
rejectComment=f"Transaction {parsed_data['txid']} rejected as wrong userchoice entered for the Smart Contract named {parsed_data['contractName']} at the address {outputlist[0]}"))
|
|
||||||
session.commit()
|
|
||||||
session.close()
|
|
||||||
pushData_SSEapi(f"Error| Transaction {parsed_data['txid']} rejected as wrong userchoice entered for the Smart Contract named {parsed_data['contractName']} at the address {outputlist[0]}")
|
|
||||||
return 0
|
|
||||||
|
|
||||||
|
|
||||||
# Check if maximum subscription amount has reached
|
|
||||||
engine = create_engine('sqlite:///smartContracts/{}-{}.db'.format(parsed_data['contractName'], outputlist[0]), echo=True)
|
|
||||||
ContractBase.metadata.create_all(bind=engine)
|
|
||||||
session = sessionmaker(bind=engine)()
|
|
||||||
result = session.query(ContractStructure).filter_by(attribute='maximumsubscriptionamount').all()
|
|
||||||
if result:
|
|
||||||
# now parse the expiry time in python
|
|
||||||
maximumsubscriptionamount = float(result[0].value.strip())
|
|
||||||
amountDeposited = session.query(func.sum(ContractParticipants.tokenAmount)).all()[0][0]
|
|
||||||
|
|
||||||
if amountDeposited is None:
|
|
||||||
amountDeposited = 0
|
|
||||||
|
|
||||||
if amountDeposited >= maximumsubscriptionamount:
|
|
||||||
logger.info("Maximum subscription amount reached\n Money will be refunded")
|
|
||||||
pushData_SSEapi('Error | Maximum subscription amount reached for contract {}-{} at transaction {}. Token will not be transferred'.format(parsed_data['contractName'], outputlist[0],
|
|
||||||
transaction_data['txid']))
|
|
||||||
return 0
|
|
||||||
else:
|
else:
|
||||||
if parsed_data['tokenAmount'] + amountDeposited <= maximumsubscriptionamount:
|
contractStructure[list(item)[0]] = list(item)[1]
|
||||||
# Check if the tokenAmount being transferred exists in the address & do the token transfer
|
if len(conditionDict) > 0:
|
||||||
returnval = transferToken(parsed_data['tokenIdentification'], parsed_data['tokenAmount'], inputlist[0], outputlist[0], transaction_data)
|
contractStructure['exitconditions'] = conditionDict
|
||||||
if returnval is not None:
|
del counter, conditionDict
|
||||||
# Store participant details in the smart contract's db
|
|
||||||
session.add(ContractParticipants(participantAddress=inputadd, tokenAmount=parsed_data['tokenAmount'], userChoice=parsed_data['userChoice'], transactionHash=transaction_data['txid'], blockNumber=transaction_data['blockheight'], blockHash=transaction_data['blockhash']))
|
|
||||||
session.commit()
|
|
||||||
|
|
||||||
# Store transfer as part of ContractTransactionHistory
|
# check if user choice has been passed, to the wrong contract type
|
||||||
blockchainReference = neturl + 'tx/' + transaction_data['txid']
|
if 'userChoice' in parsed_data and 'exitconditions' not in contractStructure:
|
||||||
session.add(ContractTransactionHistory(transactionType='participation', sourceFloAddress=inputadd, destFloAddress=outputlist[0],
|
logger.info(
|
||||||
transferAmount=parsed_data['tokenAmount'],
|
f"Transaction {parsed_data['txid']} rejected as userChoice, {parsed_data['userChoice']}, has been passed to Smart Contract named {parsed_data['contractName']} at the address {outputlist[0]} which doesn't accept any userChoice")
|
||||||
blockNumber=transaction_data['blockheight'],
|
# Store transfer as part of RejectedContractTransactionHistory
|
||||||
blockHash=transaction_data['blockhash'],
|
engine = create_engine(
|
||||||
time=transaction_data['blocktime'],
|
f"sqlite:///smartContracts/{parsed_data['contractName']}-{outputlist[0]}.db",
|
||||||
transactionHash=transaction_data['txid'],
|
echo=True)
|
||||||
blockchainReference=blockchainReference,
|
ContractBase.metadata.create_all(bind=engine)
|
||||||
jsonData=json.dumps(transaction_data)))
|
|
||||||
|
|
||||||
session.commit()
|
|
||||||
session.close()
|
|
||||||
|
|
||||||
# Store a mapping of participant address -> Contract participated in
|
|
||||||
engine = create_engine('sqlite:///system.db', echo=True)
|
|
||||||
SystemBase.metadata.create_all(bind=engine)
|
|
||||||
session = sessionmaker(bind=engine)()
|
|
||||||
session.add(ContractAddressMapping(address=inputadd, addressType='participant', tokenAmount=parsed_data['tokenAmount'],
|
|
||||||
contractName = parsed_data['contractName'], contractAddress = outputlist[0], transactionHash=transaction_data['txid'], blockNumber=transaction_data['blockheight'], blockHash=transaction_data['blockhash']))
|
|
||||||
session.commit()
|
|
||||||
|
|
||||||
updateLatestTransaction(transaction_data, parsed_data)
|
|
||||||
return
|
|
||||||
|
|
||||||
else:
|
|
||||||
logger.info("Something went wrong in the smartcontract token transfer method")
|
|
||||||
return 0
|
|
||||||
else:
|
|
||||||
# Transfer only part of the tokens users specified, till the time it reaches maximumamount
|
|
||||||
returnval = transferToken(parsed_data['tokenIdentification'], maximumsubscriptionamount-amountDeposited,
|
|
||||||
inputlist[0], outputlist[0], transaction_data)
|
|
||||||
if returnval is not None:
|
|
||||||
# Store participant details in the smart contract's db
|
|
||||||
session.add(ContractParticipants(participantAddress=inputadd,
|
|
||||||
tokenAmount=maximumsubscriptionamount-amountDeposited,
|
|
||||||
userChoice=parsed_data['userChoice'], transactionHash=transaction_data['txid'], blockNumber=transaction_data['blockheight'],
|
|
||||||
blockHash=transaction_data['blockhash']))
|
|
||||||
session.commit()
|
|
||||||
session.close()
|
|
||||||
|
|
||||||
# Store a mapping of participant address -> Contract participated in
|
|
||||||
engine = create_engine('sqlite:///system.db', echo=True)
|
|
||||||
SystemBase.metadata.create_all(bind=engine)
|
|
||||||
session = sessionmaker(bind=engine)()
|
|
||||||
session.add(ContractAddressMapping(address=inputadd, addressType='participant',
|
|
||||||
tokenAmount=maximumsubscriptionamount-amountDeposited,
|
|
||||||
contractName=parsed_data['contractName'], contractAddress = outputlist[0], transactionHash=transaction_data['txid'], blockNumber=transaction_data['blockheight'], blockHash=transaction_data['blockhash']))
|
|
||||||
session.commit()
|
|
||||||
session.close()
|
|
||||||
updateLatestTransaction(transaction_data, parsed_data)
|
|
||||||
return
|
|
||||||
|
|
||||||
else:
|
|
||||||
logger.info("Something went wrong in the smartcontract token transfer method")
|
|
||||||
return 0
|
|
||||||
|
|
||||||
###############################
|
|
||||||
# Check if the tokenAmount being transferred exists in the address & do the token transfer
|
|
||||||
returnval = transferToken(parsed_data['tokenIdentification'], parsed_data['tokenAmount'],
|
|
||||||
inputlist[0], outputlist[0], transaction_data)
|
|
||||||
if returnval is not None:
|
|
||||||
# Store participant details in the smart contract's db
|
|
||||||
session.add(ContractParticipants(participantAddress=inputadd,
|
|
||||||
tokenAmount=parsed_data['tokenAmount'],
|
|
||||||
userChoice=parsed_data['userChoice'], transactionHash=transaction_data['txid'], blockNumber=transaction_data['blockheight'],
|
|
||||||
blockHash=transaction_data['blockhash']))
|
|
||||||
session.commit()
|
|
||||||
# Store transfer as part of ContractTransactionHistory
|
|
||||||
blockchainReference = neturl + 'tx/' + transaction_data['txid']
|
|
||||||
session.add(ContractTransactionHistory(transactionType='participation', sourceFloAddress=inputadd, destFloAddress=outputlist[0],
|
|
||||||
transferAmount=parsed_data['tokenAmount'],
|
|
||||||
blockNumber=transaction_data['blockheight'],
|
|
||||||
blockHash=transaction_data['blockhash'],
|
|
||||||
time=transaction_data['blocktime'],
|
|
||||||
transactionHash=transaction_data['txid'],
|
|
||||||
blockchainReference=blockchainReference,
|
|
||||||
jsonData=json.dumps(transaction_data)))
|
|
||||||
session.commit()
|
|
||||||
session.close()
|
|
||||||
|
|
||||||
# Store a mapping of participant address -> Contract participated in
|
|
||||||
engine = create_engine('sqlite:///system.db', echo=True)
|
|
||||||
SystemBase.metadata.create_all(bind=engine)
|
|
||||||
session = sessionmaker(bind=engine)()
|
session = sessionmaker(bind=engine)()
|
||||||
session.add(ContractAddressMapping(address=inputadd, addressType='participant',
|
blockchainReference = neturl + 'tx/' + transaction_data['txid']
|
||||||
tokenAmount=parsed_data['tokenAmount'],
|
session.add(
|
||||||
contractName=parsed_data['contractName'],
|
RejectedContractTransactionHistory(transactionType='participation',
|
||||||
contractAddress=outputlist[0], transactionHash=transaction_data['txid'], blockNumber=transaction_data['blockheight'], blockHash=transaction_data['blockhash']))
|
sourceFloAddress=inputadd,
|
||||||
|
destFloAddress=outputlist[0],
|
||||||
|
transferAmount=None,
|
||||||
|
blockNumber=transaction_data['blockheight'],
|
||||||
|
blockHash=transaction_data['blockhash'],
|
||||||
|
time=transaction_data['blocktime'],
|
||||||
|
transactionHash=transaction_data['txid'],
|
||||||
|
blockchainReference=blockchainReference,
|
||||||
|
jsonData=json.dumps(transaction_data),
|
||||||
|
rejectComment=f"Transaction {parsed_data['txid']} rejected as userChoice, {parsed_data['userChoice']}, has been passed to Smart Contract named {parsed_data['contractName']} at the address {outputlist[0]} which doesn't accept any userChoice"))
|
||||||
|
session.commit()
|
||||||
|
session.close()
|
||||||
|
pushData_SSEapi(
|
||||||
|
f"Error | Transaction {parsed_data['txid']} rejected as userChoice, {parsed_data['userChoice']}, has been passed to Smart Contract named {parsed_data['contractName']} at the address {outputlist[0]} which doesn't accept any userChoice")
|
||||||
|
return 0
|
||||||
|
|
||||||
|
# check if the right token is being sent for participation
|
||||||
|
if parsed_data['tokenIdentification'] != contractStructure['tokenIdentification']:
|
||||||
|
logger.info(
|
||||||
|
f"Transaction {parsed_data['txid']} rejected as the token being transferred, {parsed_data['tokenIdentidication'].upper()}, is not part of the structure of Smart Contract named {parsed_data['contractName']} at the address {outputlist[0]}")
|
||||||
|
# Store transfer as part of RejectedContractTransactionHistory
|
||||||
|
engine = create_engine(
|
||||||
|
f"sqlite:///smartContracts/{parsed_data['contractName']}-{outputlist[0]}.db",
|
||||||
|
echo=True)
|
||||||
|
ContractBase.metadata.create_all(bind=engine)
|
||||||
|
session = sessionmaker(bind=engine)()
|
||||||
|
blockchainReference = neturl + 'tx/' + transaction_data['txid']
|
||||||
|
session.add(
|
||||||
|
RejectedContractTransactionHistory(transactionType='participation',
|
||||||
|
sourceFloAddress=inputadd,
|
||||||
|
destFloAddress=outputlist[0],
|
||||||
|
transferAmount=None,
|
||||||
|
blockNumber=transaction_data['blockheight'],
|
||||||
|
blockHash=transaction_data['blockhash'],
|
||||||
|
time=transaction_data['blocktime'],
|
||||||
|
transactionHash=transaction_data['txid'],
|
||||||
|
blockchainReference=blockchainReference,
|
||||||
|
jsonData=json.dumps(transaction_data),
|
||||||
|
rejectComment=f"Transaction {parsed_data['txid']} rejected as the token being transferred, {parsed_data['tokenIdentidication'].upper()}, is not part of the structure of Smart Contract named {parsed_data['contractName']} at the address {outputlist[0]}"))
|
||||||
|
session.commit()
|
||||||
|
session.close()
|
||||||
|
pushData_SSEapi(
|
||||||
|
f"Error| Transaction {parsed_data['txid']} rejected as the token being transferred, {parsed_data['tokenIdentidication'].upper()}, is not part of the structure of Smart Contract named {parsed_data['contractName']} at the address {outputlist[0]}")
|
||||||
|
return 0
|
||||||
|
|
||||||
|
# Check if the contract is of the type one-time-event
|
||||||
|
if contractStructure['contractType'] == 'one-time-event':
|
||||||
|
|
||||||
|
# Check if contractAmount is part of the contract structure, and enforce it if it is
|
||||||
|
if 'contractAmount' in contractStructure:
|
||||||
|
if float(contractStructure['contractAmount']) != float(parsed_data['tokenAmount']):
|
||||||
|
logger.info(
|
||||||
|
f"Transaction {parsed_data['txid']} rejected as contractAmount being transferred is not part of the structure of Smart Contract named {parsed_data['contractName']} at the address {outputlist[0]}")
|
||||||
|
# Store transfer as part of RejectedContractTransactionHistory
|
||||||
|
engine = create_engine(
|
||||||
|
f"sqlite:///smartContracts/{parsed_data['contractName']}-{outputlist[0]}.db",
|
||||||
|
echo=True)
|
||||||
|
ContractBase.metadata.create_all(bind=engine)
|
||||||
|
session = sessionmaker(bind=engine)()
|
||||||
|
blockchainReference = neturl + 'tx/' + transaction_data['txid']
|
||||||
|
session.add(
|
||||||
|
RejectedContractTransactionHistory(transactionType='participation',
|
||||||
|
sourceFloAddress=inputadd,
|
||||||
|
destFloAddress=outputlist[0],
|
||||||
|
transferAmount=None,
|
||||||
|
blockNumber=transaction_data['blockheight'],
|
||||||
|
blockHash=transaction_data['blockhash'],
|
||||||
|
time=transaction_data['blocktime'],
|
||||||
|
transactionHash=transaction_data['txid'],
|
||||||
|
blockchainReference=blockchainReference,
|
||||||
|
jsonData=json.dumps(transaction_data),
|
||||||
|
rejectComment=f"Transaction {parsed_data['txid']} rejected as contractAmount being transferred is not part of the structure of Smart Contract named {parsed_data['contractName']} at the address {outputlist[0]}"))
|
||||||
|
session.commit()
|
||||||
|
session.close()
|
||||||
|
pushData_SSEapi(
|
||||||
|
f"Error| Transaction {parsed_data['txid']} rejected as contractAmount being transferred is not part of the structure of Smart Contract named {parsed_data['contractName']} at the address {outputlist[0]}")
|
||||||
|
return 0
|
||||||
|
|
||||||
|
partialTransferCounter = 0
|
||||||
|
# Check if maximum subscription amount has reached
|
||||||
|
if 'maximumsubscriptionamount' in contractStructure:
|
||||||
|
# now parse the expiry time in python
|
||||||
|
maximumsubscriptionamount = float(contractStructure['maximumsubscriptionamount'])
|
||||||
|
engine = create_engine(
|
||||||
|
'sqlite:///smartContracts/{}-{}.db'.format(parsed_data['contractName'], outputlist[0]),
|
||||||
|
echo=True)
|
||||||
|
ContractBase.metadata.create_all(bind=engine)
|
||||||
|
session = sessionmaker(bind=engine)()
|
||||||
|
amountDeposited = session.query(func.sum(ContractParticipants.tokenAmount)).all()[0][0]
|
||||||
|
session.close()
|
||||||
|
|
||||||
|
if amountDeposited is None:
|
||||||
|
amountDeposited = 0
|
||||||
|
|
||||||
|
if amountDeposited >= maximumsubscriptionamount:
|
||||||
|
logger.info(f"Transaction {parsed_data['txid']} rejected as maximum subscription amount has been reached for the Smart contract named {parsed_data['contractName']} at the address {outputlist[0]}")
|
||||||
|
# Store transfer as part of RejectedContractTransactionHistory
|
||||||
|
engine = create_engine(
|
||||||
|
f"sqlite:///smartContracts/{parsed_data['contractName']}-{outputlist[0]}.db",
|
||||||
|
echo=True)
|
||||||
|
ContractBase.metadata.create_all(bind=engine)
|
||||||
|
session = sessionmaker(bind=engine)()
|
||||||
|
blockchainReference = neturl + 'tx/' + transaction_data['txid']
|
||||||
|
session.add(
|
||||||
|
RejectedContractTransactionHistory(transactionType='participation',
|
||||||
|
sourceFloAddress=inputadd,
|
||||||
|
destFloAddress=outputlist[0],
|
||||||
|
transferAmount=None,
|
||||||
|
blockNumber=transaction_data['blockheight'],
|
||||||
|
blockHash=transaction_data['blockhash'],
|
||||||
|
time=transaction_data['blocktime'],
|
||||||
|
transactionHash=transaction_data['txid'],
|
||||||
|
blockchainReference=blockchainReference,
|
||||||
|
jsonData=json.dumps(transaction_data),
|
||||||
|
rejectComment=f"Transaction {parsed_data['txid']} rejected as maximum subscription amount has been reached for the Smart contract named {parsed_data['contractName']} at the address {outputlist[0]}"))
|
||||||
|
session.commit()
|
||||||
|
session.close()
|
||||||
|
pushData_SSEapi(
|
||||||
|
f"Error | Transaction {parsed_data['txid']} rejected as maximum subscription amount has been reached for the Smart contract named {parsed_data['contractName']} at the address {outputlist[0]}")
|
||||||
|
return 0
|
||||||
|
elif ((float(amountDeposited) + float(parsed_data['tokenAmount'])) > maximumsubscriptionamount) and 'contractAmount' in contractStructure:
|
||||||
|
logger.info(
|
||||||
|
f"Transaction {parsed_data['txid']} rejected as the contractAmount surpasses the maximum subscription amount, {contractStructure['maximumsubscriptionamount']} {contractStructure['tokenIdentification'].upper()}, for the Smart contract named {parsed_data['contractName']} at the address {outputlist[0]}")
|
||||||
|
# Store transfer as part of RejectedContractTransactionHistory
|
||||||
|
engine = create_engine(
|
||||||
|
f"sqlite:///smartContracts/{parsed_data['contractName']}-{outputlist[0]}.db",
|
||||||
|
echo=True)
|
||||||
|
ContractBase.metadata.create_all(bind=engine)
|
||||||
|
session = sessionmaker(bind=engine)()
|
||||||
|
blockchainReference = neturl + 'tx/' + transaction_data['txid']
|
||||||
|
session.add(
|
||||||
|
RejectedContractTransactionHistory(transactionType='participation',
|
||||||
|
sourceFloAddress=inputadd,
|
||||||
|
destFloAddress=outputlist[0],
|
||||||
|
transferAmount=None,
|
||||||
|
blockNumber=transaction_data['blockheight'],
|
||||||
|
blockHash=transaction_data['blockhash'],
|
||||||
|
time=transaction_data['blocktime'],
|
||||||
|
transactionHash=transaction_data['txid'],
|
||||||
|
blockchainReference=blockchainReference,
|
||||||
|
jsonData=json.dumps(transaction_data),
|
||||||
|
rejectComment=f"Transaction {parsed_data['txid']} rejected as the contractAmount surpasses the maximum subscription amount, {contractStructure['maximumsubscriptionamount']} {contractStructure['tokenIdentification'].upper()}, for the Smart contract named {parsed_data['contractName']} at the address {outputlist[0]}"))
|
||||||
|
session.commit()
|
||||||
|
session.close()
|
||||||
|
pushData_SSEapi(
|
||||||
|
f"Error | Transaction {parsed_data['txid']} rejected as the contractAmount surpasses the maximum subscription amount, {contractStructure['maximumsubscriptionamount']} {contractStructure['tokenIdentification'].upper()}, for the Smart contract named {parsed_data['contractName']} at the address {outputlist[0]}")
|
||||||
|
return 0
|
||||||
|
else:
|
||||||
|
partialTransferCounter = 1
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
# Check if exitcondition exists as part of contractstructure and is given in right format
|
||||||
|
if 'exitconditions' in contractStructure:
|
||||||
|
# This means the contract has an external trigger, ie. trigger coming from the contract committee
|
||||||
|
exitconditionsList = []
|
||||||
|
for condition in contractStructure['exitconditions']:
|
||||||
|
exitconditionsList.append(contractStructure['exitconditions'][condition])
|
||||||
|
|
||||||
|
if parsed_data['userChoice'] in exitconditionsList:
|
||||||
|
if partialTransferCounter == 0:
|
||||||
|
# Check if the tokenAmount being transferred exists in the address & do the token transfer
|
||||||
|
returnval = transferToken(parsed_data['tokenIdentification'],
|
||||||
|
parsed_data['tokenAmount'], inputlist[0], outputlist[0],
|
||||||
|
transaction_data)
|
||||||
|
if returnval is not None:
|
||||||
|
# Store participant details in the smart contract's db
|
||||||
|
session.add(ContractParticipants(participantAddress=inputadd,
|
||||||
|
tokenAmount=parsed_data['tokenAmount'],
|
||||||
|
userChoice=parsed_data['userChoice'],
|
||||||
|
transactionHash=transaction_data['txid'],
|
||||||
|
blockNumber=transaction_data['blockheight'],
|
||||||
|
blockHash=transaction_data['blockhash']))
|
||||||
|
session.commit()
|
||||||
|
|
||||||
|
# Store transfer as part of ContractTransactionHistory
|
||||||
|
blockchainReference = neturl + 'tx/' + transaction_data['txid']
|
||||||
|
session.add(ContractTransactionHistory(transactionType='participation',
|
||||||
|
sourceFloAddress=inputadd,
|
||||||
|
destFloAddress=outputlist[0],
|
||||||
|
transferAmount=parsed_data['tokenAmount'],
|
||||||
|
blockNumber=transaction_data['blockheight'],
|
||||||
|
blockHash=transaction_data['blockhash'],
|
||||||
|
time=transaction_data['blocktime'],
|
||||||
|
transactionHash=transaction_data['txid'],
|
||||||
|
blockchainReference=blockchainReference,
|
||||||
|
jsonData=json.dumps(transaction_data)))
|
||||||
|
|
||||||
|
session.commit()
|
||||||
|
session.close()
|
||||||
|
|
||||||
|
# Store a mapping of participant address -> Contract participated in
|
||||||
|
engine = create_engine('sqlite:///system.db', echo=True)
|
||||||
|
SystemBase.metadata.create_all(bind=engine)
|
||||||
|
session = sessionmaker(bind=engine)()
|
||||||
|
session.add(ContractAddressMapping(address=inputadd, addressType='participant',
|
||||||
|
tokenAmount=parsed_data['tokenAmount'],
|
||||||
|
contractName=parsed_data['contractName'],
|
||||||
|
contractAddress=outputlist[0],
|
||||||
|
transactionHash=transaction_data['txid'],
|
||||||
|
blockNumber=transaction_data['blockheight'],
|
||||||
|
blockHash=transaction_data['blockhash']))
|
||||||
|
session.commit()
|
||||||
|
|
||||||
|
updateLatestTransaction(transaction_data, parsed_data)
|
||||||
|
return
|
||||||
|
|
||||||
|
else:
|
||||||
|
logger.info("Something went wrong in the smartcontract token transfer method")
|
||||||
|
return 0
|
||||||
|
elif partialTransferCounter == 1:
|
||||||
|
# Transfer only part of the tokens users specified, till the time it reaches maximumamount
|
||||||
|
returnval = transferToken(parsed_data['tokenIdentification'],
|
||||||
|
maximumsubscriptionamount - amountDeposited,
|
||||||
|
inputlist[0], outputlist[0], transaction_data)
|
||||||
|
if returnval is not None:
|
||||||
|
# Store participant details in the smart contract's db
|
||||||
|
session.add(ContractParticipants(participantAddress=inputadd,
|
||||||
|
tokenAmount=maximumsubscriptionamount - amountDeposited,
|
||||||
|
userChoice=parsed_data['userChoice'],
|
||||||
|
transactionHash=transaction_data['txid'],
|
||||||
|
blockNumber=transaction_data['blockheight'],
|
||||||
|
blockHash=transaction_data['blockhash']))
|
||||||
|
session.commit()
|
||||||
|
session.close()
|
||||||
|
|
||||||
|
# Store a mapping of participant address -> Contract participated in
|
||||||
|
engine = create_engine('sqlite:///system.db', echo=True)
|
||||||
|
SystemBase.metadata.create_all(bind=engine)
|
||||||
|
session = sessionmaker(bind=engine)()
|
||||||
|
session.add(ContractAddressMapping(address=inputadd, addressType='participant',
|
||||||
|
tokenAmount=maximumsubscriptionamount - amountDeposited,
|
||||||
|
contractName=parsed_data['contractName'],
|
||||||
|
contractAddress=outputlist[0],
|
||||||
|
transactionHash=transaction_data['txid'],
|
||||||
|
blockNumber=transaction_data['blockheight'],
|
||||||
|
blockHash=transaction_data['blockhash']))
|
||||||
|
session.commit()
|
||||||
|
session.close()
|
||||||
|
updateLatestTransaction(transaction_data, parsed_data)
|
||||||
|
return
|
||||||
|
|
||||||
|
else:
|
||||||
|
logger.info("Something went wrong in the smartcontract token transfer method")
|
||||||
|
return 0
|
||||||
|
|
||||||
|
else:
|
||||||
|
logger.info(f"Transaction {parsed_data['txid']} rejected as wrong userchoice entered for the Smart Contract named {parsed_data['contractName']} at the address {outputlist[0]}")
|
||||||
|
# Store transfer as part of RejectedContractTransactionHistory
|
||||||
|
engine = create_engine(
|
||||||
|
f"sqlite:///smartContracts/{parsed_data['contractName']}-{outputlist[0]}.db",
|
||||||
|
echo=True)
|
||||||
|
ContractBase.metadata.create_all(bind=engine)
|
||||||
|
session = sessionmaker(bind=engine)()
|
||||||
|
blockchainReference = neturl + 'tx/' + transaction_data['txid']
|
||||||
|
session.add(
|
||||||
|
RejectedContractTransactionHistory(transactionType='participation',
|
||||||
|
sourceFloAddress=inputadd,
|
||||||
|
destFloAddress=outputlist[0],
|
||||||
|
transferAmount=None,
|
||||||
|
blockNumber=transaction_data['blockheight'],
|
||||||
|
blockHash=transaction_data['blockhash'],
|
||||||
|
time=transaction_data['blocktime'],
|
||||||
|
transactionHash=transaction_data['txid'],
|
||||||
|
blockchainReference=blockchainReference,
|
||||||
|
jsonData=json.dumps(transaction_data),
|
||||||
|
rejectComment=f"Transaction {parsed_data['txid']} rejected as wrong userchoice entered for the Smart Contract named {parsed_data['contractName']} at the address {outputlist[0]}"))
|
||||||
|
session.commit()
|
||||||
|
session.close()
|
||||||
|
pushData_SSEapi(f"Error| Transaction {parsed_data['txid']} rejected as wrong userchoice entered for the Smart Contract named {parsed_data['contractName']} at the address {outputlist[0]}")
|
||||||
|
return 0
|
||||||
|
|
||||||
|
elif 'payeeAddress' in contractStructure:
|
||||||
|
# this means the contract if of the type internal trigger
|
||||||
|
if parsed_data['userChoice'] in exitconditionsList:
|
||||||
|
if partialTransferCounter == 0:
|
||||||
|
# Check if the tokenAmount being transferred exists in the address & do the token transfer
|
||||||
|
returnval = transferToken(parsed_data['tokenIdentification'],
|
||||||
|
parsed_data['tokenAmount'], inputlist[0], outputlist[0],
|
||||||
|
transaction_data)
|
||||||
|
if returnval is not None:
|
||||||
|
# Store participant details in the smart contract's db
|
||||||
|
session.add(ContractParticipants(participantAddress=inputadd,
|
||||||
|
tokenAmount=parsed_data['tokenAmount'],
|
||||||
|
userChoice='-',
|
||||||
|
transactionHash=transaction_data['txid'],
|
||||||
|
blockNumber=transaction_data['blockheight'],
|
||||||
|
blockHash=transaction_data['blockhash']))
|
||||||
|
session.commit()
|
||||||
|
|
||||||
|
# Store transfer as part of ContractTransactionHistory
|
||||||
|
blockchainReference = neturl + 'tx/' + transaction_data['txid']
|
||||||
|
session.add(ContractTransactionHistory(transactionType='participation',
|
||||||
|
sourceFloAddress=inputadd,
|
||||||
|
destFloAddress=outputlist[0],
|
||||||
|
transferAmount=parsed_data['tokenAmount'],
|
||||||
|
blockNumber=transaction_data['blockheight'],
|
||||||
|
blockHash=transaction_data['blockhash'],
|
||||||
|
time=transaction_data['blocktime'],
|
||||||
|
transactionHash=transaction_data['txid'],
|
||||||
|
blockchainReference=blockchainReference,
|
||||||
|
jsonData=json.dumps(transaction_data)))
|
||||||
|
|
||||||
|
session.commit()
|
||||||
|
session.close()
|
||||||
|
|
||||||
|
# Store a mapping of participant address -> Contract participated in
|
||||||
|
engine = create_engine('sqlite:///system.db', echo=True)
|
||||||
|
SystemBase.metadata.create_all(bind=engine)
|
||||||
|
session = sessionmaker(bind=engine)()
|
||||||
|
session.add(ContractAddressMapping(address=inputadd, addressType='participant',
|
||||||
|
tokenAmount=parsed_data['tokenAmount'],
|
||||||
|
contractName=parsed_data['contractName'],
|
||||||
|
contractAddress=outputlist[0],
|
||||||
|
transactionHash=transaction_data['txid'],
|
||||||
|
blockNumber=transaction_data['blockheight'],
|
||||||
|
blockHash=transaction_data['blockhash']))
|
||||||
|
session.commit()
|
||||||
|
|
||||||
|
updateLatestTransaction(transaction_data, parsed_data)
|
||||||
|
return
|
||||||
|
|
||||||
|
else:
|
||||||
|
logger.info("Something went wrong in the smartcontract token transfer method")
|
||||||
|
return 0
|
||||||
|
elif partialTransferCounter == 1:
|
||||||
|
# Transfer only part of the tokens users specified, till the time it reaches maximumamount
|
||||||
|
returnval = transferToken(parsed_data['tokenIdentification'],
|
||||||
|
maximumsubscriptionamount - amountDeposited,
|
||||||
|
inputlist[0], outputlist[0], transaction_data)
|
||||||
|
if returnval is not None:
|
||||||
|
# Store participant details in the smart contract's db
|
||||||
|
session.add(ContractParticipants(participantAddress=inputadd,
|
||||||
|
tokenAmount=maximumsubscriptionamount - amountDeposited,
|
||||||
|
userChoice='-',
|
||||||
|
transactionHash=transaction_data['txid'],
|
||||||
|
blockNumber=transaction_data['blockheight'],
|
||||||
|
blockHash=transaction_data['blockhash']))
|
||||||
|
session.commit()
|
||||||
|
session.close()
|
||||||
|
|
||||||
|
# Store a mapping of participant address -> Contract participated in
|
||||||
|
engine = create_engine('sqlite:///system.db', echo=True)
|
||||||
|
SystemBase.metadata.create_all(bind=engine)
|
||||||
|
session = sessionmaker(bind=engine)()
|
||||||
|
session.add(ContractAddressMapping(address=inputadd, addressType='participant',
|
||||||
|
tokenAmount=maximumsubscriptionamount - amountDeposited,
|
||||||
|
contractName=parsed_data['contractName'],
|
||||||
|
contractAddress=outputlist[0],
|
||||||
|
transactionHash=transaction_data['txid'],
|
||||||
|
blockNumber=transaction_data['blockheight'],
|
||||||
|
blockHash=transaction_data['blockhash']))
|
||||||
|
session.commit()
|
||||||
|
session.close()
|
||||||
|
updateLatestTransaction(transaction_data, parsed_data)
|
||||||
|
return
|
||||||
|
|
||||||
|
else:
|
||||||
|
logger.info("Something went wrong in the smartcontract token transfer method")
|
||||||
|
return 0
|
||||||
|
|
||||||
|
else:
|
||||||
|
logger.info(
|
||||||
|
f"Transaction {transaction_data['txid']} rejected as the participation doesn't belong to any valid contract type")
|
||||||
|
# Store transfer as part of RejectedContractTransactionHistory
|
||||||
|
engine = create_engine(
|
||||||
|
f"sqlite:///smartContracts/{parsed_data['contractName']}-{parsed_data['contractAddress']}.db",
|
||||||
|
echo=True)
|
||||||
|
ContractBase.metadata.create_all(bind=engine)
|
||||||
|
session = sessionmaker(bind=engine)()
|
||||||
|
blockchainReference = neturl + 'tx/' + transaction_data['txid']
|
||||||
|
session.add(
|
||||||
|
RejectedContractTransactionHistory(transactionType='participation', sourceFloAddress=inputadd,
|
||||||
|
destFloAddress=outputlist[0],
|
||||||
|
transferAmount=None,
|
||||||
|
blockNumber=transaction_data['blockheight'],
|
||||||
|
blockHash=transaction_data['blockhash'],
|
||||||
|
time=transaction_data['blocktime'],
|
||||||
|
transactionHash=transaction_data['txid'],
|
||||||
|
blockchainReference=blockchainReference,
|
||||||
|
jsonData=json.dumps(transaction_data),
|
||||||
|
rejectComment=f"Transaction {transaction_data['txid']} rejected as the participation doesn't belong to any valid contract type"))
|
||||||
session.commit()
|
session.commit()
|
||||||
session.close()
|
session.close()
|
||||||
|
|
||||||
updateLatestTransaction(transaction_data, parsed_data)
|
url = 'https://ranchimallflo.duckdns.org/'
|
||||||
|
headers = {'Accept': 'application/json', 'Content-Type': 'application/json'}
|
||||||
|
r = requests.post(url, json={
|
||||||
|
'message': f"Error | Transaction {transaction_data['txid']} rejected as the participation doesn't belong to any valid contract type"},
|
||||||
|
headers=headers)
|
||||||
|
return 0
|
||||||
|
|
||||||
pushData_SSEapi('Participation | Succesfully participated in the contract {}-{} at transaction {}'.format(
|
|
||||||
parsed_data['contractName'], outputlist[0],
|
|
||||||
transaction_data['txid']))
|
|
||||||
|
|
||||||
return
|
|
||||||
else:
|
else:
|
||||||
logger.info(
|
logger.info(
|
||||||
f"Transaction {transaction_data['txid']} rejected as a Smart Contract with the name {parsed_data['contractName']} at address {parsed_data['contractAddress']} doesnt exist")
|
f"Transaction {transaction_data['txid']} rejected as a Smart Contract with the name {parsed_data['contractName']} at address {parsed_data['contractAddress']} doesnt exist")
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user