Skip to content

Conversation

@tabaktoni
Copy link
Member

Motivation and Resolution

✅ Loading from directory
✅ Loading from sierra file
✅ Loading from casm file
✅ Error handling for missing sierra
✅ Sierra-only contracts (no casm)
✅ Integration with Contract class and declare

Implementation Details

  • Node.js only: Uses fs/path modules, not available in browsers
  • Browser stub: Provides helpful error message in browser builds
  • Build configuration: Uses esbuild plugin to swap Node/browser implementations
  • Name-based matching: Finds complementary files by basename (e.g., contract.sierra.json → contract.casm)
  • Error handling: Clear errors for missing sierra files or multiple matches

Checklist:

  • Performed a self-review of the code
  • Rebased to the last commit of the target branch (or merged it into my branch)
  • Linked the issues which this PR resolves
  • Documented the changes in code (API docs will be generated automatically)
  • Updated the tests
  • All tests are passing

@tabaktoni
Copy link
Member Author

@penovicp, Please take a look at this implementation

@tabaktoni
Copy link
Member Author

Final idea is to enable simple loading of a contract file, extending the contract to internal use contractLoader if path('pring') is provided:

      const erc20 = await Contract.factory({
        contract: './__mocks__/cairo/cairo294',
        account,
        constructorCalldata: erc20Constructor,
      });

format: ['cjs'],
globalName: 'starknet',
export default defineConfig((options) => {
const isBrowser = options.platform === 'browser';
Copy link
Collaborator

Choose a reason for hiding this comment

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

Note that the browser platform setting is only used for the iife build. I believe that most usages of starknet.js in the browser happen by bundling the esm (or cjs) build within an application. With the node specific imports being dynamic, I surmise that most users wouldn't encounter the graceful platform compatibility error but a reference error for require.

I suggest trying a different approach. Maybe something like if (typeof require === 'undefined') throw graceful error; at the start of the contractLoader function.

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.

3 participants