-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
115 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
packages/sdk/contractkit/src/wrappers/AbstractFeeCurrencyWrapper.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
})) | ||
}) | ||
) | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
packages/sdk/contractkit/src/wrappers/FeeCurrencyDirectoryWrapper.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
71 changes: 7 additions & 64 deletions
71
packages/sdk/contractkit/src/wrappers/FeeCurrencyWhitelistWrapper.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |