Skip to content

Commit

Permalink
Merge pull request swanchain#13 from swanchain/code-cleanup
Browse files Browse the repository at this point in the history
Code cleanup
  • Loading branch information
PlutoNbai authored Jan 31, 2024
2 parents 3aafee7 + 39a50c3 commit e4dd7af
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 75 deletions.
2 changes: 1 addition & 1 deletion backend/router/task.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ def check_if_refundable(task_uuid):
w3, task_contract_address, task_contract, tx_config = verify_task_contract_address(chain_id, task_uuid)

# Step 3: Update task end time
transaction = task_contract.functions.updateEndTime(end_time).buildTransaction(tx_config)
transaction = task_contract.functions.updateEndTime(int(end_time)).buildTransaction(tx_config)
signed_transaction = w3.eth.account.signTransaction(transaction, admin_private_key)
transaction_hash = w3.eth.sendRawTransaction(signed_transaction.rawTransaction)
logging.info("Transaction Hash:" + transaction_hash.hex())
Expand Down
94 changes: 20 additions & 74 deletions backend/service/space_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -312,50 +312,6 @@ def save_space_config_order(config, duration, region, start_in, order_type=None)
return cfg


def set_reward(chain_id: int, job_uuid: int, wallet_address: str, amount):
"""Set reward for CP
Reward X amount to a wallet address.
Args:
chain_id int/str: The block chain id of the deployed payment contract.
job_uuid int/str: uuid of the job we are paying out.
wallet_address str: CP public address. e.g. 0xab123...
amount float: Amount of reward. (In unit of eth, 18 decimals)
Return:
The transaction hash of the set reward transaction.
Return if sign transaction failed.
*This function requires both public and private address
of a wallet loaded from .env for signing the transaction.
"""
w3 = get_rpc(chain_id)
payment_contract = get_payment_contract(w3, chain_id)

# Try to set reward with at most 10 attempts
for i in range(10):
try:
nonce = w3.eth.get_transaction_count(PUBLIC_ADDRESS)
tx_config = {
"from": PUBLIC_ADDRESS,
"nonce": nonce,
"maxFeePerGas": 2500000000,
"maxPriorityFeePerGas": 2500000000,
}
amount_to_wei = w3.toWei(amount, "ether")
amount_to_wei = amount_to_wei * 10
tx = payment_contract.functions.setReward(
str(job_uuid), wallet_address, amount_to_wei
).build_transaction(tx_config)
signed_tx = w3.eth.account.sign_transaction(tx, PRIVATE_ADDRESS)
tx_hash = w3.eth.send_raw_transaction(signed_tx.rawTransaction)
break
except Exception as e:
logging.info(f"Error setting CP reward on attempt {i + 1}: {str(e)}")
tx_hash = None
time.sleep(5)

return tx_hash


def pay_out_job(order, job, isFinal=False, message=""):
if job is None:
return
Expand All @@ -380,7 +336,8 @@ def pay_out_job(order, job, isFinal=False, message=""):
payment_amount = 0
if not isFinal:
try:
tx_hash = terminate_task(recipient.public_address, job.task_uuid)
# tx_hash = terminate_task(recipient.public_address, job.task_uuid)
tx_hash = update_end_time(job.task_uuid, job.ended_at)
if tx_hash is None:
logging.error(
f"Error occured when terminatingTask for {job.task_uuid}"
Expand Down Expand Up @@ -583,29 +540,14 @@ def request_refund(task_uuid):
return tx_hash.hex()


def terminate_task(wallet_address, task_uuid):
chain_id = "2024"
try:
w3, task_contract_address, task_contract, tx_config = verify_task_contract_address(chain_id, task_uuid)
except Exception as e:
logging.error(f"Error occurred when getting task contact: {str(e)}")
return None
tx = task_contract.functions.terminateTask(wallet_address).build_transaction(
tx_config
)
signed_tx = w3.eth.account.sign_transaction(tx, PRIVATE_ADDRESS)
tx_hash = w3.eth.send_raw_transaction(signed_tx.rawTransaction)
tx_receipt = w3.eth.wait_for_transaction_receipt(tx_hash)
return tx_hash.hex()

def update_end_time(task_uuid, end_time):
chain_id = "2024"
try:
w3, task_contract_address, task_contract, tx_config = verify_task_contract_address(chain_id, task_uuid)
except Exception as e:
logging.error(f"Error occurred when getting task contact: {str(e)}")
return None
tx = task_contract.functions.updateEndTime(end_time).build_transaction(
tx = task_contract.functions.updateEndTime(int(end_time)).build_transaction(
tx_config
)
signed_tx = w3.eth.account.sign_transaction(tx, PRIVATE_ADDRESS)
Expand Down Expand Up @@ -636,7 +578,7 @@ def get_leading_cp(task_uuid):
logging.error(f"Error occurred when getting task contact: {str(e)}")
return None
cp_list = task_contract.functions.getCpList().call()
return cp_list
return cp_list[0]


def get_amount(tx_hash, task_uuid):
Expand All @@ -647,16 +589,20 @@ def get_amount(tx_hash, task_uuid):
logging.error(f"Error occurred when getting task contact: {str(e)}")
return None
receipt = w3.eth.get_transaction_receipt(tx_hash)
event_info = task_contract.events.TaskTerminated().processReceipt(receipt)
print("Event Info:", event_info)
if event_info:
lead_cp_amount = event_info[0]["args"]["leadingReward"]
lead_cp_amount = w3.fromWei(lead_cp_amount, "ether")
cp_amount = event_info[0]["args"]["otherReward"]
cp_amount = w3.fromWei(cp_amount, "ether")
user_amount = event_info[0]["args"]["userRefund"]
user_amount = w3.fromWei(user_amount, "mwei")
return lead_cp_amount, cp_amount, user_amount

is_task_terminated = task_contract.functions.isTaskTerminated.call()

reward_balance, refund_balance = 0, 0
if is_task_terminated:
reward_balance = task_contract.functions.rewardBalance.call()
refund_balance = task_contract.functions.refundBalance.call()
else:
print("No event info found for the given transaction hash and task UUID.")
return None
reward_balance = task_contract.functions.swanRewardAmount.call()
refund_balance = 0


lead_cp_amount = w3.fromWei(reward_balance * 0.7, "ether")
cp_amount = w3.fromWei(reward_balance * 0.3, "ether")
user_amount = w3.fromWei(refund_balance, "ether")

return lead_cp_amount, cp_amount, user_amount

0 comments on commit e4dd7af

Please sign in to comment.