@concordium/common-sdk
Advanced tools
Comparing version
@@ -1,4 +0,7 @@ | ||
import { AccountTransaction, AccountTransactionSignature, ConsensusStatus, ContractAddress, InstanceInfo, NextAccountNonce, TransactionStatus } from './types'; | ||
import { Buffer } from 'buffer/'; | ||
import { AccountInfo, AccountTransaction, AccountTransactionSignature, ConsensusStatus, ContractAddress, ContractContext, CryptographicParameters, InstanceInfo, InvokeContractResult, NextAccountNonce, TransactionStatus, Versioned } from './types'; | ||
import { AccountAddress } from './types/accountAddress'; | ||
import Provider from './providers/provider'; | ||
import { ModuleReference } from './types/moduleReference'; | ||
import { CredentialRegistrationId } from './types/CredentialRegistrationId'; | ||
export declare class JsonRpcClient { | ||
@@ -18,2 +21,42 @@ provider: Provider; | ||
getInstanceInfo(address: ContractAddress, blockHash?: string): Promise<InstanceInfo | undefined>; | ||
/** | ||
* Retrieves the account info for the given account. If the provided block | ||
* hash is in a block prior to the finalization of the account, then the account | ||
* information will not be available. | ||
* A credential registration id can also be provided, instead of an address. In this case | ||
* the node will return the account info of the account, which the corresponding credential | ||
* is (or was) deployed to. | ||
* @param accountAddress base58 account address (or a credential registration id) to get the account info for | ||
* @param blockHash the block hash to get the account info at | ||
* @returns the account info for the provided account address, undefined is the account does not exist | ||
*/ | ||
getAccountInfo(accountAddress: AccountAddress | CredentialRegistrationId, blockHash?: string): Promise<AccountInfo | undefined>; | ||
/** | ||
* Retrieves the global cryptographic parameters on the blockchain at | ||
* the provided block. | ||
* @param blockHash the block to get the cryptographic parameters at | ||
* @returns the global cryptographic parameters at the given block, or undefined it the block does not exist. | ||
*/ | ||
getCryptographicParameters(blockHash?: string): Promise<Versioned<CryptographicParameters> | undefined>; | ||
/** | ||
* Retrieves the source of the given module at | ||
* the provided block. | ||
* @param moduleReference the module's reference, which is the hex encoded hash of the source. | ||
* @param blockHash the block to get the cryptographic parameters at | ||
* @returns the source of the module as raw bytes. | ||
*/ | ||
getModuleSource(moduleReference: ModuleReference, blockHash?: string): Promise<Buffer>; | ||
/** | ||
* Invokes a smart contract. | ||
* @param context the collection of details used to invoke the contract. Must include the address of the contract and the method invoked. | ||
* @param blockHash the block hash at which the contract should be invoked at. The contract is invoked in the state at the end of this block. | ||
* @returns If the node was able to invoke, then a object describing the outcome is returned. | ||
* The outcome is determined by the `tag` field, which is either `success` or `failure`. | ||
* The `usedEnergy` field will always be present, and is the amount of NRG was used during the execution. | ||
* If the tag is `success`, then an `events` field is present, and it contains the events that would have been generated. | ||
* If invoking a V1 contract and it produces a return value, it will be present in the `returnValue` field. | ||
* If the tag is `failure`, then a `reason` field is present, and it contains the reason the update would have been rejected. | ||
* If either the block does not exist, or then node fails to parse of any of the inputs, then undefined is returned. | ||
*/ | ||
invokeContract(contractContext: ContractContext, blockHash?: string): Promise<InvokeContractResult | undefined>; | ||
} |
@@ -5,2 +5,3 @@ "use strict"; | ||
const buffer_1 = require("buffer/"); | ||
const types_1 = require("./types"); | ||
const accountAddress_1 = require("./types/accountAddress"); | ||
@@ -87,2 +88,5 @@ const serialization_1 = require("./serialization"); | ||
} | ||
else if (!(0, util_1.isValidHash)(blockHash)) { | ||
throw new Error('The input was not a valid hash: ' + blockHash); | ||
} | ||
const response = await this.provider.request('getInstanceInfo', { | ||
@@ -124,3 +128,122 @@ index: address.index, | ||
} | ||
/** | ||
* Retrieves the account info for the given account. If the provided block | ||
* hash is in a block prior to the finalization of the account, then the account | ||
* information will not be available. | ||
* A credential registration id can also be provided, instead of an address. In this case | ||
* the node will return the account info of the account, which the corresponding credential | ||
* is (or was) deployed to. | ||
* @param accountAddress base58 account address (or a credential registration id) to get the account info for | ||
* @param blockHash the block hash to get the account info at | ||
* @returns the account info for the provided account address, undefined is the account does not exist | ||
*/ | ||
async getAccountInfo(accountAddress, blockHash) { | ||
if (!blockHash) { | ||
const consensusStatus = await this.getConsensusStatus(); | ||
blockHash = consensusStatus.lastFinalizedBlock; | ||
} | ||
else if (!(0, util_1.isValidHash)(blockHash)) { | ||
throw new Error('The input was not a valid hash: ' + blockHash); | ||
} | ||
const address = accountAddress instanceof accountAddress_1.AccountAddress | ||
? accountAddress.address | ||
: accountAddress.credId; | ||
const response = await this.provider.request('getAccountInfo', { | ||
blockHash, | ||
address, | ||
}); | ||
const datePropertyKeys = ['timestamp', 'effectiveTime']; | ||
const bigIntPropertyKeys = [ | ||
'accountAmount', | ||
'accountNonce', | ||
'accountIndex', | ||
'startIndex', | ||
'total', | ||
'amount', | ||
'stakedAmount', | ||
'bakerId', | ||
'newStake', | ||
'epoch', | ||
]; | ||
const res = transformJsonResponse(response, (0, util_1.buildJsonResponseReviver)(datePropertyKeys, bigIntPropertyKeys), (0, util_1.intToStringTransformer)(bigIntPropertyKeys)); | ||
return res.result; | ||
} | ||
/** | ||
* Retrieves the global cryptographic parameters on the blockchain at | ||
* the provided block. | ||
* @param blockHash the block to get the cryptographic parameters at | ||
* @returns the global cryptographic parameters at the given block, or undefined it the block does not exist. | ||
*/ | ||
async getCryptographicParameters(blockHash) { | ||
if (!blockHash) { | ||
const consensusStatus = await this.getConsensusStatus(); | ||
blockHash = consensusStatus.lastFinalizedBlock; | ||
} | ||
else if (!(0, util_1.isValidHash)(blockHash)) { | ||
throw new Error('The input was not a valid hash: ' + blockHash); | ||
} | ||
const response = await this.provider.request('getCryptographicParameters', { | ||
blockHash, | ||
}); | ||
const res = transformJsonResponse(response); | ||
return res.result; | ||
} | ||
/** | ||
* Retrieves the source of the given module at | ||
* the provided block. | ||
* @param moduleReference the module's reference, which is the hex encoded hash of the source. | ||
* @param blockHash the block to get the cryptographic parameters at | ||
* @returns the source of the module as raw bytes. | ||
*/ | ||
async getModuleSource(moduleReference, blockHash) { | ||
if (!blockHash) { | ||
const consensusStatus = await this.getConsensusStatus(); | ||
blockHash = consensusStatus.lastFinalizedBlock; | ||
} | ||
else if (!(0, util_1.isValidHash)(blockHash)) { | ||
throw new Error('The input was not a valid hash: ' + blockHash); | ||
} | ||
const response = await this.provider.request('getModuleSource', { | ||
moduleReference: moduleReference.moduleRef, | ||
blockHash, | ||
}); | ||
return buffer_1.Buffer.from(JSON.parse(response).result, 'base64'); | ||
} | ||
/** | ||
* Invokes a smart contract. | ||
* @param context the collection of details used to invoke the contract. Must include the address of the contract and the method invoked. | ||
* @param blockHash the block hash at which the contract should be invoked at. The contract is invoked in the state at the end of this block. | ||
* @returns If the node was able to invoke, then a object describing the outcome is returned. | ||
* The outcome is determined by the `tag` field, which is either `success` or `failure`. | ||
* The `usedEnergy` field will always be present, and is the amount of NRG was used during the execution. | ||
* If the tag is `success`, then an `events` field is present, and it contains the events that would have been generated. | ||
* If invoking a V1 contract and it produces a return value, it will be present in the `returnValue` field. | ||
* If the tag is `failure`, then a `reason` field is present, and it contains the reason the update would have been rejected. | ||
* If either the block does not exist, or then node fails to parse of any of the inputs, then undefined is returned. | ||
*/ | ||
async invokeContract(contractContext, blockHash) { | ||
if (!blockHash) { | ||
const consensusStatus = await this.getConsensusStatus(); | ||
blockHash = consensusStatus.lastFinalizedBlock; | ||
} | ||
else if (!(0, util_1.isValidHash)(blockHash)) { | ||
throw new Error('The input was not a valid hash: ' + blockHash); | ||
} | ||
const invoker = (0, types_1.buildInvoker)(contractContext.invoker); | ||
const context = { | ||
...contractContext, | ||
invoker, | ||
amount: contractContext.amount && contractContext.amount.microGtuAmount, | ||
parameter: contractContext.parameter && | ||
contractContext.parameter.toString('hex'), | ||
}; | ||
const response = await this.provider.request('invokeContract', { | ||
blockHash, | ||
context, | ||
}); | ||
const bigIntPropertyKeys = ['usedEnergy', 'index', 'subindex']; | ||
const res = transformJsonResponse(response, (0, util_1.buildJsonResponseReviver)([], bigIntPropertyKeys), (0, util_1.intToStringTransformer)(bigIntPropertyKeys)); | ||
return res.result; | ||
} | ||
} | ||
exports.JsonRpcClient = JsonRpcClient; |
@@ -0,1 +1,2 @@ | ||
import { Invoker } from '../types'; | ||
interface JsonRpcResponseBase { | ||
@@ -31,3 +32,27 @@ jsonrpc: '2.0'; | ||
transaction: string; | ||
}]) => Promise<string>; | ||
}] | ['getAccountInfo', { | ||
address: string; | ||
blockHash: string; | ||
}] | ['getCryptographicParameters', { | ||
blockHash: string; | ||
}] | ['getModuleSource', { | ||
blockHash: string; | ||
moduleReference: string; | ||
}] | [ | ||
'invokeContract', | ||
{ | ||
blockHash: string; | ||
context: { | ||
contract: { | ||
index: bigint; | ||
subindex: bigint; | ||
}; | ||
method: string; | ||
amount?: bigint; | ||
invoker: Invoker; | ||
energy?: bigint; | ||
parameter?: string; | ||
}; | ||
} | ||
]) => Promise<string>; | ||
export default interface Provider { | ||
@@ -34,0 +59,0 @@ request: JsonRpcRequest; |
@@ -1016,2 +1016,20 @@ import { AccountAddress } from './types/accountAddress'; | ||
} | ||
/** | ||
* Format of invoker expected by the node for the invokeContract entrypoint. | ||
*/ | ||
export declare type Invoker = { | ||
type: 'AddressContract'; | ||
address: { | ||
index: string; | ||
subindex: string; | ||
}; | ||
} | { | ||
type: 'AddressAccount'; | ||
address: string; | ||
} | null; | ||
/** | ||
* Takes an accountAddress or ContractAddress and transforms it into the specific format used for | ||
* InvokeContract's invoker parameter. | ||
*/ | ||
export declare function buildInvoker(invoker?: AccountAddress | ContractAddress): Invoker; | ||
export interface InvokeContractSuccessResult extends Pick<SuccessfulEventResult, 'events'> { | ||
@@ -1018,0 +1036,0 @@ tag: 'success'; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.SchemaVersion = exports.isInstanceInfoV0 = exports.isInstanceInfoV1 = exports.ParameterType = exports.AccountTransactionType = exports.BlockItemKind = exports.DelegationTargetType = exports.PoolStatusType = exports.BakerPoolPendingChangeType = exports.OpenStatusText = exports.OpenStatus = exports.StakePendingChangeType = exports.instanceOfTransferWithMemoTransactionSummary = exports.RejectReasonTag = exports.TransactionStatusEnum = exports.AttributesKeys = void 0; | ||
exports.SchemaVersion = exports.buildInvoker = exports.isInstanceInfoV0 = exports.isInstanceInfoV1 = exports.ParameterType = exports.AccountTransactionType = exports.BlockItemKind = exports.DelegationTargetType = exports.PoolStatusType = exports.BakerPoolPendingChangeType = exports.OpenStatusText = exports.OpenStatus = exports.StakePendingChangeType = exports.instanceOfTransferWithMemoTransactionSummary = exports.RejectReasonTag = exports.TransactionStatusEnum = exports.AttributesKeys = void 0; | ||
var AttributesKeys; | ||
@@ -235,2 +235,31 @@ (function (AttributesKeys) { | ||
exports.isInstanceInfoV0 = isInstanceInfoV0; | ||
/** | ||
* Takes an accountAddress or ContractAddress and transforms it into the specific format used for | ||
* InvokeContract's invoker parameter. | ||
*/ | ||
function buildInvoker(invoker) { | ||
if (!invoker) { | ||
return null; | ||
} | ||
else if (invoker.address) { | ||
return { | ||
type: 'AddressAccount', | ||
address: invoker.address, | ||
}; | ||
} | ||
else if (invoker.index !== undefined) { | ||
const invokerContract = invoker; | ||
return { | ||
type: 'AddressContract', | ||
address: { | ||
subindex: invokerContract.subindex.toString(), | ||
index: invokerContract.index.toString(), | ||
}, | ||
}; | ||
} | ||
else { | ||
throw new Error('Unexpected input to build invoker'); | ||
} | ||
} | ||
exports.buildInvoker = buildInvoker; | ||
var SchemaVersion; | ||
@@ -237,0 +266,0 @@ (function (SchemaVersion) { |
100
package.json
{ | ||
"name": "@concordium/common-sdk", | ||
"version": "2.1.1", | ||
"license": "Apache-2.0", | ||
"engines": { | ||
"node": ">=14.16.0" | ||
}, | ||
"main": "lib/index.js", | ||
"types": "lib/index.d.ts", | ||
"files": [ | ||
"/lib/**/*" | ||
], | ||
"devDependencies": { | ||
"@types/bs58check": "^2.1.0", | ||
"@types/jest": "^26.0.23", | ||
"@types/json-bigint": "^1.0.1", | ||
"@types/uuid": "^8.3.4", | ||
"@typescript-eslint/eslint-plugin": "^4.28.1", | ||
"@typescript-eslint/parser": "^4.28.1", | ||
"babel-jest": "^27.0.6", | ||
"eslint": "^7.29.0", | ||
"eslint-config-prettier": "^8.3.0", | ||
"eslint-plugin-prettier": "^3.4.0", | ||
"husky": "^4.2.5", | ||
"jest": "^27.0.6", | ||
"lint-staged": "^12.0.2", | ||
"prettier": "^2.3.2", | ||
"ts-jest": "^27.0.3", | ||
"typescript": "^4.3.5" | ||
}, | ||
"prettier": { | ||
"singleQuote": true, | ||
"tabWidth": 4 | ||
}, | ||
"scripts": { | ||
"lint": "eslint . --cache --ext .ts,.tsx --max-warnings 0", | ||
"lint-fix": "yarn --silent lint --fix; exit 0", | ||
"test": "jest", | ||
"build": "tsc" | ||
}, | ||
"dependencies": { | ||
"@concordium/rust-bindings": "0.1.1", | ||
"@noble/ed25519": "^1.6.0", | ||
"bs58check": "^2.1.2", | ||
"buffer": "^6.0.3", | ||
"cross-fetch": "3.1.5", | ||
"hash.js": "^1.1.7", | ||
"json-bigint": "^1.0.0", | ||
"uuid": "^8.3.2" | ||
} | ||
} | ||
"name": "@concordium/common-sdk", | ||
"version": "2.2.0", | ||
"license": "Apache-2.0", | ||
"engines": { | ||
"node": ">=14.16.0" | ||
}, | ||
"main": "lib/index.js", | ||
"types": "lib/index.d.ts", | ||
"files": [ | ||
"/lib/**/*" | ||
], | ||
"devDependencies": { | ||
"@types/bs58check": "^2.1.0", | ||
"@types/jest": "^26.0.23", | ||
"@types/json-bigint": "^1.0.1", | ||
"@types/uuid": "^8.3.4", | ||
"@typescript-eslint/eslint-plugin": "^4.28.1", | ||
"@typescript-eslint/parser": "^4.28.1", | ||
"babel-jest": "^27.0.6", | ||
"eslint": "^7.29.0", | ||
"eslint-config-prettier": "^8.3.0", | ||
"eslint-plugin-prettier": "^3.4.0", | ||
"husky": "^4.2.5", | ||
"jest": "^27.0.6", | ||
"lint-staged": "^12.0.2", | ||
"prettier": "^2.3.2", | ||
"ts-jest": "^27.0.3", | ||
"typescript": "^4.3.5" | ||
}, | ||
"prettier": { | ||
"singleQuote": true, | ||
"tabWidth": 4 | ||
}, | ||
"scripts": { | ||
"lint": "eslint . --cache --ext .ts,.tsx --max-warnings 0", | ||
"lint-fix": "yarn --silent lint --fix; exit 0", | ||
"test": "jest", | ||
"build": "tsc" | ||
}, | ||
"dependencies": { | ||
"@concordium/rust-bindings": "0.1.1", | ||
"@noble/ed25519": "^1.6.0", | ||
"bs58check": "^2.1.2", | ||
"buffer": "^6.0.3", | ||
"cross-fetch": "3.1.5", | ||
"hash.js": "^1.1.7", | ||
"json-bigint": "^1.0.0", | ||
"uuid": "^8.3.2" | ||
} | ||
} |
288812
6.6%59
5.36%5721
7.64%