Dev Help - trying to deploy first smart contract on Move

Here is the code. I get error message "Error: {“message”:“PartialVMError with status INVALID_MAIN_FUNCTION_SIGNATURE”,“error_code”:“invalid_input”,“vm_error_code”:null}
"

module two_sum::addition {
    use std::signer;
    use std::error;

    struct NumbersHolder has key {
        num1: u64,
        num2: u64,
    }

    const ENO_NUMBERS: u64 = 0;

    #[view]
    public fun get_sum(account: signer): u64 acquires NumbersHolder {
        let account_addr = signer::address_of(&account);
        assert!(exists<NumbersHolder>(account_addr), error::not_found(ENO_NUMBERS));
        let holder = borrow_global<NumbersHolder>(account_addr);
        holder.num1 + holder.num2
    }

    public entry fun set_numbers(account: signer, a: u64, b: u64) acquires NumbersHolder {
        let account_addr = signer::address_of(&account);
        if (!exists<NumbersHolder>(account_addr)) {
            move_to(&account, NumbersHolder { num1: a, num2: b })
        } else {
            let holder = borrow_global_mut<NumbersHolder>(account_addr);
            holder.num1 = a;
            holder.num2 = b;
        }
    }


    #[test(account = @0x1)]
    public entry fun test_set_numbers_and_get_sum(account: signer) acquires NumbersHolder {
        let addr = signer::address_of(&account);
        set_numbers(account, 5, 10);
        assert!(get_sum(account) == 15, ENO_NUMBERS);
    }
}
37 Likes

I tried your code on Aptos assistant and it provided the following as corrected code:

module two_sum::addition {
use std::signer;
use std::error;

struct NumbersHolder has key {
    num1: u64,
    num2: u64,
}

const ENO_NUMBERS: u64 = 0;

#[view]
public fun get_sum(account: signer): u64 acquires NumbersHolder {
    let account_addr = signer::address_of(&account);
    assert!(exists<NumbersHolder>(account_addr), error::not_found(ENO_NUMBERS));
    let holder = borrow_global<NumbersHolder>(account_addr);
    holder.num1 + holder.num2
}

public entry fun set_numbers(account: signer, a: u64, b: u64) acquires NumbersHolder {
    let account_addr = signer::address_of(&account);
    if (!exists<NumbersHolder>(account_addr)) {
        move_to(&account, NumbersHolder { num1: a, num2: b })
    } else {
        let holder = borrow_global_mut<NumbersHolder>(account_addr);
        holder.num1 = a;
        holder.num2 = b;
    }
}

#[test(account = @0x1)]
public entry fun test_set_numbers_and_get_sum(account: signer) acquires NumbersHolder {
    let addr = signer::address_of(&account);
    set_numbers(account, 5, 10);
    assert!(get_sum(account) == 15, ENO_NUMBERS);
}

}

24 Likes

thank you will try this! appreciate the help.

11 Likes

hey Chiru did it work?

11 Likes

This is a great showcasing of Aptos Assistant - I will have to look further into this when testing my own modules.

10 Likes

good one, i hope you get the help you seek friend

6 Likes

this lot of effort, I wish you the best my friend

6 Likes

all the best fren

7 Likes

Go to the node section .

2 Likes

For any help with Move, feel free to check out the dev discussion channel of the Aptos official server.

You just need to get a dev role first in the choose role channel to see it. The Aptos Assistant is a very useful tool. Just make sure to double check its answers by asking it if it is sure about the answer. Some of these chatbots can hallucinate at times so also check in with the dev discussion chat to make sure the code answer is a solid one.

Happy learning !

7 Likes

best code cc

2 Likes

thank you!

2 Likes

hey what resources did you use to learn Move initially? (OP or anyone else in this thread) trying to get started but having a tough time tbh! :frowning:

3 Likes

commenting to follow this thread. trying to start on Move dev also.

5 Likes

Ok, update - with no change to the code, but just fixing that indentation error (based on what was shared in this thread)… still getting the same error message.

3 Likes

Wow this so amazing.

3 Likes

Very useful thread!

3 Likes

thanks! appreciate it :smiley:

2 Likes

I tried this and it worked - try this code instead, and let me know if you figure out what changed that made it work :slight_smile:

module two_sum_v2::additionv2 {
  use std::signer;
  use std::error;
  use aptos_framework::account;
  use aptos_framework::event;

  struct NumbersHolder has key {
      num1: u64,
      num2: u64,
      numbers_change_events: event::EventHandle<NumbersChangeEvent>,
  }

  struct NumbersChangeEvent has drop, store {
    from_num1: u64,
    from_num2: u64,
    to_num1: u64,
    to_num2: u64,
  }

  const ENO_NUMBERS: u64 = 1001; // Use a unique non-zero error code

  #[view]
  public fun get_sum(addr: address): u64 acquires NumbersHolder {
    assert!(exists<NumbersHolder>(addr), error::not_found(ENO_NUMBERS));
    *&borrow_global<NumbersHolder>(addr).num1 + *&borrow_global<NumbersHolder>(addr).num2
  }

  public entry fun set_numbers(account: signer, a: u64, b: u64) acquires NumbersHolder {
      let account_addr = signer::address_of(&account);
      if (!exists<NumbersHolder>(account_addr)) {
          move_to(&account, NumbersHolder { num1: a, num2: b, numbers_change_events: account::new_event_handle<NumbersChangeEvent>(&account), 
        })
      } else {
          let old_numbers_holder = borrow_global_mut<NumbersHolder>(account_addr);
          let from_num1 = *&old_numbers_holder.num1;
          let from_num2 = *&old_numbers_holder.num2;
          event::emit_event(&mut old_numbers_holder.numbers_change_events, NumbersChangeEvent {
              from_num1,
              from_num2,
              to_num1: copy a,
              to_num2: copy b,
          });
          old_numbers_holder.num1 = a;
          old_numbers_holder.num2 = b;
      }
  }

  #[test(account = @0x1)]
  public entry fun test_set_numbers_and_get_sum(account: signer) acquires NumbersHolder {
      let addr = signer::address_of(&account);
      aptos_framework::account::create_account_for_test(addr);
      set_numbers(account, 5, 10);
      assert!(get_sum(addr) == 15, ENO_NUMBERS);
  }
}
5 Likes

Hey folks! I just got this to deploy correctly via MultiSig using M-safe, Petra and Pontem wallets.

Take a look and see how it works for you! Maybe we can work together on more “advanced Mathematics” stuff if anyone finds this interesting.

4 Likes