AVM icon indicating copy to clipboard operation
AVM copied to clipboard

There should be a deploy method for the main class

Open fulldecent opened this issue 6 years ago • 2 comments

If a class has a method named deploy and this method is annotated Callable then it should be called at deploy time.

This will remove the need for accessing Blockchain.getData() directly, like this:

    /**
     * This initialization is run at deploy time.
     */
    static {
        ABIDecoder decoder = new ABIDecoder(Blockchain.getData());
        String tokenName = decoder.decodeOneString();
        String tokenSymbol = decoder.decodeOneString();
        String tokenUriPrefix = decoder.decodeOneString();
        String tokenUriPostfix = decoder.decodeOneString();
        NFTokenMock.setTokenNameSymbolAndUriAffixes(tokenName, tokenSymbol, tokenUriPrefix, tokenUriPostfix);
    }

Instead the type safe alternative is:

@Callable
public static void deploy(String newTokenName, String newTokenSymbol, String newUriPrefix, String newUriPostfix) {
    ...
}

Benefits:

  1. It hides the details of ABI decoding, which the programmer does not care about
  2. It makes the ABI more clear because the types in deploy match the types in the ABI
    • The deployment call types actually are not part of the ABI anyway and they are an implementation detail for the contract users (this is a separate issue)

Work plan

  • [ ] Call deploy if exists
  • [ ] It shall be a compiler error if a the contract has a deploy method and any class has an @Instantiatable variable

fulldecent avatar Aug 15 '19 18:08 fulldecent

This is what the @Initializable annotation is for.

jeff-aion avatar Aug 16 '19 21:08 jeff-aion

@Initializable allows you to set values only for static variables. Static variables have an extremely narrow use case and and probably none of those use cases overlap with setting the static variables at deploy time.

The only use case of @Initializable therefore is spells and counterfactuals.

fulldecent avatar Aug 16 '19 21:08 fulldecent