Passing array to entry function of smart contract in Move - possible?

Reposting from the Sui subreddit: Original Post

Hi Friends!

Suppose I want to create smart-contract which mints tokens and transfers them to the requested addresses. Actually I created one which accepts single destination wallet address. So if I want to deliver tokens to 10 addresses I need to call it 10 times.

Is there a way to pass a list / array of values as an argument of entry function? I can’t immediately find any docs on this (and how to call it say from SUI CLI, if it is possible). So that my smart-contract may accept a list of wallets and spread tokens to them according to some logic (living inside this contract also).

As workarounds, I guess i can pass them as single string, separated with spaces. Or perhaps json (as single string also), though my skills in Move currently are not so great to do it easily). Or list of arguments can be stored on-chain. But I would like to know if there is more straightforward way. Thanks!

Can anyone here help our friend RodionGork out?


User u/kristaki90 later followed up with this reply:

Hi u/RodionGork
Yes it is possible to pass an array as an argument into an entry function. Here is an example of how you can do it:

// You will need to change the entry function to accept an array of addresses.
howto::how_to_array {
use sui::tx_context::{Self, TxContext};
use sui::object::{Self, UID};
use sui::transfer;
use std::string::{Self,String};
struct VecTest has key, store{
id: UID,
myvec: vector<address>
}
public entry fun test_vec(v: vector<address>, ctx: &mut TxContext) {
transfer::transfer(
VecTest {
id: object::new(ctx),
myvec: v
},
tx_context::sender(ctx))
}
}

And then you can call it through cli like this:

sui client call \
--package 0x0a38fd5a7da36606fea72c112e0134f12abb2b6d3a545bacd21f8dbbe3f8fge6 \
--module how_to_array \
--function test_vec \
--args '[0xc6742425e89f7e4ade7de9cf20f2867b0978379f7d23a5bd7e6833b9422483af, 0xc6742425e89f7e4ade7de9cf20f2867b0978379f7d23a5bd7e6833b9422483af]' \
--gas-budget 1000
1
9 Likes

As for a simpler way, I think that passing a single line or the same json is also a good solution to this problem

4 Likes

I will look into it but can’t promise anything lol

3 Likes

You can pass a vectors

1 Like

@Rodion_at_Chirp I’m pretty sure I posted this thread on your behalf. You can continue your reddit discussion here or perhaps post what you found worked best for you.

1 Like

Hi Friends! That’s pretty correct, thanks. I initially asked at couple other places and some time later found this response, but seemingly was not yet registered here (perhaps registered specifically after seeing it here).

I dare say everything works for me, only addresses (in array) need to be passed in quoted form for some reason:

--args '["0xc6742425e89f7e4ade7de9cf20f2867b0978379f7d23a5bd7e6833b9422483af", "0xc6742425e89f7e4ade7de9cf20f2867b0978379f7d23a5bd7e6833b9422483af"]'

Otherwise I got error about type conversion when calling.

Thanks a lot once more, this was exactly the guidance I was seeking for!

2 Likes