Code Template
Steps
1. Implement CommandTrait :
CommandTrait :fn name() -> Name: return the name of the commandfn inputs() -> Vec<CmdInputDescription>: return an array of input description, fields are:name: name of the inputtype_bounds: allowed typesrequiredpassthrough: iftrue, this field will be present in the output
fn outputs() -> Vec<CmdOutputDescription>:name: name of the outputtype: type of the output
async fn run(&self, ctx: Arc<Context>, inputs: ValueSet) -> Result<ValueSet, Error> {}
2. Submit with inventory::submit
inventory::submit3. 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.
value::to_map.Documentation on https://serde.rs/ is useful.
Example

Last updated
Was this helpful?