Skip to content

Commit

Permalink
FeeCurrencyDirectory support
Browse files Browse the repository at this point in the history
  • Loading branch information
shazarre committed May 9, 2024
1 parent 34c654e commit a8fe795
Show file tree
Hide file tree
Showing 8 changed files with 115 additions and 68 deletions.
10 changes: 7 additions & 3 deletions packages/cli/src/commands/network/whitelist.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { isCel2 } from '@celo/connect'
import { BaseCommand } from '../../base'

export default class Whitelist extends BaseCommand {
Expand All @@ -14,9 +15,12 @@ export default class Whitelist extends BaseCommand {
async run() {
const kit = await this.getKit()

const feeCurrencyWhitelist = await kit.contracts.getFeeCurrencyWhitelist()
const validFeeCurrencies = await feeCurrencyWhitelist.getWhitelist()
const pairs = (await feeCurrencyWhitelist.getFeeCurrencyInformation(validFeeCurrencies)).map(
const feeCurrencyContract = await ((await isCel2(kit.web3))
? kit.contracts.getFeeCurrencyDirectory()
: kit.contracts.getFeeCurrencyWhitelist())
const validFeeCurrencies = await feeCurrencyContract.getAddresses()

const pairs = (await feeCurrencyContract.getFeeCurrencyInformation(validFeeCurrencies)).map(
({ name, symbol, address, adaptedToken }) =>
`${address} - ${name || 'unknown name'} (${symbol || 'N/A'})${
adaptedToken ? ` (adapted token: ${adaptedToken})` : ''
Expand Down
2 changes: 1 addition & 1 deletion packages/sdk/contractkit/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"lint": "yarn run --top-level eslint -c .eslintrc.js "
},
"dependencies": {
"@celo/abis": "11.0.0",
"@celo/abis": "11.0.0-fee-currency-directory.0",
"@celo/base": "^6.0.1",
"@celo/connect": "^5.3.0",
"@celo/utils": "^6.0.1",
Expand Down
1 change: 1 addition & 0 deletions packages/sdk/contractkit/src/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export enum CeloContract {
ERC20 = 'ERC20',
Escrow = 'Escrow',
FederatedAttestations = 'FederatedAttestations',
FeeCurrencyDirectory = 'FeeCurrencyDirectory',
FeeCurrencyWhitelist = 'FeeCurrencyWhitelist',
FeeHandler = 'FeeHandler',
Freezer = 'Freezer',
Expand Down
6 changes: 6 additions & 0 deletions packages/sdk/contractkit/src/contract-cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { EpochRewardsWrapper } from './wrappers/EpochRewards'
import { Erc20Wrapper } from './wrappers/Erc20Wrapper'
import { EscrowWrapper } from './wrappers/Escrow'
import { FederatedAttestationsWrapper } from './wrappers/FederatedAttestations'
import { FeeCurrencyDirectoryWrapper } from './wrappers/FeeCurrencyDirectoryWrapper'
import { FeeCurrencyWhitelistWrapper } from './wrappers/FeeCurrencyWhitelistWrapper'
import { FreezerWrapper } from './wrappers/Freezer'
import { GasPriceMinimumWrapper } from './wrappers/GasPriceMinimum'
Expand All @@ -35,6 +36,7 @@ const WrapperFactories = {
[CeloContract.ERC20]: Erc20Wrapper,
[CeloContract.Escrow]: EscrowWrapper,
[CeloContract.FederatedAttestations]: FederatedAttestationsWrapper,
[CeloContract.FeeCurrencyDirectory]: FeeCurrencyDirectoryWrapper,
[CeloContract.FeeCurrencyWhitelist]: FeeCurrencyWhitelistWrapper,
[CeloContract.Freezer]: FreezerWrapper,
[CeloContract.GasPriceMinimum]: GasPriceMinimumWrapper,
Expand Down Expand Up @@ -84,6 +86,7 @@ interface WrapperCacheMap {
[CeloContract.ERC20]?: Erc20Wrapper<IERC20>
[CeloContract.Escrow]?: EscrowWrapper
[CeloContract.FederatedAttestations]?: FederatedAttestationsWrapper
[CeloContract.FeeCurrencyDirectory]?: FeeCurrencyDirectoryWrapper
[CeloContract.FeeCurrencyWhitelist]?: FeeCurrencyWhitelistWrapper
[CeloContract.Freezer]?: FreezerWrapper
[CeloContract.GasPriceMinimum]?: GasPriceMinimumWrapper
Expand Down Expand Up @@ -155,6 +158,9 @@ export class WrapperCache implements ContractCacheType {
getFederatedAttestations() {
return this.getContract(CeloContract.FederatedAttestations)
}
getFeeCurrencyDirectory() {
return this.getContract(CeloContract.FeeCurrencyDirectory)
}
getFeeCurrencyWhitelist() {
return this.getContract(CeloContract.FeeCurrencyWhitelist)
}
Expand Down
2 changes: 2 additions & 0 deletions packages/sdk/contractkit/src/web3-contract-cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import { newValidators } from '@celo/abis/web3/Validators'
import { newReserve } from '@celo/abis/web3/mento/Reserve'
import { newStableToken } from '@celo/abis/web3/mento/StableToken'

import { newFeeCurrencyDirectory } from '@celo/abis/web3/FeeCurrencyDirectory'
import { newMentoFeeHandlerSeller } from '@celo/abis/web3/MentoFeeHandlerSeller'
import { newUniswapFeeHandlerSeller } from '@celo/abis/web3/UniswapFeeHandlerSeller'

Expand All @@ -46,6 +47,7 @@ export const ContractFactories = {
[CeloContract.ERC20]: newIERC20,
[CeloContract.Escrow]: newEscrow,
[CeloContract.FederatedAttestations]: newFederatedAttestations,
[CeloContract.FeeCurrencyDirectory]: newFeeCurrencyDirectory,
[CeloContract.FeeCurrencyWhitelist]: newFeeCurrencyWhitelist,
[CeloContract.Freezer]: newFreezer,
[CeloContract.FeeHandler]: newFeeHandler,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import { StrongAddress } from '@celo/base'
import { Contract } from '@celo/connect'
import { BaseWrapper } from './BaseWrapper'

const MINIMAL_TOKEN_INFO_ABI = [
{
type: 'function' as const,
stateMutability: 'view',
outputs: [{ type: 'string', name: '', internalType: 'string' }],
name: 'symbol',
inputs: [],
},
{
type: 'function' as const,
stateMutability: 'view',
outputs: [{ type: 'string', name: '', internalType: 'string' }],
name: 'name',
inputs: [],
},
{
type: 'function' as const,
stateMutability: 'view',
inputs: [],
outputs: [{ type: 'address', name: '', internalType: 'address' }],
name: 'adaptedToken',
},
] as const

export abstract class AbstractFeeCurrencyWrapper<
TContract extends Contract
> extends BaseWrapper<TContract> {
abstract getAddresses(): Promise<StrongAddress[]>

async getFeeCurrencyInformation(whitelist?: StrongAddress[]) {
const feeCurrencies = whitelist ?? (await this.getAddresses())

return Promise.all(
feeCurrencies.map(async (address) => {
// @ts-expect-error abi typing is not 100% correct but works
let contract = new this.connection.web3.eth.Contract(MINIMAL_TOKEN_INFO_ABI, address)

const adaptedToken = (await contract.methods
.adaptedToken()
.call()
.catch(() => undefined)) as StrongAddress | undefined

if (adaptedToken) {
// @ts-expect-error abi typing is not 100% correct but works
contract = new this.connection.web3.eth.Contract(MINIMAL_TOKEN_INFO_ABI, adaptedToken)
}

return Promise.all([
contract.methods
.name()
.call()
.catch(() => undefined) as Promise<string | undefined>,
contract.methods
.symbol()
.call()
.catch(() => undefined) as Promise<string | undefined>,
]).then(([name, symbol]) => ({
name,
symbol,
address,
adaptedToken,
}))
})
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { FeeCurrencyDirectory } from '@celo/abis/web3/FeeCurrencyDirectory'
import { StrongAddress } from '@celo/base'
import { AbstractFeeCurrencyWrapper } from './AbstractFeeCurrencyWrapper'
import { proxyCall } from './BaseWrapper'

/**
* FeeCurrencyDirectory contract listing available currencies usable to pay fees
*/
export class FeeCurrencyDirectoryWrapper extends AbstractFeeCurrencyWrapper<FeeCurrencyDirectory> {
getCurrencies = proxyCall(
this.contract.methods.getCurrencies,
undefined,
(addresses) => [...new Set(addresses)].sort() as StrongAddress[]
)

getAddresses(): Promise<StrongAddress[]> {
return this.getCurrencies()
}

// TODO add other methods as well
}
Original file line number Diff line number Diff line change
@@ -1,81 +1,24 @@
import { FeeCurrencyWhitelist } from '@celo/abis/web3/FeeCurrencyWhitelist'
import { StrongAddress } from '@celo/base'
import 'bignumber.js'
import { BaseWrapper, proxyCall } from './BaseWrapper'

const MINIMAL_TOKEN_INFO_ABI = [
{
type: 'function' as const,
stateMutability: 'view',
outputs: [{ type: 'string', name: '', internalType: 'string' }],
name: 'symbol',
inputs: [],
},
{
type: 'function' as const,
stateMutability: 'view',
outputs: [{ type: 'string', name: '', internalType: 'string' }],
name: 'name',
inputs: [],
},
{
type: 'function' as const,
stateMutability: 'view',
inputs: [],
outputs: [{ type: 'address', name: '', internalType: 'address' }],
name: 'adaptedToken',
},
] as const
import { AbstractFeeCurrencyWrapper } from './AbstractFeeCurrencyWrapper'
import { proxyCall } from './BaseWrapper'

/**
* FeeCurrencyWhitelist contract listing available currencies usable to pay fees
*/
export class FeeCurrencyWhitelistWrapper extends BaseWrapper<FeeCurrencyWhitelist> {
export class FeeCurrencyWhitelistWrapper extends AbstractFeeCurrencyWrapper<FeeCurrencyWhitelist> {
getWhitelist = proxyCall(
this.contract.methods.getWhitelist,
undefined,
(addresses) => [...new Set(addresses)].sort() as StrongAddress[]
)

async getFeeCurrencyInformation(whitelist?: StrongAddress[]) {
const feeCurrencies = whitelist ?? (await this.getWhitelist())

return Promise.all(
feeCurrencies.map(async (address) => {
// @ts-expect-error abi typing is not 100% correct but works
let contract = new this.connection.web3.eth.Contract(MINIMAL_TOKEN_INFO_ABI, address)

const adaptedToken = (await contract.methods
.adaptedToken()
.call()
.catch(() => undefined)) as StrongAddress | undefined

if (adaptedToken) {
// @ts-expect-error abi typing is not 100% correct but works
contract = new this.connection.web3.eth.Contract(MINIMAL_TOKEN_INFO_ABI, adaptedToken)
}

return Promise.all([
contract.methods
.name()
.call()
.catch(() => undefined) as Promise<string | undefined>,
contract.methods
.symbol()
.call()
.catch(() => undefined) as Promise<string | undefined>,
]).then(([name, symbol]) => ({
name,
symbol,
address,
adaptedToken,
}))
})
)
}

removeToken = proxyCall(this.contract.methods.removeToken)
addToken = proxyCall(this.contract.methods.addToken)

getAddresses(): Promise<StrongAddress[]> {
return this.getWhitelist()
}
}

export type GoldTokenWrapperType = FeeCurrencyWhitelistWrapper

0 comments on commit a8fe795

Please sign in to comment.