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: Narrow typechain SealedBool/Uint/Address utype #19

Merged

Conversation

architect-dev
Copy link
Contributor

@architect-dev architect-dev commented Dec 17, 2024

In hardhat typechain runs directly after a hardhat compile task is run. The types for SealedBool/SealedUint/SealedAddress generated by typechain look like this:

export type SealedUintStruct = { data: string; utype: BigNumberish };

export type SealedUintStructOutput = [data: string, utype: bigint] & {
  data: string;
  utype: bigint;
};

However fhenix.js expects the type of SealedUint to have utype to be constrained to the utypes associated with each Uint (0-5 for uint8-uint256). The narrowed utype field allows us to statically convert a SealedUint type into a bigint, and a SealedBool type into a bool. This allows fhenixsdk.unseal to accurately infer the unsealed return types of a sealed output function. Without the narrowed utype unseal does not correctly type the unsealed data. This PR performs a find and replace of the generated types and narrows them.

For example:

// Solidity

function getUserCounterPermitSealed(
    PermissionV2 memory permission
  ) public view withPermission(permission) returns (SealedUint memory)
  
// Counter.test.ts

const sealedCountedAmount = await counter.getUserCounterPermitSealed(args);
//          ?^ type: SealedUintStructOutput
const unsealedCountedAmount = await fhenixsdk.unseal(sealedCountedAmount);
//          ?^ type: bigint

All instances of SealedUintStruct and SealedUintStructOutput (generated in typechains configured outDir) with the narrowed versions (utype: bigint -> utype: 0 | 1 | 2 ...). This replacement step is already done in the frontend by overriding abitype and viem, and this PR adds the functionality to the hardhat side.

NOTE: This is a pretty quick and dirty way of making this replacement, the alternatives are typescript interface declaration merging (wont work since the literals would be unioned with the existing type: 0 | 1 ... | 5 | bigint), or creating a custom typechain target which changes the internals of how the types are generated (time consuming and vendor locking).

Screenshot of usage (from fhenix-hardhat-example)
CleanShot 2024-12-18 at 11 17 59@2x

@architect-dev architect-dev merged commit c3defdc into master Jan 15, 2025
1 check passed
@architect-dev architect-dev deleted the feature/typechain-sealed-struct-type-replacement branch January 15, 2025 12:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant