near-api-js
Advanced tools
Comparing version 0.31.0 to 0.32.0
import BN from 'bn.js'; | ||
import { Account } from './account'; | ||
import { Connection } from './connection'; | ||
import { PublicKey } from './utils/key_pair'; | ||
import { Action } from './transaction'; | ||
@@ -25,3 +24,2 @@ import { FinalExecutionOutcome } from './providers'; | ||
constructor(connection: Connection, accountId: string, storage: any); | ||
addKey(publicKey: string | PublicKey, contractId?: string, methodName?: string, amount?: BN): Promise<FinalExecutionOutcome>; | ||
signAndSendTransaction(receiverId: string, actions: Action[]): Promise<FinalExecutionOutcome>; | ||
@@ -28,0 +26,0 @@ signAndSendTransactions(transactions: any): Promise<void>; |
@@ -34,8 +34,2 @@ 'use strict'; | ||
} | ||
async addKey(publicKey, contractId, methodName, amount) { | ||
if (contractId) { | ||
return super.addKey(publicKey, contractId, exports.MULTISIG_CHANGE_METHODS.join(), exports.MULTISIG_ALLOWANCE); | ||
} | ||
return super.addKey(publicKey); | ||
} | ||
async signAndSendTransaction(receiverId, actions) { | ||
@@ -42,0 +36,0 @@ const { accountId } = this; |
@@ -18,2 +18,3 @@ import BN from 'bn.js'; | ||
} | ||
declare function parseJsonFromRawResponse(response: Uint8Array): any; | ||
/** | ||
@@ -118,3 +119,5 @@ * More information on [the Account spec](https://nomicon.io/DataStructures/Account.html) | ||
*/ | ||
viewFunction(contractId: string, methodName: string, args: any): Promise<any>; | ||
viewFunction(contractId: string, methodName: string, args: any, { parse }?: { | ||
parse?: typeof parseJsonFromRawResponse; | ||
}): Promise<any>; | ||
/** | ||
@@ -135,1 +138,2 @@ * @returns array of {access_key: AccessKey, public_key: PublicKey} items. | ||
} | ||
export {}; |
@@ -30,2 +30,5 @@ 'use strict'; | ||
const TX_STATUS_RETRY_WAIT_BACKOFF = 1.5; | ||
function parseJsonFromRawResponse(response) { | ||
return JSON.parse(Buffer.from(response).toString()); | ||
} | ||
/** | ||
@@ -125,3 +128,3 @@ * More information on [the Account spec](https://nomicon.io/DataStructures/Account.html) | ||
if (!result) { | ||
throw new providers_1.TypedError(`nonce retries exceeded for transaction. This usually means there are too many parallel requests with the same access key.`, 'RetriesExceeded'); | ||
throw new providers_1.TypedError('nonce retries exceeded for transaction. This usually means there are too many parallel requests with the same access key.', 'RetriesExceeded'); | ||
} | ||
@@ -281,3 +284,3 @@ const flatLogs = [result.transaction_outcome, ...result.receipts_outcome].reduce((acc, it) => { | ||
*/ | ||
async viewFunction(contractId, methodName, args) { | ||
async viewFunction(contractId, methodName, args, { parse = parseJsonFromRawResponse } = {}) { | ||
args = args || {}; | ||
@@ -289,3 +292,3 @@ this.validateArgs(args); | ||
} | ||
return result.result && result.result.length > 0 && JSON.parse(Buffer.from(result.result).toString()); | ||
return result.result && result.result.length > 0 && parse(Buffer.from(result.result)); | ||
} | ||
@@ -337,3 +340,3 @@ /** | ||
const totalBalance = new bn_js_1.default(state.amount).add(staked); | ||
const availableBalance = totalBalance.sub(staked).sub(stateStaked); | ||
const availableBalance = totalBalance.sub(bn_js_1.default.max(staked, stateStaked)); | ||
return { | ||
@@ -340,0 +343,0 @@ total: totalBalance.toString(), |
@@ -32,7 +32,7 @@ "use strict"; | ||
enumerable: true, | ||
value: nameFunction(methodName, async (args = {}, ...ignored) => { | ||
if (ignored.length || !(isObject(args) || isUint8Array(args))) { | ||
value: nameFunction(methodName, async (args = {}, options = {}, ...ignored) => { | ||
if (ignored.length || !(isObject(args) || isUint8Array(args)) || !isObject(options)) { | ||
throw new errors_1.PositionalArgsError(); | ||
} | ||
return this.account.viewFunction(this.contractId, methodName, args); | ||
return this.account.viewFunction(this.contractId, methodName, args, options); | ||
}) | ||
@@ -39,0 +39,0 @@ }); |
{ | ||
"name": "near-api-js", | ||
"description": "JavaScript library to interact with NEAR Protocol via RPC API", | ||
"version": "0.31.0", | ||
"version": "0.32.0", | ||
"repository": { | ||
@@ -6,0 +6,0 @@ "type": "git", |
@@ -46,9 +46,2 @@ 'use strict'; | ||
async addKey(publicKey: string | PublicKey, contractId?: string, methodName?: string, amount?: BN): Promise<FinalExecutionOutcome> { | ||
if (contractId) { | ||
return super.addKey(publicKey, contractId, MULTISIG_CHANGE_METHODS.join(), MULTISIG_ALLOWANCE) | ||
} | ||
return super.addKey(publicKey) | ||
} | ||
async signAndSendTransaction(receiverId: string, actions: Action[]): Promise<FinalExecutionOutcome> { | ||
@@ -55,0 +48,0 @@ const { accountId } = this; |
@@ -70,2 +70,6 @@ 'use strict'; | ||
function parseJsonFromRawResponse (response: Uint8Array): any { | ||
return JSON.parse(Buffer.from(response).toString()); | ||
} | ||
/** | ||
@@ -116,3 +120,3 @@ * More information on [the Account spec](https://nomicon.io/DataStructures/Account.html) | ||
private printLogs(contractId: string, logs: string[], prefix: string = '') { | ||
private printLogs(contractId: string, logs: string[], prefix = '') { | ||
for (const log of logs) { | ||
@@ -187,3 +191,3 @@ console.log(`${prefix}Log [${contractId}]: ${log}`); | ||
if (!result) { | ||
throw new TypedError(`nonce retries exceeded for transaction. This usually means there are too many parallel requests with the same access key.`, 'RetriesExceeded'); | ||
throw new TypedError('nonce retries exceeded for transaction. This usually means there are too many parallel requests with the same access key.', 'RetriesExceeded'); | ||
} | ||
@@ -217,5 +221,5 @@ | ||
accessKeyByPublicKeyCache: { [key: string] : AccessKey } = {} | ||
accessKeyByPublicKeyCache: { [key: string]: AccessKey } = {} | ||
private async findAccessKey(receiverId: string, actions: Action[]): Promise<{publicKey: PublicKey, accessKey: AccessKey}> { | ||
private async findAccessKey(receiverId: string, actions: Action[]): Promise<{publicKey: PublicKey; accessKey: AccessKey}> { | ||
// TODO: Find matching access key based on transaction | ||
@@ -361,3 +365,8 @@ const publicKey = await this.connection.signer.getPublicKey(this.accountId, this.connection.networkId); | ||
*/ | ||
async viewFunction(contractId: string, methodName: string, args: any): Promise<any> { | ||
async viewFunction( | ||
contractId: string, | ||
methodName: string, | ||
args: any, | ||
{ parse = parseJsonFromRawResponse } = {} | ||
): Promise<any> { | ||
args = args || {}; | ||
@@ -369,3 +378,3 @@ this.validateArgs(args); | ||
} | ||
return result.result && result.result.length > 0 && JSON.parse(Buffer.from(result.result).toString()); | ||
return result.result && result.result.length > 0 && parse(Buffer.from(result.result)); | ||
} | ||
@@ -421,3 +430,3 @@ | ||
const totalBalance = new BN(state.amount).add(staked); | ||
const availableBalance = totalBalance.sub(staked).sub(stateStaked); | ||
const availableBalance = totalBalance.sub(BN.max(staked, stateStaked)); | ||
@@ -424,0 +433,0 @@ return { |
@@ -36,7 +36,7 @@ import BN from 'bn.js'; | ||
enumerable: true, | ||
value: nameFunction(methodName, async (args: object = {}, ...ignored) => { | ||
if (ignored.length || !(isObject(args) || isUint8Array(args))) { | ||
value: nameFunction(methodName, async (args: object = {}, options = {}, ...ignored) => { | ||
if (ignored.length || !(isObject(args) || isUint8Array(args)) || !isObject(options)) { | ||
throw new PositionalArgsError(); | ||
} | ||
return this.account.viewFunction(this.contractId, methodName, args); | ||
return this.account.viewFunction(this.contractId, methodName, args, options); | ||
}) | ||
@@ -43,0 +43,0 @@ }); |
@@ -145,2 +145,12 @@ | ||
test('make function calls via account with custom parser', async() => { | ||
const result = await workingAccount.viewFunction( | ||
contractId, | ||
'hello', // this is the function defined in hello.wasm file that we are calling | ||
{name: 'trex'}, | ||
{ parse: x => JSON.parse(x.toString()).replace('trex', 'friend') } | ||
); | ||
expect(result).toEqual('hello friend'); | ||
}); | ||
test('make function calls via contract', async() => { | ||
@@ -147,0 +157,0 @@ const result = await contract.hello({ name: 'trex' }); |
@@ -5,4 +5,4 @@ const { Contract } = require('../lib/contract'); | ||
const account = { | ||
viewFunction() { | ||
return this; | ||
viewFunction(contractId, methodName, args, options) { | ||
return { this: this, contractId, methodName, args, options }; | ||
}, | ||
@@ -60,2 +60,28 @@ functionCall() { | ||
describe('viewMethod', () => { | ||
test('passes options through to account viewFunction', async () => { | ||
function customParser () {} | ||
const stubbedReturnValue = await contract.viewMethod({}, { parse: customParser }); | ||
expect(stubbedReturnValue.options.parse).toBe(customParser); | ||
}); | ||
describe.each([ | ||
1, | ||
'lol', | ||
[], | ||
new Date(), | ||
null, | ||
new Set(), | ||
])('throws PositionalArgsError if 2nd arg is not an object', badArg => { | ||
test(String(badArg), async () => { | ||
try { | ||
await contract.viewMethod({ a: 1 }, badArg); | ||
throw new Error(`Calling \`contract.viewMethod({ a: 1 }, ${badArg})\` worked. It shouldn't have worked.`); | ||
} catch (e) { | ||
if (!(e instanceof PositionalArgsError)) throw e; | ||
} | ||
}); | ||
}); | ||
}); | ||
describe('changeMethod', () => { | ||
@@ -62,0 +88,0 @@ test('throws error message for invalid gas argument', () => { |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
2310411
29327