starknet-devnet
Version:
Starknet Devnet provider
155 lines (154 loc) • 6.85 kB
JavaScript
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.DevnetProvider = void 0;
const axios_1 = __importDefault(require("axios"));
const postman_1 = require("./postman");
const cheats_1 = require("./cheats");
const rpc_provider_1 = require("./rpc-provider");
const types_1 = require("./types");
const constants_1 = require("./constants");
class DevnetProvider {
constructor(config) {
this.url = config?.url || constants_1.DEFAULT_DEVNET_URL;
this.httpProvider = axios_1.default.create({
baseURL: this.url,
timeout: config?.timeout ?? constants_1.DEFAULT_HTTP_TIMEOUT,
});
this.rpcProvider = new rpc_provider_1.RpcProvider(this.httpProvider, this.url);
this.postman = new postman_1.Postman(this.rpcProvider);
this.cheats = new cheats_1.Cheats(this.rpcProvider);
}
/**
* @returns `true` if the underlying Devnet instance is responsive; `false` otherwise
*/
async isAlive() {
return this.httpProvider
.get("/is_alive")
.then((resp) => resp.status === axios_1.default.HttpStatusCode.Ok)
.catch(() => false);
}
/**
* Restart the state of the underlying Devnet instance. You may opt to restart L1-L2 messaging.
* https://0xspaceshard.github.io/starknet-devnet/docs/dump-load-restart#restarting
*/
async restart(params = {}) {
await this.rpcProvider.sendRequest("devnet_restart", {
restart_l1_to_l2_messaging: params.restartL1ToL2Messaging,
});
}
/**
* Generate funds at the provided address. For return spec and more info, see
* https://0xspaceshard.github.io/starknet-devnet/docs/balance#mint-token---local-faucet
* @param address the account address to receive funds
* @param amount how much to mint
* @param unit specifier of the currency unit; defaults to FRI
*/
async mint(address, amount, unit = "FRI") {
const paramsSerialized = `{
"address": "${address}",
"amount": ${amount},
"unit": "${unit}"
}`;
const respData = await this.rpcProvider.sendRequest("devnet_mint", paramsSerialized);
return {
new_balance: BigInt(respData.new_balance),
unit: respData.unit,
tx_hash: respData.tx_hash,
};
}
/**
* https://0xspaceshard.github.io/starknet-devnet/docs/predeployed#how-to-get-predeployment-info
* @returns a list of containing information on predeployed accounts. Load an account using e.g. starknet.js.
*/
async getPredeployedAccounts(additionalArgs = { withBalance: false }) {
return await this.rpcProvider.sendRequest("devnet_getPredeployedAccounts", {
with_balance: additionalArgs.withBalance,
});
}
/**
* https://0xspaceshard.github.io/starknet-devnet/docs/blocks
* @returns the block hash of the newly created block
*/
async createBlock() {
return await this.rpcProvider.sendRequest("devnet_createBlock");
}
/**
* https://0xspaceshard.github.io/starknet-devnet/docs/blocks
* @param staringBlockId the block ID of the block after which (inclusive) all blocks
* should be aborted. See docs {@link BlockId} for more info.
* @returns hash values of aborted blocks
*/
async abortBlocks(startingBlockId) {
return await this.rpcProvider.sendRequest("devnet_abortBlocks", {
starting_block_id: (0, types_1.toRpcBlockId)(startingBlockId),
});
}
/**
* https://0xspaceshard.github.io/starknet-devnet/docs/next/starknet-time#set-time
* @returns the new time in unix seconds and, if block creation requested, the hash of the created block
*/
async setTime(time, additionalArgs = { generateBlock: false }) {
return await this.rpcProvider.sendRequest("devnet_setTime", {
time,
generate_block: additionalArgs.generateBlock,
});
}
/**
* Increase the time by the provided `increment` seconds.
* https://0xspaceshard.github.io/starknet-devnet/docs/next/starknet-time#increase-time
* @returns the new time in unix seconds
*/
async increaseTime(increment) {
return await this.rpcProvider.sendRequest("devnet_increaseTime", {
time: increment,
});
}
/**
* https://0xspaceshard.github.io/starknet-devnet/docs/dump-load-restart#dumping
* @param path the path where your Devnet instance will be serialized; if not provided, defaults to the dump-path provided via CLI on Devnet startup.
*/
async dump(path) {
return await this.rpcProvider.sendRequest("devnet_dump", { path });
}
/**
* After loading, this DevnetProvider instance will be connected to the loaded Devnet instance.
* https://0xspaceshard.github.io/starknet-devnet/docs/dump-load-restart#dumping
* @param path the path from which a Devnet instance will be deserialized
*/
async load(path) {
return await this.rpcProvider.sendRequest("devnet_load", { path });
}
/**
* Modify gas prices, according to https://0xspaceshard.github.io/starknet-devnet/docs/gas
* @param price new gas prices; any gas price can be ommitted
* @param generateBlock if `true`, a new block is generated immediately, having new gas prices;
* otherwise (by default) the price change takes effect with the usual next block generation
* @returns gas prices after modification, including the unchanged ones
*/
async setGasPrice(price, generateBlock) {
const newGasPrices = await this.rpcProvider.sendRequest("devnet_setGasPrice", `{
"gas_price_fri": ${price.l1GasPrice ?? null},
"data_gas_price_fri": ${price.l1DataGasPrice ?? null},
"l2_gas_price_fri": ${price.l2GasPrice ?? null},
"generate_block": ${generateBlock ?? null}
}`);
return {
l1_gas_price: BigInt(newGasPrices.gas_price_fri),
l1_data_gas_price: BigInt(newGasPrices.data_gas_price_fri),
l2_gas_price: BigInt(newGasPrices.l2_gas_price_fri),
};
}
/**
* More info at: https://0xspaceshard.github.io/starknet-devnet/docs/api#config-api
* @returns the configuration of the underlying Devnet instance. The returned object is marked
* as `any` because it may change too often.
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
async getConfig() {
return await this.rpcProvider.sendRequest("devnet_getConfig");
}
}
exports.DevnetProvider = DevnetProvider;
;