Skip to content

Commit 9f9cc95

Browse files
committed
docs: add fallback example and use empty buffer for unknown selector
1 parent 60cfaac commit 9f9cc95

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
lines changed

crates/pvm-contract-macros/src/codegen/contract.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -390,9 +390,9 @@ pub fn expand_contract(args: ContractArgs, input: ItemMod) -> syn::Result<TokenS
390390
}
391391
} else {
392392
if use_alloc {
393-
quote! { Err(b"UnknownSelector".to_vec()) }
393+
quote! { Err(Vec::new()) }
394394
} else {
395-
quote! { Err(b"UnknownSelector") }
395+
quote! { Err(b"") }
396396
}
397397
};
398398

crates/pvm-contract-macros/src/lib.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ use syn::{parse_macro_input, ItemFn, ItemMod};
4848
///
4949
/// #[pvm_contract::method]
5050
/// pub fn transfer(to: Address, amount: U256) -> Result<(), Error> { Ok(()) }
51+
///
52+
/// #[pvm_contract::fallback]
53+
/// pub fn fallback() -> Result<(), Error> { Err(Error::UnknownSelector) }
5154
/// }
5255
/// ```
5356
///
@@ -73,6 +76,9 @@ use syn::{parse_macro_input, ItemFn, ItemMod};
7376
///
7477
/// #[pvm_contract::method]
7578
/// pub fn transfer(to: Address, amount: U256) -> Result<(), Error> { Ok(()) }
79+
///
80+
/// #[pvm_contract::fallback]
81+
/// pub fn fallback() -> Result<(), Error> { Err(Error::UnknownSelector) }
7682
/// }
7783
/// ```
7884
///
@@ -143,14 +149,14 @@ use syn::{parse_macro_input, ItemFn, ItemMod};
143149
/// pvm_contract::api::call_data_copy(&mut call_data, 0);
144150
///
145151
/// let result: Result<Option<Vec<u8>>, Vec<u8>> = (|| {
146-
/// if call_data.len() < 4 { return Err(b"UnknownSelector".to_vec()); }
152+
/// if call_data.len() < 4 { return Err(Vec::new()); }
147153
/// let selector: [u8; 4] = call_data[0..4].try_into().unwrap();
148154
///
149155
/// match selector {
150156
/// [0x18, 0x16, 0x0d, 0xdd] => { // totalSupply()
151157
/// Ok(Some(my_token::total_supply().to_be_bytes::<32>().to_vec()))
152158
/// }
153-
/// _ => Err(b"UnknownSelector".to_vec()),
159+
/// _ => Err(Vec::new()),
154160
/// }
155161
/// })();
156162
///

0 commit comments

Comments
 (0)