solana-idl-foundation / solana-idl-spec

Specifying a Solana IDL Standard, inspired from the Anchor IDL Standard
Apache License 2.0
20 stars 2 forks source link

Represent constants in a less ugly way #16

Open LucasSte opened 1 year ago

LucasSte commented 1 year ago

When we define a constant struct in an Anchor contract, it appears in the IDL as a string formatted as a Rust declaration. For instance, the following constant:

struct MyStr {
    suns: u64,
    mclass: Planet, // This is an enum
    data: [u8; 3],
}

#[constant]
const MY_CONST : MyStr = MyStr {suns: 5, mclass: Planet::Mercury, data: [9, 70, 33]};

Is represented in the IDL as:

"constants": [
    {
      "name": "MY_CONST",
      "type": {
        "defined": "MyStr"
      },
      "value": "MyStr { suns : 5 , mclass : Planet :: Mercury , data : [9 , 70 , 33] , }"
    }
  ]

It is awful to parse and generate the struct in a string with such a formatting when we use languages other than Rust.