Importing custom module in another custom module

I am trying to create my own coin for learning purposes.

I created a dummy TCCoin like this

    module TCCoin {

    resource T {
        // The value of the coin. May be zero
        value: u64,
    }
} 

And importing it like this

module TCSubAccount {
    
    import 0x0.LibraAccount;
    import {{sender}}.TCCoin;

    resource T {
        balance: TCCoin.T,
        libraAccount: LibraAccount.T,
        sequence_number: u64,
    }

}

Compilation for the TCCoin works fine but when I try to compile the TCSubAccount, I get this error

ErrorMessage { msg: "Dependency not provided for 0000000000000000000000000000000000000000000000000000000000000000.TCCoin" }

Question: How are custom modules imported in the correct way?

1 Like

Hi gcameo!

Would you provide how you compile the TCCoin? If you were doing things in functional_tests you should be fine. I just created a file with following content in functional_tests and it worked fine for me:

module TCCoin {
    resource T {
        // The value of the coin. May be zero
        value: u64,
    }
}
//! new-transaction
module TCSubAccount {
    import 0x0.LibraAccount;
    import {{default}}.TCCoin;
    resource T {
        balance: TCCoin.T,
        libraAccount: LibraAccount.T,
        sequence_number: u64,
    }
}

And if you are compiling the code with the client, would you please attach the command that you typed? I would imagine it’s because the sender address doesn’t get passed correctly and thus caused the compiler to emit a default address there (the long 0x0).

Thanks @runtianz

I first run libra locally using this command

cargo run -p libra_swarm -- -l -c swarm_config

Then I run the resulting command that needs to be used for the client in another tab passing in the generated faucet files

cargo run --bin client -- -a localhost -p 61842 -s "swarm_config/consensus_peers.config.toml" -m "/var/folders/tf/v574yvn96_dbnp7cvyr101l00000gn/T/a4bb0c6100d840e052face09378735a2/temp_faucet_keys"

then I create an account like this

libra% account create

Creating/retrieving next account from wallet
Created/retrieved account #0 address 15bd475d105fbb28d596af294db6ec958c85f67ea5c9b5007da42fb5a2d16909

Then I compile the tw_coin like this

libra% dev compile 0 /<full_path>/src/modules/tc_coin.mvir module

Compiling program
Finished dev [unoptimized + debuginfo] target(s) in 2.73s
Running target/debug/compiler -l /var/folders/tf/v574yvn96_dbnp7cvyr101l00000gn/T/6c5d0a5800f7311bd01683e8bb986854 -m
Finished dev [unoptimized + debuginfo] target(s) in 2.09s
Running target/debug/compiler /var/folders/tf/v574yvn96_dbnp7cvyr101l00000gn/T/6c5d0a5800f7311bd01683e8bb986854 -a 15bd475d105fbb28d596af294db6ec958c85f67ea5c9b5007da42fb5a2d16909 -o /var/folders/tf/v574yvn96_dbnp7cvyr101l00000gn/T/f68c3bcfb40ef03ba712565978e5ead7 -m
Successfully compiled a program at /var/folders/tf/v574yvn96_dbnp7cvyr101l00000gn/T/f68c3bcfb40ef03ba712565978e5ead7

Then I compile the sub account like this

libra% dev compile 0 /<full_path>/src/modules/tc_subaccount.mvir module

Compiling program
Finished dev [unoptimized + debuginfo] target(s) in 2.48s
Running target/debug/compiler -l /var/folders/tf/v574yvn96_dbnp7cvyr101l00000gn/T/ec9266e50d7bd9e747e639aae371b302 -m
Finished dev [unoptimized + debuginfo] target(s) in 1.92s
Running target/debug/compiler /var/folders/tf/v574yvn96_dbnp7cvyr101l00000gn/T/ec9266e50d7bd9e747e639aae371b302 -a 15bd475d105fbb28d596af294db6ec958c85f67ea5c9b5007da42fb5a2d16909 -o /var/folders/tf/v574yvn96_dbnp7cvyr101l00000gn/T/89c2b3e093d8a963a1017fd67e6748f3 -m
thread ‘main’ panicked at ‘called Result::unwrap() on an Err value: ErrorMessage { msg: “Dependency not provided for 0000000000000000000000000000000000000000000000000000000000000000.KWCoin” }’, src/libcore/result.rs:999:5
note: run with RUST_BACKTRACE=1 environment variable to display a backtrace.
compilation failed

Hi there,

The first thing is, you probably need to deploy the TCCoin coin module before you compile TCSubAccount, the reason being that the compiler will try to find the dependencies on chain only. Thus it won’t be able to locate TCCoin as it wasn’t deployed yet.

Secondly the error msg “Dependency not provided for 0000000000000000000000000000000000000000000000000000000000000000.KWCoin” }’, seems pretty suspicious to me. I tried to run your command on my machine and I got ErrorMessage { msg: "Dependency not provided for 0b30317754dccd9abf56fcf819d38bddec1dad1472c2181a3998da029202e798.TCCoin" }', src/libcore/result.rs:999:5 which is kinda what I would expect as the 0b30317754dccd9abf56fcf819d38bddec1dad1472c2181a3998da029202e798 is indeed the account that I just created via a c.

Hope that would solve your issue.

Best
Runtian