@ton.js/core
Version:
TonWeb - JavaScript API for TON blockchain
252 lines • 7.71 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.HttpProvider = exports.defaultHost = void 0;
const http_provider_utils_1 = require("./http-provider-utils");
// @todo: set `fetch` to "node-fetch" in Node.js via Webpack
const SHARD_ID_ALL = '-9223372036854775808'; // 0x8000000000000000
exports.defaultHost = 'https://toncenter.com/api/v2/jsonRPC';
class HttpProvider {
constructor(host = exports.defaultHost, options = {}) {
this.host = host;
this.options = options;
}
/**
* @todo: change params type to Array<any>
*/
send(method, params) {
return this.sendImpl(this.host, { id: 1, jsonrpc: '2.0', method, params });
}
/**
* Use this method to get information about address:
* balance, code, data, last_transaction_id.
*
* {@link https://toncenter.com/api/v2/#/accounts/get_address_information_getAddressInformation_get}
*/
async getAddressInfo(address) {
return this.send('getAddressInformation', { address });
}
/**
* Similar to previous one but tries to parse additional
* information for known contract types. This method is
* based on `generic.getAccountState()` thus number of
* recognizable contracts may grow. For wallets, we
* recommend to use `getWalletInformation()`.
*
* {@link https://toncenter.com/api/v2/#/accounts/get_extended_address_information_getExtendedAddressInformation_get}
*/
async getExtendedAddressInfo(address) {
return this.send('getExtendedAddressInformation', { address });
}
/**
* Use this method to retrieve wallet information.
*
* This method parses contract state and currently
* supports more wallet types than
* `getExtendedAddressInformation()`: simple wallet,
* standard wallet and v3 wallet.
*
* {@link https://toncenter.com/api/v2/#/accounts/get_wallet_information_getWalletInformation_get}
*/
async getWalletInfo(address) {
return this.send('getWalletInformation', { address });
}
/**
* Use this method to get transaction history of a given address.
*
* Returns array of transaction objects.
*
* {@link https://toncenter.com/api/v2/#/accounts/get_transactions_getTransactions_get}
*/
async getTransactions(address, limit = 20, lt, hash, to_lt, archival) {
return this.send('getTransactions', {
address,
limit,
lt,
hash,
to_lt,
archival,
});
}
;
/**
* Use this method to get balance (in nanograms)
* of a given address.
*
* {@link https://toncenter.com/api/v2/#/accounts/get_address_balance_getAddressBalance_get}
*/
async getBalance(address) {
return this.send('getAddressBalance', { address });
}
/**
* Use this method to send serialized boc file:
* fully packed and serialized external message.
*
* {@link https://toncenter.com/api/v2/#/send/send_boc_sendBoc_post}
*/
async sendBoc(
/**
* base64 string of boc bytes `Cell.toBoc`
*/
base64) {
return this.send('sendBoc', { boc: base64 });
}
;
/**
* Estimates fees required for query processing.
*
* {@link https://toncenter.com/api/v2/#/send/estimate_fee_estimateFee_post}
*/
async getEstimateFee(query) {
return this.send('estimateFee', query);
}
;
/**
* Invokes get-method of smart contract.
*
* @todo: rename to `runGetMethodRaw()`
*
* {@link https://toncenter.com/api/v2/#/run%20method/run_get_method_runGetMethod_post}
*/
async call(
/**
* Contract address.
*/
address,
/**
* Method name or method ID.
*/
method,
/**
* Array of stack elements.
*/
params = []) {
/**
* @todo: think about throw error
* if result.exit_code !== 0
* (the change breaks backward compatibility)
*/
return this.send('runGetMethod', {
address: address,
method: method,
stack: params,
});
}
/**
* Invokes get-method of smart contract.
*
* @todo: rename to `runGetMethod()`
*
* {@link https://toncenter.com/api/v2/#/run%20method/run_get_method_runGetMethod_post}
*/
async call2(
/**
* Contract address.
*/
address,
/**
* Method name or method ID.
*/
method,
/**
* Array of stack elements.
*/
params = []
// @todo: properly type the result
) {
const result = await this.send('runGetMethod', {
address,
method,
stack: params,
});
return http_provider_utils_1.HttpProviderUtils.parseResponse(result);
}
/**
* Returns ID's of last and init block of masterchain.
*
* {@link https://toncenter.com/api/v2/#/blocks/get_masterchain_info_getMasterchainInfo_get}
*/
async getMasterchainInfo() {
return this.send('getMasterchainInfo', {});
}
/**
* Returns ID's of shardchain blocks included
* in this masterchain block.
*
* {@link https://toncenter.com/api/v2/#/blocks/shards_shards_get}
*/
async getBlockShards(masterchainBlockNumber) {
return this.send('shards', {
seqno: masterchainBlockNumber,
});
}
/**
* Returns transactions hashes included in this block.
*
* {@link https://toncenter.com/api/v2/#/blocks/get_block_transactions_getBlockTransactions_get}
*/
async getBlockTransactions(workchain, shardId, shardBlockNumber) {
return this.send('getBlockTransactions', {
workchain,
shard: shardId,
seqno: shardBlockNumber,
});
}
/**
* Returns transactions hashes included
* in this masterchain block.
*/
async getMasterchainBlockTransactions(masterchainBlockNumber) {
return this.getBlockTransactions(-1, SHARD_ID_ALL, masterchainBlockNumber);
}
/**
* Returns block header and his previous blocks ID's.
*
* {@link https://toncenter.com/api/v2/#/blocks/get_block_header_getBlockHeader_get}
*/
async getBlockHeader(workchain, shardId, shardBlockNumber) {
return this.send('getBlockHeader', {
workchain,
shard: shardId,
seqno: shardBlockNumber,
});
}
/**
* Returns masterchain block header and his previous block ID.
*/
async getMasterchainBlockHeader(masterchainBlockNumber) {
return this.getBlockHeader(-1, SHARD_ID_ALL, masterchainBlockNumber);
}
/**
* Sends external message.
*
* {@link https://toncenter.com/api/v2/#/send/send_query_cell_sendQuerySimple_post}
*
* @deprecated
*/
async sendQuery(query) {
return this.send('sendQuerySimple', query);
}
;
/**
* @private
*/
sendImpl(apiUrl, request) {
const headers = {
'Content-Type': 'application/json',
};
if (this.options.apiKey) {
headers['X-API-Key'] = this.options.apiKey;
}
// @todo: use async/await/throw
return fetch(apiUrl, {
method: 'POST',
headers,
body: JSON.stringify(request),
})
.then(response => response.json())
.then(({ result, error }) => (result || Promise.reject(error)));
}
}
exports.HttpProvider = HttpProvider;
HttpProvider.SHARD_ID_ALL = SHARD_ID_ALL;
//# sourceMappingURL=http-provider.js.map