Skip to content

Commit

Permalink
Merge pull request #32 from diwu1989/chainIdFix
Browse files Browse the repository at this point in the history
Get rid of repeated web3.eth.getChainId() calls
  • Loading branch information
zviadm authored Mar 22, 2022
2 parents 815cf45 + efdaa89 commit 2ce093c
Show file tree
Hide file tree
Showing 18 changed files with 85 additions and 87 deletions.
12 changes: 7 additions & 5 deletions src/pair.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import BigNumber from "bignumber.js";
import BigNumber from "bignumber.js"

export type Address = string

Expand All @@ -17,21 +17,23 @@ export abstract class Pair {
public pairKey: string | null = null;
public tokenA: Address = "";
public tokenB: Address = "";
protected swappaPairAddress: Address = "";
private swappaPairAddress: Address = "";

constructor(swappaPairAddress: Address) {
this.swappaPairAddress = swappaPairAddress
}

public async init(): Promise<void> {
const r = await this._init()
this.pairKey = r.pairKey
this.tokenA = r.tokenA
this.tokenB = r.tokenB
this.swappaPairAddress = r.swappaPairAddress
return this.refresh()
}
protected abstract _init(): Promise<{
pairKey: string | null,
tokenA: Address,
tokenB: Address,
swappaPairAddress: Address,
}>;
public abstract refresh(): Promise<void>;
public swapData(inputToken: Address): SwapData {
Expand Down Expand Up @@ -94,7 +96,7 @@ export abstract class PairXYeqK extends Pair {
return {
fee: this.fee.toFixed(),
bucketA: this.bucketA.toFixed(),
bucketB: this.bucketB.toFixed()
bucketB: this.bucketB.toFixed(),
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/pairs/atoken-v2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@ export class PairATokenV2 extends Pair {
private pool: ILendingPoolV2

constructor(
chainId: number,
private web3: Web3,
private poolAddr: Address,
private reserve: Address,
) {
super()
super(selectAddress(chainId, {mainnet: pairATokenV2Address}))
this.pool = new this.web3.eth.Contract(ILendingPoolV2ABI, this.poolAddr) as unknown as ILendingPoolV2
}

Expand All @@ -28,7 +29,6 @@ export class PairATokenV2 extends Pair {
return {
pairKey: null,
tokenA, tokenB,
swappaPairAddress: await selectAddress(this.web3, {mainnet: pairATokenV2Address})
}
}
public async refresh(): Promise<void> {}
Expand Down
4 changes: 2 additions & 2 deletions src/pairs/atoken.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@ export class PairAToken extends Pair {
private provider: ILendingPoolAddressesProvider

constructor(
chainId: number,
private kit: ContractKit,
private providerAddr: Address,
private reserve: Address,
) {
super()
super(selectAddress(chainId, {mainnet: pairATokenAddress}))
this.provider = new kit.web3.eth.Contract(
LendingPoolAddressProviderABI, providerAddr) as unknown as ILendingPoolAddressesProvider
}
Expand All @@ -35,7 +36,6 @@ export class PairAToken extends Pair {
return {
pairKey: null,
tokenA, tokenB,
swappaPairAddress: await selectAddress(this.kit.web3 as unknown as Web3, {mainnet: pairATokenAddress})
}
}
public async refresh(): Promise<void> {}
Expand Down
13 changes: 5 additions & 8 deletions src/pairs/bpool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,27 +25,25 @@ export class PairBPool extends Pair {
private balanceB: BigNumber = ZERO

constructor(
private web3: Web3,
chainId: number,
web3: Web3,
private poolAddr: Address,
public tokenA: Address,
public tokenB: Address
public tokenB: Address,
) {
super()
super(selectAddress(chainId, {mainnet: pairBPoolAddress}))
this.bPool = new web3.eth.Contract(BPoolABI, poolAddr) as unknown as IbPool
}

protected async _init() {
const [
swapFee,
weightA,
weightB,
swappaPairAddress
weightB
] = await Promise.all([
this.bPool.methods.getSwapFee().call(),
this.bPool.methods.getDenormalizedWeight(this.tokenA).call(),
this.bPool.methods.getDenormalizedWeight(this.tokenB).call(),
// TODO: change this after merge to the actual deployed PairBPool swap address
selectAddress(this.web3, {mainnet: pairBPoolAddress})
])
this.swapFee = new BigNumber(swapFee).div(BONE)
this.weightA = new BigNumber(weightA)
Expand All @@ -55,7 +53,6 @@ export class PairBPool extends Pair {
pairKey: this.poolAddr,
tokenA: this.tokenA,
tokenB: this.tokenB,
swappaPairAddress
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/pairs/mento.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@ export class PairMento extends PairXYeqK {
private sortedOracles?: SortedOraclesWrapper

constructor(
chainId: number,
private kit: ContractKit,
private stableToken: StableToken,
) {
super()
super(selectAddress(chainId, {mainnet: pairMentoAddress}))
}

protected async _init() {
Expand All @@ -33,7 +34,6 @@ export class PairMento extends PairXYeqK {
pairKey: this.exchange.address,
tokenA: celo.address,
tokenB: cSTB.address,
swappaPairAddress: await selectAddress(this.kit.web3 as unknown as Web3, {mainnet: pairMentoAddress})
}
}

Expand Down Expand Up @@ -95,4 +95,4 @@ export class PairMento extends PairXYeqK {
protected swapExtraData() {
return this.exchange!.address
}
}
}
10 changes: 5 additions & 5 deletions src/pairs/opensumswap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,26 +21,26 @@ export class PairOpenSumSwap extends Pair {
private balances: BigNumber[] = []

constructor(
private web3: Web3,
chainId: number,
web3: Web3,
private swapPoolAddr: Address,
) {
super()
super(selectAddress(chainId, {mainnet: pairOpenSumSwapAddress}))
this.swapPool = new web3.eth.Contract(SwapABI, swapPoolAddr) as unknown as IOpenSumSwap
}

protected async _init() {
const [
tokenA,
tokenB,
swappaPairAddress,
] = await Promise.all([
this.swapPool.methods.getToken(0).call(),
this.swapPool.methods.getToken(1).call(),
selectAddress(this.web3, {mainnet: pairOpenSumSwapAddress}),
])
return {
pairKey: this.swapPoolAddr,
tokenA, tokenB, swappaPairAddress}
tokenA, tokenB,
}
}

public async refresh() {
Expand Down
4 changes: 2 additions & 2 deletions src/pairs/savingscelo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@ export class PairSavingsCELO extends Pair {
private totalSupplies?: {celoTotal: BigNumber, savingsTotal: BigNumber}

constructor(
chainId: number,
private kit: ContractKit,
savingsCELOAddr: Address,
) {
super()
super(selectAddress(chainId, {mainnet: pairSavingsCELOAddress}))
this.savingsKit = new SavingsKit(kit, savingsCELOAddr)
}

Expand All @@ -33,7 +34,6 @@ export class PairSavingsCELO extends Pair {
return {
pairKey: null,
tokenA, tokenB,
swappaPairAddress: await selectAddress(this.kit.web3 as unknown as Web3, {mainnet: pairSavingsCELOAddress})
}
}
public async refresh(): Promise<void> {
Expand Down
8 changes: 4 additions & 4 deletions src/pairs/stableswap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,22 +30,21 @@ export class PairStableSwap extends Pair {
static readonly A_PRECISION = 100

constructor(
chainId: number,
private web3: Web3,
private swapPoolAddr: Address,
) {
super()
super(selectAddress(chainId, {mainnet: pairStableSwapAddress}))
this.swapPool = new web3.eth.Contract(SwapABI, swapPoolAddr) as unknown as ISwap
}

protected async _init() {
const [
tokenA,
tokenB,
swappaPairAddress,
] = await Promise.all([
this.swapPool.methods.getToken(0).call(),
this.swapPool.methods.getToken(1).call(),
selectAddress(this.web3, {mainnet: pairStableSwapAddress}),
])
const erc20A = new this.web3.eth.Contract(Erc20ABI, tokenA) as unknown as Erc20
const erc20B = new this.web3.eth.Contract(Erc20ABI, tokenB) as unknown as Erc20
Expand All @@ -62,7 +61,8 @@ export class PairStableSwap extends Pair {
]
return {
pairKey: this.swapPoolAddr,
tokenA, tokenB, swappaPairAddress}
tokenA, tokenB,
}
}

public async refresh() {
Expand Down
9 changes: 4 additions & 5 deletions src/pairs/symmetricswap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,13 @@ export class PairSymmetricSwap extends Pair {
private balanceB: BigNumber = ZERO

constructor(
private web3: Web3,
chainId: number,
web3: Web3,
private swapPoolAddr: Address,
public tokenA: Address,
public tokenB: Address
public tokenB: Address,
) {
super()
super(selectAddress(chainId, {mainnet: pairSymmetricSwapAddress}))
// Unfortunately SymmetricSwap contract doesn't expose token addresses that it stores,
// thus they have to be hardcoded in the constructor and can't be fetched from swapPool
// directly.
Expand All @@ -41,12 +42,10 @@ export class PairSymmetricSwap extends Pair {
}

protected async _init() {
const swappaPairAddress = await selectAddress(this.web3, {mainnet: pairSymmetricSwapAddress})
return {
pairKey: this.swapPoolAddr,
tokenA: this.tokenA,
tokenB: this.tokenB,
swappaPairAddress
}
}

Expand Down
8 changes: 4 additions & 4 deletions src/pairs/uniswapv2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@ export class PairUniswapV2 extends PairXYeqK {
private feeKData: string

constructor(
chainId: number,
private web3: Web3,
private pairAddr: Address,
private fixedFee: BigNumber = new BigNumber(0.997),
) {
super()
super(selectAddress(chainId, {mainnet: pairUniswapV2Address}))
this.pair = new this.web3.eth.Contract(PairABI, pairAddr) as unknown as IUniswapV2Pair
const feeKInv = new BigNumber(1000).minus(this.fixedFee.multipliedBy(1000))
if (!feeKInv.isInteger() || !feeKInv.gt(0) || !feeKInv.lt(100)) {
Expand All @@ -27,14 +28,13 @@ export class PairUniswapV2 extends PairXYeqK {
}

protected async _init() {
const [tokenA, tokenB, swappaPairAddress] = await Promise.all([
const [tokenA, tokenB] = await Promise.all([
this.pair.methods.token0().call(),
this.pair.methods.token1().call(),
selectAddress(this.web3, {mainnet: pairUniswapV2Address}),
])
return {
pairKey: this.pairAddr,
tokenA, tokenB, swappaPairAddress,
tokenA, tokenB,
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/registries/aave-v2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@ export class RegistryAaveV2 extends Registry {
}

findPairs = async (tokenWhitelist: Address[]) => {
const chainId = await this.web3.eth.getChainId()
const poolAddr: string = await this.provider.methods.getLendingPool().call()
const lendingPool: ILendingPoolV2 = new this.web3.eth.Contract(ILendingPoolV2ABI, poolAddr) as unknown as ILendingPoolV2
const reserves: Address[] = await lendingPool.methods.getReservesList().call()
const reservesMatched = reserves.filter((r) => tokenWhitelist.indexOf(r) >= 0)
const pairs = reservesMatched.map((r) => (new PairATokenV2(this.web3, poolAddr, r)))
const pairs = reservesMatched.map((r) => (new PairATokenV2(chainId, this.web3, poolAddr, r)))
return initPairsAndFilterByWhitelist(pairs, tokenWhitelist)
}
}
3 changes: 2 additions & 1 deletion src/registries/aave.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export class RegistryAave extends Registry {
}

findPairs = async (tokenWhitelist: Address[]) => {
const chainId = await this.kit.web3.eth.getChainId()
const lendingPoolAddr = await this.lendingPoolAddrProvider.methods.getLendingPool().call()
const lendingPool = new this.kit.web3.eth.Contract(LendingPoolABI, lendingPoolAddr) as unknown as ILendingPool
const reserves = await lendingPool.methods.getReserves().call()
Expand All @@ -25,7 +26,7 @@ export class RegistryAave extends Registry {
...reserves.filter((r) => tokenWhitelist.indexOf(r) >= 0),
]
const pairs = reservesMatched.map((r) => (
new PairAToken(this.kit, this.lendingPoolAddrProvider.options.address, r)))
new PairAToken(chainId, this.kit, this.lendingPoolAddrProvider.options.address, r)))
return initPairsAndFilterByWhitelist(pairs, tokenWhitelist)
}
}
3 changes: 2 additions & 1 deletion src/registries/balancer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export class RegistryBalancer extends Registry {
}

findPairs = async (tokenWhitelist: Address[]): Promise<Pair[]> => {
const chainId = await this.web3.eth.getChainId()
const pairsToFetch: {tokenA: Address, tokenB: Address}[] = []
for (let i = 0; i < tokenWhitelist.length - 1; i += 1) {
for (let j = i + 1; j < tokenWhitelist.length; j += 1) {
Expand All @@ -37,7 +38,7 @@ export class RegistryBalancer extends Registry {
}

for (const poolAddr of pools) {
const pool = new PairBPool(this.web3, poolAddr, toFetch.tokenA, toFetch.tokenB)
const pool = new PairBPool(chainId, this.web3, poolAddr, toFetch.tokenA, toFetch.tokenB)
// bpool can be used for each input & output combination
let key
if (toFetch.tokenA.toLowerCase().localeCompare(toFetch.tokenB.toLowerCase()) > 0) {
Expand Down
3 changes: 2 additions & 1 deletion src/registries/mento.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ export class RegistryMento extends Registry{
wrapper: wrapper,
}))
})
const pairs = cSTBs.map((cSTB) => (new PairMento(this.kit, cSTB.name)))
const chainId = await this.kit.web3.eth.getChainId()
const pairs = cSTBs.map((cSTB) => (new PairMento(chainId, this.kit, cSTB.name)))
return initPairsAndFilterByWhitelist(pairs, tokenWhitelist)
}
}
4 changes: 2 additions & 2 deletions src/registries/static.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ import { Registry } from "../registry"
import { initPairsAndFilterByWhitelist } from "../utils"

export class RegistryStatic extends Registry{
constructor(name: string, private pairsAll: Pair[]) {
constructor(name: string, private pairsAll: Promise<Pair[]>) {
super(name)
}

findPairs = async (tokenWhitelist: Address[]) => {
return initPairsAndFilterByWhitelist(this.pairsAll, tokenWhitelist)
return initPairsAndFilterByWhitelist(await this.pairsAll, tokenWhitelist)
}
}
Loading

0 comments on commit 2ce093c

Please sign in to comment.