Skip to content
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

Add Input::Message case to predicate_address() #6117

Merged
merged 17 commits into from
Jun 20, 2024
Merged
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
13 changes: 11 additions & 2 deletions sway-lib-std/src/auth.sw
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ use ::contract_id::ContractId;
use ::identity::Identity;
use ::option::Option::{self, *};
use ::result::Result::{self, *};
use ::inputs::{Input, input_coin_owner, input_count, input_type};
use ::inputs::{Input, input_coin_owner, input_count, input_message_recipient, input_type};
use ::revert::revert;

/// The error type used when an `Identity` cannot be determined.
pub enum AuthError {
Expand Down Expand Up @@ -206,5 +207,13 @@ pub fn predicate_address() -> Address {
r1: u64
};

input_coin_owner(predicate_index).unwrap()
let type_of_input = input_type(predicate_index);

match type_of_input {
Input::Coin => input_coin_owner(predicate_index).unwrap(),
Input::Message => input_message_recipient(predicate_index),
_ => {
revert(0)
}
}
}
88 changes: 86 additions & 2 deletions test/src/sdk-harness/test_projects/auth/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ use fuels::{
wallet::{Wallet, WalletUnlocked},
},
prelude::*,
types::ContractId,
tx::UtxoId,
types::{ContractId, message::{Message, MessageStatus}, coin::{Coin, CoinStatus}, Bytes32},
};
use std::str::FromStr;

Expand Down Expand Up @@ -123,7 +124,7 @@ async fn can_get_predicate_address() {

// Setup Predciate
let hex_predicate_address: &str =
"0xf868533e744aeda1df2fcd5589d822db7087e61702662356961029622b6b75f2";
"0x413c19386a356dc93c35f384cd34efa65f8779d8da3f1bac783bc18edab08a3c";
let predicate_address =
Address::from_str(hex_predicate_address).expect("failed to create Address from string");
let predicate_bech32_address = Bech32Address::from(predicate_address);
Expand Down Expand Up @@ -240,3 +241,86 @@ async fn when_incorrect_predicate_address_passed() {
.await
.unwrap();
}


#[tokio::test]
async fn can_get_predicate_address_in_message() {
// Setup Predciate address
let hex_predicate_address: &str =
"0x413c19386a356dc93c35f384cd34efa65f8779d8da3f1bac783bc18edab08a3c";
let predicate_address =
Address::from_str(hex_predicate_address).expect("failed to create Address from string");
let predicate_bech32_address = Bech32Address::from(predicate_address);

// Setup message
let message_amount = 1;
let message = Message {
sender: Bech32Address::default(),
recipient: predicate_bech32_address.clone(),
nonce: 0.into(),
amount: message_amount,
data: vec![],
da_height: 0,
status: MessageStatus::Unspent,
};
let mut message_vec: Vec<Message> = Vec::new();
message_vec.push(message);

// Setup Coin
let coin_amount = 0;
let coin = Coin {
owner: predicate_bech32_address.clone(),
utxo_id: UtxoId::new(Bytes32::zeroed(), 0),
amount: coin_amount,
asset_id: AssetId::default(),
status: CoinStatus::Unspent,
block_created: Default::default(),
};
let mut coin_vec: Vec<Coin> = Vec::new();
coin_vec.push(coin);

let mut wallet = WalletUnlocked::new_random(None);
let provider = setup_test_provider(coin_vec, message_vec, None, None).await.unwrap();
wallet.set_provider(provider.clone());

// Setup Predciate
let predicate_data = AuthPredicateEncoder::default()
.encode_data(predicate_bech32_address)
.unwrap();
let predicate: Predicate =
Predicate::load_from("test_artifacts/auth_predicate/out/release/auth_predicate.bin")
.unwrap()
.with_provider(wallet.try_provider().unwrap().clone())
.with_data(predicate_data);

// Check predicate balance.
let balance = predicate
.get_asset_balance(&AssetId::default())
.await
.unwrap();
assert_eq!(balance, message_amount);

// Spend the message
predicate
.transfer(
wallet.address(),
message_amount,
AssetId::default(),
TxPolicies::default(),
)
.await.unwrap();

// The predicate has spent the funds
let predicate_balance = predicate
.get_asset_balance(&AssetId::default())
.await
.unwrap();
assert_eq!(predicate_balance, 0);

// Funds were transferred
let wallet_balance = wallet
.get_asset_balance(&AssetId::default())
.await
.unwrap();
assert_eq!(wallet_balance, message_amount);
}
Loading