-
-
Save tomrpl/3cfd34e04a01f9cbae2b16887f8026cf to your computer and use it in GitHub Desktop.
MarketParamsToID - Blue markets
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
// "ethers": "^5.8.1" | |
import { ethers } from "ethers"; | |
// Define the MarketParams structure | |
interface MarketParams { | |
loanToken: string; | |
collateralToken: string; | |
oracle: string; | |
irm: string; | |
lltv: BigInt; | |
} | |
// Function to compute the ID | |
function computeMarketParamsId(params: MarketParams): string { | |
// Convert each address to 32 bytes and numbers to a 32-byte hex string | |
const loanToken = ethers.utils.hexZeroPad(params.loanToken, 32); | |
const collateralToken = ethers.utils.hexZeroPad(params.collateralToken, 32); | |
const oracle = ethers.utils.hexZeroPad(params.oracle, 32); | |
const irm = ethers.utils.hexZeroPad(params.irm, 32); | |
const lltv = ethers.utils.hexZeroPad( | |
ethers.BigNumber.from(params.lltv).toHexString(), | |
32 | |
); | |
// Concatenate all the parameters | |
const concatenatedParams = | |
loanToken + | |
collateralToken.slice(2) + | |
oracle.slice(2) + | |
irm.slice(2) + | |
lltv.slice(2); | |
// Compute the Keccak256 hash | |
return ethers.utils.keccak256(concatenatedParams); | |
} | |
// Example usage | |
const marketParams: MarketParams = { | |
loanToken: "0xB4FBF271143F4FBf7B91A5ded31805e42b2208d6", | |
collateralToken: "0x11fE4B6AE13d2a6055C8D9cF65c55bac32B5d844", | |
oracle: "0xa3762f93066a9e02FB38866D2D75e3277F0fd2FF", | |
irm: "0x9ee101eB4941d8D7A665fe71449360CEF3C8Bb87", | |
lltv: 900000000000000000n, | |
}; | |
console.log("Computed ID:", computeMarketParamsId(marketParams)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment