Skip to content

Commit

Permalink
Use @airswap/utils for numbers and use INFURA
Browse files Browse the repository at this point in the history
  • Loading branch information
Don Mosites committed Feb 1, 2021
1 parent 05dd143 commit aa550c9
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 18 deletions.
3 changes: 2 additions & 1 deletion src/commands/balances.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { getTable } from 'console.table'
import { balanceCheckerAddresses } from '@airswap/constants'
import deltaBalancesABI from '../lib/deltaBalances.json'
import { cancelled } from '../lib/prompt'
import { toDecimalString } from '@airswap/utils'

const IERC20 = require('@airswap/tokens/build/contracts/IERC20.json')
const swapDeploys = require('@airswap/swap/deploys.json')
Expand All @@ -30,7 +31,7 @@ export default class Balances extends Command {
for (let i = 0; i < addresses.length; i++) {
const token = metadata.byAddress[addresses[i]]
if (!balances[i].eq(0)) {
const balanceDecimal = utils.getDecimalValue(balances[i], token.address, metadata)
const balanceDecimal = toDecimalString(balances[i], metadata.byAddress[token.address].decimals)
try {
const tokenContract = new ethers.Contract(token.address, IERC20.abi, wallet)
const allowance = await tokenContract.allowance(wallet.address, swapAddress)
Expand Down
5 changes: 3 additions & 2 deletions src/commands/indexer/get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { getSideAndTokens, cancelled } from '../../lib/prompt'
import constants from '../../lib/constants.json'
import { getTable } from 'console.table'
import { protocolNames, stakingTokenAddresses, ADDRESS_ZERO, INDEX_HEAD } from '@airswap/constants'
import { toDecimalString } from '@airswap/utils'

const Indexer = require('@airswap/indexer/build/contracts/Indexer.json')
const indexerDeploys = require('@airswap/indexer/deploys.json')
Expand Down Expand Up @@ -59,12 +60,12 @@ export default class IntentGet extends Command {
for (let i = 0; i < result.locators.length; i++) {
try {
rows.push({
Staked: utils.getDecimalValue(result.scores[i], stakingTokenAddresses[chainId], metadata),
Staked: toDecimalString(result.scores[i], metadata.byAddress[stakingTokenAddresses[chainId]].decimals),
Locator: ethers.utils.parseBytes32String(result.locators[i]),
})
} catch (e) {
rows.push({
Staked: utils.getDecimalValue(result.scores[i], stakingTokenAddresses[chainId], metadata),
Staked: toDecimalString(result.scores[i], metadata.byAddress[stakingTokenAddresses[chainId]].decimals),
Locator: `(Could not parse (${result.locators[i]}))`,
})
}
Expand Down
6 changes: 3 additions & 3 deletions src/commands/order/best.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import * as utils from '../../lib/utils'
import { printOrder, confirm, cancelled } from '../../lib/prompt'
import * as requests from '../../lib/requests'
import { Validator } from '@airswap/protocols'

import { toDecimalString } from '@airswap/utils'
const Swap = require('@airswap/swap/build/contracts/Swap.json')
const swapDeploys = require('@airswap/swap/deploys.json')

Expand Down Expand Up @@ -48,12 +48,12 @@ export default class OrderBest extends Command {
signerWallet: order.signer.wallet,
signerToken: order.signer.token,
signerAmount: `${order.signer.amount} (${chalk.cyan(
utils.getDecimalValue(order.signer.amount, request.signerToken.address, metadata).toFixed(),
toDecimalString(order.signer.amount, metadata.byAddress[request.signerToken.address].decimals),
)})`,
senderWallet: `${order.sender.wallet} (${chalk.cyan('You')})`,
senderToken: order.sender.token,
senderAmount: `${order.sender.amount} (${chalk.cyan(
utils.getDecimalValue(order.sender.amount, request.senderToken.address, metadata).toFixed(),
toDecimalString(order.sender.amount, metadata.byAddress[request.senderToken.address].decimals),
)})`,
},
chainId,
Expand Down
9 changes: 5 additions & 4 deletions src/commands/quote/max.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import * as utils from '../../lib/utils'
import { get, getTokens, cancelled } from '../../lib/prompt'
import * as requests from '../../lib/requests'
import chalk from 'chalk'
import { toDecimalString } from '@airswap/utils'
export default class QuoteMax extends Command {
static description = 'get a max quote from a peer'
async run() {
Expand Down Expand Up @@ -51,11 +52,11 @@ export default class QuoteMax extends Command {
let maxAmount
let maxFor
if (side === 'buy') {
maxAmount = utils.getDecimalValue(order.signer.amount, order.signer.token, metadata).toFixed()
maxFor = utils.getDecimalValue(order.sender.amount, order.sender.token, metadata).toFixed()
maxAmount = toDecimalString(order.signer.amount, metadata.byAddress[order.signer.token].decimals)
maxFor = toDecimalString(order.sender.amount, metadata.byAddress[order.sender.token].decimals)
} else {
maxAmount = utils.getDecimalValue(order.sender.amount, order.sender.token, metadata).toFixed()
maxFor = utils.getDecimalValue(order.signer.amount, order.signer.token, metadata).toFixed()
maxAmount = toDecimalString(order.sender.amount, metadata.byAddress[order.sender.token].decimals)
maxFor = toDecimalString(order.signer.amount, metadata.byAddress[order.signer.token].decimals)
}
this.log(chalk.underline.bold(`Response: ${locator}`))
let verb = 'Buying'
Expand Down
5 changes: 3 additions & 2 deletions src/commands/weth/deposit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { ethers } from 'ethers'
import { Command } from '@oclif/command'
import * as utils from '../../lib/utils'
import { get, confirm, cancelled } from '../../lib/prompt'
import { toDecimalString } from '@airswap/utils'

const WETH9 = require('@airswap/tokens/build/contracts/WETH9.json')

Expand All @@ -18,8 +19,8 @@ export default class IntentUnset extends Command {

const WETH = metadata.bySymbol['WETH']
const balance = await wallet.provider.getBalance(wallet.address)
const balanceDecimal = utils.getDecimalValue(balance.toString(), WETH.address, metadata)
this.log(`ETH available to deposit: ${chalk.bold(balanceDecimal.toFixed())}`)
const balanceDecimal = toDecimalString(balance.toString(), metadata.byAddress[WETH.address].decimals)
this.log(`ETH available to deposit: ${chalk.bold(balanceDecimal)}`)
this.log(chalk.gray('Some ETH must be saved to execute the transaction.\n'))

const { amount }: any = await get({
Expand Down
5 changes: 3 additions & 2 deletions src/commands/weth/withdraw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { ethers } from 'ethers'
import { Command } from '@oclif/command'
import * as utils from '../../lib/utils'
import { get, confirm, cancelled } from '../../lib/prompt'
import { toDecimalString } from '@airswap/utils'

const WETH9 = require('@airswap/tokens/build/contracts/WETH9.json')

Expand All @@ -19,8 +20,8 @@ export default class IntentUnset extends Command {
const WETH = metadata.bySymbol['WETH']
const tokenContract = new ethers.Contract(WETH.address, WETH9.abi, wallet)
const tokenBalance = await tokenContract.balanceOf(wallet.address)
const balanceDecimal = utils.getDecimalValue(tokenBalance.toString(), WETH.address, metadata)
this.log(`WETH available to withdraw: ${chalk.bold(balanceDecimal.toFixed())}\n`)
const balanceDecimal = toDecimalString(tokenBalance.toString(), metadata.byAddress[WETH.address].decimals)
this.log(`WETH available to withdraw: ${chalk.bold(balanceDecimal)}\n`)

const { amount }: any = await get({
amount: {
Expand Down
1 change: 1 addition & 0 deletions src/lib/constants.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"DEFAULT_GAS_PRICE": "5",
"DEFAULT_PORT": 3000,
"ETH_GAS_STATION_URL": "https://ethgasstation.info/json/ethgasAPI.json",
"INFURA_ID": "bfed78c822b04995b30ea4ef5f6e18c7",
"MAX_LOCATORS": 10,
"REQUEST_TIMEOUT": 4000
}
9 changes: 5 additions & 4 deletions src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import axios from 'axios'
import BigNumber from 'bignumber.js'

import { chainNames, etherscanDomains, protocols, chainIds } from '@airswap/constants'
import { ETH_GAS_STATION_URL, DEFAULT_CONFIRMATIONS, DEFAULT_GAS_PRICE } from './constants.json'
import { ETH_GAS_STATION_URL, DEFAULT_CONFIRMATIONS, DEFAULT_GAS_PRICE, INFURA_ID } from './constants.json'

import TokenMetadata from '@airswap/metadata'

Expand Down Expand Up @@ -52,7 +52,8 @@ export async function getChainId(ctx: any): Promise<string> {
export async function getProvider(ctx: any) {
const chainId = await getChainId(ctx)
const selectedChain = chainNames[chainId].toLowerCase()
return ethers.getDefaultProvider(selectedChain)

return new ethers.providers.JsonRpcProvider(`https://${selectedChain}.infura.io/v3/${INFURA_ID}`, selectedChain)
}

export async function getWallet(ctx: any, requireBalance?: boolean) {
Expand All @@ -64,7 +65,7 @@ export async function getWallet(ctx: any, requireBalance?: boolean) {
const chainId = await getChainId(ctx)
const selectedChain = chainNames[chainId].toLowerCase()
const signerPrivateKey = Buffer.from(account, 'hex')
const provider = ethers.getDefaultProvider(selectedChain)
const provider = await getProvider(ctx)
const wallet = new ethers.Wallet(signerPrivateKey, provider)

const balance = await provider.getBalance(wallet.address)
Expand Down Expand Up @@ -97,7 +98,7 @@ export async function getMetadata(ctx: any, chainId: string) {
export async function updateMetadata(ctx: any, chainId: string) {
const startTime = Date.now()

const provider = ethers.getDefaultProvider(chainNames[chainId].toLowerCase())
const provider = await getProvider(ctx)
const tokenMetadata = new TokenMetadata(provider)
const tokens = await tokenMetadata.fetchKnownTokens()
const metadataPath = path.join(ctx.config.configDir, `metadata-${chainNames[chainId]}.json`)
Expand Down

0 comments on commit aa550c9

Please sign in to comment.