Skip to content

Commit

Permalink
feat(wip): removed signer from viewMethods by dropping NAJ account.Vi…
Browse files Browse the repository at this point in the history
…ewFunction (#35)

* feat(wip): removed signer from viewMethods by dropping accounts.ViewFunction

* chore: fixed docs and removed comments

* chore: fixed linting issues

* fix: removed the use of Buffer as it won't work in browser environments

* fix: put try/catch for btoa
  • Loading branch information
jaswinder6991 authored Jul 24, 2024
1 parent b8bb3cb commit a57edc4
Show file tree
Hide file tree
Showing 20 changed files with 281 additions and 80 deletions.
12 changes: 6 additions & 6 deletions docs/advanced/reading-data.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ Getting specific values, like the example from above, you can use a set of keys:
'alice.near/profile/image/url',
'bob.near/profile/name',
],
signer, // an initialized near-api-js account
rpcURL //rpc url for your network.
});

console.log(result);
Expand Down Expand Up @@ -95,7 +95,7 @@ Getting specific values, like the example from above, you can use a set of keys:
'alice.near/profile/image/url',
'bob.near/profile/name',
],
signer, // an initialized near-api-js account
rpcURL //rpc url for your network.
}).then((result) => {
console.log(result);
/*
Expand Down Expand Up @@ -135,7 +135,7 @@ Getting specific values, like the example from above, you can use a set of keys:
'alice.near/profile/image/url',
'bob.near/profile/name',
],
signer, // an initialized near-api-js account
rpcURL //rpc url for your network.
});
console.log(result);
Expand Down Expand Up @@ -186,7 +186,7 @@ The [`get`](../api-reference/social#getoptions) function also supports wildcard
keys: [
'alice.near/profile/**',
],
signer, // an initialized near-api-js account
rpcURL //rpc url for your network.
});
console.log(result);
Expand Down Expand Up @@ -214,7 +214,7 @@ The [`get`](../api-reference/social#getoptions) function also supports wildcard
keys: [
'alice.near/profile/**',
],
signer, // an initialized near-api-js account
rpcURL, //rpc url for your network.
}).then((result) => {
console.log(result);
/*
Expand Down Expand Up @@ -243,7 +243,7 @@ The [`get`](../api-reference/social#getoptions) function also supports wildcard
keys: [
'alice.near/profile/**'
],
signer, // an initialized near-api-js account
rpcURL, //rpc url for your network.
});
console.log(result);
Expand Down
3 changes: 1 addition & 2 deletions docs/api-reference/social.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ import TOCInline from '@theme/TOCInline';

| Name | Type | Required | Default | Description |
|---------|------------------------------------|----------|---------|------------------------------------------------------------|
| options | [`IGetOptions`](types#igetoptions) | yes | - | Options that include the signer account and a set of keys. |
| options | [`IGetOptions`](types#igetoptions) | yes | - | Options that include the rpc url and a set of keys. |

#### Returns

Expand Down Expand Up @@ -78,7 +78,6 @@ import TOCInline from '@theme/TOCInline';
### `isWritePermissionGranted(options)`

> Checks if an account, specified in `options.granteeAccountId`, has been granted write access for a key.
> If the signer and the supplied `options.granteeAccountId` match, true will be returned.
#### Parameters

Expand Down
4 changes: 2 additions & 2 deletions docs/api-reference/types.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import TOCInline from '@theme/TOCInline';
| Name | Type | Required | Default | Description |
|-----------------|------------------------------------------------------------------------------------------|----------|---------|--------------------------------------------------------------------------------------------------------------------------------------------------------------|
| keys | `string[]` | yes | - | A set of key patterns to return. Each key is in the form of a pattern to return data from. For example `alice.near/profile/**` or `alice.near/profile/name`. |
| signer | [`Account`](https://near.github.io/near-api-js/classes/near_api_js.account.Account.html) | yes | - | An account that will read the data. For details on getting the account object see the NEAR API [docs](https://docs.near.org/tools/near-api-js/account). |
| rpcURL | `string` | yes | - | URL to your current network's RPC node, for example on mainnet, you connect to https://rpc.mainnet.near.org/ |
| returnDeleted | `boolean` | no | `false` | If true, will include deleted keys with the value `null`. |
| withBlockHeight | `boolean` | no | `false` | If true, for every value and a node will add the block height of the data with the key `:block`. |
| withNodeId | `boolean` | no | `false` | If true, for every node will add the node index with the key `:node`. |
Expand All @@ -34,7 +34,7 @@ import TOCInline from '@theme/TOCInline';
|------------------|------------------------------------------------------------------------------------------|----------|---------|---------------------------------------------------------------------------------------------------------------------------------------------------------|
| granteeAccountId | `string` | yes | - | The account ID to check if it has write permissions for a key. |
| key | `string` | yes | - | The key to check if the grantee has write permission. |
| signer | [`Account`](https://near.github.io/near-api-js/classes/near_api_js.account.Account.html) | yes | - | An account that will read the data. For details on getting the account object see the NEAR API [docs](https://docs.near.org/tools/near-api-js/account). |
| rpcURL | `string` | yes | - | URL to your current network's RPC node, for example on mainnet, you connect to https://rpc.mainnet.near.org/ |

### `INewSocialOptions`

Expand Down
7 changes: 7 additions & 0 deletions src/constants/Config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export const networkRPCs = {
testnet: 'https://rpc.testnet.near.org',
mainnet: 'https://rpc.mainnet.near.org',
betanet: 'https://rpc.betanet.near.org',
localnet: 'http://localhost:3030',
// Add more networks if needed
};
1 change: 1 addition & 0 deletions src/constants/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './Fees';
export * from './Config';
16 changes: 2 additions & 14 deletions src/controllers/Social.get.test.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,11 @@
import type { Account } from 'near-api-js';

// credentials
import { account_id as socialContractAccountId } from '@test/credentials/localnet/social.test.near.json';

// controllers
import Social from './Social';

// helpers
import createEphemeralAccount from '@test/helpers/createEphemeralAccount';
import { networkRPCs } from '@app/constants';

describe(`${Social.name}#get`, () => {
let signer: Account;

beforeEach(async () => {
const result = await createEphemeralAccount();

signer = result.account;
});

it('should return an empty object when the contract does not know the account', async () => {
// arrange
const client = new Social({
Expand All @@ -26,7 +14,7 @@ describe(`${Social.name}#get`, () => {
// act
const result = await client.get({
keys: ['unknown.test.near/profile/name'],
signer,
rpcURL: networkRPCs.localnet,
});

// assert
Expand Down
18 changes: 4 additions & 14 deletions src/controllers/Social.getVersion.test.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,20 @@
import type { Account } from 'near-api-js';

// credentials
import { account_id as socialContractAccountId } from '@test/credentials/localnet/social.test.near.json';

// controllers
import Social from './Social';

// helpers
import createEphemeralAccount from '@test/helpers/createEphemeralAccount';
import { networkRPCs } from '@app/constants';

describe(`${Social.name}#getVersion`, () => {
let signer: Account;

beforeEach(async () => {
const result = await createEphemeralAccount();

signer = result.account;
});

it('should return the version of the social contract', async () => {
// arrange
const client = new Social({
contractId: socialContractAccountId,
});
// act
const version = await client.getVersion({ signer });
const version = await client.getVersion({
rpcURL: networkRPCs.localnet,
});

// assert
expect(version).toMatchSnapshot();
Expand Down
6 changes: 4 additions & 2 deletions src/controllers/Social.grantWritePermission.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import { account_id as socialContractAccountId } from '@test/credentials/localne
// controllers
import Social from './Social';

import { networkRPCs } from '@app/constants';

// enums
import { ErrorCodeEnum } from '@app/enums';

Expand Down Expand Up @@ -173,7 +175,7 @@ describe(`${Social.name}#grantWritePermission`, () => {
result = await client.isWritePermissionGranted({
granteeAccountId: granteeAccount.accountId,
key,
signer: granteeAccount,
rpcURL: networkRPCs.localnet,
});

expect(result).toBe(true);
Expand Down Expand Up @@ -203,7 +205,7 @@ describe(`${Social.name}#grantWritePermission`, () => {
result = await client.isWritePermissionGranted({
granteePublicKey: granteeKeyPair.getPublicKey(),
key,
signer: granteeAccount,
rpcURL: networkRPCs.localnet,
});

expect(result).toBe(true);
Expand Down
12 changes: 7 additions & 5 deletions src/controllers/Social.isWritePermissionGranted.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import { account_id as socialContractAccountId } from '@test/credentials/localne
// controllers
import Social from './Social';

import { networkRPCs } from '@app/constants';

// enums
import { ErrorCodeEnum } from '@app/enums';

Expand Down Expand Up @@ -87,7 +89,7 @@ describe(`${Social.name}#isWritePermissionGranted`, () => {
await client.isWritePermissionGranted({
granteeAccountId: invalidGranteeAccountId,
key,
signer: granterAccount,
rpcURL: networkRPCs.localnet,
});
} catch (error) {
// assert
Expand All @@ -106,7 +108,7 @@ describe(`${Social.name}#isWritePermissionGranted`, () => {
const result = await client.isWritePermissionGranted({
granteeAccountId: granterAccount.accountId,
key,
signer: granterAccount,
rpcURL: networkRPCs.localnet,
});

// assert
Expand All @@ -119,7 +121,7 @@ describe(`${Social.name}#isWritePermissionGranted`, () => {
const result = await client.isWritePermissionGranted({
granteeAccountId: granteeAccount.accountId,
key,
signer: granterAccount,
rpcURL: networkRPCs.localnet,
});

// assert
Expand All @@ -144,7 +146,7 @@ describe(`${Social.name}#isWritePermissionGranted`, () => {
const result = await client.isWritePermissionGranted({
granteeAccountId: granteeAccount.accountId,
key,
signer: granterAccount,
rpcURL: networkRPCs.localnet,
});

// assert
Expand All @@ -169,7 +171,7 @@ describe(`${Social.name}#isWritePermissionGranted`, () => {
const result = await client.isWritePermissionGranted({
granteePublicKey: granteeKeyPair.getPublicKey(),
key,
signer: granterAccount,
rpcURL: networkRPCs.localnet,
});

// assert
Expand Down
6 changes: 4 additions & 2 deletions src/controllers/Social.set.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import { MINIMUM_STORAGE_IN_BYTES } from '@app/constants';
// controllers
import Social from './Social';

import { networkRPCs } from '@app/constants';

// enums
import { ErrorCodeEnum } from '@app/enums';

Expand Down Expand Up @@ -119,7 +121,7 @@ describe(`${Social.name}#set`, () => {

result = await client.get({
keys: [`${signer.accountId}/profile/name`],
signer,
rpcURL: networkRPCs.localnet,
});

expect(result).toEqual(data);
Expand Down Expand Up @@ -154,7 +156,7 @@ describe(`${Social.name}#set`, () => {

result = await client.get({
keys: [`${signer.accountId}/profile/name`],
signer,
rpcURL: networkRPCs.localnet,
});

expect(result).toEqual(data);
Expand Down
1 change: 0 additions & 1 deletion src/controllers/Social.storageDeposit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,6 @@ describe(`${Social.name}#storageDeposit`, () => {
contractId: socialContractAccountId,
methodName: ViewMethodEnum.StorageBalanceOf,
});
console.log(result);
expect(result.total).toEqual(deposit);
});
});
Loading

0 comments on commit a57edc4

Please sign in to comment.