Finhaven / EthereumStatusCodes

Status codes for Ethereum smart contracts
Apache License 2.0
10 stars 1 forks source link

Implement custom code embedding/inclusion #13

Closed expede closed 6 years ago

expede commented 6 years ago

Via @carchrae

the examples are great, but i wonder if there isn't a more elegant way to map a validator to the hex for app specific reasons, (eg, x05 in https://github.com/Finhaven/EthereumStatusCodes/blob/2679e92250462cad6a34af863d3ec66c732c203a/contracts/validators/FinancialValidator.sol#L7 ). like if there was a helper that mapped `FinancialValidator.reasons = customCodes([ 'insufficientFunds']) - or some other method to avoid pointing to magic strings. )

A user should be able to map names from a custom enum into the main code set

For example

contract Phone {
    enum Call {
        Disconnected,
        Connected,
        CallStarted,
        OnHold
    }
}

customCode(Phone.Call.Disconnected) == hex"A0"
// true
carchrae commented 6 years ago

so, from FinancialValidator could you have

contract FinancialValidator
enum Reasons { 
   InsufficientFunds
 } 

.... 

if (_amount < 1000) { return customCode(FinancialValidator.Reasons.InsufficientFunds); }
carchrae commented 6 years ago

but i'm not sure. it would be nicer to have

if (_amount < 1000) { return FinancialValidator.Reasons.InsufficientFunds; }
carchrae commented 6 years ago

or maybe just

contract FinancialValidator {
    byte InsufficientFunds =  hex"A0";
    function checkBalance(int _amount) internal pure returns (Status.Reason) {
        if (_amount < 1000) { 
           return InsufficientFunds; 
        }
        else { 
          return Status.Reason.Success; 
        }
    }
carchrae commented 6 years ago

my eyes are burning - i'm not sure my last example makes sense. time to step away from the machine. 8)

expede commented 6 years ago

Hmm, yeah. It's a fair point. byte InsufficientFunds = hex"A0"; works 👍🏻 I don't think that it'll compile as Status.Reason. There's currently no way to go from an number to an enum in Solidity 😭 byte is a built-in type, so we can cast the Status.Reason.Success to byte.

I agree that if you have some custom codes, using the hex values directly makes sense. If someone wanted to restrict return values via a type, the enum is more restricted than byte.

I think that I see where the confusion is, and I'll add some notes to the code examples 😄