Skip to main content

Methods

Everything we have learned so far converges on this point... methods. The methods are the instruction/endpoints of our smart contract, the client and other smart contracts will be calling or extending these methods.

tip

You can define any number of methods required for your use case.

tip

If you are new to the CIDL, we recommend you start learning from understanding what the CIDL

Definition

methods is an array of objects where each object is a method

methods:
- name: create_article
summary: Creates an NFT with the corresponding article type as metadata, where the metadata will be compress
solana:
compress:
- type: Article
mode: append
signers:
- name: fee_payer
type: sol:account
attributes: [ sol:writable ]
- input: delegate
uses:
- csl_spl_token.initialize_mint2
- csl_spl_assoc_token.create
- csl_spl_token.mint_to
- csl_spl_token.set_authority
inputs:
- name: warehouse
type: sol:account<Warehouse, seeds.UniquePerUser(owner=owner)>
- name: mint
type: sol:account<csl_spl_token.Mint>
attributes: [ sol:init ]
description: The Non-Fungible Token of this Article
- name: delegate
type: sol:account
attributes: [ sol:owner=11111111111111111111111111111111 ]
- name: name
type: string
- name: description
type: string
- name: thumbnail
type: string
tip

Check the Inventory guide to learn how to implement methods like the above

KeywordTypeOptionalityDescription
methodsArray<MethodBody>Required

Method Body

KeywordTypeOptionalityDescription
namestringRequiredThe name of the method, it must follow the targeted programming naming convention
summarystringRecommendedDocumentation summary of the method. Supports markdown
solanaSolanaOptional
signersArray<SignerBody>Optional
inputsArray<InputBody>Optional
usesArray<string>OptionalList of reference methods in the form of ref.method_name, where ref is the value set in the imports
Solana Method Extension
KeywordTypeOptionalityDescription
default-payerbooleanOptionalBy default, it is set to true. Set to false with an empty signer list to remove all signers from the method
compressArray<CompressBody>OptionalList of types to be compress.
Compress Body
KeywordTypeOptionalityDescription
typeType | ref.TypeRequiredCan reference imported CIDLs in the form of ref.type_name, where ref is the value set in the imports
modeappend | insert_or_append | replace_leaf | verify_leafRequiredHow should this type be compress. Each mode corresponds to a method of the state compression program
Signer Body
KeywordTypeOptionalityDescription
namestringMutually exclusiveThe name for the signing account.
typeType | ref.TypeMutually exclusiveCan reference imported CIDLs in the form of ref.type_name, where ref is the value set in the imports
inputstringMutually exclusiveReference to an input's name. It will set the input as a signer
attributesArray<string>OptionalAttributes to modify/extend the type. Only applicable when type is defined
info

name/type are mutually exclusive with input. This means that name/type needs to be set or input, both cannot be defined at the same time.

Defined signers or inputs marked as signers will generate the following security check.

methods:
- name: method_with_custom_signer
signers:
- name: fee_payer
type: sol:account
attributes: [ sol:writable ]
- input: delegate
inputs:
- name: delegate
type: sol:account
attributes: [ sol:owner=11111111111111111111111111111111 ]
pub fn process_method_with_custom_signer(/* ... */) -> ProgramResult {
// ...

if fee_payer_info.is_signer != true {
return Err(CounterError::InvalidSignerPermission.into());
}

if delegate_info.is_signer != true {
return Err(CounterError::InvalidSignerPermission.into());
}

// ...
}
Input Body
KeywordTypeOptionalityDescription
namestringRequiredThe name of the parameter, it must follow the targeted programming naming convention
typenative | extended | Type | ref.TypeRequiredThe data type of this parameter<br /><br />Can reference imported CIDLs in the form of ref.type_name, where ref is the value set in the imports
descriptionstringRecommendedDocumentation of the input. Supports markdown
attributesArray<string>OptionalAttributes to modify/extend the type
info

We can specify any native, extended, or custom data type to the input type.

Web-based documentation

The methods object can be visualized in the CIDL web-based doc. The documentation is automatically generated from our vscode-codigo extension.

[//] # (This CIDL web-based doc can be generated by typing the command:)

[//] # (```shell)

[//] # (codigo solana generate ./counter.cidl --doc)

[//] # (```)

CIDL Web-based doc

Next steps

Congratulations! 🎉👏 at this point, you should have a basic understanding of the CIDL, how to work with native, extended, and custom-defined types, and import other CIDL and implement methods. The next step on your journey is to start implementing use cases. You can follow our guides to start implementing use cases step by step or check our examples here

Join the Código community 💚

Código is a growing community of developers. Join us on Discord and GitHub