Skip to content

Commit

Permalink
Merge pull request #188 from 0xMOZ/fix/ibc
Browse files Browse the repository at this point in the history
IBC Adapter
  • Loading branch information
vrtnd authored May 29, 2024
2 parents eab42f1 + 40d5903 commit 10cefa8
Show file tree
Hide file tree
Showing 21 changed files with 1,649 additions and 790 deletions.
103 changes: 68 additions & 35 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@
"daily-volume": "export $(cat .env.test | xargs) && tsx ./src/utils/testDailyVolume.ts"
},
"devDependencies": {
"@types/async-retry": "^1.4.8",
"@types/aws-lambda": "^8.10.101",
"@types/node": "^18.6.4",
"@types/node-fetch": "^2.6.2",
"@types/retry": "^0.12.5",
"babel-loader": "^8.2.5",
"eslint-config-prettier": "^8.3.0",
"prettier": "^2.5.1",
Expand All @@ -24,6 +26,7 @@
},
"dependencies": {
"@defillama/sdk": "^5.0.54",
"@graphql-typed-document-node/core": "^3.2.0",
"@solana/web3.js": "^1.87.3",
"async-retry": "^1.3.1",
"axios": "^0.21.0",
Expand All @@ -32,7 +35,7 @@
"dotenv": "^8.2.0",
"ethers": "^5",
"graphql": "^16.0.0",
"graphql-request": "^4.0.0",
"graphql-request": "^6.1.0",
"node-fetch": "^2.6.7",
"postgres": "^3.2.4",
"serverless-webpack": "^5.8.0",
Expand Down
5 changes: 5 additions & 0 deletions src/adapters/ibc/errors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class LatestBlockNotFoundError extends Error {
constructor(zoneId: string) {
super(`Latest block not found for ${zoneId}`);
}
}
106 changes: 66 additions & 40 deletions src/adapters/ibc/index.ts
Original file line number Diff line number Diff line change
@@ -1,47 +1,73 @@
import { BridgeNetwork } from "../../data/types";
import { BridgeAdapter } from "../../helpers/bridgeAdapter.type";
import { getIbcVolumeByZoneName } from "../../helpers/mapofzones";

const chainsMapping = {
osmosis: "osmosis-1",
secret: "secret-4",
injective: "injective-1",
terra: "columbus-5",
crescent: "crescent-1",
cronos: "cronosmainnet_25-1",
evmos: "evmos_9001-2",
juno: "juno-1",
kujira: "kaiyo-1",
sifchain: "sifchain-1",
stride: "stride-1",
cosmos: "cosmoshub-4",
canto: "canto_7700-1",
// these ones I'm not sure about separating into own bridges
/*
axelar: "axelar-dojo-1",
gravity_bridge: "gravity-bridge-3",
*/
// these ones I'm not sure about including (i.e., if they have any defi that makes them relevant)
// there are also about 25 more with almost no volume I did not include here
/*
bostrom: "bostrom",
crypto_org: "crypto-org-chain-mainnet-1",
agoric: "agoric-3",
akash: "akashnet-2",
comdex: "comdex-1",
fetch_ai: "fetchhub-4",
asset_mantle: "mantle-1",
sentinel: "sentinelhub-2",
stargaze: "stargaze-1",
umee: "umee-1",
medibloc: "panacea-3",
band: ""
*/
} as { [chain: string]: string };
import {
getBlockFromTimestamp,
getIbcVolumeByZoneId,
getLatestBlockForZone
} from "../../helpers/mapofzones";
import bridges from "../../data/bridgeNetworkData";

const ibcBridgeNetwork = bridges.find((bridge) => bridge.bridgeDbName === "ibc");

export const getLatestBlockForZoneFromMoz = async (zoneId: string): Promise<{
number: number;
timestamp: number;
}> => {
const block = await getLatestBlockForZone(zoneId);
if (!block) {
throw new LatestBlockNotFoundError(zoneId);
}
return {
number: block.block,
timestamp: block.timestamp,
};
}

// this returns height only
export const getLatestBlockHeightForZoneFromMoz = async (zoneId: string): Promise<number> => {
const block = await getLatestBlockForZone(zoneId);
if (!block) {
throw new LatestBlockNotFoundError(zoneId);
}
return block.block;
}

export const findChainId = (bridgeNetwork: BridgeNetwork, chain: string) => {
if (bridgeNetwork.chainMapping === undefined) {
throw new Error("Chain mapping is undefined for ibc bridge network.");
}

if (bridgeNetwork.chainMapping[chain]) {
return bridgeNetwork.chainMapping[chain];
} else if (Object.values(bridgeNetwork.chainMapping).includes(chain)) {
return chain;
}
}

export const ibcGetBlockFromTimestamp = async (bridge: BridgeNetwork, timestamp: number, chainName: string, position?: 'First' | 'Last') => {
if(position === undefined) {
throw new Error("Position is required for ibcGetBlockFromTimestamp");
}
const chainId = findChainId(bridge, chainName);
if(chainId === undefined) {
throw new Error(`Could not find chain id for chain name ${chainName}`);
}
return await getBlockFromTimestamp(timestamp, chainId, position);
}

const chainExports = () => {
if (ibcBridgeNetwork === undefined) {
throw new Error("Could not find ibc bridge network.");
}

const chainNames = ibcBridgeNetwork.chains;

const chainBreakdown = {} as BridgeAdapter;
Object.entries(chainsMapping).map(([chainName, zoneName]) => {
chainBreakdown[chainName] = getIbcVolumeByZoneName(chainName, zoneName);
chainNames.forEach((chainName) => {
const chainId = findChainId(ibcBridgeNetwork, chainName);
if(chainId) {
chainBreakdown[chainName.toLowerCase()] = getIbcVolumeByZoneId(chainId);
}
});
return chainBreakdown;
};
Expand Down
Loading

0 comments on commit 10cefa8

Please sign in to comment.