aiken-lang / aiken

A modern smart contract platform for Cardano
https://aiken-lang.org
Apache License 2.0
474 stars 92 forks source link

Escrow Contract #1036

Closed avi69night closed 1 week ago

avi69night commented 1 week ago

Aiken-lang smart contract?

is there any bugs i9n escrow_contract?

the code

contract EscrowContract { entrypoint release_funds(oracle: Oracle, tx: Tx, threshold: Int) -> Bool { let price = oracle.get_latest_price() if price > threshold { tx.contains_output_to_address(tx.sender()) -- releases funds back to sender } else { False } }

Is there any Bugs on this escrow_contract Code?

Yes, there are a couple of issues and potential improvements in the code you provided for the EscrowContract in Aiken.

Can You Describe the points of Bugs?

  1. Implicit Return in Conditional Expression In Aiken, all branches of a conditional expression (if statement) must have the same return type. In the current form, the if branch returns a Boolean result, but the else branch returns False. You need to ensure both branches return a Boolean.

  2. Action vs. Check tx.contains_output_to_address(tx.sender()) checks whether the transaction contains an output to the sender's address, but this does not release funds. To release funds, you'd likely need to create an actual transaction that sends funds to the sender.

  3. Explicit Return Ensure the final expression returns a Boolean result, even in the if branch. Right now, tx.contains_output_to_address(tx.sender()) might not return True or False, depending on the underlying implementation.

  4. Non-returning Else Returning False from the else branch is fine as long as the if block also returns a Boolean. Let's refactor.

Refactored code

. . . contract EscrowContract { entrypoint release_funds(oracle: Oracle, tx: Tx, threshold: Int) -> Bool { let price = oracle.get_latest_price() if price > threshold { tx.contains_output_to_address(tx.sender()) == True -- Check if the funds were sent to the sender } else { False } } }

. . .

Key Points of the Bugs

  1. The condition tx.contains_output_to_address(tx.sender()) == True ensures that the if block returns a Boolean value.
  2. Theelseblock already correctly returns False, indicating that no funds are released.

However, for the further review whether tx.contains_output_to_address(tx.sender()) properly reflects the fund release logic, or if additional logic is required for actually transferring funds.

If the function's purpose is to release funds, there may need to be actual logic for sending the funds to the sender rather than merely checking for an output.