Skip to content

Update main.sw #812

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
20 changes: 9 additions & 11 deletions OTC-swap-predicate/swap-predicate/src/main.sw
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
predicate;

use std::{
inputs::{
input_coin_owner,
@@ -10,7 +8,6 @@ use std::{
output_amount,
output_asset_id,
output_asset_to,
output_pointer,
output_type,
},
};
@@ -20,11 +17,11 @@ configurable {
ASK_AMOUNT: u64 = 42,
/// The asset to be paid.
ASK_ASSET: AssetId = AssetId::from(0x0101010101010101010101010101010101010101010101010101010101010101),
/// The receiver to whom the swapped asset will be sent.
/// The receiver address to whom the swapped asset will be sent.
RECEIVER: Address = Address::from(0x09c0b2d1a486c439a87bcba6b46a7a1a23f3897cc83a94521a96da5c23bc58db),
}

/// Validates conditions within the transaction to perform a swap
/// Validates conditions within the transaction to perform a swap.
///
/// # Additional Information
///
@@ -34,12 +31,13 @@ configurable {
///
/// * [bool] - `true` if the spender is the receiver or if the terms of the order are met, `false` otherwise.
fn main() -> bool {
// The spending transaction must have an output that sends `ask_amount` of `ask_asset` to `receiver`
// The transaction must have an output that sends `ASK_AMOUNT` of `ASK_ASSET` to `RECEIVER`.

// Check if the transaction contains a single input coin from the receiver, to cancel their own order (in addition to this predicate)
// Check if the transaction contains exactly two input coins and if one of them belongs to the receiver
if input_count() == 2u8 {
match (input_coin_owner(0), input_coin_owner(1)) {
(Some(owner1), Some(owner2)) => {
// If either of the input coins belongs to the receiver, the transaction is valid.
if owner1 == RECEIVER || owner2 == RECEIVER {
return true;
}
@@ -49,16 +47,16 @@ fn main() -> bool {
}

// Otherwise, evaluate the terms of the order:
// The output which pays the receiver must be the first output
// The output that pays the receiver must be the first output.
let output_index = 0;

// Revert if output is not an Output::Coin
// Revert if the output is not of type Output::Coin
match output_type(output_index) {
Output::Coin => (),
_ => return false,
};

// Since output is known to be a Coin, the following are always valid
// Since the output type is known to be a Coin, we can safely retrieve the following details
let to = match output_asset_to(output_index) {
Some(address) => address,
None => return false,
@@ -71,6 +69,6 @@ fn main() -> bool {

let amount = output_amount(output_index);

// Evaluate the predicate
// Evaluate the predicate based on the provided conditions
(to == RECEIVER) && (amount == ASK_AMOUNT) && (asset_id == ASK_ASSET)
}