Skip to content

Commit

Permalink
Merge pull request #243 from nickp96/squid_update
Browse files Browse the repository at this point in the history
Squid_update
  • Loading branch information
vrtnd authored Jul 4, 2024
2 parents f41902f + 83fde3f commit 34b277c
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 70 deletions.
44 changes: 26 additions & 18 deletions src/adapters/squid/constants.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,28 @@
export const squidRouterAddress = "0xce16F69375520ab01377ce7B88f5BA8C48F8D666";
export const squidRouterAddresses = {
default: "0xce16F69375520ab01377ce7B88f5BA8C48F8D666",
blast: "0x492751eC3c57141deb205eC2da8bFcb410738630",
fraxtal: "0xDC3D8e1Abe590BCa428a8a2FC4CfDbD1AcF57Bd9",
};

export const axelarGatewayAddresses = {
ethereum: "0x4F4495243837681061C4743b74B3eEdf548D56A5",
bsc: "0x304acf330bbE08d1e512eefaa92F6a57871fD895",
polygon: "0x6f015F16De9fC8791b234eF68D486d2bF203FBA8",
avax: "0x5029C0EFf6C34351a0CEc334542cDb22c7928f78",
fantom: "0x304acf330bbE08d1e512eefaa92F6a57871fD895",
arbitrum: "0xe432150cce91c13a887f7D836923d5597adD8E31",
base: "0xe432150cce91c13a887f7d836923d5597add8e31",
linea: "0xe432150cce91c13a887f7d836923d5597add8e31",
celo: "0xe432150cce91c13a887f7d836923d5597add8e31",
moonbeam: "0x4F4495243837681061C4743b74B3eEdf548D56A5",
kava: "0xe432150cce91c13a887f7D836923d5597adD8E31",
filecoin: "0xe432150cce91c13a887f7D836923d5597adD8E31",
optimism: "0xe432150cce91c13a887f7D836923d5597adD8E31",
mantle: "0xe432150cce91c13a887f7D836923d5597adD8E31",
} as {
[chain: string]: string;
};
ethereum: "0x4F4495243837681061C4743b74B3eEdf548D56A5",
bsc: "0x304acf330bbE08d1e512eefaa92F6a57871fD895",
polygon: "0x6f015F16De9fC8791b234eF68D486d2bF203FBA8",
avax: "0x5029C0EFf6C34351a0CEc334542cDb22c7928f78",
fantom: "0x304acf330bbE08d1e512eefaa92F6a57871fD895",
arbitrum: "0xe432150cce91c13a887f7D836923d5597adD8E31",
base: "0xe432150cce91c13a887f7d836923d5597add8e31",
linea: "0xe432150cce91c13a887f7d836923d5597add8e31",
celo: "0xe432150cce91c13a887f7d836923d5597add8e31",
moonbeam: "0x4F4495243837681061C4743b74B3eEdf548D56A5",
kava: "0xe432150cce91c13a887f7D836923d5597adD8E31",
filecoin: "0xe432150cce91c13a887f7D836923d5597adD8E31",
optimism: "0xe432150cce91c13a887f7D836923d5597adD8E31",
mantle: "0xe432150cce91c13a887f7D836923d5597adD8E31",
scroll: "0xe432150cce91c13a887f7D836923d5597adD8E31",
blast: "0xe432150cce91c13a887f7D836923d5597adD8E31",
fraxtal: "0xe432150cce91c13a887f7D836923d5597adD8E31",
immutable: "0xe432150cce91c13a887f7D836923d5597adD8E31",
} as {
[chain: string]: string;
};
47 changes: 20 additions & 27 deletions src/adapters/squid/index.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,21 @@
import { Chain } from "@defillama/sdk/build/general";
import { BridgeAdapter, PartialContractEventParams } from "../../helpers/bridgeAdapter.type";
import { constructTransferParams } from "../../helpers/eventParams";
import { getTxDataFromEVMEventLogs } from "../../helpers/processTransactions";
import { ethers } from "ethers";
import { fetchAssets, getTokenAddress} from "./utils";
import { axelarGatewayAddresses, squidRouterAddress } from "./constants";
import { axelarGatewayAddresses, squidRouterAddresses } from "./constants";


const constructGatewayWithdrawalParams = (assets: any[], chain: string) => {
let GatewayWithdrawalParams: PartialContractEventParams;
const constructGatewayWithdrawalParams = (assets: any[], chain: string) => {
const squidRouterAddress = squidRouterAddresses[chain as keyof typeof squidRouterAddresses] || squidRouterAddresses.default;

return GatewayWithdrawalParams = {
return {
target: "",
topic: "ContractCallWithToken(address,string,string,bytes32,bytes,string,uint256)",
topics: [ethers.utils.id("ContractCallWithToken(address,string,string,bytes32,bytes,string,uint256)"), ethers.utils.hexZeroPad(squidRouterAddress,32)],
abi: ["event ContractCallWithToken(address indexed sender, string destinationChain, string destinationContractAddress, bytes32 indexed payloadHash, bytes payload, string symbol, uint256 amount)"],
logKeys: {
blockNumber: "blockNumber",
txHash: "transactionHash",
// token: "token"
},
argKeys: {
from: "payload",
Expand All @@ -27,30 +24,26 @@ import { axelarGatewayAddresses, squidRouterAddress } from "./constants";
token: "symbol"
},
argGetters: {
from: (log: any) => "0x".concat(log.payload.substr(90,40)), // note: this is not the real sender address
from: (log: any) => "0x".concat(log.payload.substr(90,40)),
amount: (log: any) => log.amount,
to: (log: any) => "0x".concat(log.payload.substr(log.payload.lastIndexOf(log.payload.substr(90,40)),40)),
token: (log: any) => getTokenAddress(log.symbol, chain, assets)
},
isDeposit: false,

};
}

}


const constructGatewayDepositParams = (assets: any[], chain: string) => {
let GatewayDepositParams: PartialContractEventParams;

return GatewayDepositParams = {
const constructGatewayDepositParams = (assets: any[], chain: string) => {
const squidRouterAddress = squidRouterAddresses[chain as keyof typeof squidRouterAddresses] || squidRouterAddresses.default;

return {
target: "",
topic: "ContractCallApprovedWithMint(bytes32,string,string,address,bytes32,string,uint256,bytes32,uint256)",
topics: [ethers.utils.id("ContractCallApprovedWithMint(bytes32,string,string,address,bytes32,string,uint256,bytes32,uint256)"), null, ethers.utils.hexZeroPad(squidRouterAddress,32)],
abi: ["event ContractCallApprovedWithMint(bytes32 indexed commandId, string sourceChain, string sourceAddress, address indexed contractAddress, bytes32 indexed payloadHash, string symbol, uint256 amount, bytes32 sourceTxHash, uint256 sourceEventIndex)"],
logKeys: {
blockNumber: "blockNumber",
txHash: "transactionHash",
// token: "token"
},
argKeys: {
from: "sourceAddress",
Expand All @@ -66,35 +59,30 @@ import { axelarGatewayAddresses, squidRouterAddress } from "./constants";
},
isDeposit: true,
};
}

}

const constructParams = (chain: string) => {

return async (fromBlock: number, toBlock: number) => {
let eventParams = [] as PartialContractEventParams[];
// fetch all assets from axelarscan
const assets = await fetchAssets();

const GatewayDepositParams = constructGatewayDepositParams(assets, chain);
const deposit = {...GatewayDepositParams, target: axelarGatewayAddresses[chain], };
const deposit = {...GatewayDepositParams, target: axelarGatewayAddresses[chain]};

const GatewayWithdrawalParams = constructGatewayWithdrawalParams(assets, chain);
const withdraw = {...GatewayWithdrawalParams, target: axelarGatewayAddresses[chain], };

const withdraw = {...GatewayWithdrawalParams, target: axelarGatewayAddresses[chain]};

eventParams.push(deposit, withdraw);

return getTxDataFromEVMEventLogs("squid", chain as Chain, fromBlock, toBlock, eventParams);
}

};

const adapter: BridgeAdapter = {
polygon: constructParams("polygon"),
fantom: constructParams("fantom"),
avalanche: constructParams("avax"),
// bsc: constructParams("bsc"),
bsc: constructParams("bsc"),
ethereum: constructParams("ethereum"),
arbitrum: constructParams("arbitrum"),
base: constructParams("base"),
Expand All @@ -105,6 +93,11 @@ const adapter: BridgeAdapter = {
filecoin: constructParams("filecoin"),
optimism: constructParams("optimism"),
mantle: constructParams("mantle"),
//new chains
scroll: constructParams("scroll"),
blast: constructParams("blast"),
fraxtal: constructParams("fraxtal"),
immutable: constructParams("immutable"),
};

export default adapter;
export default adapter;
43 changes: 20 additions & 23 deletions src/adapters/squid/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,49 +2,46 @@ import fetch from "node-fetch";
const retry = require("async-retry");

export const getTokenAddress = (symbol: string, chain: string, assets: any[]) => {

symbol = getSymbol(symbol);
chain = getChain(chain);
// find the correct token address given the chain
let tokenAddress = assets.find((asset) => asset.symbol === symbol)?.addresses?.[chain]?.address;

// if the token address is not listed, then it means that the token is not that important.
// We return some hardcoded address for this token to circumvent the token address not found in defillama bridge server.
if (tokenAddress == undefined) tokenAddress = "0x000000000000000000000000000000000000dEaD"

return tokenAddress;
}

const getChain = (chain: string) => {
if (chain === "avax") return "avalanche";
if (chain === "bsc") return "binance";
else return chain;
switch (chain) {
case "avax": return "avalanche";
case "bsc": return "binance";
// Add any new chain mappings here if needed
default: return chain;
}
}

const getSymbol = (rawSymbol: string) => {
// the symbol is usually of the form axlUSDC, axlDAI, axlUSDT, etc. or if AXL is being transferred then AXL.
let symbol: string = rawSymbol;
// for tokens of the form axl-TOKEN, remove the first 4 chars.
if (symbol.startsWith("axl-")) symbol = symbol.slice(4);
// for tokens of the form axlUSDC, remove the first 3 chars.
if (symbol.startsWith("axl")) symbol = symbol.slice(3);
// if the symbol is of a native token then prepend w, i.e., AVAX -> WAVAX. This is because axelar represents native tokens as wrapped version.
if (symbol === "AVAX") symbol = "WAVAX";
if (symbol === "FTM") symbol = "WFTM";
if (symbol === "BNB") symbol = "WBNB";
if (symbol === "MATIC") symbol = "WMATIC";
if (symbol === "FIL") symbol = "WFIL";
if (symbol === "ETH") symbol = "WETH";
if (symbol === "wAXL") symbol = "AXL";
// make all uppercase if the token starts with an uppercase letter, e.g., Lqdr.
if (symbol.charAt(0) === symbol.toUpperCase().charAt(0)) symbol = symbol.toUpperCase();

const nativeTokenMap: {[key: string]: string} = {
"AVAX": "WAVAX",
"FTM": "WFTM",
"BNB": "WBNB",
"MATIC": "WMATIC",
"FIL": "WFIL",
"ETH": "WETH",
"wAXL": "AXL",
// Add any new native token mappings here if needed
};

return symbol;
symbol = nativeTokenMap[symbol] || symbol;

return symbol.charAt(0) === symbol.toUpperCase().charAt(0) ? symbol.toUpperCase() : symbol;
}

export const fetchAssets = () => {
// fetch from axelarscan and pass {"method": "getAssets"} as json body
return retry(() =>
fetch("https://api.axelarscan.io/", {
method: "POST",
Expand All @@ -56,4 +53,4 @@ export const fetchAssets = () => {
}),
}).then((res) => res.json())
);
}
}
8 changes: 6 additions & 2 deletions src/data/bridgeNetworkData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -759,7 +759,7 @@ export default [
"Ethereum",
"Polygon",
"Avalanche",
// "BSC",
"BSC",
"Fantom",
"Arbitrum",
"Base",
Expand All @@ -769,9 +769,13 @@ export default [
"Kava",
"Optimism",
"Mantle",
"Scroll",
"Blast",
"Fraxtal",
],
chainMapping: {
avalanche: "avax", // this is needed temporarily, need to fix and remove
avalanche: "avax",
// Add any other necessary mappings here
},
},
{
Expand Down

0 comments on commit 34b277c

Please sign in to comment.