Code Template

Steps

1. Implement CommandTrait :

  1. fn name() -> Name : return the name of the command

  2. fn inputs() -> Vec<CmdInputDescription> : return an array of input description, fields are:

    1. name: name of the input

    2. type_bounds : allowed types

    3. required

    4. passthrough : if true, this field will be present in the output

  3. fn outputs() -> Vec<CmdOutputDescription>:

    1. name: name of the output

    2. type: type of the output

  4. async fn run(&self, ctx: Arc<Context>, inputs: ValueSet) -> Result<ValueSet, Error> {}

2. Submit with inventory::submit

3. To get inputs, define a struct that implement Deserialize:

#[derive(Serialize, Deserialize, Debug)]
pub struct Input {
    // special types like Keypair, Pubkey, Signature, Decimal
    // have to be decorated with `#[serde(with = "...")]`
    #[serde(with = "value::keypair")]
    pub sender: Keypair,
    #[serde(with = "value::pubkey")]
    pub recipient: Pubkey,
    // use default and ::opt for optional value
    #[serde(default, with = "value::pubkey::opt")]
    pub opt_pubkey: Option<Pubkey>,
    #[serde(with = "value::decimal")]
    pub amount: Decimal,
    // optional input with a default value,
    // use `#[serde(default = ...)]`
    #[serde(default = "value::default::bool_true")]
    pub submit: bool,
}

Then get the input from a ValueSet with value::from_map :

4. Output is the same, but derive Serialize on it and use value::to_map.

Documentation on https://serde.rs/ is useful.

Example

Last updated

Was this helpful?