|
| 1 | +use fuels::{accounts::{predicate::Predicate}, prelude::*}; |
| 2 | + |
| 3 | +// Load abi from json |
| 4 | +abigen!(Predicate( |
| 5 | + name = "MyPredicate", |
| 6 | + abi = "out/debug/{{project-name}}-abi.json" |
| 7 | +)); |
| 8 | + |
| 9 | +async fn get_predicate_instance() -> (WalletUnlocked, Predicate) { |
| 10 | + let mut wallets = launch_custom_provider_and_get_wallets( |
| 11 | + WalletsConfig::new( |
| 12 | + Some(1), /* Single wallet */ |
| 13 | + Some(1), /* Single coin (UTXO) */ |
| 14 | + Some(1_000_000_000), /* Amount per coin */ |
| 15 | + ), |
| 16 | + None, |
| 17 | + None, |
| 18 | + ) |
| 19 | + .await |
| 20 | + .unwrap(); |
| 21 | + |
| 22 | + let wallet = wallets.pop().unwrap(); |
| 23 | + |
| 24 | + let provider = wallet.provider().clone().unwrap(); |
| 25 | + |
| 26 | + let bin_path = "./out/debug/{{project-name}}.bin"; |
| 27 | + |
| 28 | + let instance: Predicate = Predicate::load_from(bin_path).unwrap().with_provider(provider.clone()); |
| 29 | + |
| 30 | + (wallet, instance) |
| 31 | +} |
| 32 | + |
| 33 | +async fn check_balances( |
| 34 | + wallet: &WalletUnlocked, |
| 35 | + instance: &Predicate, |
| 36 | + expected_wallet_balance: Option<u64>, |
| 37 | + expected_predicate_balance: Option<u64>, |
| 38 | +) -> (u64, u64) { |
| 39 | + let wallet_bal = wallet.get_asset_balance(&AssetId::default()).await.unwrap(); |
| 40 | + let predicate_bal = instance.get_asset_balance(&AssetId::default()).await.unwrap(); |
| 41 | + |
| 42 | + if let Some(expected) = expected_wallet_balance { |
| 43 | + assert_eq!(wallet_bal, expected); |
| 44 | + } |
| 45 | + |
| 46 | + if let Some(expected) = expected_predicate_balance { |
| 47 | + assert_eq!(predicate_bal, expected); |
| 48 | + } |
| 49 | + |
| 50 | + (wallet_bal, predicate_bal) |
| 51 | +} |
| 52 | + |
| 53 | +#[tokio::test] |
| 54 | +async fn can_get_predicate_instance() { |
| 55 | + let (wallet, instance) = get_predicate_instance().await; |
| 56 | + let predicate_root = instance.address(); |
| 57 | + |
| 58 | + // Check balances before funding predicate |
| 59 | + check_balances(&wallet, &instance, Some(1_000_000_000u64), None).await; |
| 60 | + |
| 61 | + // Fund predicate from wallet |
| 62 | + let _ = wallet.transfer(predicate_root, 1234, BASE_ASSET_ID, TxPolicies::default()).await; |
| 63 | + |
| 64 | + // Check balances after funding predicate |
| 65 | + check_balances(&wallet, &instance, Some(999_998_766u64), Some(1234u64)).await; |
| 66 | + |
| 67 | + let _ = instance.transfer(wallet.address(), 1234, BASE_ASSET_ID, TxPolicies::default()).await; |
| 68 | + |
| 69 | + // Check balances after transfering funds out of predicate |
| 70 | + check_balances(&wallet, &instance, Some(1_000_000_000u64), Some(0u64)).await; |
| 71 | +} |
0 commit comments