diff --git a/contracts/package.json b/contracts/package.json index bc6725159..a62b05cee 100644 --- a/contracts/package.json +++ b/contracts/package.json @@ -1,6 +1,6 @@ { "name": "@bisonai/orakl-contracts", - "version": "1.3.2", + "version": "1.3.4", "description": "The Orakl Network Smart Contracts", "files": [ "./dist", @@ -28,7 +28,7 @@ "prepare": "cd .. && husky install contracts/.husky", "clean": "yarn hardhat clean && rm -rf dist", "compile": "yarn hardhat compile", - "prebuild": "yarn compile && yarn --cwd='../..' vrf build", + "prebuild": "yarn compile && yarn --cwd='../..' vrf build && cp -r ./deployments ./typechain-types/deployments && cp -r ./utils ./typechain-types/utils", "build": "tsc", "test": "yarn compile && npx hardhat test --parallel", "test-vrf": "yarn hardhat test test/v0.1/vrf/*.cjs --parallel", diff --git a/contracts/tsconfig.json b/contracts/tsconfig.json index 2cd633892..b447bdc68 100644 --- a/contracts/tsconfig.json +++ b/contracts/tsconfig.json @@ -1,5 +1,6 @@ { "compilerOptions": { + "resolveJsonModule": true, "skipLibCheck": true, "module": "commonjs", "moduleResolution": "node", @@ -7,7 +8,7 @@ "strict": true, "esModuleInterop": true, "outDir": "dist", - "declaration": true + "declaration": true, }, - "include": ["./typechain-types"] + "include": ["./typechain-types", "./typechain-types/**/*.json"] } diff --git a/contracts/utils/index.ts b/contracts/utils/index.ts new file mode 100644 index 000000000..9492a53b9 --- /dev/null +++ b/contracts/utils/index.ts @@ -0,0 +1,97 @@ +import * as fs from 'fs' +import * as path from 'path' + +interface deployments { + [network: string]: { + [contractName: string]: string + } +} + +const deploymentsPath = path.resolve(__dirname, '..') + '/deployments/' + +const isValidPath = (_path: string): boolean => { + //checks if json file depth is 2, which stands for /{network}/{contract}.json + const splitted = _path.replace(deploymentsPath, '').split('/') + return splitted.length == 2 && path.extname(_path) === '.json' +} + +const readDeployments = async (folderPath: string): Promise => { + const result: deployments = {} + + const readFolder = async (_folderPath: string): Promise => { + const files = await fs.promises.readdir(_folderPath) + + await Promise.all( + files.map(async (file) => { + const filePath = path.join(_folderPath, file) + const stats = await fs.promises.stat(filePath) + + if (stats.isDirectory()) { + await readFolder(filePath) + } else if (isValidPath(filePath)) { + const network = filePath.replace(deploymentsPath, '').split('/')[0] + const fileContent = await fs.promises.readFile(filePath, 'utf-8') + + let contractName = path.basename(file, '.json') + if (contractName.split('_').length > 1) { + // remove last part which normally holds version name + const splitted = contractName.replace(' ', '').split('_') + splitted.pop() + contractName = splitted.join('_') + } + + try { + const jsonObject = JSON.parse(fileContent) + const contractAddress = jsonObject.address + if (!contractAddress) { + return + } + + result[network] = { ...result[network], [contractName]: contractAddress } + } catch (error: any) { + console.error(`Error parsing JSON file ${file}: ${error.message}`) + } + } + }) + ) + } + + await readFolder(folderPath) + return result +} + +export const getContractAddress = async (network: string, contractName: string) => { + const contractAddressObject = await readDeployments(deploymentsPath) + const contracts = contractAddressObject[network] + return contracts[contractName] || '' +} + +const _getContractAddressWithTokenPairs = async ( + network: string, + contractName: string, + token_0: string, + token_1: string +) => { + const name = `${contractName}_${token_0.toUpperCase()}-${token_1.toUpperCase()}` + return await getContractAddress(network, name) +} + +export const getAggregatorAddress = async (network: string, token_0: string, token_1: string) => { + return await _getContractAddressWithTokenPairs(network, 'Aggregator', token_0, token_1) +} + +export const getAggregatorProxyAddress = async ( + network: string, + token_0: string, + token_1: string +) => { + return await _getContractAddressWithTokenPairs(network, 'AggregatorProxy', token_0, token_1) +} + +export const getPorAddress = async (network: string, token_0: string, token_1: string) => { + return _getContractAddressWithTokenPairs(network, 'POR', token_0, token_1) +} + +export const getPorProxyAddress = async (network: string, token_0: string, token_1: string) => { + return _getContractAddressWithTokenPairs(network, 'PORProxy', token_0, token_1) +}