How to push string array into vector ? #112
-
Hello, I'm new to the Move language and I don't understand how to add an array of strings in a vector. So, I made a contract that registers wallet addresses. My issue is that when a user calls the registerWallets contract with, for example, the argument [a, b], it saves this [a, b] array. However, when the user calls it again with different arguments [c, d], it only saves [c, d] and not [a, b, c, d]. `module WalletRegistrator::walletregister {
}` |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
How to append a vector onto another vectorSo what you're trying to accomplish is like most languages where you want to do the following:
You can do this with the How do you apply it to your examplestruct UserWallets has key {
walletAddress: vector<string::String>,
}
public entry fun registerWallets(caller: &signer, walletAddress: vector<string::String>) acquires UserWallets {
let caller_address = signer::address_of(caller);
if (!exists<UserWallets>(caller_address)) {
move_to(caller, UserWallets {
walletAddress: walletAddress,
})
} else {
let account_addresses = borrow_global_mut<UserWallets>(caller_address);
// Simply use append here
vector::append(&mut account_addresses.walletAddress, walletAddress);
}
}
#[view]
public fun get_registered_wallets(addr: address): vector<string::String> acquires UserWallets {
borrow_global<UserWallets>(addr).walletAddress
} |
Beta Was this translation helpful? Give feedback.
How to append a vector onto another vector
So what you're trying to accomplish is like most languages where you want to do the following:
You can do this with the
vector::append()
See https://github.com/aptos-labs/aptos-core/blob/main/aptos-move/framework/move-stdlib/sources/vector.move#L100-L104How do you apply it to your example