#Calling initialize method with multiple arguments, including a Struct

6 messages · Page 1 of 1 (latest)

gray jetty
#

I need to design a custom token which will receive many parameters, more than 10, that's why I want to use a struct as parameter, besides simple parameters. This results in the following initialize method, simplified just for testing, but will have more parameters in the end.
pub fn initialize(
e: Env,
admin: Address,
decimal: u32,
name: String,
symbol: String,
token_props: ConfigProps
)
where the struct is the following one:
#[derive(Clone)]
#[contracttype]
pub struct ConfigProps {
pub _is_mintable: bool,
pub _is_burnable: bool,
}
I try to call the initialize method, after I deploy the contract, using this command, passing the struct as JSON:
stellar contract invoke `

--id ID --source-account alice
--network testnet --
initialize --admin alice
--decimal 7 --name test-token
--symbol "symbol" `
--token_props ‘{"_is_mintable":false,"_is_burnable":true}’

and I get the following error. thread 'main' panicked at ...
not yet implemented: Not implemented for UdtStructV0(
ScSpecUdtStructV0 {
doc: StringM(),
lib: StringM(),
name: StringM(ConfigProps),
fields: VecM(
[
ScSpecUdtStructFieldV0 {
doc: StringM(),
name: StringM(is_burnable),
type
: Bool,
},
ScSpecUdtStructFieldV0 {
doc: StringM(),
name: StringM(is_mintable),
type
: Bool,
},
....

Can you help me solve this? or is there another way in which I can pass parameters and structs in my function calls ? Is this issue present when calling from Javscript?

rich nexus
#

Consider an alternative approach:

Instead of passing token_props into initialize as a struct, consider passing each of its properties into initialize like the other arguments. Then, instantiate the token_props struct with those properties within the initialize function as shown in the screenshot.

gray jetty
#

Thank you for reaching out. I have considered this option, but I will need around 15-20 parameters in the constructor, and when I tried this way, I got an error that rust soroban accepts only 10 parameters in a function

gray jetty
#

Another note, I read the other discussion from a thread regarding this topic, I saw that even yesterday someone called a method with a JSON and other parameters and says it worked, but I do not see anything different from those calls and my call which fails

rich nexus
#

I created a simple contract and successfully invoked it by passing a struct into it, similar to what you are trying to do, without any issues.

There may be something else in your contract causing this problem. Can you share it so I can take a look?

gray jetty
#

Thank you for helping out. I don't know if I can share it fully, as I have used the token example from the official soroban examples and it has a lot of files. You can replicate this by using that example, adding in the "storage_types.rs" this code

#[derive(Clone)]
#[contracttype]
pub struct ConfigProps {
pub _is_mintable: bool,
pub _is_burnable: bool,
}

pub static mut CONFIG_PROPS: Option<ConfigProps> = None;

and then just import it in the contract.rs file, and use it in the initialize method

impl Token {
pub fn initialize(
e: Env,
admin: Address,
decimal: u32,
name: String,
symbol: String,
token_props: ConfigProps
) { ... }
I did not call it in the initialize I just wanted to make sure I can pass it.