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

feat!: prettify typegen api #2824

Merged
merged 101 commits into from
Aug 6, 2024
Merged

feat!: prettify typegen api #2824

merged 101 commits into from
Aug 6, 2024

Conversation

arboleya
Copy link
Member

@arboleya arboleya commented Jul 23, 2024

Release notes

In this release, we:

  • Made typegen API more straightforward and ergonomic

Summary

Parts of our great experience are shipped through the Typegend files, which contain part of the boilerplate necessary to make some things work. This PR revisits and trims these templates nicely to support a more ergonomic and intuitive API.

All main Typegen templates have changed:

  1. Contract
  2. ContractFactory ⁽¹⁾
  3. Script
  4. Predicate

⁽¹⁾ Contract bytecode lives inside the Factory class now, no more .hex.ts files

To follow these changes, we also modified:

  • launchTestNode utility
  • Predicate class

Special thanks ❤️

Goes to @petertonysmith94, for the rigorous reviews and additional API prettification via:

Thanks also to @nedsalk for all the involvement, not letting this fade out, the original issues, reviews, and also for:

Releasing this will undoubtedly pave the way for other good things to happen!

Breaking Changes

Predicate class

  • Predicate class constructor parameters renamed: inputData > data
// before
import { Predicate } from 'fuels';

const predicate = new Predicate({
  ...unchangedParameters,
  inputData,
});
// after
import { Predicate } from 'fuels';

const predicate = new Predicate({
  ...unchangedParameters,
  data,
});
  • Typegen extended/generated Predicate now accepts a single parameter for initialization
// before
import { TestPredicateAbi__factory } from './typegend';

TestPredicateAbi__factory.createInstance(provider, data, configurableConstants);
// after
import { TestPredicate } from './typegen';

new TestPredicate({
  provider,
  data,
  configurableConstants
});

launchTestNode utility

  • Renamed contractsConfigs[].deployer to contractsConfigs[].factory
  • Removed contractsConfigs[].bytecode and .hex.ts file

The bytecode is now saved within the factory class, so you don't have to deal with it.

// before
import { TokenAbi__factory } from './typegend';
import TokenAbiHex from './typegend/contracts/TokenAbi.hex';

using launched = await launchTestNode({
  contractsConfigs: [{
    deployer: TokenAbi__factory,
    bytecode: TokenAbiHex
  }],
});
// after
import { TokenFactory } from './typegend';

using launched = await launchTestNode({
  contractsConfigs: [{
    factory: TokenFactory,
  }],
})

Renamed method deployContract to deploy

Removed the redundant suffix on the ContractFactory class method name.

// before
import { ContractFactory } from 'fuels';

const factory = new ContractFactory(wallet);

factory.deployContract();
// after
import { ContractFactory } from 'fuels';

const factory = new ContractFactory(wallet);

factory.deploy();

Typegen Contract template

  • Removed Abi__factory suffix from class names
  • The file <name>.hex was removed (access it via <Name>.bytecode)
  • The files <name>__factory.ts and <name>.d.dts were merged into <name>.ts
  • The class <Name> and the interface <Name>Abi are now just <Name>
  • Method <Name>Factory.deployContract() renamed to <Name>Factory.deploy()
  • You may need to remove the previously generated <typegenDir>/contracts/factories directory
// before
import { TestContractAbi, TestContract__factory } from './typegen'
import testContractBytecode from './typegen/contracts/TestContract.hex'

const instance = await TestContract__factory.connect(id, wallet);

const deploy = await TestContract__factory.deployContract(testContractBytecode, wallet);
const { contract } = await deploy.waitForResult();
// after
import { TestContract, TestContractFactory } from './typegen'

const instance = new TestContract(id, wallet);

const deploy = await TestContractFactory.deploy(wallet);
const { contract } = await deploy.waitForResult();

Typegen Predicate template

  • Removed Abi__factory suffix from class names
  • Started accepting a single param object in constructor
  • You may need to remove the previously generated <typegenDir>/predicates/factories directory
// before
import { TestPredicateAbi__factory } from './typegen'

const predicate = TestPredicateAbi__factory.createInstance(provider);
// after
import { TestPredicate } from './typegen'

const predicate = new TestPredicate({ provider });

Typegen Script template

  • Removed Abi__factory suffix from class names
  • You may need to remove the previously generated <typegenDir>/scripts/factories directory
// before
import { TestPredicateAbi__factory } from './typegen'

const script = TestScriptAbi__factory.createInstance(wallet);
// after
import { TestPredicate } from './typegen'

const script = new TestScript(wallet);

Checklist

  • I addedtests to prove my changes
  • I updated — all the necessary docs
  • I reviewed — the entire PR myself, using the GitHub UI
  • I described — all breaking changes and the Migration Guide

nedsalk
nedsalk previously approved these changes Aug 6, 2024
Copy link
Contributor

github-actions bot commented Aug 6, 2024

Coverage Report:

Lines Branches Functions Statements
79.51%(+0%) 71.75%(-0.05%) 77.49%(+0.01%) 79.65%(+0%)
Changed Files:
Ok File (✨=New File) Lines Branches Functions Statements
✨ packages/abi-typegen/src/templates/contract/main.ts 100%
(+100%)
100%
(+100%)
100%
(+100%)
100%
(+100%)
✨ packages/abi-typegen/src/templates/predicate/main.ts 100%
(+100%)
100%
(+100%)
100%
(+100%)
100%
(+100%)
✨ packages/abi-typegen/src/templates/script/main.ts 100%
(+100%)
100%
(+100%)
100%
(+100%)
100%
(+100%)
🔴 packages/abi-typegen/src/templates/utils/formatEnums.ts 100%
(+0%)
50%
(-50%)
100%
(+0%)
100%
(+0%)
🔴 packages/abi-typegen/src/templates/utils/formatStructs.ts 100%
(+0%)
50%
(-50%)
100%
(+0%)
100%
(+0%)

Copy link
Contributor

@petertonysmith94 petertonysmith94 left a comment

Choose a reason for hiding this comment

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

🚢

Copy link
Contributor

@nedsalk nedsalk left a comment

Choose a reason for hiding this comment

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

:shipit:

@petertonysmith94 petertonysmith94 merged commit 799db38 into master Aug 6, 2024
20 checks passed
@petertonysmith94 petertonysmith94 deleted the aa/feat/typegen-prettify branch August 6, 2024 12:52
@petertonysmith94
Copy link
Contributor

petertonysmith94 commented Aug 7, 2024

@arboleya after this change, whenever I rebuild locally, the old factories remain in place:

contracts/factories/TestContractAbi__factory.ts

Then I have experienced type errors:

template-nextjs:build: Type error: Property 'deployContract' does not exist on type 'ContractFactory'.
template-nextjs:build: 
template-nextjs:build:   102 |     const factory = new ContractFactory(bytecode, _abi, wallet);
template-nextjs:build:   103 |
template-nextjs:build: > 104 |     return factory.deployContract<TestContractAbi>({
template-nextjs:build:       |                    ^
template-nextjs:build:   105 |       storageSlots: _storageSlots,
template-nextjs:build:   106 |       ...options,
template-nextjs:build:   107 |     });

May it be worth deleting the typegen folder before a typegen build?

@arboleya
Copy link
Member Author

arboleya commented Aug 7, 2024

@petertonysmith94 Deleting things is always very dangerous.

However, these old files should not be a problem if they're just there and never imported.

The error you pasted looks valid, no? The deployContract method was renamed to deploy.

@petertonysmith94
Copy link
Contributor

@arboleya You're right - deleting does seem dangerous.

The error is valid and will cause issues if consumers have TS checking across their project.

Maybe we could add an advisory in the release notes that this factory folder should be deleted?

@arboleya
Copy link
Member Author

arboleya commented Aug 8, 2024

Oh, this is a very specific tip.

Would you mind editing the PR description and adding this mention?

It will be re-fetched and used in the changelog before the next release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feat Issue is a feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add bytecode to factory outputted by typegen Prettify typegen API
6 participants