near-api-js icon indicating copy to clipboard operation
near-api-js copied to clipboard

Extend Contract class to enable view function calls without Account

Open denbite opened this issue 11 months ago • 2 comments

Pre-flight checklist

  • [x] I have read the Contributing Guidelines on pull requests.
  • [x] Commit messages follow the conventional commits spec
  • [x] If this is a code change: I have written unit tests.
  • [x] If this changes code in a published package: I have run pnpm changeset to create a changeset JSON document appropriate for this change.
  • [ ] If this is a new API or substantial change: the PR has an accompanying issue (closes #0000) and the maintainers have approved on my working plan.

Motivation

Many developers complained about the confusing implementation of the Contract class, which requires an Account parameter in the constructor even for view function calls

This PullRequest solves the issue by extending the interface of the first parameter in the constructor and additionally enables full backward compatibility with older versions

Old implementation

// account is required
const contract = new Contract(account, options);

contract.view_method({});
contract.call_method({});

New implementation

const contract = new Contract(connection, options);

// no account required
contract.view_method({});

// signerAccount must be specified to sign the transaction
contract.call_method({ signerAccount: account });

Test Plan

Related issues/PRs

https://github.com/near/near-api-js/pull/1066 https://github.com/near/near-api-js/discussions/1064

denbite avatar Mar 22 '24 07:03 denbite

🦋 Changeset detected

Latest commit: 24a01dcc50672f75efb109d06f462f767ccb9b04

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 4 packages
Name Type
@near-js/accounts Minor
@near-js/cookbook Patch
near-api-js Patch
@near-js/wallet-account Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

changeset-bot[bot] avatar Mar 22 '24 07:03 changeset-bot[bot]

Looks very good, only one comment and I belive the biggest change I request is with the changeset, as there are quire a few breaking changes. Please adjust the changeset file accordingly(should be a major version increment most probably). If you're not sure about how semantic versioning works, you can check the rules out here

These changes were made to be backward compatible, so releasing them to users shouldn't cause any issues (and it's not a breaking change from that perspective of view)

denbite avatar Mar 26 '24 07:03 denbite