Skip to content

Commit

Permalink
progress
Browse files Browse the repository at this point in the history
  • Loading branch information
ashwinarora committed Jul 13, 2024
1 parent e67f700 commit 0ec8c0c
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 217 deletions.
4 changes: 2 additions & 2 deletions src/components/Provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ const ThePoolzProvider = ({ children, overrides }: { children: React.ReactNode,
useEffect(() => {
const fetchAbis = async () => {
if(!requiredContracts.length) return
let newContractAbis: AbiRecord = {} as AbiRecord
let newContractAbis = contractAbis ? {...contractAbis} : {} as AbiRecord
await Promise.all(requiredContracts.map(async (contractName) => {
if(contractAbis?.[contractName]) return
if(newContractAbis?.[contractName]) return
const chainConfig = AVAILABLE_CHAINS.get(thePoolzInstance.chainId)
if(!chainConfig) return
const contract = chainConfig[contractName] as IContractInfo
Expand Down
11 changes: 8 additions & 3 deletions src/hooks/useThePoolz.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
import { useContext } from "react"
import { useContext, useEffect } from "react"
import ThePoolzContext from "../components/Context"
import { RequiredContract } from "../types/IThePoolzInterface"

export const useSetProvider = () => {
const { setProvider } = useContext(ThePoolzContext)
return setProvider
}
export const useThePoolz = (requiredContracts: RequiredContract[]) => {
export const useThePoolz = (requiredContracts?: RequiredContract[]) => {
const { thePoolz, setRequiredContracts } = useContext(ThePoolzContext)
setRequiredContracts(requiredContracts)

useEffect(() => {
if (!requiredContracts) return
setRequiredContracts(requiredContracts)
}, [requiredContracts])

return thePoolz
}
224 changes: 12 additions & 212 deletions src/poolz/ThePoolz.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Web3 from "web3"
import type { Contract } from "web3-eth-contract"
import type { AbiItem } from "web3-utils"
import type { IThePoolzInterface, IERC20Info, TChainConfig, AbiRecord } from "../types/IThePoolzInterface"
import { type IThePoolzInterface, type IERC20Info, type TChainConfig, type AbiRecord, type IContractInfo, contractMap, IChainConfig } from "../types/IThePoolzInterface"
import { CUSTOMER_ACCOUNT_VARIABLE, DEFAULT_CHAIN_ID, AVAILABLE_CHAINS } from "../constants"
import ERC20 from "../contracts/abi/ERC20.json"
import POOLZ from "../contracts/abi/ThePoolz.json"
Expand Down Expand Up @@ -92,23 +92,8 @@ class ThePoolz implements EnforceInterface<IThePoolzInterface, ThePoolz> {
poolzTokenAddress,
poolzAddress,
whiteListAddress,
lockedDealV2,
poolzBackWithdraw,
signUpAddress,
delayVault,
lockDealNFT,
vaultManager,
dealProvider,
lockDealProvider,
timedDealProvider,
collateralProvider,
refundProvider,
simpleBuilder,
simpleRefundBuilder,
multiSenderV2,
delayVaultProvider,
delayVaultMigrator,
tokenNFTConnector
} = chainConfig

this.poolzTokenAddress = poolzTokenAddress
Expand Down Expand Up @@ -153,203 +138,18 @@ class ThePoolz implements EnforceInterface<IThePoolzInterface, ThePoolz> {
}
}

const abifetchPromises = []

if (lockedDealV2) {
abifetchPromises.push(
this.fetchContractAbi(lockedDealV2.nameVersion)
.then((abi) => {
this.lockedDealV2 = { ...lockedDealV2, contract: new this.web3.eth.Contract(abi as AbiItem[], lockedDealV2.address) }
})
.catch((e) => {
console.error(e)
})
)
}
if (delayVault) {
abifetchPromises.push(
this.fetchContractAbi(delayVault.nameVersion)
.then((abi) => {
this.delayVaultContract = { ...delayVault, contract: new this.web3.eth.Contract(abi as AbiItem[], delayVault.address) }
})
.catch((e) => {
console.error(e)
})
)
}
if (lockDealNFT) {
abifetchPromises.push(
this.fetchContractAbi(lockDealNFT.nameVersion)
.then((abi) => {
this.lockDealNFTContract = { ...lockDealNFT, contract: new this.web3.eth.Contract(abi as AbiItem[], lockDealNFT.address) }
})
.catch((e) => {
console.error(e)
})
)
}
if (vaultManager) {
abifetchPromises.push(
this.fetchContractAbi(vaultManager.nameVersion)
.then((abi) => {
this.vaultManagerContract = { ...vaultManager, contract: new this.web3.eth.Contract(abi as AbiItem[], vaultManager.address) }
})
.catch((e) => {
console.error(e)
})
)
}
if (dealProvider) {
abifetchPromises.push(
this.fetchContractAbi(dealProvider.nameVersion)
.then((abi) => {
this.dealProviderContract = { ...dealProvider, contract: new this.web3.eth.Contract(abi as AbiItem[], dealProvider.address) }
})
.catch((e) => {
console.error(e)
})
)
}
if (lockDealProvider) {
abifetchPromises.push(
this.fetchContractAbi(lockDealProvider.nameVersion)
.then((abi) => {
this.lockDealProviderContract = {
...lockDealProvider,
contract: new this.web3.eth.Contract(abi as AbiItem[], lockDealProvider.address)
}
})
.catch((e) => {
console.error(e)
})
)
}
if (timedDealProvider) {
abifetchPromises.push(
this.fetchContractAbi(timedDealProvider.nameVersion)
.then((abi) => {
this.timedDealProviderContract = {
...timedDealProvider,
contract: new this.web3.eth.Contract(abi as AbiItem[], timedDealProvider.address)
}
})
.catch((e) => {
console.error(e)
})
)
}
if (collateralProvider) {
abifetchPromises.push(
this.fetchContractAbi(collateralProvider.nameVersion)
.then((abi) => {
this.collateralProviderContract = {
...collateralProvider,
contract: new this.web3.eth.Contract(abi as AbiItem[], collateralProvider.address)
}
})
.catch((e) => {
console.error(e)
})
)
}
if (refundProvider) {
abifetchPromises.push(
this.fetchContractAbi(refundProvider.nameVersion)
.then((abi) => {
this.refundProviderContract = {
...refundProvider,
contract: new this.web3.eth.Contract(abi as AbiItem[], refundProvider.address)
}
})
.catch((e) => {
console.error(e)
})
)
}
if (simpleBuilder) {
abifetchPromises.push(
this.fetchContractAbi(simpleBuilder.nameVersion)
.then((abi) => {
this.simpleBuilderContract = { ...simpleBuilder, contract: new this.web3.eth.Contract(abi as AbiItem[], simpleBuilder.address) }
})
.catch((e) => {
console.error(e)
})
)
}
if (simpleRefundBuilder) {
abifetchPromises.push(
this.fetchContractAbi(simpleRefundBuilder.nameVersion)
.then((abi) => {
this.simpleRefundBuilderContract = {
...simpleRefundBuilder,
contract: new this.web3.eth.Contract(abi as AbiItem[], simpleRefundBuilder.address)
}
})
.catch((e) => {
console.error(e)
})
)
}
if (multiSenderV2) {
abifetchPromises.push(
this.fetchContractAbi(multiSenderV2.nameVersion)
.then((abi) => {
this.multiSenderV2Contract = { ...multiSenderV2, contract: new this.web3.eth.Contract(abi as AbiItem[], multiSenderV2.address) }
})
.catch((e) => {
console.error(e)
})
)
}
if (delayVaultProvider) {
abifetchPromises.push(
this.fetchContractAbi(delayVaultProvider.nameVersion)
.then((abi) => {
this.delayVaultProviderContract = {
...delayVaultProvider,
contract: new this.web3.eth.Contract(abi as AbiItem[], delayVaultProvider.address)
}
})
.catch((e) => {
console.error(e)
})
)
}
if (delayVaultMigrator) {
abifetchPromises.push(
this.fetchContractAbi(delayVaultMigrator.nameVersion)
.then((abi) => {
this.delayVaultMigratorContract = {
...delayVaultMigrator,
contract: new this.web3.eth.Contract(abi as AbiItem[], delayVaultMigrator.address)
}
})
.catch((e) => {
console.error(e)
})
)
}
if (tokenNFTConnector) {
abifetchPromises.push(
this.fetchContractAbi(tokenNFTConnector.nameVersion)
.then((abi) => {
this.tokenNFTConnectorContract = {
...tokenNFTConnector,
contract: new this.web3.eth.Contract(abi as AbiItem[], tokenNFTConnector.address)
}
})
.catch((e) => {
console.error(e)
})
)
}
Object.entries(abiRecord).forEach(([key, value]) => {
const contractName = key as keyof IChainConfig
const contractInfo = chainConfig[contractName] as Omit<IContractInfo, "contract">
const thisItem = contractMap[contractName]
if(!thisItem) return
(this as any)[thisItem] = {

Check warning on line 146 in src/poolz/ThePoolz.ts

View check run for this annotation

codefactor.io / CodeFactor

src/poolz/ThePoolz.ts#L146

Unexpected any. Specify a different type. (@typescript-eslint/no-explicit-any)
nameVersion: contractInfo.nameVersion,
address: contractInfo.address,
contract: new this.web3.eth.Contract(value as AbiItem[], contractInfo.address)
}
})

try {
await Promise.allSettled(abifetchPromises)
} catch (e) {
console.error(e)
}
}

async getChaincoinInfo(chainId?: typeof this.chainId) {
Expand Down
28 changes: 28 additions & 0 deletions src/types/IThePoolzInterface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,34 @@ export interface IChainConfig {
tokenNFTConnector: Omit<NonNullable<IThePoolzInterface["tokenNFTConnectorContract"]>, "contract">
}

export const contractMap: Record<keyof IChainConfig, keyof IThePoolzInterface> = {
poolzAddress: "poolzAddress",
poolzContract: "poolzContract",
lockedDealV2: "lockedDealV2",
whiteListAddress: "whiteListAddress",
whiteListContract: "whiteListContract",
CWhiteList: "CWhiteList",
signUpAddress: "signUpAddress",
signUpContract: "signUpContract",
CSignUp: "CSignUp",
poolzBackWithdraw: "poolzBackWithdraw",
poolzBackWithdrawContract: "poolzBackWithdrawContract",
poolzTokenAddress: "poolzTokenAddress",
delayVault: "delayVaultContract",
lockDealNFT: "lockDealNFTContract",
vaultManager: "vaultManagerContract",
dealProvider: "dealProviderContract",
lockDealProvider: "lockDealProviderContract",
timedDealProvider: "timedDealProviderContract",
collateralProvider: "collateralProviderContract",
refundProvider: "refundProviderContract",
simpleBuilder: "simpleBuilderContract",
simpleRefundBuilder: "simpleRefundBuilderContract",
multiSenderV2: "multiSenderV2Contract",
delayVaultProvider: "delayVaultProviderContract",
delayVaultMigrator: "delayVaultMigratorContract",
tokenNFTConnector: "tokenNFTConnectorContract"
}

// copy chain data from https://chainid.network/chains.json
export interface IChainInfo {
Expand Down

0 comments on commit 0ec8c0c

Please sign in to comment.