I am trying to implement a custom token with custom functionality which implemets the soroban_sdk::token trait . However I do realize that all of the methods in soroban_sdk::token trait panic when there is an error rather than return result. Is there a reason for this design? if I implement this trait I have no choice but to panic when there is an error in all the methods such as transfer, burn etc and this will make it impossible to provde specific errors when other contracts interract with my token or even a user interface. why was this design decision made? is this a best practice for soraban smart contracts to panic rather than return results ? or is there another token trait I could implmenet rather than soraban_sdk::token?
#Why do the functions in token::interface panic instead of return error Result e.g
5 messages · Page 1 of 1 (latest)
panic is generally cheaper from the Wasm size standpoint (which, in turn, is one of the main factors of your contract's cost). you can use panic_with_error! to return the precise error without significant costs. from the caller perspective it would look exactly the same as if your contract return Result<(), MyTokenError (when they're calling it with try_.., of course)
e.g. Stellar Asset contract returns errors in this way
do note though that errors aren't a part of the standard (which makes sense - different implementations might have different error conditions).
Thanks for the explanation. it works perfectly with panic_with_error.