Search code examples
substraterust-ink

How do I save a string value on Substrate's smart contract platform, ink?


  1. I initially tried the implementation as taught in this question. (How can I save string value on Substrate)
  2. However, an error occurred in relation to "ink_abi" and the struct could not be defined.
  3. Looking at the latest "ink! example"(), I tried to copy it because the struct was defined, but the following command does not work. (https://github.com/paritytech/ink/blob/master/examples/runtime-storage/lib.rs)
cargo +nightly generate-metadata
  1. How can I save the string data to the blockchain with "substrate ink!"?
  2. I would like to see a sample source if available.

Solution

  • Use ink_prelude::string::String

    #![cfg_attr(not(feature = "std"), no_std)]
    use ink_lang as ink;
    
    #[ink::contract]
    mod foo {
        use ink_prelude::string::String;
    
        // ...
    }
    

    And don't forget to add ink_prelude to your [dependencies] section in your .toml

    see: https://paritytech.github.io/ink/ink_prelude/string/struct.String.html