Skip to content

Commit

Permalink
add verbose output flag and make CLI work with forno wss
Browse files Browse the repository at this point in the history
  • Loading branch information
diwu1989 committed Feb 7, 2022
1 parent 4e7df04 commit bb561a0
Showing 1 changed file with 17 additions and 4 deletions.
21 changes: 17 additions & 4 deletions src/cli/cli.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env node
import commander from 'commander';
import { ContractKit, newKit } from '@celo/contractkit';
import Web3 from 'web3';
import { ContractKit, newKit, newKitFromWeb3 } from '@celo/contractkit';
import BigNumber from 'bignumber.js';
import { toTransactionObject } from '@celo/connect';

Expand Down Expand Up @@ -28,6 +29,7 @@ const program = commander.program
.option("--max-slippage <max-slippage>", "Maximum allowed slippage.", "0.9999")
.option("--no-precheck", "If provided, will skip expected output precheck.")
.option("--benchmark <iterations>", "If non-zero, benchmark the route finding for N iterations.", "0")
.option("-v, --verbose", "Verbose output.")
.parse(process.argv)

process.on('unhandledRejection', (reason: any, _promise: any) => {
Expand Down Expand Up @@ -59,7 +61,7 @@ function tokenByAddrOrSymbol(addressOrSymbol: string) {

async function main() {
const opts = program.opts()
const kit = await newKit(opts.network)
const kit = newKitFromWeb3(new Web3(opts.network) as any)
const chainId = await kit.web3.eth.getChainId()

const tokenWhitelist = ubeswapTokens.tokens.filter((v) => v.chainId === chainId).map((v) => v.address)
Expand All @@ -80,8 +82,10 @@ async function main() {
console.info(
`${registry.getName().padEnd(12)}` +
`${(pair as any).constructor?.name}:${pair.pairKey}: ` +
`${tokenByAddrOrSymbol(pair.tokenA).symbol} / ${tokenByAddrOrSymbol(pair.tokenB).symbol}` +
`\n snapshot: ${JSON.stringify(pair.snapshot())}`)
`${tokenByAddrOrSymbol(pair.tokenA).symbol} / ${tokenByAddrOrSymbol(pair.tokenB).symbol}`)
if (opts.verbose) {
console.info(` snapshot: ${JSON.stringify(pair.snapshot())}`)
}
}
}

Expand Down Expand Up @@ -122,6 +126,14 @@ async function main() {
console.info(
`Output: ${route.outputAmount.shiftedBy(-(outputToken.decimals || 18)).toFixed(10)} -- ` +
`${path.join(":")}:${outputToken.symbol}`)
if (opts.verbose) {
const routeData = route.pairs.map((p, idx) => p.swapData(route.path[idx]))
console.info('TX Data:\n' + JSON.stringify({
path: route.path,
pairs: routeData.map((d) => d.addr),
extras: routeData.map((d) => d.extra)
}, null, 2))
}
}

const from = opts.from
Expand All @@ -146,6 +158,7 @@ async function main() {
const receipt = await tx.sendAndWaitForReceipt({from: from})
console.info(`TX Done: ${receipt.transactionHash}`)
}
process.exit(0)
}

main()

0 comments on commit bb561a0

Please sign in to comment.