diff --git a/README.md b/README.md index 49250b9..594dc8e 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,31 @@ # loom-cli DPoS / Transfer Gateway CLI + +## General Bindings + +- `map-accounts`: Connects the user's dappchain/ethereum keys together. **THIS MUST BE EXECUTED WHEN CONNECTING A NEW KEYPAIR TO THE DAPPCHAIN** +- `coin-balance`: Retrieves the user's DAppChain ERC20 balance. Optionally + provide `--eth` to show the Ethereum balance instead. Optionally provide `--account` to retrieve another user's balance. +- `resolve `: Retrieve the `contractName`'s dappchain address from the address mapper + +## Transfer Gateway Bindings + +- `deposit `: Deposits `amount` LOOM tokens to the gateway. If not + enough tokens approved before hand, it will also approve the missing amount +- `withdraw `: Withdraws `amount` LOOM tokens from the gateway. +- `resume-withdrawal`: Resumes an interrupted withdrawal that didn't consume + the last withdrawal receipt +- `receipt`: Retrieves the currently pending withdrawal receipt (or null if + there is none) + +## DPoS Bindings + +- `list-validators`: Returns the current DPoS validators +- `list-candidates`: Returns information about the current DPoS candidates + + their metadata +- `check-delegations -v validatorAddress -d delegatorAddress`: Checks how much LOOM has been delegated by `delegatorAddress` to `validatorAddress` +- `claim-delegations`: Claims the user's rewards. Optionally can supply + `--account to withdraw to a different address +- `delegate `: Lock up `amount` and delegate it to `validator` +- `undelegate `: Unbond `amount` from `validator` + diff --git a/src/dappchain.ts b/src/dappchain.ts index 92c6534..a01e80c 100644 --- a/src/dappchain.ts +++ b/src/dappchain.ts @@ -162,9 +162,14 @@ export const depositCoinToDAppChainGateway = async ( ethereumAddress ); console.log( - `${amount.div(coinMultiplier).toString()} tokens deposited to DAppChain Gateway...` + `${amount + .div(coinMultiplier) + .toString()} tokens deposited to DAppChain Gateway...` ); - while (pendingReceipt === null || pendingReceipt.oracleSignature.length === 0) { + while ( + pendingReceipt === null || + pendingReceipt.oracleSignature.length === 0 + ) { pendingReceipt = await getPendingWithdrawalReceipt(account); await sleep(2000); } @@ -194,7 +199,7 @@ export const checkDelegations = async ( account: Account, validator: string, delegator: string -): Promise => { +): Promise => { const dpos = await getDAppChainDPOSContract(account); const validatorAddress = prefixAddress(account.client, validator); const delegatorAddress = prefixAddress(account.client, delegator); @@ -202,12 +207,15 @@ export const checkDelegations = async ( validatorAddress, delegatorAddress ); - return delegation!; // @todo -> remove ! and handle null case + return delegation; }; -export const claimDelegations = async (account: Account, withdrawalAddress: Address) => { +export const claimDelegations = async ( + account: Account, + withdrawalAddress: Address +) => { const dpos = await getDAppChainDPOSContract(account); - return dpos.claimDistributionAsync(withdrawalAddress) + return dpos.claimDistributionAsync(withdrawalAddress); }; export const delegate = async ( diff --git a/src/index.ts b/src/index.ts index b60b133..bade58f 100644 --- a/src/index.ts +++ b/src/index.ts @@ -9,7 +9,7 @@ import { loadMainnetAccount, rinkebyGatewayAddress, coinMultiplier, - withdrawCoinFromRinkebyGateway, + withdrawCoinFromRinkebyGateway } from "./loom_mainnet"; import { @@ -157,11 +157,14 @@ program const receipt = await getPendingWithdrawalReceipt(account); if (receipt) { console.log(`Pending receipt:`); - console.log("Token owner:", receipt.tokenOwner.toString()) - console.log("Contract:", receipt.tokenContract.toString()) - console.log("Token kind:", receipt.tokenKind) - console.log("Nonce:", receipt.withdrawalNonce) - console.log("Signature:", CryptoUtils.bytesToHexAddr(receipt.oracleSignature)) + console.log("Token owner:", receipt.tokenOwner.toString()); + console.log("Contract:", receipt.tokenContract.toString()); + console.log("Token kind:", receipt.tokenKind); + console.log("Nonce:", receipt.withdrawalNonce); + console.log( + "Signature:", + CryptoUtils.bytesToHexAddr(receipt.oracleSignature) + ); } else { console.log(`No pending receipt`); } @@ -206,8 +209,8 @@ program const candidates = await listCandidates(account); console.log(`Current candidates:`, candidates); candidates.forEach(c => { - console.log(" Pubkey:", c.pubKey); - console.log(" Power:", c.address); + console.log(" Pubkey:", CryptoUtils.Uint8ArrayToB64(c.pubKey)); + console.log(" Power:", c.address.toString()); console.log(" Fee:", c.fee); console.log(" Description:", c.description); console.log(" Name:", c.name); @@ -258,11 +261,11 @@ program chainId ); try { - let withdrawalAddress + let withdrawalAddress; if (options.account) { withdrawalAddress = Address.fromString(`${chainId}:${options.account}`); } else { - withdrawalAddress = account.address + withdrawalAddress = account.address; } const rewards = await claimDelegations(account, withdrawalAddress); console.log(`User claimed back rewards: ${rewards}`); @@ -272,44 +275,34 @@ program }); program - .command("delegate ") + .command("delegate ") .description("Delegate `amount` to a candidate / validator") - .option("-v, --validator ") - .action(async function(amount: string, option) { + .action(async function(amount: string, validator: string) { const account = loadDAppChainAccount( dappchainEndpoint, dappchainPrivateKey, chainId ); try { - await delegate( - account, - option.validator, - new BN(amount).mul(coinMultiplier) - ); - console.log(`Delegated ${amount} LOOM to ${option.validator}`); + await delegate(account, validator, new BN(amount).mul(coinMultiplier)); + console.log(`Delegated ${amount} LOOM to ${validator}`); } catch (err) { console.error(err); } }); program - .command("undelegate ") - .description("Undelegate `amount` from a candidate / validator") + .command("undelegate ") .option("-v, --validator ") - .action(async function(amount: string, option) { + .action(async function(amount: string, validator: string) { const account = loadDAppChainAccount( dappchainEndpoint, dappchainPrivateKey, chainId ); try { - await undelegate( - account, - option.validator, - new BN(amount).mul(coinMultiplier) - ); - console.log(`Undelegated ${amount} LOOM to ${option.validator}`); + await undelegate(account, validator, new BN(amount).mul(coinMultiplier)); + console.log(`Undelegated ${amount} LOOM to ${validator}`); } catch (err) { console.error(err); } @@ -362,8 +355,8 @@ program ? rinkebyGatewayAddress : options.account; } - balance = await getMainnetBalance(wallet, ownerAddress) - balance = balance.div(ethers.utils.parseEther('1')) + balance = await getMainnetBalance(wallet, ownerAddress); + balance = balance.div(ethers.utils.parseEther("1")); } else { // Retrieve dappchain balance const account = loadDAppChainAccount( @@ -374,7 +367,7 @@ program ownerAddress = account.address; try { balance = await getDAppChainBalance(account, options.account); - balance = balance.div(coinMultiplier) + balance = balance.div(coinMultiplier); } catch (err) { throw err; } finally { diff --git a/src/loom_mainnet.ts b/src/loom_mainnet.ts index 80935be..32376d0 100644 --- a/src/loom_mainnet.ts +++ b/src/loom_mainnet.ts @@ -57,8 +57,11 @@ export const depositCoinToRinkebyGateway = async ( console.log("Current approval:", currentApproval); if (amount.gt(currentApproval)) { - let tx: ContractTransaction = await loom.approve(gateway.address, amount.sub(currentApproval).toString()); - await tx.wait() + let tx: ContractTransaction = await loom.approve( + gateway.address, + amount.sub(currentApproval).toString() + ); + await tx.wait(); console.log("Approved an extra", amount.sub(currentApproval)); } return gateway.depositERC20(amount.toString(), loom.address, { @@ -72,5 +75,7 @@ export const withdrawCoinFromRinkebyGateway = async ( sig: string ): Promise => { const gateway = getRinkebyGatewayContract(wallet); - return gateway.withdrawERC20(amount.toString(), sig, rinkebyLoomAddress, {gasLimit: 500000}); + return gateway.withdrawERC20(amount.toString(), sig, rinkebyLoomAddress, { + gasLimit: 500000 + }); };