Move script that accepts address of sender, receiver and amount to be transferred

Hello,

I have recently started learning the Move language. I was wondering how can i modify the following example from official docs such that it accepts takes three arguments: sender’s address, receiver’s address, and amount to be transferred.

Example from docs:

syed@localnet:~$ cat my_module.move
address 0x9f9e410eaf01862e5fd2d1b537d32122:

module MyModule {
  use 0x0::Libra;
  use 0x0::LBR;

  // The identity function: takes a Libra::T<LBR::T> as input and hands it back
  public fun id(c: Libra::T<LBR::T>): Libra::T<LBR::T> {
    c
  }
}


syed@localnet:~$ cat custom_script.move

use 0x0::LibraAccount;
use 0x0::LBR;
use 0x0::Transaction;
use 0x9f9e410eaf01862e5fd2d1b537d32122::MyModule;

fun main(amount: u64) {
  let coin = LibraAccount::withdraw_from_sender<LBR::T>(amount);
  // Calls the id procedure defined in our custom module
  LibraAccount::deposit<LBR::T>(Transaction::sender(), MyModule::id(coin));
}

I am trying to come up with something that could be executed like this:

libra% dev execute “sender” “receiver” “amount” /tmp/*.mv

Thanks!

Cheers,
Syed

I was wondering how can i modify the following example from official docs such that it accepts takes three arguments: sender’s address, receiver’s address, and amount to be transferred.

Here is one attempt; let me know if this is not what you were looking for!

use 0x0::LibraAccount;
use 0x0::LBR;
use 0x0::Transaction;
use 0x9f9e410eaf01862e5fd2d1b537d32122::MyModule;
fun main(sender: address, recipient: address, amount: u64) {
  Transaction::assert(sender == Transaction::sender,  0);
  let coin = LibraAccount::withdraw_from_sender<LBR::T>(amount);
  // Calls the id procedure defined in our custom module
  LibraAccount::deposit<LBR::T>(recipient, MyModule::id(coin));
}

Note that the sender parameter is somewhat redundant because the sender can also be retrieved via Transaction::sender().

Also, it’s worth noting that MyModule::id can be simplified a bit using Move generics:

module MyModule {
  public fun id<Typ>(t: Typ): Typ {
    t
  }
}

Hi Sam,

Thanks a lot for your message. The code above worked liked a charm after adding parentheses to the Transaction::sender() call.

libra% dev compile 0 /home/syed/custom_script.move /home/syed/my_module.move /home/syed/libra/language/stdlib/modules
>> Compiling program
    Finished dev [unoptimized + debuginfo] target(s) in 0.24s
     Running `target/debug/move-build -f /home/syed/custom_script.move -s 9f9e410eaf01862e5fd2d1b537d32122 -o /tmp/00676c8908703c5f4d15e1ae8c09daa5 -d /home/syed/my_module.move -d /home/syed/libra/language/stdlib/modules`
Successfully compiled a program at /tmp/00676c8908703c5f4d15e1ae8c09daa5/transaction_0_script.mv

libra% account list
User account index: 0, address: 9f9e410eaf01862e5fd2d1b537d32122, sequence number: 5, status: Persisted
User account index: 1, address: 8a6cb5444e49cddc20b27d8fb2e4f09e, sequence number: 2, status: Persisted
Faucet account address: 0000000000000000000000000a550c18, sequence_number: 3, status: Persisted

libra% dev execute 0 /tmp/00676c8908703c5f4d15e1ae8c09daa5/transaction_0_script.mv 9f9e410eaf01862e5fd2d1b537d32122 8a6cb5444e49cddc20b27d8fb2e4f09e 13
waiting for 9f9e410eaf01862e5fd2d1b537d32122 with sequence number 6INFO 2020-05-15 22:43:39 client/cli/src/libra_client.rs:225 Verified version change to: 105781
.....INFO 2020-05-15 22:43:39 client/cli/src/libra_client.rs:225 Verified version change to: 105782
.................................INFO 2020-05-15 22:43:40 client/cli/src/libra_client.rs:225 Verified version change to: 105783
...............................INFO 2020-05-15 22:43:40 client/cli/src/libra_client.rs:225 Verified version change to: 105784
.................................INFO 2020-05-15 22:43:41 client/cli/src/libra_client.rs:225 Verified version change to: 105786
transaction is stored!
Successfully finished execution

libra% query balance 0
INFO 2020-05-15 22:43:57 client/cli/src/libra_client.rs:225 Verified version change to: 105827
Balance is: 119.999987


libra% query balance 1
INFO 2020-05-15 22:44:07 client/cli/src/libra_client.rs:225 Verified version change to: 105854
Balance is: 60.000013

I was wondering if you could point me towards any resources which i could use to speed up my learning process? The official documentation is unfortunately not enough at the moment.

Thanks, again!

Cheers,
Syed

Hi Syed,
We are indeed a bit behind on Move documentation and hope to rectify that in the near future! In the meantime, here are some pointers to other resources.

  • The Move standard library contributor guide) is a good starting point for playing with Move. If you drop your own modules into stdlib/modules, you’ll be able to compile + test your code without running a local node. The functional testing framework (explained briefly in the guide and thoroughly in a blog post linked from the guide) makes it easy to write test scenarios with multiple transactions and different senders to fully exercise a module.

  • The big documentation we’re lacking is a guide to the Move source language syntax. But hopefully a combination of helpful compiler error messages (we’ve worked a lot on this because it’s the one source of documentation users can’t fail to find :slight_smile: ), some familiarity with Rust, and some browsing of the existing code in language/stdlib/modules will go a long way.

  • The Move deep dive is still the most comprehensive paper on the language.

  • Here is a more recent technical paper on formalizing and proving Move resource safety, the “laws of conversation for money”.

  • This forum has some good historical questions/answers if you dig a bit.

Hi Sam,

Thanks a million for taking the time to write such a detailed reply. These pointers will definitely help a lot in finding a way around Move.

Thanks again!

Cheers,
Syed