@renproject/ren
Version:
Official Ren JavaScript SDK for bridging crypto assets cross-chain.
172 lines • 7.75 kB
JavaScript
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
import { RenVMProvider } from "@renproject/provider";
import { assertType, ErrorWithCode, RenJSError, RenNetwork, } from "@renproject/utils";
import { Gateway } from "./gateway";
import { GatewayTransaction } from "./gatewayTransaction";
import { defaultTransactionHandler } from "./utils/defaultTransactionHandler";
import { estimateTransactionFee } from "./utils/fees";
export { Gateway } from "./gateway";
export { GatewayTransaction } from "./gatewayTransaction";
export { RenVMTxSubmitter } from "./renVMTxSubmitter";
/**
* This is the main exported class from `@renproject/ren`.
*
* ```typescript
* import RenJS from "@renproject/ren";
* ```
*
* By default, RenJS will connect to the RenVM mainnet network. To connect
* to `testnet` or to configure a custom connection, RenJS takes an optional
* provider object. See the [[constructor]] for more details.
*
* ```typescript
* new RenJS(); // Same as `new RenJS("mainnet");`
* new RenJS("testnet");
* new RenJS(custom provider object);
* ```
*
* It then exposes two main functions:
* 1. [[gateway]] - for initiating new cross-chain transfers.
* 2. [[gatewayTransaction]] - for continuing existing cross-chain transfers.
*
* Also see:
* 1. [[getFees]] - for estimating the fees that will be incurred by minting or
* burning.
* 2. [[defaultTransactionHandler]] - a static function for handling
* GatewayTransactions.
*
*/
export class RenJS {
/**
* Accepts the name of a network, or a network object.
*
* @param providerOrNetwork Provider the name of a RenNetwork or a RenVM
* provider instance.
* @param config Provider RenJS config such as a logger.
*/
constructor(providerOrNetwork = RenNetwork.Mainnet, config) {
/**
* In order to add support for chains, `withChains` must be called,
* providing chain handlers that implement the Chain interface.
*/
this.chains = {};
/**
* Register one or more chain handlers, each implementing the Chain
* interface. By default, RenJS has no chain handlers, so this is required
* for all chains being bridged from or to.
*
* Note that any Gateway or GatewayTransaction instance that has already
* been created will continue pointing to the chain handler at the time it
* was created.
*/
this.withChains = (...chains) => {
for (const chain of chains) {
this.chains[chain.chain] = chain;
}
return this;
};
this.withChain = this.withChains;
/**
* Return the chain handler previously added using [[withChains]].
*/
this.getChain = (name) => {
assertType("string", { name });
if (!this.chains[name]) {
throw ErrorWithCode.updateError(new Error(`Chain ${name} not found. (Must call 'renJS.withChains(${name.toLowerCase()})')`), RenJSError.PARAMETER_ERROR);
}
return this.chains[name];
};
/**
* Calculate the RenVM and blockchain fees for a transaction.
*
* @example
* renJS.getFees({
* asset: "BTC",
* from: "Bitcoin",
* to: "Ethereum",
* })
*
* @example
* renJS.getFees({
* asset: "BTC",
* from: bitcoin.GatewayAddress(),
* to: ethereum.Account(),
* })
*/
this.getFees = ({ asset, from, to, }) => __awaiter(this, void 0, void 0, function* () {
const fromChain = this.getChain(typeof from === "string" ? from : from.chain);
const toChain = this.getChain(typeof to === "string" ? to : to.chain);
return yield estimateTransactionFee(this.provider, asset, fromChain, toChain);
});
/**
* Return the recommended shard for processing transactions of the specified
* asset.
*/
this.selectShard = (asset) => __awaiter(this, void 0, void 0, function* () { return this.provider.selectShard(asset); });
/**
* `gateway` initiates a new Gateway for bridging an asset between two
* chains.
*
* See [[Gateway]] for all the options that can be set.
*
* @example
* const gateway = renJS.gateway({
* asset: "BTC",
* from: bitcoin.GatewayAddress(),
* to: ethereum.Account(),
* });
* ```
*
* @param params See [[GatewayParams]]. This is a serializable object,
* allowing gateways to be re-created.
* @param config Optional RenJS config, such as a logger.
*/
this.gateway = (params, config = {}) => __awaiter(this, void 0, void 0, function* () {
return new Gateway(this.provider, this.getChain(params.from.chain), this.getChain(params.to.chain), Object.assign(Object.assign(Object.assign({}, params), { from: Object.assign({}, params.from), to: Object.assign({}, params.to) }), (params.shard ? { shard: Object.assign({}, params.shard) } : {})), Object.assign(Object.assign({}, this._config), config)).initialize();
});
/**
* `gatewayTransaction` allows you to re-create a transaction emitted
* by `gateway.on("transaction", (tx) => {...})`.
*
* @param params The same type as `tx.params` on an emitted `tx`. This is a
* serializable object.
* @param config Optional RenJS config, such as a logger.
*/
this.gatewayTransaction = (params, config) => __awaiter(this, void 0, void 0, function* () {
return new GatewayTransaction(this.provider, this.getChain(params.fromTx.chain), this.getChain(params.to.chain), Object.assign(Object.assign(Object.assign({}, params), { fromTx: Object.assign({}, params.fromTx), to: Object.assign({}, params.to) }), (params.shard ? { shard: Object.assign({}, params.shard) } : {})), undefined, Object.assign(Object.assign({}, this._config), config)).initialize();
});
this._config = config || {};
this.provider =
typeof providerOrNetwork === "string"
? new RenVMProvider(providerOrNetwork, this._config.logger)
: providerOrNetwork;
}
}
/**
* `Networks` exposes the network options that can be passed in to the RenJS
* constructor. `Networks.Mainnet` resolves to the string `"mainnet"`.
*/
RenJS.Networks = RenNetwork;
/**
* `RenJS.defaultTransactionHandler` can be passed as a transaction callback when
* minting. It will handle submitting to RenVM and then to the mint-chain,
* as long as a valid provider for the mint-chain is given.
*
* This is not recommended for front-ends, since it may trigger a wallet
* pop-up unexpectedly when the mint is ready to be submitted.
*
* ```ts
* gateway.on("transaction", RenJS.defaultTransactionHandler);
* ```
*/
RenJS.defaultTransactionHandler = defaultTransactionHandler;
export default RenJS;
//# sourceMappingURL=index.js.map