Skip to content

Commit

Permalink
feat: allow borsh-serialized contract fn arguments
Browse files Browse the repository at this point in the history
Let's say you have a `contract` object instantiated in your code, and
the contract it wraps has an `add_person` function which receives a
borsh-serialized struct. The following code now works:

    import { serialize } from 'near-api-js/lib/utils/serialize'

    class Person {
      constructor (proof) {
        Object.assign(this, proof)
      }
    }

    const personSchema = new Map([
      [Person, {
        kind: 'struct',
        fields: [
          ['age', 'u64'],
          ['name', 'String'],
        ]
      }]
    ])

    const person = new Person({ age: 33, name: 'Jesus' })

    contract.add_person(serialize(personSchema, person))
  • Loading branch information
chadoh committed Oct 7, 2020
1 parent d4d4cf1 commit b929507
Show file tree
Hide file tree
Showing 9 changed files with 104 additions and 84 deletions.
4 changes: 2 additions & 2 deletions lib/browser-index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/common-index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 7 additions & 2 deletions lib/contract.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions lib/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions lib/near.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/utils/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions lib/utils/rpc_errors.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

145 changes: 74 additions & 71 deletions lib/utils/serialize.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 14 additions & 3 deletions src/contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,19 @@ function nameFunction(name, body) {
}[name];
}

function isUint8Array (x) {
return x.byteLength !== undefined && x.byteLength === x.length;
}


/**
* Defines a smart contract on NEAR including the mutable and non-mutable methods
*/
export class Contract {
readonly account: Account;
readonly contractId: string;

constructor(account: Account, contractId: string, options: { viewMethods: string[], changeMethods: string[] }) {
constructor(account: Account, contractId: string, options: { viewMethods: string[]; changeMethods: string[] }) {
this.account = account;
this.contractId = contractId;
const { viewMethods = [], changeMethods = [] } = options;
Expand All @@ -28,7 +33,10 @@ export class Contract {
writable: false,
enumerable: true,
value: nameFunction(methodName, async (args: object = {}, ...ignored) => {
if (ignored.length || Object.prototype.toString.call(args) !== '[object Object]') {
if (
ignored.length ||
(Object.prototype.toString.call(args) !== '[object Object]' && !isUint8Array(args))
) {
throw new PositionalArgsError();
}
return this.account.viewFunction(this.contractId, methodName, args);
Expand All @@ -40,7 +48,10 @@ export class Contract {
writable: false,
enumerable: true,
value: nameFunction(methodName, async (args: object = {}, gas?: BN, amount?: BN, ...ignored) => {
if (ignored.length || Object.prototype.toString.call(args) !== '[object Object]') {
if (
ignored.length ||
(Object.prototype.toString.call(args) !== '[object Object]' && !isUint8Array(args))
) {
throw new PositionalArgsError();
}
validateBNLike({ gas, amount });
Expand Down

0 comments on commit b929507

Please sign in to comment.