Funding script for Connext REST API multi client
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const axios = require("axios"); | |
const ethers = require("ethers"); | |
// For REST API clietn | |
const baseUrl = "<INSERT_CONNEXT_REST_API_URL>"; | |
// For Rinkeby provider | |
const ethProviderUrl = "<INSERT_ETH_PROVIDER_URL>"; | |
// this mnemonic is used to mint tokens and fund clients with some ETH for gas (currently funded with some Rinkeby ETH) | |
const mnemonic = "confirm hello defy demand try raccoon stove glare clerk female pull bright"; | |
const amounts = { | |
eth: { | |
transfer: ethers.BigNumber.from("2" + "0".repeat(16)), // 0.02 ETH | |
}, | |
token: { | |
mint: ethers.BigNumber.from("1" + "0".repeat(26)), // 100M tokens | |
deposit: ethers.BigNumber.from("1" + "0".repeat(25)), // 10M tokens | |
}, | |
}; | |
// Connext Rinkeby Token | |
const token = { | |
address: "0x50C94BeCAd95bEe21aF226dc799365Ee6B134459", | |
abi: ["function mint(address tokenOwner, uint256 tokens) returns (bool success)"], | |
}; | |
// gets active clients' publicIdentifiers | |
async function getClients() { | |
const url = `${baseUrl}/clients`; | |
console.log(`GET ${url}`); | |
return (await axios.get(url)).data; | |
} | |
// get client channelProvider config | |
async function getConfig(publicIdentifier) { | |
const url = `${baseUrl}/config/${publicIdentifier}`; | |
console.log(`GET ${url}`); | |
return (await axios.get(url)).data; | |
} | |
// deposit asset on channel | |
async function postDeposit(amount, assetId, publicIdentifier) { | |
const url = `${baseUrl}/deposit`; | |
console.log(`POST ${url}`); | |
const body = { amount, assetId, publicIdentifier }; | |
console.log(`body: ${JSON.stringify(body)}`); | |
return (await axios.post(url, body)).data; | |
} | |
// funding script for all active clients | |
// 1. transfer some ETH for gas | |
// 2. mint tokens for client signerAddress | |
// 3. deposit tokens on client channel | |
async function fund() { | |
const clients = await getClients(); | |
console.log(`Found ${clients.length} clients`); | |
const provider = new ethers.providers.JsonRpcProvider(ethProviderUrl); | |
const wallet = ethers.Wallet.fromMnemonic(mnemonic).connect(provider); | |
console.log(`Wallet address: ${wallet.address}`); | |
console.log(`Wallet ETH balance: ${(await wallet.getBalance()).toString()}`); | |
for (const { publicIdentifier } of clients) { | |
console.log(`publicIdentifier: ${publicIdentifier}`); | |
const config = await getConfig(publicIdentifier); | |
console.log(`signerAddress: ${config.signerAddress}`); | |
const ethBalance = await provider.getBalance(config.signerAddress); | |
console.log(`eth balance: ${ethBalance}`); | |
if (ethBalance.toString() === "0") { | |
const transferTx = await wallet.sendTransaction({ | |
from: wallet.address, | |
to: config.signerAddress, | |
value: amounts.eth.transfer, | |
data: "0x", | |
}); | |
console.log(`sent ${amounts.eth.transfer.toString()} ETH to ${config.signerAddress}`); | |
await transferTx.wait(2); | |
} | |
const minter = new ethers.Contract(token.address, token.abi, wallet); | |
const mintTx = await minter.mint(config.signerAddress, amounts.token.mint); | |
console.log(`minted ${amounts.token.mint.toString()} tokens to ${config.signerAddress}`); | |
await mintTx.wait(2); | |
await postDeposit(amounts.token.deposit.toString(), token.address, publicIdentifier); | |
console.log(`deposited ${amounts.token.deposit.toString()} tokens to ${publicIdentifier}`); | |
} | |
console.log(`Successfully funded ${clients.length} clients`); | |
} | |
// run | |
fund(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment