Skip to content

Commit

Permalink
use config file passed by cli to connect
Browse files Browse the repository at this point in the history
  • Loading branch information
gakonst committed Apr 1, 2019
1 parent d857de9 commit 30b6984
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 124 deletions.
13 changes: 13 additions & 0 deletions src/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"ethPrivateKey":
"0xDE03930EA2DAB535A1717BE8B5249E14388F0B9836DE8CF3C9299AEB25EDAD98",
"dappchainPrivateKey":
"POcupxN+lMdekrgztzNtLOiyT0UUs+UdkQiC/lRKV4kIRQy+j86BSajcfZ+EG5WXhMHdsKf5DU3oRL+SjC68Nw==",

"ethEndpoint": "https://rinkeby.infura.io/5Ic91y0T9nLh6qUg33K0",
"dappchainEndpoint": "http://plasma.dappchains.com",

"loomGatewayEthAddress": "0x426de37D73B0B768648335A5e1243e46b3a5E0A9",

"chainId": "default"
}
156 changes: 32 additions & 124 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,39 @@ import program from "commander";
import BN from "bn.js";

import { DPOSUser, CryptoUtils, GatewayVersion } from "loom-js";
import { config } from "./trudy_oracle_devnet";
import { ICandidate } from "loom-js/dist/contracts/dpos";

// See https://loomx.io/developers/docs/en/testnet-plasma.html#contract-addresses-transfer-gateway
// for the most up to date address.

const coinMultiplier = new BN(10).pow(new BN(18))

program
.version('0.1.0')
.option('-c, --config <path>', 'config file absolute path')
.parse(process.argv)

const config = require(program.config)

const createUser = async (config: any) : Promise<DPOSUser> => {
return DPOSUser.createOfflineUserAsync(
config.ethEndpoint,
config.ethPrivateKey,
config.dappchainEndpoint,
config.dappchainPrivateKey,
config.chainId,
config.loomGatewayEthAddress,
GatewayVersion.SINGLESIG
);
}

program
.command("deposit <amount>")
.description(
"deposit the specified amount of LOOM tokens into the Transfer Gateway"
)
.action(async function(amount: string) {
const user = await DPOSUser.createOfflineUserAsync(
config.ethEndpoint,
config.ethPrivateKey,
config.dappchainEndpoint,
config.dappchainPrivateKey,
config.chainId,
config.loomGatewayEthAddress,
GatewayVersion.SINGLESIG
);
const user = await createUser(config)
try {
const tx = await user.depositAsync(new BN(amount).mul(coinMultiplier));
await tx.wait();
Expand All @@ -47,17 +57,8 @@ program
"Number of seconds to wait for withdrawal to be processed"
)
.action(async function(amount: string, options: any) {
let user;
const user = await createUser(config)
try {
user = await DPOSUser.createOfflineUserAsync(
config.ethEndpoint,
config.ethPrivateKey,
config.dappchainEndpoint,
config.dappchainPrivateKey,
config.chainId,
config.loomGatewayEthAddress,
GatewayVersion.SINGLESIG
);
const actualAmount = new BN(amount).mul(coinMultiplier);
const tx = await user.withdrawAsync(actualAmount);
await tx.wait();
Expand All @@ -74,43 +75,24 @@ program
.command("resume-withdrawal")
.description("Resumes a withdrawal from a pending receipt")
.action(async function() {
let user;
const user = await createUser(config)
try {
user = await DPOSUser.createOfflineUserAsync(
config.ethEndpoint,
config.ethPrivateKey,
config.dappchainEndpoint,
config.dappchainPrivateKey,
config.chainId,
config.loomGatewayEthAddress,
GatewayVersion.SINGLESIG
);
const tx = await user.resumeWithdrawalAsync();
if (tx) {
await tx.wait();
}
} catch (err) {
console.error(err);
} finally {
if (user) {
user.disconnect();
}
}
});

program
.command("receipt")
.description("Returns the currently pending receipt")
.action(async function() {
const user = await DPOSUser.createOfflineUserAsync(
config.ethEndpoint,
config.ethPrivateKey,
config.dappchainEndpoint,
config.dappchainPrivateKey,
config.chainId,
config.loomGatewayEthAddress,
GatewayVersion.SINGLESIG
);
const user = await createUser(config)
try {
const receipt = await user.getPendingWithdrawalReceiptAsync();
if (receipt) {
Expand Down Expand Up @@ -138,37 +120,20 @@ program
.command("map-accounts")
.description("Connects the user's eth/dappchain addresses")
.action(async function() {
const user = await DPOSUser.createOfflineUserAsync(
config.ethEndpoint,
config.ethPrivateKey,
config.dappchainEndpoint,
config.dappchainPrivateKey,
config.chainId,
config.loomGatewayEthAddress,
GatewayVersion.SINGLESIG
);
const user = await createUser(config)
try {
console.log('trying to map acc')
await user.mapAccountsAsync();
console.log('mapped acc')
} catch (err) {
console.log("got err")
console.error(err);
}
});
program
.command("list-validators")
.description("Show the current DPoS validators")
.action(async function() {
const user = await DPOSUser.createOfflineUserAsync(
config.ethEndpoint,
config.ethPrivateKey,
config.dappchainEndpoint,
config.dappchainPrivateKey,
config.chainId,
config.loomGatewayEthAddress,
GatewayVersion.SINGLESIG
);
const user = await createUser(config)
try {
const validators = await user.listValidatorsAsync();
console.log(`Current validators:`);
Expand All @@ -192,15 +157,7 @@ program
.command("list-candidates")
.description("Show the current DPoS candidates (along with their metadata)")
.action(async function() {
const user = await DPOSUser.createOfflineUserAsync(
config.ethEndpoint,
config.ethPrivateKey,
config.dappchainEndpoint,
config.dappchainPrivateKey,
config.chainId,
config.loomGatewayEthAddress,
GatewayVersion.SINGLESIG
);
const user = await createUser(config)
try {
const candidates = await user.listCandidatesAsync();
console.log(`Current candidates:`);
Expand All @@ -225,15 +182,7 @@ program
.option("-v, --validator <dappchain b64 address>")
.option("-d, --delegator <dappchain b64 address>")
.action(async function(option) {
const user = await DPOSUser.createOfflineUserAsync(
config.ethEndpoint,
config.ethPrivateKey,
config.dappchainEndpoint,
config.dappchainPrivateKey,
config.chainId,
config.loomGatewayEthAddress,
GatewayVersion.SINGLESIG
);
const user = await createUser(config)
try {
const delegation = await user.checkDelegationsAsync(
option.validator,
Expand All @@ -259,15 +208,7 @@ program
.description("Get back the user rewards")
.option("-a, --account <account to withdraw the rewards to>")
.action(async function() {
const user = await DPOSUser.createOfflineUserAsync(
config.ethEndpoint,
config.ethPrivateKey,
config.dappchainEndpoint,
config.dappchainPrivateKey,
config.chainId,
config.loomGatewayEthAddress,
GatewayVersion.SINGLESIG
);
const user = await createUser(config)
try {
const rewards = await user.claimDelegationsAsync();
console.log(`User claimed back rewards: ${rewards}`);
Expand All @@ -280,15 +221,7 @@ program
.command("delegate <amount> <validator> <tier>")
.description("Delegate `amount` to a candidate / validator")
.action(async function(amount: string, validator: string, tier: string) {
const user = await DPOSUser.createOfflineUserAsync(
config.ethEndpoint,
config.ethPrivateKey,
config.dappchainEndpoint,
config.dappchainPrivateKey,
config.chainId,
config.loomGatewayEthAddress,
GatewayVersion.SINGLESIG
);
const user = await createUser(config)
try {
const actualAmount = new BN(amount).mul(coinMultiplier);
console.log(`Delegating ${actualAmount.toString()} to validator`);
Expand All @@ -303,15 +236,7 @@ program
.command("undelegate <amount> <validator>")
.option("-v, --validator <dappchain b64 address>")
.action(async function(amount: string, validator: string) {
const user = await DPOSUser.createOfflineUserAsync(
config.ethEndpoint,
config.ethPrivateKey,
config.dappchainEndpoint,
config.dappchainPrivateKey,
config.chainId,
config.loomGatewayEthAddress,
GatewayVersion.SINGLESIG
);
const user = await createUser(config)
try {
await user.undelegateAsync(validator, new BN(amount).mul(coinMultiplier));
console.log(`Undelegated ${amount} LOOM to ${validator}`);
Expand All @@ -332,16 +257,8 @@ program
"Account address"
)
.action(async function(options) {
const user = await createUser(config)
try {
const user = await DPOSUser.createOfflineUserAsync(
config.ethEndpoint,
config.ethPrivateKey,
config.dappchainEndpoint,
config.dappchainPrivateKey,
config.chainId,
config.loomGatewayEthAddress,
GatewayVersion.SINGLESIG
);
const balance = await user.getDAppChainBalanceAsync(options.account);
console.log(`The account's balance is ${balance}`);
} catch (err) {
Expand All @@ -353,17 +270,8 @@ program
.command("my-delegations")
.description("display the user's delegations to all candidates")
.action(async function() {
const user = await createUser(config)
try {
const user = await DPOSUser.createOfflineUserAsync(
config.ethEndpoint,
config.ethPrivateKey,
config.dappchainEndpoint,
config.dappchainPrivateKey,
config.chainId,
config.loomGatewayEthAddress,
GatewayVersion.SINGLESIG
);

const candidates = await user.listCandidatesAsync();
for (let i in candidates) {
const c = candidates[i];
Expand Down

0 comments on commit 30b6984

Please sign in to comment.