Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Stub out bsc testnet and mainnet access via loom cli #16

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions configs/bscmainnet.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"ethPrivateKeyFile":
"ethereum_private_key",
"dappchainPrivateKeyFile":
"mainnet_private_key",

"dappchainEndpoint": "http://localhost:46658",

"loomGatewayEthAddress": "",

"chainId": "default",

"infuraURL": "https://bsc-dataseed1.binance.org",
"isBsc": true

}

16 changes: 16 additions & 0 deletions configs/bsctestnet.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"ethPrivateKeyFile":
"ethereum_private_key",
"dappchainPrivateKeyFile":
"mainnet_private_key",

"dappchainEndpoint": "http://localhost:46658",

"loomGatewayEthAddress": "0x0cee0FB12205b9Ca9d4Fbed502091dEfD7ae6ff5",

"chainId": "default",

"infuraURL": "https://data-seed-prebsc-1-s1.binance.org:8545",
"isBsc": true
}

4 changes: 3 additions & 1 deletion configs/mainnet.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

"loomGatewayEthAddress": "0xfcF1E3fA575A313fd81feA2caA06269B49F1A528",

"chainId": "default"
"chainId": "default",

"infuraURL": "https://mainnet.infura.io/v3/"

}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
"ethereumjs-tx": "^1.3.7",
"ethereumjs-util": "^5.2.0",
"ethereumjs-wallet": "^0.6.3",
"loom-js": "1.57.0",
"loom-js": "1.75.2",
"lowdb": "^1.0.0",
"repl.history": "^0.1.4",
"rlp": "^2.1.0",
Expand Down
40 changes: 31 additions & 9 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ import { ethers } from 'ethers'
import { CryptoUtils, Contracts, Address, LocalAddress, Client, EthersSigner } from "loom-js";
import { createDefaultClient, sleep } from 'loom-js/dist/helpers';

const ERC20ABI = require('loom-js/dist/mainnet-contracts/ERC20.json')
const ERC20GatewayABI = require('loom-js/dist/mainnet-contracts/ERC20Gateway.json')
//const ERC20GatewayABI = require('loom-js/dist/mainnet-contracts/ERC20Gateway.json')
import { abi as ERC20ABI } from 'loom-js/dist/mainnet-contracts/ERC20Factory'
import { abi as ERC20GatewayABI } from 'loom-js/dist/mainnet-contracts/EthereumGatewayV1Factory'

// LOOM has 18 decimals
const coinMultiplier = new BN(10).pow(new BN(18));
Expand Down Expand Up @@ -46,6 +47,8 @@ interface IConfig {
dappchainPrivateKeyFile: string;
ethPrivateKeyFile: string;
chainId: string;
infuraURL: string;
isBsc: boolean;
}

interface IUser {
Expand All @@ -59,27 +62,35 @@ interface IUser {
ethAddress: string;
}

const ethEndPoint = `https://mainnet.infura.io/v3/${process.env.INFURA_API_KEY}`

/**
* Creates clients to interact with Loom Mainnet and Ethereum Mainnet on behalf of a user.
*
* @param config Network and key configuration.
*/
async function createUser(config: IConfig): Promise<IUser> {
if (!process.env.INFURA_API_KEY) {
throw new Error("INFURA_API_KEY env var not set")
let ethEndPoint = ``
if(config.isBsc) {
ethEndPoint = `${config.infuraURL}`
}

else {
if (!process.env.INFURA_API_KEY) {
throw new Error("INFURA_API_KEY env var not set")
}
ethEndPoint = `${config.infuraURL}${process.env.INFURA_API_KEY}`
}

const dappchainPrivateKey = fs.readFileSync(config.dappchainPrivateKeyFile, 'utf-8').toString().trim()
const ethPrivateKey = fs.readFileSync(config.ethPrivateKeyFile, 'utf-8').toString().trim()


const { client, publicKey } = createDefaultClient(
dappchainPrivateKey,
config.dappchainEndpoint,
config.chainId
)

console.log(`Connecting to EthEndpoint -${ethEndPoint}`)
const provider = new ethers.providers.JsonRpcProvider(ethEndPoint)
const wallet = new ethers.Wallet(ethPrivateKey, provider)
const ethAddress = await wallet.getAddress()
Expand Down Expand Up @@ -120,8 +131,10 @@ async function mapAccountsAsync(
return
}


const signer = new EthersSigner(wallet)
console.log(`Mapping ${loomAddress} to ${ethAddress}...`)

await mapper.addIdentityMappingAsync(loomAddress, ethAddress, signer)
console.log('Mapping complete')
}
Expand All @@ -147,6 +160,7 @@ async function depositToLoomGatewayAsync(
await dappchainLoom.approveAsync(gateway.address, amount);
const userEthAddress = Address.fromString(`eth:${user.ethAddress}`);
const loomEthAddress = Address.fromString(`eth:${loomEthAddressStr}`);
console.log(`userEthAddress->${userEthAddress} loomEthAddress->${loomEthAddress}`)
console.log(`Transferring ${amount.div(coinMultiplier).toString()} to Loom Mainnet Gateway...`);
await gateway.withdrawLoomCoinAsync(amount, loomEthAddress, userEthAddress);
console.log(`Transfer complete, waiting for signed receipt...`);
Expand Down Expand Up @@ -254,12 +268,18 @@ program
try {
const ethereumGateway = new ethers.Contract(config.loomGatewayEthAddress, ERC20GatewayABI, user.wallet);
const loomEthAddress: string = await ethereumGateway.functions.loomAddress();
const dappchainGateway = await Contracts.LoomCoinTransferGateway.createAsync(user.client, user.loomAddress);
let dappchainGateway = await Contracts.LoomCoinTransferGateway.createAsync(user.client, user.loomAddress);

if(config.isBsc) {
console.log("Setting up a bsc gateway")
dappchainGateway = await Contracts.BscTransferGateway.createAsync(user.client, user.loomAddress);
}

const amountBN = new BN(amount).mul(coinMultiplier);
const sig = await depositToLoomGatewayAsync(dappchainGateway, amountBN, loomEthAddress, user);
console.log('Withdrawing from Ethereum Gateway...');
const tx = await ethereumGateway.functions.withdrawERC20(amountBN.toString(), sig, loomEthAddress);
console.log('Waiting for tx confirmation...');
console.log('Waiting for tx confirmation...--- ${tx}');
await tx.wait();
console.log(`${amount} tokens withdrawn from Ethereum Gateway.`);
console.log(`Ethereum tx hash: ${tx.hash}`);
Expand Down Expand Up @@ -325,7 +345,9 @@ program
console.log("Token owner:", receipt.tokenOwner.toString());
console.log("Token address:", loomEthAddress)
console.log("Gateway address:", ethereumGateway.address)
console.log("Contract:", receipt.tokenContract.toString());
if (receipt.tokenContract) {
console.log("Contract:", receipt.tokenContract.toString());
}
console.log("Token kind:", receipt.tokenKind);
console.log("Nonce:", receipt.withdrawalNonce.toString());
console.log("Contract Nonce:", ethNonce.toString());
Expand Down
Loading