Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support for nearcore 1.37 changes #12

Closed
wants to merge 15 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"
}
}
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"
}
}
40 changes: 26 additions & 14 deletions packages/providers/src/json-rpc-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import {
import { exponentialBackoff } from './exponential-backoff';
import { Provider } from './provider';
import { ConnectionInfo, fetchJson } from './fetch_json';
import { TxExecutionStatus } from '@near-js/types/src/provider/protocol';

/** @hidden */
// Default number of retries before giving up on a request.
Expand Down Expand Up @@ -104,15 +105,25 @@ export class JsonRpcProvider extends Provider {
return this.sendJsonRpc('status', []);
}

/**
* Sends a signed transaction to the RPC
*
* @param signedTransaction The signed transaction being sent
* @param waitUntil
*/
async sendTransactionUntil(signedTransaction: SignedTransaction, waitUntil: TxExecutionStatus): Promise<FinalExecutionOutcome> {
const bytes = encodeTransaction(signedTransaction);
return this.sendJsonRpc('send_tx', { signed_tx_base64: Buffer.from(bytes).toString('base64'), wait_until: 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)
*
* @param signedTransaction The signed transaction being sent
*/
async sendTransaction(signedTransaction: SignedTransaction): Promise<FinalExecutionOutcome> {
const bytes = encodeTransaction(signedTransaction);
return this.sendJsonRpc('broadcast_tx_commit', [Buffer.from(bytes).toString('base64')]);
return this.sendTransactionUntil(signedTransaction, 'FINAL');
}

/**
Expand All @@ -122,8 +133,7 @@ export class JsonRpcProvider extends Provider {
* @returns {Promise<FinalExecutionOutcome>}
*/
async sendTransactionAsync(signedTransaction: SignedTransaction): Promise<FinalExecutionOutcome> {
const bytes = encodeTransaction(signedTransaction);
return this.sendJsonRpc('broadcast_tx_async', [Buffer.from(bytes).toString('base64')]);
return this.sendTransactionUntil(signedTransaction, 'NONE');
}

/**
Expand All @@ -132,36 +142,38 @@ export class JsonRpcProvider extends Provider {
*
* @param txHash A transaction hash as either a Uint8Array or a base58 encoded string
* @param accountId The NEAR account that signed the transaction
* @param waitUntil
*/
async txStatus(txHash: Uint8Array | string, accountId: string): Promise<FinalExecutionOutcome> {
async txStatus(txHash: Uint8Array | string, accountId: string, waitUntil: TxExecutionStatus = 'FINAL'): Promise<FinalExecutionOutcome> {
if (typeof txHash === 'string') {
return this.txStatusString(txHash, accountId);
return this.txStatusString(txHash, accountId, waitUntil);
} else {
return this.txStatusUint8Array(txHash, accountId);
return this.txStatusUint8Array(txHash, accountId, waitUntil);
}
}

private async txStatusUint8Array(txHash: Uint8Array, accountId: string): Promise<FinalExecutionOutcome> {
return this.sendJsonRpc('tx', [baseEncode(txHash), accountId]);
private async txStatusUint8Array(txHash: Uint8Array, accountId: string, waitUntil: TxExecutionStatus): Promise<FinalExecutionOutcome> {
return this.sendJsonRpc('tx', { tx_hash: baseEncode(txHash), sender_account_id: accountId, wait_until: waitUntil });
}

private async txStatusString(txHash: string, accountId: string): Promise<FinalExecutionOutcome> {
return this.sendJsonRpc('tx', [txHash, accountId]);
private async txStatusString(txHash: string, accountId: string, waitUntil: string): Promise<FinalExecutionOutcome> {
return this.sendJsonRpc('tx', { tx_hash: txHash, sender_account_id: accountId, wait_until: waitUntil });
}

/**
* Gets a transaction's status from the RPC with receipts
* See [docs for more info](https://docs.near.org/docs/develop/front-end/rpc#transaction-status-with-receipts)
* @param txHash The hash of the transaction
* @param accountId The NEAR account that signed the transaction
* @param waitUntil
* @returns {Promise<FinalExecutionOutcome>}
*/
async txStatusReceipts(txHash: Uint8Array | string, accountId: string): Promise<FinalExecutionOutcome> {
async txStatusReceipts(txHash: Uint8Array | string, accountId: string, waitUntil: TxExecutionStatus = 'FINAL'): Promise<FinalExecutionOutcome> {
if (typeof txHash === 'string') {
return this.sendJsonRpc('EXPERIMENTAL_tx_status', [txHash, accountId]);
return this.sendJsonRpc('EXPERIMENTAL_tx_status', { tx_hash: txHash, sender_account_id: accountId, wait_until: waitUntil });
}
else {
return this.sendJsonRpc('EXPERIMENTAL_tx_status', [baseEncode(txHash), accountId]);
return this.sendJsonRpc('EXPERIMENTAL_tx_status', { tx_hash: baseEncode(txHash), sender_account_id: accountId, wait_until: waitUntil });
}
}

Expand Down
2 changes: 2 additions & 0 deletions packages/types/src/provider/protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ export type BlockId = BlockHash | BlockHeight;

export type Finality = 'optimistic' | 'near-final' | 'final'

export type TxExecutionStatus = 'NONE' | 'INCLUDED' | 'INCLUDED_FINAL' | 'EXECUTED' | 'FINAL';

export type BlockReference = { blockId: BlockId } | { finality: Finality } | { sync_checkpoint: 'genesis' | 'earliest_available' }

export interface TotalWeight {
Expand Down
4 changes: 2 additions & 2 deletions packages/types/src/provider/response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
* NEAR RPC API request types and responses
* @module
*/

import { BlockHash, BlockHeight, MerklePath } from './protocol';
import { BlockHash, BlockHeight, MerklePath, TxExecutionStatus } from './protocol';

export enum ExecutionStatusBasic {
Unknown = 'Unknown',
Expand Down Expand Up @@ -55,6 +54,7 @@ export interface ExecutionOutcomeWithIdView {
}

export interface FinalExecutionOutcome {
final_execution_status: TxExecutionStatus;
status: FinalExecutionStatus | FinalExecutionStatusBasic;
transaction: any;
transaction_outcome: ExecutionOutcomeWithId;
Expand Down
Loading
Loading