order | parent | ||||
---|---|---|---|---|---|
1 |
|
ViteAPI wraps and extends Gvite-RPC API. Functions of network request/listening are included.
ES6:
import { ViteAPI } from '@vite/vitejs';
Common:
const { ViteAPI } = require('@vite/vitejs');
-
Constructor Parameters
provider : Provider
onInitCallback : function
: Callback function that will be called when connection is established
-
Example
import WS_RPC from '@vite/vitejs-ws';
import { ViteAPI } from '@vite/vitejs';
const wsService = new WS_RPC("ws://example.com");
const api = new ViteAPI(wsService, () => {
console.log("Connected.");
});
api.request('ledger_getSnapshotChainHeight').then((height) => {
console.log(height);
});
Return account balance, including balance not received
-
Parameters
Address
Address of account
-
Return
- Promise<
{ balance, unreceived }
>
- Promise<
-
Example
// ...
provider.getBalanceInfo('vite_098dfae02679a4ca05a4c8bf5dd00a8757f0c622bfccce7d68')
.then(({ balance, unreceived }) => {
console.log(balance, unreceived);
})
.catch(err => {
console.warn(err);
});
Return transaction list by account
-
Parameters
__namedParameters: object
address: Address
Address of accountpageIndex: number
Page indexpageSize?: number
Page size. Default is 50
String[] | 'all'
The contract transaction type of which the internal fields of contract need to be resolved. For default, all contract transactions are resolved
-
Return:
- Promise<
Array<Transaction>
>
- Promise<
-
Example
Request:
provider.getTransactionList({
address: 'vite_553462bca137bac29f440e9af4ab2e2c1bb82493e41d2bc8b2',
pageIndex: 0,
pageSize: 50
});
Responce:
[{
"accountAddress": "vite_553462bca137bac29f440e9af4ab2e2c1bb82493e41d2bc8b2",
"amount": "100000000",
"blockType": 2,
"data": "y/Dk+gAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAOAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABIAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAjhvJvwQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAIAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAI4byb8EAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAtjc3Rlc3R0b2tlbgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAEQ1NUVAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=",
"toAddress": "vite_000000000000000000000000000000000000000595292d996d",
"transactionType": "Mintage",
"contractParams": {
"0": "1",
"1": "cstesttoken",
"2": "CSTT",
"3": "10000000000000000",
"4": "2",
"5": "10000000000000000",
"6": "1",
"decimals": "2",
"isReIssuable": "1",
"maxSupply": "10000000000000000",
"ownerBurnOnly": "1",
"tokenName": "cstesttoken",
"tokenSymbol": "CSTT",
"totalSupply": "10000000000000000"
}
}]
Call contract's offchain method
-
Parameters
__namedParameters: object
address : Address
Address of contractabi
ABIcode : Base64
Binary code for offchain query. This is the value of "OffChain Binary" section generated when compiling the contract with--bin
params
Encoded passed-in parameters
-
Return:
- Promise<
Base64
>
- Promise<
Add new transaction type. When provider.getTransactionList
is called, transactions returned will be grouped by transaction type.
-
Parameters
__namedParameters: Object
Name of transaction type is stored as the keycontractAddress : Address
Address of contractabi
ABI
-
Example
// ...
provider.addTransactionType({
helloWorld: {
contractAddr: 'vite_0000000000000000000000000000000000000003f6af7459b9',
abi: { methodName: 'hello', inputs: [], type: 'function' }
}
});
Set new provider
- Parameters
provider : Provider Instance
New provideronInitCallback : Function
Callback function that will be called when connection is establishedabort : boolean
Iftrue
, the ongoing request connection of original provider will be interrupted
Call RPC API and return response
-
Parameters
methods : string
Name of API method...args
Passed-in parameters
-
Returns:
- Promise<
JsonRPC response
> RPC response
- Promise<
-
Example
// ......
// {
// jsonrpc: "2.0",
// id: 33
// method: "rpcMethodName"
// params: [1, 1, 2]
// }
myNetProcessor.request('rpcMethodName', 1, 1, 2).then(() => {
// ...
});
Call RPC API and do not return response
- Parameters
methods : string
Name of API method...args
Passed-in parameters
Call a batch of RPC APIs
-
Parameters (RPCrequest[])
__namedParameters: Object
type: string<request | notification>
methodName: string
Name of API methodparams: any
Passed-in parameters
-
Returns:
- Promise<
JsonRPC response
> RPC response
- Promise<
-
Example
// ......
// [{
// jsonrpc: "2.0",
// id: 33
// method: "rpcMethodName"
// params: [1, 1, 2]
// }]
myNetProcessor.batch([{
type: 'request',
methodName: 'rpcMethodName',
params: [1, 1, 2]
}]).then(() => {
// ...
});
Subscribe to event
:::tip Tips Polling, instead of subscription, will be used by the method if gvite connection is established in HTTP. Refer to Vite RPC Subscription :::
-
Parameters
methods : string
Name of method...args
Passed-in parameters
-
Returns:
- Promise<
event
> Event
- Promise<
-
event:
- on(
callback : Function
): Start listening to the event. The callback function will be called when the event occurs. - off: Stop listening
- on(
-
Example
// ...
provider.subscribe('newAccountBlock').then((event) => {
event.on((result) => {
console.log(result);
});
// event.off();
}).catch(err => {
console.warn(err);
});
Cancel subscription
-
Parameters:
event
: Event returned bysubscribe
method
-
Example
// ...
provider.unsubscribe(event);
Cancel all subscriptions
- Example
// ...
provider.unsubscribeAll();