use borsh::{BorshDeserialize, BorshSerialize};
use serde::{Deserialize, Serialize};
use super::state_schema::StateSchema;
#[derive(
Clone,
Copy,
Debug,
Default,
Eq,
PartialEq,
Hash,
BorshSerialize,
BorshDeserialize,
Serialize,
Deserialize,
)]
pub struct FunctionAttrs {
pub bits: u32,
}
impl FunctionAttrs {
pub const VIEW: u32 = 1 << 0;
pub const PAYABLE: u32 = 1 << 1;
pub const REENTRANT: u32 = 1 << 2;
pub const SPONSORED: u32 = 1 << 3;
pub const CONSTRUCTOR: u32 = 1 << 4;
pub const FALLBACK: u32 = 1 << 5;
pub const RECEIVE: u32 = 1 << 6;
pub const ENTRY: u32 = 1 << 7;
#[must_use]
pub const fn from_bits(bits: u32) -> Self {
Self { bits }
}
#[must_use]
pub const fn has(&self, bits: u32) -> bool {
self.bits & bits != 0
}
#[must_use]
pub const fn has_all(&self, bits: u32) -> bool {
self.bits & bits == bits
}
#[must_use]
pub const fn is_view(&self) -> bool {
self.has(Self::VIEW)
}
#[must_use]
pub const fn is_payable(&self) -> bool {
self.has(Self::PAYABLE)
}
#[must_use]
pub const fn is_constructor(&self) -> bool {
self.has(Self::CONSTRUCTOR)
}
}
#[derive(
Clone, Debug, Eq, PartialEq, Hash, BorshSerialize, BorshDeserialize, Serialize, Deserialize,
)]
pub enum ParamType {
U8,
U16,
U32,
U64,
U128,
I8,
I16,
I32,
I64,
I128,
Bool,
Address,
Bytes,
String,
FixedBytes(u32),
Vec(Box<ParamType>),
Map {
key: Box<ParamType>,
value: Box<ParamType>,
},
Option(Box<ParamType>),
Custom(String),
}
#[derive(
Clone, Debug, Eq, PartialEq, Hash, BorshSerialize, BorshDeserialize, Serialize, Deserialize,
)]
pub struct ParamAbi {
pub name: String,
pub ty: ParamType,
}
#[derive(
Clone, Debug, Eq, PartialEq, Hash, BorshSerialize, BorshDeserialize, Serialize, Deserialize,
)]
pub struct FunctionAbi {
pub selector: [u8; 4],
pub name: String,
pub attrs: FunctionAttrs,
pub params: Vec<ParamAbi>,
pub returns: Option<ParamType>,
}
#[derive(
Clone, Debug, Eq, PartialEq, Hash, BorshSerialize, BorshDeserialize, Serialize, Deserialize,
)]
pub struct EventAbi {
pub name: String,
pub params: Vec<ParamAbi>,
pub indexed_mask: u8,
}
impl EventAbi {
#[must_use]
pub fn topic_count(&self) -> u8 {
self.indexed_mask.count_ones() as u8
}
#[must_use]
pub fn is_indexed(&self, index: u8) -> bool {
if index >= 8 {
return false;
}
(self.indexed_mask >> index) & 1 == 1
}
}
#[derive(
Clone, Debug, Eq, PartialEq, Hash, BorshSerialize, BorshDeserialize, Serialize, Deserialize,
)]
pub struct TypeAbi {
pub name: String,
pub kind: TypeKind,
}
#[derive(
Clone, Debug, Eq, PartialEq, Hash, BorshSerialize, BorshDeserialize, Serialize, Deserialize,
)]
pub enum TypeKind {
Struct {
fields: Vec<ParamAbi>,
},
Enum {
variants: Vec<EnumVariant>,
},
}
#[derive(
Clone, Debug, Eq, PartialEq, Hash, BorshSerialize, BorshDeserialize, Serialize, Deserialize,
)]
pub struct EnumVariant {
pub name: String,
}
#[derive(
Clone,
Copy,
Debug,
Eq,
PartialEq,
Hash,
BorshSerialize,
BorshDeserialize,
Serialize,
Deserialize,
)]
#[borsh(use_discriminant = true)]
#[repr(u8)]
pub enum ContractType {
Contract = 0x00,
Parachain = 0x01,
}
#[derive(
Clone, Debug, Eq, PartialEq, Hash, BorshSerialize, BorshDeserialize, Serialize, Deserialize,
)]
pub struct ContractAbi {
pub pyde_abi_version: u32,
pub contract_type: ContractType,
pub functions: Vec<FunctionAbi>,
pub state_schema_hash: [u8; 32],
pub constructor_index: Option<u32>,
pub fallback_index: Option<u32>,
pub receive_index: Option<u32>,
pub name: String,
pub version: String,
pub events: Vec<EventAbi>,
pub parachain_imports: Vec<String>,
pub state_schema: StateSchema,
pub types: Vec<TypeAbi>,
}
impl ContractAbi {
pub const V1_0: u32 = 0x0001_0000;
pub const V1_1: u32 = 0x0001_0001;
pub const V1_2: u32 = 0x0001_0002;
pub const SCHEMA_V1: u32 = Self::V1_2;
pub const MAX_SUPPORTED: u32 = Self::V1_2;
#[must_use]
pub const fn major(version: u32) -> u16 {
(version >> 16) as u16
}
#[must_use]
pub const fn minor(version: u32) -> u16 {
(version & 0xFFFF) as u16
}
#[must_use]
pub fn function_by_selector(&self, selector: &[u8; 4]) -> Option<&FunctionAbi> {
self.functions.iter().find(|f| &f.selector == selector)
}
#[must_use]
pub fn function_by_name(&self, name: &str) -> Option<&FunctionAbi> {
self.functions.iter().find(|f| f.name == name)
}
#[must_use]
pub fn event_by_name(&self, name: &str) -> Option<&EventAbi> {
self.events.iter().find(|e| e.name == name)
}
#[must_use]
pub fn type_by_name(&self, name: &str) -> Option<&TypeAbi> {
self.types.iter().find(|t| t.name == name)
}
}
#[cfg(test)]
mod tests {
#![allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)]
use super::*;
#[test]
fn function_attrs_bit_layout_is_stable() {
assert_eq!(FunctionAttrs::VIEW, 1 << 0);
assert_eq!(FunctionAttrs::PAYABLE, 1 << 1);
assert_eq!(FunctionAttrs::REENTRANT, 1 << 2);
assert_eq!(FunctionAttrs::SPONSORED, 1 << 3);
assert_eq!(FunctionAttrs::CONSTRUCTOR, 1 << 4);
assert_eq!(FunctionAttrs::FALLBACK, 1 << 5);
assert_eq!(FunctionAttrs::RECEIVE, 1 << 6);
assert_eq!(FunctionAttrs::ENTRY, 1 << 7);
}
#[test]
fn function_attrs_helpers() {
let a = FunctionAttrs::from_bits(FunctionAttrs::VIEW | FunctionAttrs::PAYABLE);
assert!(a.is_view());
assert!(a.is_payable());
assert!(!a.is_constructor());
assert!(a.has_all(FunctionAttrs::VIEW | FunctionAttrs::PAYABLE));
}
#[test]
fn contract_type_tags_stable() {
assert_eq!(borsh::to_vec(&ContractType::Contract).unwrap(), vec![0x00]);
assert_eq!(borsh::to_vec(&ContractType::Parachain).unwrap(), vec![0x01]);
}
#[test]
fn version_packing() {
assert_eq!(ContractAbi::V1_0, 0x0001_0000);
assert_eq!(ContractAbi::V1_2, 0x0001_0002);
assert_eq!(ContractAbi::major(0x0002_0007), 2);
assert_eq!(ContractAbi::minor(0x0002_0007), 7);
}
#[test]
fn event_topic_count_and_indexed() {
let e = EventAbi {
name: "Transfer".into(),
params: vec![],
indexed_mask: 0b0000_0101,
};
assert_eq!(e.topic_count(), 2);
assert!(e.is_indexed(0));
assert!(!e.is_indexed(1));
assert!(e.is_indexed(2));
assert!(!e.is_indexed(8));
}
fn sample_abi() -> ContractAbi {
ContractAbi {
pyde_abi_version: ContractAbi::V1_2,
contract_type: ContractType::Contract,
functions: vec![FunctionAbi {
selector: [0x12, 0x34, 0x56, 0x78],
name: "transfer".into(),
attrs: FunctionAttrs::from_bits(FunctionAttrs::ENTRY | FunctionAttrs::PAYABLE),
params: vec![
ParamAbi {
name: "to".into(),
ty: ParamType::Address,
},
ParamAbi {
name: "amount".into(),
ty: ParamType::U128,
},
],
returns: Some(ParamType::Bool),
}],
state_schema_hash: [0xAB; 32],
constructor_index: None,
fallback_index: None,
receive_index: None,
name: "Token".into(),
version: "0.1.0".into(),
events: vec![EventAbi {
name: "Transferred".into(),
params: vec![
ParamAbi {
name: "from".into(),
ty: ParamType::Address,
},
ParamAbi {
name: "to".into(),
ty: ParamType::Address,
},
ParamAbi {
name: "amount".into(),
ty: ParamType::U128,
},
],
indexed_mask: 0b011,
}],
parachain_imports: vec![],
state_schema: StateSchema::empty(),
types: vec![],
}
}
#[test]
fn contract_abi_borsh_round_trip() {
let abi = sample_abi();
let bytes = borsh::to_vec(&abi).unwrap();
let decoded: ContractAbi = borsh::from_slice(&bytes).unwrap();
assert_eq!(abi, decoded);
}
#[test]
fn lookups_work() {
let abi = sample_abi();
assert!(abi.function_by_name("transfer").is_some());
assert!(abi.function_by_name("nope").is_none());
assert!(abi
.function_by_selector(&[0x12, 0x34, 0x56, 0x78])
.is_some());
assert!(abi.event_by_name("Transferred").is_some());
}
}