Skip to content

Commit

Permalink
registry: update commands; add protocols (#120)
Browse files Browse the repository at this point in the history
* update commands; add protocols

* correct command names

* libraries upgrade

* update to latest apis

* readme update
  • Loading branch information
dmosites authored Nov 13, 2023
1 parent c7949e4 commit 0050e3a
Show file tree
Hide file tree
Showing 21 changed files with 725 additions and 357 deletions.
195 changes: 135 additions & 60 deletions README.md

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "airswap",
"description": "Command Line Interface (CLI) for the AirSwap Network",
"version": "4.1.3",
"version": "4.1.4",
"license": "MIT",
"repository": {
"type": "git",
Expand All @@ -22,7 +22,7 @@
"pretty:fix": "prettier --write \"./**/*.ts\""
},
"dependencies": {
"@airswap/libraries": "4.1.9",
"@airswap/libraries": "4.1.10",
"@airswap/metadata": "4.1.8",
"@airswap/tokens": "0.1.4",
"@oclif/command": "^1.8.22",
Expand Down
14 changes: 7 additions & 7 deletions src/commands/token/approve.ts → src/commands/approve.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
import chalk from 'chalk'
import { Command } from '@oclif/command'
import { ethers } from 'ethers'
import * as utils from '../../lib/utils'
import { getWallet } from '../../lib/wallet'
import { getTokens, confirm, cancelled } from '../../lib/prompt'
import constants from '../../lib/constants.json'
import * as utils from '../lib/utils'
import { getWallet } from '../lib/wallet'
import { getTokens, confirm, cancelled } from '../lib/prompt'
import constants from '../lib/constants.json'

const IERC20 = require('@airswap/tokens/build/contracts/IERC20.json')
const swapDeploys = require('@airswap/swap-erc20/deploys.js')

export default class TokenApprove extends Command {
export default class Approve extends Command {
public static description = 'approve a token for trading'
public async run() {
try {
const wallet = await getWallet(this, true)
const chainId = (await wallet.provider.getNetwork()).chainId
const metadata = await utils.getMetadata(this, chainId)
const gasPrice = await utils.getGasPrice(this)
utils.displayDescription(this, TokenApprove.description, chainId)
utils.displayDescription(this, Approve.description, chainId)

const swapAddress = swapDeploys[chainId]
if (!swapAddress) {
Expand Down Expand Up @@ -57,7 +57,7 @@ export default class TokenApprove extends Command {
)
) {
tokenContract
.approve(swapAddress, constants.MAX_APPROVAL_AMOUNT, { gasPrice })
.approve(swapAddress, constants.APPROVAL_AMOUNT, { gasPrice })
.then(utils.handleTransaction)
.catch(utils.handleError)
}
Expand Down
123 changes: 123 additions & 0 deletions src/commands/protocols/add.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
import chalk from 'chalk'
import { ethers } from 'ethers'
import { Command } from '@oclif/command'
import { getTable } from 'console.table'
import * as utils from '../../lib/utils'
import { getWallet } from '../../lib/wallet'
import { confirm, cancelled, getProtocolList } from '../../lib/prompt'

import {
Protocols,
protocolNames,
stakingTokenAddresses,
} from '@airswap/constants'
import { Registry } from '@airswap/libraries'

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

export default class ProtocolsAdd extends Command {
public static description = 'add supported protocols to the registry'
public async run() {
try {
const wallet = await getWallet(this, true)
const chainId = (await wallet.provider.getNetwork()).chainId
const metadata = await utils.getMetadata(this, chainId)
const gasPrice = await utils.getGasPrice(this)
utils.displayDescription(this, ProtocolsAdd.description, chainId)

this.log(chalk.white(`Registry ${Registry.getAddress(chainId)}\n`))

const registryContract = Registry.getContract(wallet, chainId)
const url = (
await registryContract.getServerURLsForStakers([wallet.address])
)[0]
if (!url) {
this.log(chalk.yellow('\nServer URL is not set'))
this.log(`Set your server URL with ${chalk.bold('registry:url')}\n`)
} else {
this.log(chalk.white(`Server URL ${chalk.bold(url)}\n`))
}

const alreadySupported = await registryContract.getProtocolsForStaker(
wallet.address
)
if (alreadySupported.length) {
this.log(`Your activated protocols:\n`)
const result = []
alreadySupported.map((interfaceId) => {
result.push({
ID: interfaceId,
Name: protocolNames[interfaceId],
})
})
this.log(getTable(result))
}

const stakingTokenContract = new ethers.Contract(
stakingTokenAddresses[chainId],
IERC20.abi,
wallet
)
const allowance = await stakingTokenContract.allowance(
wallet.address,
Registry.getAddress(chainId)
)

if (allowance.eq(0)) {
this.log(chalk.yellow('Registry not enabled'))
this.log(
`Enable staking on the Registry with ${chalk.bold(
'registry:approve'
)}\n`
)
} else {
this.log(`All available protocols:\n`)

const result = []
for (const protocol in Protocols) {
result.push({
ID: Protocols[protocol],
Name: protocol,
})
}
this.log(getTable(result))

const protocols: any = await getProtocolList(
protocolNames,
'protocols to activate (comma separated)'
)
const protocolIds = []
const protocolLabels = []

for (const interfaceId in protocols) {
protocolIds.push(interfaceId)
protocolLabels.push(`${interfaceId} (${protocols[interfaceId]})`)
}

const supportCost = (await registryContract.supportCost()).toNumber()
const totalCost = supportCost * protocolIds.length

this.log()
if (
await confirm(
this,
metadata,
'addProtocols',
{
protocols: protocolLabels.join('\n'),
stake: `${totalCost / 10000} AST`,
},
chainId
)
) {
registryContract
.addProtocols(protocolIds, { gasPrice })
.then(utils.handleTransaction)
.catch(utils.handleError)
}
}
} catch (e) {
cancelled(e)
}
}
}
45 changes: 45 additions & 0 deletions src/commands/protocols/list.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import chalk from 'chalk'
import { Command } from '@oclif/command'
import * as utils from '../../lib/utils'
import { getWallet } from '../../lib/wallet'
import { cancelled } from '../../lib/prompt'
import { getTable } from 'console.table'
import { Registry } from '@airswap/libraries'

import { protocolNames } from '@airswap/constants'

export default class ProtocolsList extends Command {
public static description = 'list supported protocols from registry'
public async run() {
try {
const wallet = await getWallet(this, true)
const chainId = (await wallet.provider.getNetwork()).chainId
utils.displayDescription(this, ProtocolsList.description, chainId)

this.log(chalk.white(`Registry ${Registry.getAddress(chainId)}\n`))

const registryContract = Registry.getContract(wallet, chainId)
const protocols = await registryContract.getProtocolsForStaker(
wallet.address
)

const result = []
protocols.map((interfaceId) => {
result.push({
ID: interfaceId,
name: protocolNames[interfaceId],
})
})
if (result.length) {
this.log(getTable(result))
} else {
this.log(chalk.yellow('No supported protocols'))
this.log(
`Add protocols you support with ${chalk.bold('protocols:add')}\n`
)
}
} catch (e) {
cancelled(e)
}
}
}
86 changes: 86 additions & 0 deletions src/commands/protocols/remove.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import chalk from 'chalk'
import { Command } from '@oclif/command'
import { getTable } from 'console.table'
import * as utils from '../../lib/utils'
import { getWallet } from '../../lib/wallet'
import { confirm, cancelled, getProtocolList } from '../../lib/prompt'

import { protocolNames } from '@airswap/constants'
import { Registry } from '@airswap/libraries'

export default class ProtocolsRemove extends Command {
public static description = 'add supported protocols to the registry'
public async run() {
try {
const wallet = await getWallet(this, true)
const chainId = (await wallet.provider.getNetwork()).chainId
const metadata = await utils.getMetadata(this, chainId)
const gasPrice = await utils.getGasPrice(this)
utils.displayDescription(this, ProtocolsRemove.description, chainId)

this.log(chalk.white(`Registry ${Registry.getAddress(chainId)}\n`))

const registryContract = Registry.getContract(wallet, chainId)
const url = (
await registryContract.getServerURLsForStakers([wallet.address])
)[0]
if (!url) {
this.log(chalk.yellow('\nServer URL is not set'))
this.log(`Set your server URL with ${chalk.bold('registry:url')}\n`)
} else {
this.log(chalk.white(`Server URL ${chalk.bold(url)}\n`))
}

const alreadySupported = await registryContract.getProtocolsForStaker(
wallet.address
)
if (alreadySupported.length) {
this.log(`Your activated protocols:\n`)
const result = []
alreadySupported.map((interfaceId) => {
result.push({
ID: interfaceId,
Name: protocolNames[interfaceId],
})
})
this.log(getTable(result))
}

const protocols: any = await getProtocolList(
protocolNames,
'protocols to deactivate (comma separated)'
)
const protocolIds = []
const protocolLabels = []

for (const interfaceId in protocols) {
protocolIds.push(interfaceId)
protocolLabels.push(`${interfaceId} (${protocols[interfaceId]})`)
}

const supportCost = (await registryContract.supportCost()).toNumber()
const totalCost = supportCost * protocolIds.length

this.log()
if (
await confirm(
this,
metadata,
'removeProtocols',
{
protocols: protocolLabels.join('\n'),
stake: `${totalCost / 10000} AST`,
},
chainId
)
) {
registryContract
.removeProtocols(protocolIds, { gasPrice })
.then(utils.handleTransaction)
.catch(utils.handleError)
}
} catch (e) {
cancelled(e)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,35 +7,34 @@ import { confirm, cancelled } from '../../lib/prompt'
import constants from '../../lib/constants.json'

import { stakingTokenAddresses } from '@airswap/constants'
import { Registry } from '@airswap/libraries'

const IERC20 = require('@airswap/tokens/build/contracts/IERC20.json')
const registryDeploys = require('@airswap/maker-registry/deploys.js')

export default class RegistryEnable extends Command {
export default class RegistryApprove extends Command {
public static description = 'enable staking on the registry'
public async run() {
try {
const wallet = await getWallet(this, true)
const chainId = (await wallet.provider.getNetwork()).chainId
const metadata = await utils.getMetadata(this, chainId)
const gasPrice = await utils.getGasPrice(this)
utils.displayDescription(this, RegistryEnable.description, chainId)
utils.displayDescription(this, RegistryApprove.description, chainId)

const registryAddress = registryDeploys[chainId]
const stakingTokenContract = new ethers.Contract(
stakingTokenAddresses[chainId],
IERC20.abi,
wallet
)
const allowance = await stakingTokenContract.allowance(
wallet.address,
registryAddress
Registry.getAddress(chainId)
)

if (!allowance.eq(0)) {
this.log(chalk.yellow('Registry already enabled'))
this.log(
`Add tokens to the Registry with ${chalk.bold('registry:add')}\n`
`Add tokens to the Registry with ${chalk.bold('tokens:add')}\n`
)
} else {
if (
Expand All @@ -45,13 +44,13 @@ export default class RegistryEnable extends Command {
'approve',
{
token: `${stakingTokenAddresses[chainId]} (AST)`,
spender: `${registryAddress} (Registry)`,
spender: `${Registry.getAddress(chainId)} (Registry)`,
},
chainId
)
) {
stakingTokenContract
.approve(registryAddress, constants.MAX_APPROVAL_AMOUNT, {
.approve(Registry.getAddress(chainId), constants.APPROVAL_AMOUNT, {
gasPrice,
})
.then(utils.handleTransaction)
Expand Down
Loading

0 comments on commit 0050e3a

Please sign in to comment.