Skip to content

Commit

Permalink
Update README and bindings
Browse files Browse the repository at this point in the history
  • Loading branch information
gakonst committed Dec 3, 2018
1 parent 414606b commit c6d21ba
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 40 deletions.
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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 <contractName>`: Retrieve the `contractName`'s dappchain address from the address mapper

## Transfer Gateway Bindings

- `deposit <amount>`: Deposits `amount` LOOM tokens to the gateway. If not
enough tokens approved before hand, it will also approve the missing amount
- `withdraw <amount>`: 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 <amount> <validator>`: Lock up `amount` and delegate it to `validator`
- `undelegate <amount> <validator>`: Unbond `amount` from `validator`

20 changes: 14 additions & 6 deletions src/dappchain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -194,20 +199,23 @@ export const checkDelegations = async (
account: Account,
validator: string,
delegator: string
): Promise<IDelegation> => {
): Promise<IDelegation | null> => {
const dpos = await getDAppChainDPOSContract(account);
const validatorAddress = prefixAddress(account.client, validator);
const delegatorAddress = prefixAddress(account.client, delegator);
const delegation = await dpos.checkDelegationAsync(
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 (
Expand Down
55 changes: 24 additions & 31 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
loadMainnetAccount,
rinkebyGatewayAddress,
coinMultiplier,
withdrawCoinFromRinkebyGateway,
withdrawCoinFromRinkebyGateway
} from "./loom_mainnet";

import {
Expand Down Expand Up @@ -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`);
}
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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}`);
Expand All @@ -272,44 +275,34 @@ program
});

program
.command("delegate <amount>")
.command("delegate <amount> <validator>")
.description("Delegate `amount` to a candidate / validator")
.option("-v, --validator <dappchain b64 address>")
.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 <amount>")
.description("Undelegate `amount` from a candidate / validator")
.command("undelegate <amount> <validator>")
.option("-v, --validator <dappchain b64 address>")
.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);
}
Expand Down Expand Up @@ -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(
Expand All @@ -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 {
Expand Down
11 changes: 8 additions & 3 deletions src/loom_mainnet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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, {
Expand All @@ -72,5 +75,7 @@ export const withdrawCoinFromRinkebyGateway = async (
sig: string
): Promise<ethers.ContractTransaction> => {
const gateway = getRinkebyGatewayContract(wallet);
return gateway.withdrawERC20(amount.toString(), sig, rinkebyLoomAddress, {gasLimit: 500000});
return gateway.withdrawERC20(amount.toString(), sig, rinkebyLoomAddress, {
gasLimit: 500000
});
};

0 comments on commit c6d21ba

Please sign in to comment.