#Custom error

20 messages · Page 1 of 1 (latest)

wide pebble
#

How to add a custom error in assertEquals?
Is there a require statement like in Solidity?

require(this.sender == owner, "CUSTOM ERROR" );

import {
  Bool,
  PublicKey,
  State,
  state,
  SmartContract,
  method,
  AccountUpdate,
  ProvablePure,
  provablePure,
} from 'snarkyjs';

export { Ownable };

type IOwnable = {
  transferOwnership: (newOwner: PublicKey) => void;
  // events
  events: {
    OwnershipTransferred: ProvablePure<{
      oldOwner: PublicKey;
      newOwner: PublicKey;
    }>;
  };
};
class Ownable extends SmartContract implements IOwnable {
  @state(PublicKey) owner = State<PublicKey>();

  events = {
    OwnershipTransferred: provablePure({
      oldOwner: PublicKey,
      newOwner: PublicKey,
    }),
  };

  @method
  public initialize(owner: PublicKey) {
    const provedState = this.account.provedState.get();
    this.account.provedState.assertEquals(provedState);
    this.owner.set(owner);
  }

  @method onlyOwner(): Bool {
    const owner = this.owner.get();
    this.owner.assertEquals(owner);

    const ownerAccountUpdate = AccountUpdate.create(owner);
    ownerAccountUpdate.requireSignature();

    return Bool(true);
  }

  /**
   * @dev Transfers ownership of the contract to a new account (`newOwner`).
   * Can only be called by the current owner.
   */
  @method transferOwnership(newOwner: PublicKey) {
    const owner = this.owner.get();
    this.owner.assertEquals(owner);

    this.sender.assertEquals(owner);

    this.owner.set(newOwner);
    this.emitEvent('OwnershipTransferred', {
      oldOwner: owner,
      newOwner: newOwner,
    });
  }
}
#
    "snarkyjs": "0.9.7"
  }```
gentle python
#

second argument for assertEquals is a custom error message, excpet for preconditions

wide pebble
gentle python
#

I don't really know why the message isnt available on certain types, it is available on Field, so for know you can turn your type into a field and assert that instead with a custom message

@void plover is there any reason to why a public key or UInt64 wouldnt allow a message in an assertion?

#

i understand that having a custom failure message for preconditions might require a deeper protocol integration - on the node side as well, but why not for types like a PublicKey or CircuitValue?

void plover
#

this.owner isn't a public key, it's a State<PublicKey>. calling assertEquals on it isn't checked immediately in the proof, instead it adds a precondition which is checked later by the verifying Mina node. There's no way for snarkyjs to know that the precondition will fail, so there's no use for a custom error message (because there is no error)

dense vale
#

@void plover is that something we could surface via typescript doc comments? As in, see it in the intellisense ?

gentle python
#

@dusk olive you should be able to see that its a different type/interface entirely, at least i do in vscode

#

but for that you have to hover, it only shows the fn name which is the same in intellisense

void plover
#

@dense vale great suggestion! the latest release (0.10) happened to add doccomments on these methods, what do you think of them?

#

fwiw -- i"ve been arguing for changing the terminology assertEquals() here, to not confuse it with the other kind of assertEquals() which directly becomes part of the proof as a constraint

something like assertEqualsOnChain(). wdyt @gentle python @dense vale @wide pebble ?

gentle python
#

how about precondition.assertEquals() as a prefix/namespace?

void plover
#

you mean like this.x.precondition.assertEquals()?

#

another nice suggestion from @scarlet flume was

this.x.expect(someValue)
#

but that's less explicit than onChain or precondition

gentle python
void plover
#

Two considerations:

  • we want API to be easily discoverable from intellisense, and I think something.precondition.assertEquals/assertBetween makes this worse than the existing API
  • precondition is a technical term that's probably not intuitive for newcomers, until they read about it.. onChain feels better in that regard
gentle python
#

personally i dont understand the discoverability argument, you hit something. and the precondition will pop up just fine no?

#

also precondition is a technical term, but its a crucial part of how the smart contracts work and i think we should not shy away from it with another layer of naming abstraction. Precondition vs onChain, its a precondition in the end.