Testing Issue: Unable to Take SUI Coin from Address in Test Scenario [EEmptyInventory]

So I have a function that takes sui_coin as an arg so I am trying to write test for it

public entry fun create_offer(
        currency_code: vector<u8>,
        price: u64,
        payment_type: vector<u8>,
        sui_coin: Coin<SUI>,
        offer_registry: &mut OfferRegistry,
        _: &ProfileRegistry,
        ctx: &mut TxContext
    )

and I minted some Sui coin which I believe should be owner by the seller automatically since the seller initiated the transaction but I still sent the coin object to the seller

        scenario.next_tx(seller);
        {
            let mut coin = coin::mint_for_testing<Coin<SUI>>(1000, scenario.ctx());
            transfer::public_transfer(coin, seller);
        };

Then in the next transaction I try to take the coin object from the seller , I also tried taking from the sender but all failed

        scenario.next_tx(seller);
        {   
            let price = 1000;
            let sui_coin = test_scenario::take_from_address<Coin<SUI>>(&scenario, seller);
            //let sui_coin = test_scenario::take_from_sender<Coin<SUI>>(&scenario);
            let currency_code: vector<u8> = b"Joe";
            let payment_type: vector<u8> = b"BankTransfer";

            let profile_registry = scenario.take_shared<Escrow::ProfileRegistry>();
            let mut offer_registry = scenario.take_shared<Escrow::OfferRegistry>();

            Escrow::create_offer(currency_code, price, payment_type, sui_coin, &mut offer_registry, &profile_registry, scenario.ctx());
            test_scenario::return_shared(profile_registry);
            test_scenario::return_shared(offer_registry);
        }

I always get this error

│ 247 │ public fun take_from_address<T: key>(scenario: &Scenario, account: address): T {
│     │            ----------------- In this function in sui::test_scenario
│ 248 │     let id_opt = most_recent_id_for_address<T>(account);
│ 249 │     assert!(id_opt.is_some(), EEmptyInventory);
│     │     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Test was not expected to error, but it aborted with code 3 originating in the module sui::test_scenario rooted here

Which means there is no Coin object tied to the seller address/account and the error points back to line let sui_coin = test_scenario::take_from_address<Coin<SUI>>(&scenario, seller);

I have been trying to debug this but I cannot seem to find a way my main aim is for the seller to be able create an Escrow but a coin object has to be passed into the function can anyone help on this?

Okay guys I fixed the issue and here is how I did it


        scenario.next_tx(seller);
        {   
            let mut coin = coin::mint_for_testing<SUI>(1000, scenario.ctx());

            Escrow::create_offer(currency_code, price, payment_type, coin, &mut offer_registry, &profile_registry, scenario.ctx());
        };

So when minting the coin I directly passed it into my create_offer function which consumes it so no need to handle it anymore.
and also If you want to declare the coin you just minted into a variable you can do this after minting


        scenario.next_tx(seller);
        {   
            let mut coin = coin::mint_for_testing<SUI>(1000, scenario.ctx());
 }

So now minted_coin holds 1000 sui tokens

 scenario.next_tx(seller);
{
let minted_coin = scenario.take_from_sender<Coin<SUI>>();
}

Hope this helps

1 Like