near / near-api-js

JavaScript library to interact with NEAR Protocol via RPC API
https://near.github.io/near-api-js
MIT License
388 stars 240 forks source link

Extend Contract class to enable view function calls without Account #1325

Closed denbite closed 3 months ago

denbite commented 3 months ago

Pre-flight checklist

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({ args: {} });

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, args: {} });

Test Plan

Related issues/PRs

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

changeset-bot[bot] commented 3 months ago

🦋 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

denbite commented 3 months ago

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)