aptosmove-lang

Could not infer this type. Try adding an annotation


This is the code fragment from here.

    public fun publish_balance<CoinType>(account: &signer) {
        let empty_coin = Coin<CoinType> { value: 0 };
        assert!(!exists<Balance<CoinType>>(signer::address_of(account)), EALREADY_HAS_BALANCE);
        move_to(account, Balance<CoinType> { coin: empty_coin });
    }

    spec publish_balance {
        include Schema_publish<CoinType> {addr: signer::address_of(account), amount: 0};
    }

    spec schema Schema_publish<CoinType> {
        addr: address;
        amount: u64;

        aborts_if exists<Balance<CoinType>>(addr);

        ensures exists<Balance<CoinType>>(addr);
        let post balance_post = global<Balance<CoinType>>(addr).coin.value;

        ensures balance_post == amount;
    }

I was creating a test case for this function like this.

    #[test(account = @0xCAFE)]
    fun test_publish_balance(account: signer) acquires Balance {
        publish_balance(&account);
        let addr = signer::address_of(&account);
        assert!(balance_of(addr) == 0, 0);
    }

But I am getting this error. enter image description here


Solution

  • Check out the signature of publish_balance:

    public fun publish_balance<CoinType>(account: &signer) {
    

    You can see that this function takes a generic type parameter called CoinType but when you call the function in your test you are not specifying the generic type param.

    Try something like this instead:

    publish_balance<AptosCoin>(&account);