Skip to content

Commit

Permalink
Nearcore 1 37 changes (#1317)
Browse files Browse the repository at this point in the history
* feat: add support for nearcore 1.37 changes

* chore: add a changeset file

* fix: fix TxOutcome import. update sendTransaction functions

* fix: fix wait_until and signed_tx_base64 parameters

* feat: revert TxOutcome struct changes

* fix: reorder imports

* chore: update changeset file

* chore: bump near-workspaces

* chore: fix workspaces

* chore: add missing changes

* chore: add provider changes

* chore: bump near-api-js

* fix: revert provider changes

* chore: move resolutions

* Revert "chore: move resolutions"

This reverts commit f44d39e.

* chore: update tests

---------

Co-authored-by: vikinatora <[email protected]>
Co-authored-by: Viktor Todorov <[email protected]>
  • Loading branch information
3 people committed Apr 24, 2024
1 parent f6e5f65 commit 06baa81
Show file tree
Hide file tree
Showing 17 changed files with 2,487 additions and 2,543 deletions.
6 changes: 6 additions & 0 deletions .changeset/slimy-brooms-refuse.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@near-js/accounts": minor
"@near-js/types": minor
---

Add support for nearcore 1.37 changes
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,9 @@
"turbo": "1.4.5",
"typedoc": "0.25.3",
"typescript": "4.9.4"
},
"resolutions": {
"near-sandbox": "0.0.18",
"near-api-js": "4.0.0"
}
}
8 changes: 6 additions & 2 deletions packages/accounts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,15 @@
"bs58": "4.0.0",
"jest": "26.0.1",
"near-hello": "0.5.1",
"near-workspaces": "3.4.0",
"near-workspaces": "3.5.0",
"ts-jest": "26.5.6",
"typescript": "4.9.4"
},
"files": [
"lib"
]
],
"resolutions": {
"near-sandbox": "0.0.18",
"near-api-js": "4.0.0"
}
}
21 changes: 11 additions & 10 deletions packages/accounts/test/account.access_key.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ beforeAll(async () => {
beforeEach(async () => {
contractId = testUtils.generateUniqueString('test');
workingAccount = await testUtils.createAccount(nearjs);
contract = await testUtils.deployContract(workingAccount, contractId);
contract = await testUtils.deployContract(nearjs.accountCreator.masterAccount, contractId);
});

test('make function call using access key', async() => {
Expand All @@ -33,28 +33,29 @@ test('make function call using access key', async() => {
test('remove access key no longer works', async() => {
const keyPair = KeyPair.fromRandom('ed25519');
let publicKey = keyPair.getPublicKey();
await workingAccount.addKey(publicKey, contractId, '', 400000);
await workingAccount.deleteKey(publicKey);
await nearjs.accountCreator.masterAccount.addKey(publicKey, contractId, '', 400000);
await nearjs.accountCreator.masterAccount.deleteKey(publicKey);
// Override in the key store the workingAccount key to the given access key.
await nearjs.connection.signer.keyStore.setKey(testUtils.networkId, workingAccount.accountId, keyPair);
await nearjs.connection.signer.keyStore.setKey(testUtils.networkId, nearjs.accountCreator.masterAccount.accountId, keyPair);
try {
await contract.setValue({ args: { value: 'test' } });
fail('should throw an error');
} catch (e) {
expect(e.message).toEqual(`Can not sign transactions for account ${workingAccount.accountId} on network ${testUtils.networkId}, no matching key pair exists for this account`);
expect(e.message).toEqual(`Can not sign transactions for account ${nearjs.accountCreator.masterAccount.accountId} on network ${testUtils.networkId}, no matching key pair exists for this account`);
expect(e.type).toEqual('KeyNotFound');
}
nearjs = await testUtils.setUpTestConnection();
});

test('view account details after adding access keys', async() => {
const keyPair = KeyPair.fromRandom('ed25519');
await workingAccount.addKey(keyPair.getPublicKey(), contractId, '', 1000000000);
await nearjs.accountCreator.masterAccount.addKey(keyPair.getPublicKey(), contractId, '', 1000000000);

const contract2 = await testUtils.deployContract(workingAccount, testUtils.generateUniqueString('test_contract2'));
const contract2 = await testUtils.deployContract(nearjs.accountCreator.masterAccount, testUtils.generateUniqueString('test_contract2'));
const keyPair2 = KeyPair.fromRandom('ed25519');
await workingAccount.addKey(keyPair2.getPublicKey(), contract2.contractId, '', 2000000000);
await nearjs.accountCreator.masterAccount.addKey(keyPair2.getPublicKey(), contract2.contractId, '', 2000000000);

const details = await workingAccount.getAccountDetails();
const details = await nearjs.accountCreator.masterAccount.getAccountDetails();
const expectedResult = {
authorizedApps: [{
contractId: contractId,
Expand Down Expand Up @@ -86,7 +87,7 @@ test('loading account after adding a full key', async() => {

test('load invalid key pair', async() => {
// Override in the key store with invalid key pair
await nearjs.connection.signer.keyStore.setKey(testUtils.networkId, workingAccount.accountId, '');
await nearjs.connection.signer.keyStore.setKey(testUtils.networkId, nearjs.accountCreator.masterAccount.accountId, '');
try {
await contract.setValue({ args: { value: 'test' } });
fail('should throw an error');
Expand Down
6 changes: 3 additions & 3 deletions packages/accounts/test/account.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ test('create account and then view account returns the created account', async (
const newAccountPublicKey = '9AhWenZ3JddamBoyMqnTbp7yVbRuvqAv3zwfrWgfVRJE';
const { amount } = await workingAccount.state();
const newAmount = BigInt(amount) / BigInt(10);
await workingAccount.createAccount(newAccountName, newAccountPublicKey, newAmount);
await nearjs.accountCreator.masterAccount.createAccount(newAccountName, newAccountPublicKey, newAmount);
const newAccount = new Account(nearjs.connection, newAccountName);
const state = await newAccount.state();
expect(state.amount).toEqual(newAmount.toString());
Expand Down Expand Up @@ -122,8 +122,8 @@ describe('with deploy contract', () => {
beforeAll(async () => {
const newPublicKey = await nearjs.connection.signer.createKey(contractId, testUtils.networkId);
const data = [...fs.readFileSync(HELLO_WASM_PATH)];
await workingAccount.createAndDeployContract(contractId, newPublicKey, data, HELLO_WASM_BALANCE);
contract = new Contract(workingAccount, contractId, {
await nearjs.accountCreator.masterAccount.createAndDeployContract(contractId, newPublicKey, data, HELLO_WASM_BALANCE);
contract = new Contract(nearjs.accountCreator.masterAccount, contractId, {
viewMethods: ['hello', 'getValue', 'returnHiWithLogs'],
changeMethods: ['setValue', 'generateLogs', 'triggerAssert', 'testSetRemove', 'crossContract']
});
Expand Down
12 changes: 4 additions & 8 deletions packages/accounts/test/contract.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,16 +108,14 @@ describe('changeMethod', () => {

describe('local view execution', () => {
let nearjs;
let workingAccount;
let contract;
let blockQuery;

jest.setTimeout(60000);

beforeAll(async () => {
nearjs = await testUtils.setUpTestConnection();
workingAccount = await testUtils.createAccount(nearjs);
contract = await testUtils.deployContractGuestBook(workingAccount, testUtils.generateUniqueString('guestbook'));
contract = await testUtils.deployContractGuestBook(nearjs.accountCreator.masterAccount, testUtils.generateUniqueString('guestbook'));

await contract.add_message({ text: 'first message' });
await contract.add_message({ text: 'second message' });
Expand Down Expand Up @@ -178,16 +176,14 @@ describe('local view execution', () => {

describe('contract without account', () => {
let nearjs;
let workingAccount;
let contract;

jest.setTimeout(60000);

beforeAll(async () => {
nearjs = await testUtils.setUpTestConnection();
workingAccount = await testUtils.createAccount(nearjs);
const contractId = testUtils.generateUniqueString('guestbook');
await testUtils.deployContractGuestBook(workingAccount, contractId);
await testUtils.deployContractGuestBook(nearjs.accountCreator.masterAccount, contractId);

contract = new Contract(nearjs.connection, contractId, {
viewMethods: ['total_messages', 'get_messages'],
Expand All @@ -200,13 +196,13 @@ describe('contract without account', () => {
expect(totalMessagesBefore).toBe(0);

await contract.add_message({
signerAccount: workingAccount,
signerAccount: nearjs.accountCreator.masterAccount,
args: {
text: 'first message',
}
});
await contract.add_message({
signerAccount: workingAccount,
signerAccount: nearjs.accountCreator.masterAccount,
args: {
text: 'second message',
}
Expand Down
8 changes: 3 additions & 5 deletions packages/accounts/test/promise.test.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
const testUtils = require('./test-utils');

let nearjs;
let workingAccount;

jasmine.DEFAULT_TIMEOUT_INTERVAL = 120000;

const CONTRACT_CALL_GAS = BigInt(300000000000000);

beforeAll(async () => {
nearjs = await testUtils.setUpTestConnection();
workingAccount = await testUtils.createAccount(nearjs);
});

describe('with promises', () => {
Expand All @@ -21,9 +19,9 @@ describe('with promises', () => {
let contractName2 = testUtils.generateUniqueString('cnt');

beforeAll(async () => {
contract = await testUtils.deployContract(workingAccount, contractName);
contract1 = await testUtils.deployContract(workingAccount, contractName1);
contract2 = await testUtils.deployContract(workingAccount, contractName2);
contract = await testUtils.deployContract(nearjs.accountCreator.masterAccount, contractName);
contract1 = await testUtils.deployContract(nearjs.accountCreator.masterAccount, contractName1);
contract2 = await testUtils.deployContract(nearjs.accountCreator.masterAccount, contractName2);
});

beforeEach(async () => {
Expand Down
15 changes: 5 additions & 10 deletions packages/accounts/test/providers.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,7 @@ describe('providers', () => {
});

test('json rpc query view_state', async () => {
const account = await testUtils.createAccount(near);
const contract = await testUtils.deployContract(account, testUtils.generateUniqueString('test'));
const contract = await testUtils.deployContract(near.accountCreator.masterAccount, testUtils.generateUniqueString('test'));

await contract.setValue({ args: { value: 'hello' } });

Expand All @@ -69,17 +68,15 @@ describe('providers', () => {
expect(response).toEqual({
block_height: expect.any(Number),
block_hash: expect.any(String),
proof: expect.any(Array),
values: [
{ key: 'bmFtZQ==', proof: expect.any(Array), value: 'aGVsbG8=' }
{ key: 'bmFtZQ==', value: 'aGVsbG8=' }
]
});
});
});

test('json rpc query view_code', async () => {
const account = await testUtils.createAccount(near);
const contract = await testUtils.deployContract(account, testUtils.generateUniqueString('test'));
const contract = await testUtils.deployContract(near.accountCreator.masterAccount, testUtils.generateUniqueString('test'));

return testUtils.waitFor(async () => {
const response = await provider.query({
Expand All @@ -98,8 +95,7 @@ describe('providers', () => {
});

test('json rpc query call_function', async () => {
const account = await testUtils.createAccount(near);
const contract = await testUtils.deployContract(account, testUtils.generateUniqueString('test'));
const contract = await testUtils.deployContract(near.accountCreator.masterAccount, testUtils.generateUniqueString('test'));

await contract.setValue({ args: { value: 'hello' } });

Expand Down Expand Up @@ -192,9 +188,8 @@ describe('providers', () => {

describe('providers errors', () => {
test('JSON RPC Error - MethodNotFound', async () => {
const account = await testUtils.createAccount(near);
const contract = await testUtils.deployContract(
account,
near.accountCreator.masterAccount,
testUtils.generateUniqueString('test')
);

Expand Down
2 changes: 1 addition & 1 deletion packages/accounts/test/test-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ function generateUniqueString(prefix) {
let result = `${prefix}-${Date.now()}-${Math.round(Math.random() * 1000000)}`;
let add_symbols = Math.max(RANDOM_ACCOUNT_LENGTH - result.length, 1);
for (let i = add_symbols; i > 0; --i) result += '0';
return result;
return result + '.test.near';
}

async function createAccount({ accountCreator, connection }) {
Expand Down
2 changes: 1 addition & 1 deletion packages/near-api-js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
"semver": "7.1.1",
"ts-jest": "26.5.6",
"uglifyify": "5.0.1",
"near-workspaces": "3.4.0"
"near-workspaces": "3.5.0"
},
"keywords": [],
"license": "(MIT AND Apache-2.0)",
Expand Down
10 changes: 7 additions & 3 deletions packages/providers/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@
"dependencies": {
"@near-js/transactions": "workspace:*",
"@near-js/types": "workspace:*",
"@near-js/utils": "workspace:*",
"@near-js/utils": "workspace:*",
"borsh": "1.0.0",
"http-errors": "1.7.2"
},
"devDependencies": {
"@types/node": "18.11.18",
"jest": "26.0.1",
"near-workspaces": "3.4.0",
"near-workspaces": "3.5.0",
"ts-jest": "26.5.6",
"typescript": "4.9.4"
},
Expand All @@ -34,5 +34,9 @@
},
"files": [
"lib"
]
],
"resolutions": {
"near-sandbox": "0.0.18",
"near-api-js": "4.0.0"
}
}
15 changes: 11 additions & 4 deletions packages/providers/src/failover-rpc-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import {
} from '@near-js/types';
import { SignedTransaction } from '@near-js/transactions';
import { Provider } from './provider';
import { TxExecutionStatus } from '@near-js/types/src/provider/protocol';

/**
* Client class to interact with the [NEAR RPC API](https://docs.near.org/api/rpc/introduction).
Expand Down Expand Up @@ -106,6 +107,10 @@ export class FailoverRpcProvider extends Provider {
return this.withBackoff((currentProvider) => currentProvider.status());
}

async sendTransactionUntil(signedTransaction: SignedTransaction, waitUntil: TxExecutionStatus): Promise<FinalExecutionOutcome> {
return this.withBackoff((currentProvider) => currentProvider.sendTransactionUntil(signedTransaction, waitUntil));
}

/**
* Sends a signed transaction to the RPC and waits until transaction is fully complete
* @see [https://docs.near.org/docs/develop/front-end/rpc#send-transaction-await](https://docs.near.org/docs/develop/front-end/rpc#general-validator-status)
Expand Down Expand Up @@ -141,9 +146,10 @@ export class FailoverRpcProvider extends Provider {
*/
async txStatus(
txHash: Uint8Array | string,
accountId: string
accountId: string,
waitUntil: TxExecutionStatus
): Promise<FinalExecutionOutcome> {
return this.withBackoff((currentProvider) => currentProvider.txStatus(txHash, accountId)
return this.withBackoff((currentProvider) => currentProvider.txStatus(txHash, accountId, waitUntil)
);
}

Expand All @@ -156,9 +162,10 @@ export class FailoverRpcProvider extends Provider {
*/
async txStatusReceipts(
txHash: Uint8Array | string,
accountId: string
accountId: string,
waitUntil: TxExecutionStatus
): Promise<FinalExecutionOutcome> {
return this.withBackoff((currentProvider) => currentProvider.txStatusReceipts(txHash, accountId)
return this.withBackoff((currentProvider) => currentProvider.txStatusReceipts(txHash, accountId, waitUntil)
);
}

Expand Down
Loading

0 comments on commit 06baa81

Please sign in to comment.