Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Contract Types Plugin - Initial Implementation #91

Merged
merged 65 commits into from
Feb 18, 2022

Conversation

ricklove
Copy link
Collaborator

@ricklove ricklove commented Jan 20, 2022

Full readme: taqueria-plugin-contract-type-generator/Readme.md

Requires:

Related Issues:

Completes

  • contract.methods and contract.methodsObject parameters are fully typed
  • contract.storage is fully typed
  • originate storage parameter is fully typed

This plugin will generate typescript types for a contract.

In the simple case, it reads the artifacts folder for any *.tz Michelson files and will generate a types file with the matching filename for each.

Example Usage (based on an nft auction contract from open minter sdk)

export const exampleContractMethods1 = async () => {

    const Tezos = new TezosToolkit(`https://YOUR_PREFERRED_RPC_URL`)

    const contract = await Tezos.contract.at<TestContract>(`tz123`);

    contract.methods.bid(tas.nat(0));
    contract.methods.configure(
        /*opening_price:*/ tas.mutez(10),
        /*min_raise_percent:*/ tas.nat(10),
        /*min_raise:*/ tas.mutez(10),
        /*round_time:*/ tas.nat(10),
        /*extend_time:*/ tas.nat(10),
        /*asset:*/ [{
            fa2_address: tas.address(`tz123`),
            fa2_batch: [{
                amount: tas.nat(100),
                token_id: tas.nat(`100000000000000`),
            }],
        }],
        /*start_time:*/ tas.timestamp(new Date()),
        /*end_time:*/ tas.timestamp(`2020-01-01`),
    );

    // methodsObject
    contract.methodsObject.bid(tas.nat(0));
    contract.methodsObject.configure({
        asset: [{
            fa2_address: tas.address(`tz123`),
            fa2_batch: [{
                amount: tas.nat(100),
                token_id: tas.nat(`100000000000000`),
            }],
        }],
        start_time: tas.timestamp(new Date()),
        end_time: tas.timestamp(`2020-01-01`),
        extend_time: tas.nat(10),
        min_raise: tas.mutez(10),
        min_raise_percent: tas.nat(10),
        opening_price: tas.mutez(10),
        round_time: tas.nat(10),
    });

};

Example typegen task

$ taqueria typegen --typescriptDir ./types
generateTypes
{
  "typescriptDir": "./types"
}
Generating Types: /home/rick/projects/crypto/taqueria/example/artifacts => /home/rick/projects/crypto/taqueria/example/types
Contracts Found:
        - /home/rick/projects/crypto/taqueria/example/artifacts/example-contract-1.tz
Processing /example-contract-1.tz...example-contract-1.tz: Types generated

Example type output

type Storage = {
    pauseable_admin?: {
        admin: address;
        paused: boolean;
        pending_admin?: address;
    };
    current_id: nat;
    max_auction_time: nat;
    max_config_to_start_time: nat;
    auctions: BigMap<nat, {
        seller: address;
        current_bid: mutez;
        start_time: timestamp;
        last_bid_time: timestamp;
        round_time: int;
        extend_time: int;
        asset: Array<{
            fa2_address: address;
            fa2_batch: Array<{
                token_id: nat;
                amount: nat;
            }>;
        }>;
        min_raise_percent: nat;
        min_raise: mutez;
        end_time: timestamp;
        highest_bidder: address;
    }>;
};

type Methods = {
    confirm_admin: () => Promise<void>;
    pause: (param: boolean) => Promise<void>;
    set_admin: (param: address) => Promise<void>;
    bid: (param: nat) => Promise<void>;
    cancel: (param: nat) => Promise<void>;
    configure: (
        opening_price: mutez,
        min_raise_percent: nat,
        min_raise: mutez,
        round_time: nat,
        extend_time: nat,
        asset: Array<{
            fa2_address: address;
            fa2_batch: Array<{
                token_id: nat;
                amount: nat;
            }>;
        }>,
        start_time: timestamp,
        end_time: timestamp,
    ) => Promise<void>;
    resolve: (param: nat) => Promise<void>;
};

@github-actions
Copy link
Contributor

github-actions bot commented Jan 20, 2022

Git Commit Artifacts
23dfbbd Linux
MacOS
Windows
npm package installation instructions
npm install @taqueria/[email protected]
taq install @taqueria/[email protected]
taq install @taqueria/[email protected]
taq install @taqueria/[email protected]
taq install @taqueria/[email protected]
taq install @taqueria/[email protected]

@github-actions
Copy link
Contributor

Artifacts
Linux
Windows
MacOS

@cloudflare-workers-and-pages
Copy link

cloudflare-workers-and-pages bot commented Jan 21, 2022

Deploying with  Cloudflare Pages  Cloudflare Pages

Latest commit: 7725e38
Status: ✅  Deploy successful!
Preview URL: https://7d5bffd8.taqueria.pages.dev

View logs

@github-actions
Copy link
Contributor

Artifacts
Linux
Windows
MacOS

@github-actions
Copy link
Contributor

Artifacts
Linux
Windows
MacOS

@github-actions
Copy link
Contributor

Artifacts
Linux
Windows
MacOS

@github-actions
Copy link
Contributor

Artifacts
Linux
Windows
MacOS

@github-actions
Copy link
Contributor

Artifacts
Linux
Windows
MacOS

Copy link
Collaborator

@mweichert mweichert left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ricklove as usual, good stuff!

A few thoughts/suggestions:

  1. The init and scaffold tasks accept the output directory as a positional argument. Perhaps to keep to the UE consistent, we could accept a positional argument here as well instead of the typescriptDir option?

  2. We're using verbs for task names everywhere else. Similarly to the above, to achieve consistently, perhaps we could rename the task from "types" to "gen types"? @russellmorton any verb suggestions?

  3. There were a couple of inline TODOs, which is fine. Could we also create issues for those and list the issue #s beside the TODOs please?

  4. I'm glad to see all of the automated tests. I just need to check with the SDET guys to see if they have any preference to move these somewhere else. @alexzbusko ?

  5. I took forever to get you a code review on this. As such, this branch has diverged from main quite a bit. Do you mind merging main into your branch please? I promise not to take so long for the final sign-off.

Thanks again Rick. Really appreciate all of the effort on this. @alexzbusko when you're able, would you be able to pull this branch down, build it, and give it a spin once Rick makes some of the adjustments above? I'll do the same. We'll then have some manual test coverage on both Windows and Mac.

Copy link
Collaborator

@mweichert mweichert left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't wait to see this merged in. Just a few minor adjustments @ricklove

taqueria-plugin-contract-types/package.json Outdated Show resolved Hide resolved
@mweichert mweichert added the feature New feature or request label Feb 11, 2022
@mweichert mweichert added this to the Milestone 2 milestone Feb 11, 2022
@rick rick mentioned this pull request Feb 14, 2022
@russellmorton russellmorton removed this from the Stage 2 milestone Feb 16, 2022
@ricklove
Copy link
Collaborator Author

ricklove commented Feb 17, 2022

@ricklove as usual, good stuff!

A few thoughts/suggestions:

  1. The init and scaffold tasks accept the output directory as a positional argument. Perhaps to keep to the UE consistent, we could accept a positional argument here as well instead of the typescriptDir option?
  2. We're using verbs for task names everywhere else. Similarly to the above, to achieve consistently, perhaps we could rename the task from "types" to "gen types"? @russellmorton any verb suggestions?
  3. There were a couple of inline TODOs, which is fine. Could we also create issues for those and list the issue #s beside the TODOs please?
  4. I'm glad to see all of the automated tests. I just need to check with the SDET guys to see if they have any preference to move these somewhere else. @alexzbusko ?
  5. I took forever to get you a code review on this. As such, this branch has diverged from main quite a bit. Do you mind merging main into your branch please? I promise not to take so long for the final sign-off.

Thanks again Rick. Really appreciate all of the effort on this. @alexzbusko when you're able, would you be able to pull this branch down, build it, and give it a spin once Rick makes some of the adjustments above? I'll do the same. We'll then have some manual test coverage on both Windows and Mac.

@mweichert

  1. ✔ Makes sense, will fix that

  2. ✔Changed to generate types with alias for gen types and gentypes

  3. ✔ Actually, those were obsolete comments. I cleaned up and removed old comments like that.

  4. ❗ I don't think it would make sense to move these outside the plugin, they aren't tests about the plugin, but are about the correctness of the internal implementation, i.e. they are very tightly coupled to the implementation (and are more the domain of the implementer, not of testers).

    If anything it might make sense to move these to a peer folder (so they can have a different node_modules dependency set), but it would not make sense to move these tests out of the control of the plugin implementation or far away from the implementation code.

    I do have an idea to expand these substantially, so this will require a longer discussion.

  5. ✔ Sure - will merge in latest main

@ricklove
Copy link
Collaborator Author

This should be ready, just need to test the positional argument

@ricklove
Copy link
Collaborator Author

Having trouble using the positional argument, debugging now

Copy link
Collaborator

@mweichert mweichert left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great!

@mweichert mweichert merged commit 576e84f into main Feb 18, 2022
@mweichert mweichert modified the milestones: Current Release, v0.1.0 Feb 18, 2022
@hu3man hu3man deleted the feature/contract-types-generator-plugin branch August 5, 2022 18:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

TASK: Contract Types Plugin - Initial Implementation
4 participants