Quick Start
If you are already familiar with RenVM, RenJS or Web3 libraries, you may be able to jump into using RenJS by reading through the following example and then referring to the sections in the sidebar as required.
If you need more of an introduction to RenVM and RenJS, continue on to Introduction.
To install:
- yarn
- npm
yarn add @renproject/ren @renproject/chains
npm install --save @renproject/ren @renproject/chains
Swap tabs to change between an example of moving BTC from Bitcoin to Ethereum, and an example of bridging DAI from Ethereum to Binance Smart Chain:
- BTC to Ethereum
- DAI to Binance Smart Chain
import { providers, Wallet } from "ethers";
import { Bitcoin, Ethereum } from "@renproject/chains";
import RenJS from "@renproject/ren";
// Test account - do not send real funds.
const mnemonic =
"black magic humor turtle symptom liar salmon rally hurt concert tower run";
const network = "testnet";
const main = async () => {
// Initialize Bitcoin and Ethereum.
const bitcoin = new Bitcoin({ network });
const ethereum = new Ethereum({
network,
provider: new providers.JsonRpcProvider(
Ethereum.configMap[network].config.rpcUrls[0]
),
signer: Wallet.fromMnemonic(mnemonic),
});
// Create RenJS instance. NOTE - chains must now be linked to RenJS using
// `withChains`.
const renJS = new RenJS(network).withChains(bitcoin, ethereum);
// Create gateway - mints and burns are both initialized with `gateway`.
// Gateway parameters are serializable.
const gateway = await renJS.gateway({
asset: bitcoin.assets.BTC, // "BTC"
from: bitcoin.GatewayAddress(),
to: ethereum.Account(),
});
// `gateway.fees` exposes values and helpers for calculating fees.
console.log(gateway.fees);
console.log(`Deposit ${gateway.params.asset} to ${gateway.gatewayAddress}`);
// NOTE: Event has been renamed from "deposit" to "transaction".
gateway.on("transaction", (tx) => {
(async () => {
// GatewayTransaction parameters are serializable. To re-create
// the transaction, call `renJS.gatewayTransaction`.
console.log(tx.params);
// Wait for remaining confirmations for input transaction.
await tx.in.wait();
// RenVM transaction also follows the submit/wait pattern.
await tx.renVM.submit().on("progress", console.log);
await tx.renVM.wait();
// `submit` accepts a `txConfig` parameter for overriding
// transaction config.
await tx.out.submit({
txConfig: {
gasLimit: 1000000,
},
});
await tx.out.wait();
// All transactions return a `ChainTransaction` object in the
// progress, with a `txid` field (base64) and a `txidFormatted`
// field (chain-dependent).
const outTx = tx.out.progress.transaction;
console.log("Done:", outTx.txidFormatted);
// All chain classes expose a common set of helper functions (see
// `Chain` class.)
console.log(tx.toChain.transactionExplorerLink(outTx));
})().catch(console.error);
});
};
main().catch((error) => {
console.error(error);
process.exit(1);
});
import { providers, Wallet } from "ethers";
import { BinanceSmartChain, Ethereum } from "@renproject/chains-ethereum";
import RenJS from "@renproject/ren";
// Test account - do not send real funds.
const mnemonic =
"black magic humor turtle symptom liar salmon rally hurt concert tower run";
const network = "testnet";
const main = async () => {
// Initialize Ethereum and BSC chains.
const ethereum = new Ethereum({
network,
provider: new providers.JsonRpcProvider(
Ethereum.configMap[network].config.rpcUrls[0]
),
signer: Wallet.fromMnemonic(mnemonic),
});
const bsc = new BinanceSmartChain({
network,
provider: new providers.JsonRpcProvider(
BinanceSmartChain.configMap[network].config.rpcUrls[0]
),
signer: Wallet.fromMnemonic(mnemonic),
});
// Create RenJS instance. NOTE - chains must now be linked to RenJS using
// `withChains`.
const renJS = new RenJS(network).withChains(ethereum, bsc);
// Create gateway - mints and burns are both initialized with `gateway`.
// Gateway parameters are serializable.
const gateway = await renJS.gateway({
asset: ethereum.assets.DAI, // "DAI"
from: ethereum.Account({ amount: 0.1, convertUnit: true }),
to: bsc.Account(),
});
// `gateway.fees` exposes values and helpers for calculating fees.
console.log(gateway.fees);
// `gateway.inSetup` may contain multiple transactions. Instead of
// hardcoding each setup, you can instead loop through `gateway.inSetup`.
await gateway.inSetup.approval.submit();
// All transactions now follow a submit/wait pattern - see TxSubmitter
// interface.
await gateway.inSetup.approval.wait();
// Lock the DAI on Ethereum - for an asset like BTC, you should instead
// display `gateway.gatewayAddress` for receiving funds.
// All transactions emit a `progress` whenever an update is available
// (confirmations, errors, etc.)
await gateway.in.submit().on("progress", console.log);
// Wait for the first confirmation.
await gateway.in.wait(1);
// NOTE: Event has been renamed from "deposit" to "transaction".
gateway.on("transaction", (tx) => {
(async () => {
// GatewayTransaction parameters are serializable. To re-create
// the transaction, call `renJS.gatewayTransaction`.
console.log(tx.params);
// Wait for remaining confirmations for input transaction.
await tx.in.wait();
// RenVM transaction also follows the submit/wait pattern.
await tx.renVM.submit().on("progress", console.log);
await tx.renVM.wait();
// `submit` accepts a `txConfig` parameter for overriding
// transaction config.
await tx.out.submit({
txConfig: {
gasLimit: 1000000,
},
});
await tx.out.wait();
// All transactions return a `ChainTransaction` object in the
// progress, with a `txid` field (base64) and a `txidFormatted`
// field (chain-dependent).
const outTx = tx.out.progress.transaction;
console.log("Done:", outTx.txidFormatted);
// All chain classes expose a common set of helper functions (see
// `Chain` class.)
console.log(tx.toChain.transactionExplorerLink(outTx));
})().catch(console.error);
});
};
main().catch((error) => {
console.error(error);
process.exit(1);
});