Skip to content

Commit

Permalink
Merge pull request DefiLlama#1 from DefiLlama/master
Browse files Browse the repository at this point in the history
Update from upstream
  • Loading branch information
EVMlord authored Mar 4, 2024
2 parents bcee99e + 648a54d commit efcce31
Show file tree
Hide file tree
Showing 305 changed files with 8,626 additions and 8,957 deletions.
33 changes: 26 additions & 7 deletions adapters/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ export type FetchResultBase = {
block?: number;
};

export type FetchResultV2 = {
[key: string]: FetchResponseValue | undefined;
};

export type FetchResultGeneric = FetchResultBase & {
[key: string]: FetchResponseValue | undefined;
}
Expand All @@ -23,10 +27,17 @@ export type FetchOptions = {
getLogs: (params: FetchGetLogsOptions) => Promise<any[]>;
toTimestamp: number;
fromTimestamp: number;
startOfDay: number;
getFromBlock: () => Promise<number>;
getToBlock: () => Promise<number>;
chain: string,
api: ChainApi,
fromApi: ChainApi,
toApi: ChainApi,
startTimestamp: number,
endTimestamp: number,
getStartBlock: () => Promise<number>,
getEndBlock: () => Promise<number>,
}

export type FetchGetLogsOptions = {
Expand All @@ -39,22 +50,27 @@ export type FetchGetLogsOptions = {
toBlock?: number,
flatten?: boolean,
cacheInCloud?: boolean,
entireLog?: boolean,
skipCacheRead?: boolean,
topics?: string[],
}

export type Fetch = (
timestamp: number,
chainBlocks: ChainBlocks,
options?: FetchOptions,
options: FetchOptions,
) => Promise<FetchResult>;

export type FetchV2 = (
options: FetchOptions,
) => Promise<FetchResultV2>;

export type IStartTimestamp = () => Promise<number>

export type BaseAdapter = {
[chain: string]: {
start: IStartTimestamp | number
fetch: Fetch;
fetch: Fetch|FetchV2;
runAtCurrTime?: boolean;
customBackfill?: Fetch;
meta?: {
Expand All @@ -72,18 +88,21 @@ export enum ProtocolType {
COLLECTION = 'collection',
}

export type SimpleAdapter = {
export type AdapterBase = {
timetravel?: boolean
adapter: BaseAdapter
isExpensiveAdapter?: boolean,
protocolType?: ProtocolType;
version?: number;
}

export type BreakdownAdapter = {
timetravel?: boolean
export type SimpleAdapter = AdapterBase & {
adapter: BaseAdapter
}

export type BreakdownAdapter = AdapterBase & {
breakdown: {
[version: string]: BaseAdapter
};
protocolType?: ProtocolType;
};

export type Adapter = SimpleAdapter | BreakdownAdapter;
Expand Down
60 changes: 51 additions & 9 deletions adapters/utils/runAdapter.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import { Balances, ChainApi, getEventLogs } from '@defillama/sdk'
import { BaseAdapter, ChainBlocks, DISABLED_ADAPTER_KEY, FetchGetLogsOptions, FetchResultGeneric, } from '../types'
import { Balances, ChainApi, getEventLogs, getProvider } from '@defillama/sdk'
import { BaseAdapter, ChainBlocks, DISABLED_ADAPTER_KEY, Fetch, FetchGetLogsOptions, FetchOptions, FetchResultGeneric, FetchV2, } from '../types'
import { getBlock } from "../../helpers/getBlock";
import { getUniqStartOfTodayTimestamp } from '../../helpers/getUniSubgraphFees';

const ONE_DAY_IN_SECONDS = 60 * 60 * 24

export default async function runAdapter(volumeAdapter: BaseAdapter, cleanCurrentDayTimestamp: number, chainBlocks: ChainBlocks, id?: string, version?: string) {
export default async function runAdapter(volumeAdapter: BaseAdapter, cleanCurrentDayTimestamp: number, chainBlocks: ChainBlocks, id?: string, version?: string, {
adapterVersion = 1
}: any = {}) {
const closeToCurrentTime = Math.trunc(Date.now() / 1000) - cleanCurrentDayTimestamp < 24 * 60 * 60 // 12 hours
const chains = Object.keys(volumeAdapter).filter(c => c !== DISABLED_ADAPTER_KEY)
const validStart = {} as {
Expand All @@ -20,8 +23,16 @@ export default async function runAdapter(volumeAdapter: BaseAdapter, cleanCurren
async function getChainResult(chain: string) {
const fetchFunction = volumeAdapter[chain].customBackfill ?? volumeAdapter[chain].fetch
try {
const options = getOptionsObject(cleanCurrentDayTimestamp, chain, chainBlocks)
const result: FetchResultGeneric = await fetchFunction(options.toTimestamp, chainBlocks, options);
const options = await getOptionsObject(cleanCurrentDayTimestamp, chain, chainBlocks)
let result: any
if (adapterVersion === 1) {
result = await (fetchFunction as Fetch)(options.toTimestamp, chainBlocks, options);
} else if (adapterVersion === 2) {
result = await (fetchFunction as FetchV2)(options);
result.timestamp = options.toTimestamp
} else {
throw new Error(`Adapter version ${adapterVersion} not supported`)
}
const ignoreKeys = ['timestamp', 'block']
// if (id)
// console.log("Result before cleaning", id, version, cleanCurrentDayTimestamp, chain, result, JSON.stringify(chainBlocks ?? {}))
Expand All @@ -47,7 +58,7 @@ export default async function runAdapter(volumeAdapter: BaseAdapter, cleanCurren
}
}

function getOptionsObject(timestamp: number, chain: string, chainBlocks: ChainBlocks) {
async function getOptionsObject(timestamp: number, chain: string, chainBlocks: ChainBlocks): Promise<FetchOptions> {
const withinTwoHours = Math.trunc(Date.now() / 1000) - timestamp < 2 * 60 * 60 // 2 hours
const createBalances: () => Balances = () => {
return new Balances({ timestamp: closeToCurrentTime ? undefined : timestamp, chain })
Expand All @@ -57,12 +68,30 @@ export default async function runAdapter(volumeAdapter: BaseAdapter, cleanCurren
const fromChainBlocks = {}
const getFromBlock = async () => await getBlock(fromTimestamp, chain, fromChainBlocks)
const getToBlock = async () => await getBlock(toTimestamp, chain, chainBlocks)
const getLogs = async ({ target, targets, onlyArgs = true, fromBlock, toBlock, flatten = true, eventAbi, topics, topic, cacheInCloud = false, skipCacheRead = false, }: FetchGetLogsOptions) => {
const getLogs = async ({ target, targets, onlyArgs = true, fromBlock, toBlock, flatten = true, eventAbi, topics, topic, cacheInCloud = false, skipCacheRead = false, entireLog = false, }: FetchGetLogsOptions) => {
fromBlock = fromBlock ?? await getFromBlock()
toBlock = toBlock ?? await getToBlock()

return getEventLogs({ fromBlock, toBlock, chain, target, targets, onlyArgs, flatten, eventAbi, topics, topic, cacheInCloud, skipCacheRead, })
return getEventLogs({ fromBlock, toBlock, chain, target, targets, onlyArgs, flatten, eventAbi, topics, topic, cacheInCloud, skipCacheRead, entireLog, })
}

// we intentionally add a delay to avoid fetching the same block before it is cached
await randomDelay()

let fromBlock, toBlock
// we fetch current block and previous blocks only for evm chains/ chains we have RPC for
if (getProvider(chain)) {
fromBlock = await getFromBlock()
toBlock = await getToBlock()
}
const fromApi = new ChainApi({ chain, timestamp: fromTimestamp, block: fromBlock })
const api = new ChainApi({ chain, timestamp: withinTwoHours ? undefined : timestamp, block: toBlock })
const startOfDay = getUniqStartOfTodayTimestamp(new Date(toTimestamp * 1000))
const startTimestamp = fromTimestamp
const endTimestamp = toTimestamp
const getStartBlock = getFromBlock
const getEndBlock = getToBlock
const toApi = api

return {
createBalances,
Expand All @@ -73,10 +102,23 @@ export default async function runAdapter(volumeAdapter: BaseAdapter, cleanCurren
getToBlock,
getLogs,
chain,
api: new ChainApi({ chain, timestamp: withinTwoHours ? undefined : timestamp, }),
fromApi,
toApi,
api,
startOfDay,
startTimestamp,
endTimestamp,
getStartBlock,
getEndBlock,
}
}

// code for random 1-4 second delay
async function randomDelay() {
const delay = Math.floor(Math.random() * 4) + 1
return new Promise((resolve) => setTimeout(resolve, delay * 1000))
}

async function setChainValidStart(chain: string) {
const cleanPreviousDayTimestamp = cleanCurrentDayTimestamp - ONE_DAY_IN_SECONDS
const _start = volumeAdapter[chain]?.start
Expand Down
3 changes: 2 additions & 1 deletion aggregators/1inch-agg/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { FetchResult, SimpleAdapter } from "../../adapters/types";
import { FetchResult, } from "../../adapters/types";
import { getUniqStartOfTodayTimestamp } from "../../helpers/getUniSubgraphVolume";
import { fetchURLWithRetry } from "../../helpers/duneRequest";

Expand Down Expand Up @@ -43,6 +43,7 @@ const adapter: any = {
};
}, {}),
},
isExpensiveAdapter: true,
};

export default adapter;
3 changes: 2 additions & 1 deletion aggregators/arcane-dex/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import ADDRESSES from '../../helpers/coreAssets.json'

import request from "graphql-request"
import { FetchResultVolume, SimpleAdapter } from "../../adapters/types"
Expand Down Expand Up @@ -38,7 +39,7 @@ const fetchVolume = async (timestamp: number): Promise<FetchResultVolume> => {
}
`
const result: IResponse = await request("https://api.thegraph.com/subgraphs/name/0xandee/arcanedex", query)
const ethAddress = "ethereum:0x0000000000000000000000000000000000000000";
const ethAddress = "ethereum:" + ADDRESSES.null;

const dailyVolumeInEth = Number(result.today.totalVolumeInEth) - Number(result.yesterday.totalVolumeInEth)
const totalVolumeInEth = Number(result.today.totalVolumeInEth)
Expand Down
89 changes: 86 additions & 3 deletions aggregators/bebop/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,90 @@
import { getAdapter } from "../../helpers/aggregators/duneAdapter";
import { ethers } from "ethers";
import { ChainBlocks, FetchOptions } from "../../adapters/types";
import { getTransactions } from "../../helpers/getTxReceipts";

const chains = ["arbitrum", "ethereum", "polygon"];
const abis = {
"AggregateOrderExecuted": "event AggregateOrderExecuted(bytes32 order_hash)",
"OrderSignerRegistered": "event OrderSignerRegistered(address maker, address signer, bool allowed)",
"AGGREGATED_ORDER_TYPE_HASH": "function AGGREGATED_ORDER_TYPE_HASH() view returns (bytes32)",
"DOMAIN_SEPARATOR": "function DOMAIN_SEPARATOR() view returns (bytes32)",
"EIP712_DOMAIN_TYPEHASH": "function EIP712_DOMAIN_TYPEHASH() view returns (bytes32)",
"PARTIAL_AGGREGATED_ORDER_TYPE_HASH": "function PARTIAL_AGGREGATED_ORDER_TYPE_HASH() view returns (bytes32)",
"SettleAggregateOrder": "function SettleAggregateOrder((uint256 expiry, address taker_address, address[] maker_addresses, uint256[] maker_nonces, address[][] taker_tokens, address[][] maker_tokens, uint256[][] taker_amounts, uint256[][] maker_amounts, address receiver, bytes commands) order, (uint8 signatureType, bytes signatureBytes) takerSig, ((uint8 signatureType, bytes signatureBytes) signature, bool usingPermit2)[] makerSigs) payable returns (bool)",
"SettleAggregateOrderWithTakerPermits": "function SettleAggregateOrderWithTakerPermits((uint256 expiry, address taker_address, address[] maker_addresses, uint256[] maker_nonces, address[][] taker_tokens, address[][] maker_tokens, uint256[][] taker_amounts, uint256[][] maker_amounts, address receiver, bytes commands) order, (uint8 signatureType, bytes signatureBytes) takerSig, ((uint8 signatureType, bytes signatureBytes) signature, bool usingPermit2)[] makerSigs, (bytes[] permitSignatures, bytes signatureBytesPermit2, uint48[] noncesPermit2, uint48 deadline) takerPermitsInfo) payable returns (bool)",
"hashAggregateOrder": "function hashAggregateOrder((uint256 expiry, address taker_address, address[] maker_addresses, uint256[] maker_nonces, address[][] taker_tokens, address[][] maker_tokens, uint256[][] taker_amounts, uint256[][] maker_amounts, address receiver, bytes commands) order) view returns (bytes32)",
"hashPartialOrder": "function hashPartialOrder((uint256 expiry, address taker_address, address maker_address, uint256 maker_nonce, address[] taker_tokens, address[] maker_tokens, uint256[] taker_amounts, uint256[] maker_amounts, address receiver, bytes commands) order) view returns (bytes32)",
"Trade": "event Trade(address indexed owner, address sellToken, address buyToken, uint256 sellAmount, uint256 buyAmount, uint256 feeAmount, bytes orderUid)", // gnosis
"swap": "function swap((bytes32 poolId, uint256 assetInIndex, uint256 assetOutIndex, uint256 amount, bytes userData)[] swaps, address[] tokens, (uint256 sellTokenIndex, uint256 buyTokenIndex, address receiver, uint256 sellAmount, uint256 buyAmount, uint32 validTo, bytes32 appData, uint256 feeAmount, uint256 flags, uint256 executedAmount, bytes signature) trade)", // gnosis
"settle": "function settle(address[] tokens, uint256[] clearingPrices, (uint256 sellTokenIndex, uint256 buyTokenIndex, address receiver, uint256 sellAmount, uint256 buyAmount, uint32 validTo, bytes32 appData, uint256 feeAmount, uint256 flags, uint256 executedAmount, bytes signature)[] trades, (address target, uint256 value, bytes callData)[][3] interactions)", // gnosis
"sellOrderSwap": "function sellOrderSwap((uint256 deadline, address tokenIn, uint256 amountIn, uint256 nonce, bytes signature, address allowanceTarget, address swapper, bytes swapData, address tokenOut, uint256 minAmountOut, (address recipient, uint256 shareBps)[] transferOut) _params) payable returns (uint256 _amountIn, uint256 _amountOut)",

const adapter = getAdapter(chains, {}, "bebop", 1680307200);
// maestro

"swapAndDeposit": "function swapAndDeposit(address tokenToSell, address tokenToDeposit, (address router, address spender, uint256 amountIn, uint256 minAmountOut, bytes swapBytes) swapData) returns (uint256)",
"swapAndDepositNative": "function swapAndDepositNative(address tokenToDeposit, (address router, address spender, uint256 amountIn, uint256 minAmountOut, bytes swapBytes) swapData) payable returns (uint256)",
"swapAndDepositWithPermit": "function swapAndDepositWithPermit(address tokenToSell, address tokenToDeposit, (address router, address spender, uint256 amountIn, uint256 minAmountOut, bytes swapBytes) swapData, (uint256 amount, uint256 deadline, bytes32 r, bytes32 vs) permit) returns (uint256)",
"swapAndDepositWithPermit2": "function swapAndDepositWithPermit2(address tokenToSell, address tokenToDeposit, (address router, address spender, uint256 amountIn, uint256 minAmountOut, bytes swapBytes) swapData, (uint256 amount, uint256 deadline, bytes32 r, bytes32 vs) permit) returns (uint256)",
"swapAndWithdraw": "function swapAndWithdraw(address tokenToSell, address tokenToReceive, (address router, address spender, uint256 amountIn, uint256 minAmountOut, bytes swapBytes) swapData, address to) returns (uint256)",
"swapAndWithdrawNative": "function swapAndWithdrawNative(address tokenToSell, (address router, address spender, uint256 amountIn, uint256 minAmountOut, bytes swapBytes) swapData, address to) returns (uint256 output)",
"trade": "function trade((bytes32 positionId, int256 quantity, uint256 limitPrice, uint8 cashflowCcy, int256 cashflow) tradeParams, (address spender, address router, uint256 swapAmount, bytes swapBytes, address flashLoanProvider) execParams) returns (bytes32, (int256 quantity, (uint8 inputCcy, int256 input, int256 output, uint256 price) swap, uint8 cashflowCcy, int256 cashflow, uint256 fee, uint8 feeCcy, uint256 forwardPrice))",
"tradeAndLinkedOrder": "function tradeAndLinkedOrder((bytes32 positionId, int256 quantity, uint256 limitPrice, uint8 cashflowCcy, int256 cashflow) tradeParams, (address spender, address router, uint256 swapAmount, bytes swapBytes, address flashLoanProvider) execParams, (uint128 limitPrice, uint128 tolerance, uint8 cashflowCcy, uint32 deadline, uint8 orderType) linkedOrderParams) payable returns (bytes32 positionId, (int256 quantity, (uint8 inputCcy, int256 input, int256 output, uint256 price) swap, uint8 cashflowCcy, int256 cashflow, uint256 fee, uint8 feeCcy, uint256 forwardPrice) trade_, bytes32 linkedOrderId)",
"tradeAndLinkedOrders": "function tradeAndLinkedOrders((bytes32 positionId, int256 quantity, uint256 limitPrice, uint8 cashflowCcy, int256 cashflow) tradeParams, (address spender, address router, uint256 swapAmount, bytes swapBytes, address flashLoanProvider) execParams, (uint128 limitPrice, uint128 tolerance, uint8 cashflowCcy, uint32 deadline, uint8 orderType) linkedOrderParams1, (uint128 limitPrice, uint128 tolerance, uint8 cashflowCcy, uint32 deadline, uint8 orderType) linkedOrderParams2) payable returns (bytes32 positionId, (int256 quantity, (uint8 inputCcy, int256 input, int256 output, uint256 price) swap, uint8 cashflowCcy, int256 cashflow, uint256 fee, uint8 feeCcy, uint256 forwardPrice) trade_, bytes32 linkedOrderId1, bytes32 linkedOrderId2)",
"tradeAndWithdraw": "function tradeAndWithdraw((bytes32 positionId, int256 quantity, uint256 limitPrice, uint8 cashflowCcy, int256 cashflow) tradeParams, (address spender, address router, uint256 swapAmount, bytes swapBytes, address flashLoanProvider) execParams, address to) returns (bytes32 positionId, (int256 quantity, (uint8 inputCcy, int256 input, int256 output, uint256 price) swap, uint8 cashflowCcy, int256 cashflow, uint256 fee, uint8 feeCcy, uint256 forwardPrice) trade_, uint256 amount)",
"tradeAndWithdrawNative": "function tradeAndWithdrawNative((bytes32 positionId, int256 quantity, uint256 limitPrice, uint8 cashflowCcy, int256 cashflow) tradeParams, (address spender, address router, uint256 swapAmount, bytes swapBytes, address flashLoanProvider) execParams, address to) returns (bytes32 positionId, (int256 quantity, (uint8 inputCcy, int256 input, int256 output, uint256 price) swap, uint8 cashflowCcy, int256 cashflow, uint256 fee, uint8 feeCcy, uint256 forwardPrice) trade_, uint256 amount)",

}
const contract_interface = new ethers.Interface(Object.values(abis));


const fetch = async (timestamp: number, _: ChainBlocks, { createBalances, getLogs, chain, api }: FetchOptions) => {
const dailyVolume = createBalances()
const cowswapData: any = {}

const logs = await getLogs({
target: '0xBeB09000fa59627dc02Bb55448AC1893EAa501A5',
topics: ['0xc59522161f93d59c8c4520b0e7a3635fb7544133275be812a4ea970f4f14251b'] // AggregateOrderExecuted
});
if (chain === 'ethereum') {
const cowswapLogs = await getLogs({
target: '0x9008d19f58aabd9ed0d60971565aa8510560ab41',
eventAbi: abis.Trade,
onlyArgs: false,
entireLog: true,
});
cowswapLogs.forEach((log: any) => {
cowswapData[log.transactionHash.toLowerCase()] = contract_interface.parseLog(log)?.args
})
}
const data: any = await getTransactions(chain, logs.map((log: any) => log.transactionHash), { cacheKey: 'bebop' })
for (const d of data) {
if (!d) continue;
const decoded = contract_interface.parseTransaction(d)
if (!decoded) {
api.log('no decoded', d.hash, d.input.slice(0, 10), d.to, chain)
continue;
}
if (decoded.args.order) {
const { order: { taker_tokens, taker_amounts } } = decoded.args as any
dailyVolume.add(taker_tokens.flat(), taker_amounts.flat())
} else if (cowswapData[d.hash.toLowerCase()]) {
const { buyToken, buyAmount } = cowswapData[d.hash.toLowerCase()]
dailyVolume.add(buyToken, buyAmount)
} else if (decoded.args._params?.minAmountOut) {
const { tokenIn, amountIn } = decoded.args._params as any
dailyVolume.add(tokenIn, amountIn)
} else {
api.log('no order', d.hash, d.input.slice(0, 10), d.to, chain, decoded.signature)
}
}
return { timestamp, dailyVolume }
};

const adapter: any = {
adapter: {
arbitrum: { fetch, start: 1685491200, },
ethereum: { fetch, start: 1685491200, },
polygon: { fetch, start: 1685491200, },
},
};

export default adapter;
1 change: 1 addition & 0 deletions aggregators/conveyor/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ const adapter: any = {
};
}, {}),
},
isExpensiveAdapter: true,
};

export default adapter;
Loading

0 comments on commit efcce31

Please sign in to comment.