From f7700afdad212b3300f537da9db1d9809b82fd86 Mon Sep 17 00:00:00 2001 From: cheetah_rollie Date: Thu, 4 Jul 2024 18:01:08 +0800 Subject: [PATCH 1/2] update: add rollie adapter --- adapters/rollie/hourly_blocks.csv | 2 + adapters/rollie/package.json | 27 + adapters/rollie/src/abi/PairInfo.ts | 1681 +++++++++++++++++++++++ adapters/rollie/src/abi/TradingVault.ts | 1009 ++++++++++++++ adapters/rollie/src/index.ts | 511 +++++++ adapters/rollie/src/queries.ts | 78 ++ adapters/rollie/tsconfig.json | 109 ++ 7 files changed, 3417 insertions(+) create mode 100644 adapters/rollie/hourly_blocks.csv create mode 100644 adapters/rollie/package.json create mode 100644 adapters/rollie/src/abi/PairInfo.ts create mode 100644 adapters/rollie/src/abi/TradingVault.ts create mode 100644 adapters/rollie/src/index.ts create mode 100644 adapters/rollie/src/queries.ts create mode 100644 adapters/rollie/tsconfig.json diff --git a/adapters/rollie/hourly_blocks.csv b/adapters/rollie/hourly_blocks.csv new file mode 100644 index 0000000..f0407f9 --- /dev/null +++ b/adapters/rollie/hourly_blocks.csv @@ -0,0 +1,2 @@ +number,timestamp +7043812,1719856798 diff --git a/adapters/rollie/package.json b/adapters/rollie/package.json new file mode 100644 index 0000000..3f32430 --- /dev/null +++ b/adapters/rollie/package.json @@ -0,0 +1,27 @@ +{ + "name": "rollie-script", + "version": "1.0.0", + "description": "", + "main": "src/index.ts", + "scripts": { + "start": "ts-node src/index.ts", + "test": "echo \"Error: no test specified\" && exit 1" + }, + "keywords": [], + "author": "", + "license": "ISC", + "dependencies": { + "@types/lodash": "^4.17.5", + "csv-parser": "^3.0.0", + "fast-csv": "^5.0.1", + "json-2-csv": "^5.5.1", + "lodash": "^4.17.21", + "viem": "^2.14.0" + }, + "devDependencies": { + "fs": "^0.0.1-security", + "path": "^0.12.7", + "ts-node": "^10.9.2", + "typescript": "^5.4.5" + } +} diff --git a/adapters/rollie/src/abi/PairInfo.ts b/adapters/rollie/src/abi/PairInfo.ts new file mode 100644 index 0000000..8ea2556 --- /dev/null +++ b/adapters/rollie/src/abi/PairInfo.ts @@ -0,0 +1,1681 @@ +const PairInfoABI = [ + { + "inputs": [], + "stateMutability": "nonpayable", + "type": "constructor" + }, + { + "anonymous": false, + "inputs": [ + { + "indexed": false, + "internalType": "uint256", + "name": "pairIndex", + "type": "uint256" + }, + { + "indexed": false, + "internalType": "int256", + "name": "valueLong", + "type": "int256" + }, + { + "indexed": false, + "internalType": "int256", + "name": "valueShort", + "type": "int256" + } + ], + "name": "AccFundingFeesStored", + "type": "event" + }, + { + "anonymous": false, + "inputs": [ + { + "indexed": false, + "internalType": "uint256", + "name": "pairIndex", + "type": "uint256" + }, + { + "indexed": false, + "internalType": "uint256", + "name": "value", + "type": "uint256" + } + ], + "name": "AccRolloverFeesStored", + "type": "event" + }, + { + "anonymous": false, + "inputs": [ + { + "indexed": false, + "internalType": "uint256", + "name": "index", + "type": "uint256" + }, + { + "indexed": false, + "internalType": "string", + "name": "name", + "type": "string" + } + ], + "name": "FeeAdded", + "type": "event" + }, + { + "anonymous": false, + "inputs": [ + { + "indexed": false, + "internalType": "uint256", + "name": "index", + "type": "uint256" + } + ], + "name": "FeeUpdated", + "type": "event" + }, + { + "anonymous": false, + "inputs": [ + { + "indexed": false, + "internalType": "uint256", + "name": "index", + "type": "uint256" + }, + { + "indexed": false, + "internalType": "string", + "name": "name", + "type": "string" + } + ], + "name": "GroupAdded", + "type": "event" + }, + { + "anonymous": false, + "inputs": [ + { + "indexed": false, + "internalType": "uint256", + "name": "index", + "type": "uint256" + } + ], + "name": "GroupUpdated", + "type": "event" + }, + { + "anonymous": false, + "inputs": [ + { + "indexed": false, + "internalType": "uint8", + "name": "version", + "type": "uint8" + } + ], + "name": "Initialized", + "type": "event" + }, + { + "anonymous": false, + "inputs": [ + { + "indexed": true, + "internalType": "address", + "name": "previousOwner", + "type": "address" + }, + { + "indexed": true, + "internalType": "address", + "name": "newOwner", + "type": "address" + } + ], + "name": "OwnershipTransferStarted", + "type": "event" + }, + { + "anonymous": false, + "inputs": [ + { + "indexed": true, + "internalType": "address", + "name": "previousOwner", + "type": "address" + }, + { + "indexed": true, + "internalType": "address", + "name": "newOwner", + "type": "address" + } + ], + "name": "OwnershipTransferred", + "type": "event" + }, + { + "anonymous": false, + "inputs": [ + { + "indexed": false, + "internalType": "uint256", + "name": "index", + "type": "uint256" + }, + { + "indexed": false, + "internalType": "string", + "name": "pairName", + "type": "string" + } + ], + "name": "PairAdded", + "type": "event" + }, + { + "anonymous": false, + "inputs": [ + { + "indexed": false, + "internalType": "uint256", + "name": "pairIndex", + "type": "uint256" + }, + { + "components": [ + { + "internalType": "uint256", + "name": "onePercentDepthAbove", + "type": "uint256" + }, + { + "internalType": "uint256", + "name": "onePercentDepthBelow", + "type": "uint256" + }, + { + "internalType": "uint256", + "name": "rolloverFeePerBlockP", + "type": "uint256" + }, + { + "internalType": "uint256", + "name": "fundingFeePerBlockP", + "type": "uint256" + } + ], + "indexed": false, + "internalType": "struct PairInfos.PairParams", + "name": "value", + "type": "tuple" + } + ], + "name": "PairParamsUpdated", + "type": "event" + }, + { + "anonymous": false, + "inputs": [ + { + "indexed": false, + "internalType": "uint256", + "name": "index", + "type": "uint256" + } + ], + "name": "PairUpdated", + "type": "event" + }, + { + "anonymous": false, + "inputs": [ + { + "indexed": false, + "internalType": "uint256", + "name": "pairIndex", + "type": "uint256" + }, + { + "components": [ + { + "internalType": "uint256", + "name": "maxOI", + "type": "uint256" + }, + { + "internalType": "uint256", + "name": "maxNetOI", + "type": "uint256" + } + ], + "indexed": false, + "internalType": "struct PairInfos.InterestLimt", + "name": "_limit", + "type": "tuple" + } + ], + "name": "SetInterestLimt", + "type": "event" + }, + { + "anonymous": false, + "inputs": [ + { + "indexed": false, + "internalType": "address", + "name": "_keeper", + "type": "address" + } + ], + "name": "SetKeeper", + "type": "event" + }, + { + "anonymous": false, + "inputs": [ + { + "indexed": false, + "internalType": "contract ITradingStorage", + "name": "_tradingStorage", + "type": "address" + } + ], + "name": "SetStorage", + "type": "event" + }, + { + "inputs": [], + "name": "acceptOwnership", + "outputs": [], + "stateMutability": "nonpayable", + "type": "function" + }, + { + "inputs": [ + { + "components": [ + { + "internalType": "string", + "name": "name", + "type": "string" + }, + { + "internalType": "uint256", + "name": "openFeeP", + "type": "uint256" + }, + { + "internalType": "uint256", + "name": "closeFeeP", + "type": "uint256" + } + ], + "internalType": "struct PairInfos.Fee", + "name": "_fee", + "type": "tuple" + } + ], + "name": "addFee", + "outputs": [], + "stateMutability": "nonpayable", + "type": "function" + }, + { + "inputs": [ + { + "components": [ + { + "internalType": "string", + "name": "name", + "type": "string" + }, + { + "internalType": "bool", + "name": "guaranteedSlEnable", + "type": "bool" + }, + { + "internalType": "uint256", + "name": "maxCollateralP", + "type": "uint256" + } + ], + "internalType": "struct PairInfos.Group", + "name": "_group", + "type": "tuple" + } + ], + "name": "addGroup", + "outputs": [], + "stateMutability": "nonpayable", + "type": "function" + }, + { + "inputs": [ + { + "components": [ + { + "internalType": "string", + "name": "pairName", + "type": "string" + }, + { + "internalType": "bytes32", + "name": "feedId", + "type": "bytes32" + }, + { + "internalType": "uint256", + "name": "spreadP", + "type": "uint256" + }, + { + "internalType": "uint256", + "name": "feeIndex", + "type": "uint256" + }, + { + "internalType": "uint256", + "name": "groupIndex", + "type": "uint256" + }, + { + "internalType": "uint256", + "name": "maxSpread", + "type": "uint256" + }, + { + "internalType": "uint256", + "name": "minLeverage", + "type": "uint256" + }, + { + "internalType": "uint256", + "name": "maxLeverage", + "type": "uint256" + }, + { + "internalType": "uint256", + "name": "minMagin", + "type": "uint256" + }, + { + "internalType": "uint256", + "name": "minLevPos", + "type": "uint256" + }, + { + "internalType": "uint256", + "name": "maxLevPos", + "type": "uint256" + }, + { + "internalType": "bool", + "name": "priceRollover", + "type": "bool" + } + ], + "internalType": "struct PairInfos.Pair[]", + "name": "_pairs", + "type": "tuple[]" + } + ], + "name": "addPairs", + "outputs": [], + "stateMutability": "nonpayable", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "uint256", + "name": "orderId", + "type": "uint256" + } + ], + "name": "delTradeInitialAccFees", + "outputs": [], + "stateMutability": "nonpayable", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "uint256", + "name": "", + "type": "uint256" + } + ], + "name": "fees", + "outputs": [ + { + "internalType": "string", + "name": "name", + "type": "string" + }, + { + "internalType": "uint256", + "name": "openFeeP", + "type": "uint256" + }, + { + "internalType": "uint256", + "name": "closeFeeP", + "type": "uint256" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [], + "name": "feesCount", + "outputs": [ + { + "internalType": "uint256", + "name": "", + "type": "uint256" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "uint256", + "name": "orderId", + "type": "uint256" + }, + { + "internalType": "uint256", + "name": "pairIndex", + "type": "uint256" + }, + { + "internalType": "uint256", + "name": "position", + "type": "uint256" + }, + { + "internalType": "bool", + "name": "long", + "type": "bool" + } + ], + "name": "getFundingAndRolloverFee", + "outputs": [ + { + "internalType": "int256", + "name": "fundingFee", + "type": "int256" + }, + { + "internalType": "uint256", + "name": "rolloverFee", + "type": "uint256" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "uint256", + "name": "pairIndex", + "type": "uint256" + } + ], + "name": "getPendingAccFundingFees", + "outputs": [ + { + "internalType": "int256", + "name": "valueLong", + "type": "int256" + }, + { + "internalType": "int256", + "name": "valueShort", + "type": "int256" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "uint256", + "name": "pairIndex", + "type": "uint256" + } + ], + "name": "getPendingAccRolloverFees", + "outputs": [ + { + "internalType": "uint256", + "name": "", + "type": "uint256" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "uint256", + "name": "orderId", + "type": "uint256" + }, + { + "internalType": "uint256", + "name": "pairIndex", + "type": "uint256" + }, + { + "internalType": "uint256", + "name": "openPrice", + "type": "uint256" + }, + { + "internalType": "uint256", + "name": "collateral", + "type": "uint256" + }, + { + "internalType": "uint256", + "name": "leverage", + "type": "uint256" + }, + { + "internalType": "bool", + "name": "long", + "type": "bool" + } + ], + "name": "getTradeLiquidationPrice", + "outputs": [ + { + "internalType": "uint256", + "name": "", + "type": "uint256" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "uint256", + "name": "openPrice", + "type": "uint256" + }, + { + "internalType": "uint256", + "name": "pairIndex", + "type": "uint256" + }, + { + "internalType": "bool", + "name": "long", + "type": "bool" + }, + { + "internalType": "uint256", + "name": "tradeOpenInterest", + "type": "uint256" + } + ], + "name": "getTradePriceImpact", + "outputs": [ + { + "internalType": "uint256", + "name": "priceAfterImpact", + "type": "uint256" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "uint256", + "name": "", + "type": "uint256" + } + ], + "name": "groups", + "outputs": [ + { + "internalType": "string", + "name": "name", + "type": "string" + }, + { + "internalType": "bool", + "name": "guaranteedSlEnable", + "type": "bool" + }, + { + "internalType": "uint256", + "name": "maxCollateralP", + "type": "uint256" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "uint256", + "name": "", + "type": "uint256" + } + ], + "name": "groupsCollaterals", + "outputs": [ + { + "internalType": "uint256", + "name": "long", + "type": "uint256" + }, + { + "internalType": "uint256", + "name": "short", + "type": "uint256" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [], + "name": "groupsCount", + "outputs": [ + { + "internalType": "uint256", + "name": "", + "type": "uint256" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "uint256", + "name": "_pairIndex", + "type": "uint256" + } + ], + "name": "guaranteedSlEnable", + "outputs": [ + { + "internalType": "bool", + "name": "", + "type": "bool" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "contract ITradingStorage", + "name": "_tradingStorage", + "type": "address" + }, + { + "internalType": "address", + "name": "_keeper", + "type": "address" + }, + { + "internalType": "address", + "name": "_owner", + "type": "address" + } + ], + "name": "initialize", + "outputs": [], + "stateMutability": "nonpayable", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "uint256", + "name": "", + "type": "uint256" + } + ], + "name": "interestLimts", + "outputs": [ + { + "internalType": "uint256", + "name": "maxOI", + "type": "uint256" + }, + { + "internalType": "uint256", + "name": "maxNetOI", + "type": "uint256" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "uint256", + "name": "pairIndex", + "type": "uint256" + }, + { + "internalType": "uint256", + "name": "collateral", + "type": "uint256" + }, + { + "internalType": "bool", + "name": "long", + "type": "bool" + } + ], + "name": "isExceedGroupsCollateralLimit", + "outputs": [ + { + "internalType": "bool", + "name": "", + "type": "bool" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "uint256", + "name": "pairIndex", + "type": "uint256" + }, + { + "internalType": "uint256", + "name": "position", + "type": "uint256" + }, + { + "internalType": "bool", + "name": "isLong", + "type": "bool" + } + ], + "name": "isInPairInterestLimit", + "outputs": [ + { + "internalType": "bool", + "name": "", + "type": "bool" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "string", + "name": "", + "type": "string" + } + ], + "name": "isPairListed", + "outputs": [ + { + "internalType": "bool", + "name": "", + "type": "bool" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [], + "name": "keeper", + "outputs": [ + { + "internalType": "address", + "name": "", + "type": "address" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "uint256", + "name": "pairIndex", + "type": "uint256" + }, + { + "internalType": "uint256", + "name": "margin", + "type": "uint256" + }, + { + "internalType": "uint256", + "name": "leverage", + "type": "uint256" + } + ], + "name": "legalPositionSizeAndLeverage", + "outputs": [ + { + "internalType": "bool", + "name": "", + "type": "bool" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [], + "name": "occupiedCollateral", + "outputs": [ + { + "internalType": "uint256", + "name": "", + "type": "uint256" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "uint256", + "name": "", + "type": "uint256" + } + ], + "name": "openInterests", + "outputs": [ + { + "internalType": "uint256", + "name": "long", + "type": "uint256" + }, + { + "internalType": "uint256", + "name": "short", + "type": "uint256" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [], + "name": "owner", + "outputs": [ + { + "internalType": "address", + "name": "", + "type": "address" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "uint256", + "name": "_pairIndex", + "type": "uint256" + } + ], + "name": "pairCloseFeeP", + "outputs": [ + { + "internalType": "uint256", + "name": "", + "type": "uint256" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "uint256", + "name": "_pairIndex", + "type": "uint256" + } + ], + "name": "pairFeedId", + "outputs": [ + { + "internalType": "bytes32", + "name": "", + "type": "bytes32" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "uint256", + "name": "", + "type": "uint256" + } + ], + "name": "pairFundingFees", + "outputs": [ + { + "internalType": "int256", + "name": "accPerOiLong", + "type": "int256" + }, + { + "internalType": "int256", + "name": "accPerOiShort", + "type": "int256" + }, + { + "internalType": "uint256", + "name": "lastUpdateBlock", + "type": "uint256" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "uint256", + "name": "_pairIndex", + "type": "uint256" + } + ], + "name": "pairOpenFeeP", + "outputs": [ + { + "internalType": "uint256", + "name": "", + "type": "uint256" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "uint256", + "name": "", + "type": "uint256" + } + ], + "name": "pairParams", + "outputs": [ + { + "internalType": "uint256", + "name": "onePercentDepthAbove", + "type": "uint256" + }, + { + "internalType": "uint256", + "name": "onePercentDepthBelow", + "type": "uint256" + }, + { + "internalType": "uint256", + "name": "rolloverFeePerBlockP", + "type": "uint256" + }, + { + "internalType": "uint256", + "name": "fundingFeePerBlockP", + "type": "uint256" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "uint256", + "name": "", + "type": "uint256" + } + ], + "name": "pairRolloverFees", + "outputs": [ + { + "internalType": "uint256", + "name": "accPerCollateral", + "type": "uint256" + }, + { + "internalType": "uint256", + "name": "lastUpdateBlock", + "type": "uint256" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "uint256", + "name": "_pairIndex", + "type": "uint256" + } + ], + "name": "pairSpreadP", + "outputs": [ + { + "internalType": "uint256", + "name": "", + "type": "uint256" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "uint256", + "name": "", + "type": "uint256" + } + ], + "name": "pairs", + "outputs": [ + { + "internalType": "string", + "name": "pairName", + "type": "string" + }, + { + "internalType": "bytes32", + "name": "feedId", + "type": "bytes32" + }, + { + "internalType": "uint256", + "name": "spreadP", + "type": "uint256" + }, + { + "internalType": "uint256", + "name": "feeIndex", + "type": "uint256" + }, + { + "internalType": "uint256", + "name": "groupIndex", + "type": "uint256" + }, + { + "internalType": "uint256", + "name": "maxSpread", + "type": "uint256" + }, + { + "internalType": "uint256", + "name": "minLeverage", + "type": "uint256" + }, + { + "internalType": "uint256", + "name": "maxLeverage", + "type": "uint256" + }, + { + "internalType": "uint256", + "name": "minMagin", + "type": "uint256" + }, + { + "internalType": "uint256", + "name": "minLevPos", + "type": "uint256" + }, + { + "internalType": "uint256", + "name": "maxLevPos", + "type": "uint256" + }, + { + "internalType": "bool", + "name": "priceRollover", + "type": "bool" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [], + "name": "pairsCount", + "outputs": [ + { + "internalType": "uint256", + "name": "", + "type": "uint256" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [], + "name": "pendingOwner", + "outputs": [ + { + "internalType": "address", + "name": "", + "type": "address" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "uint256", + "name": "_pairIndex", + "type": "uint256" + } + ], + "name": "priceRollover", + "outputs": [ + { + "internalType": "bool", + "name": "", + "type": "bool" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [], + "name": "refreshTrading", + "outputs": [], + "stateMutability": "nonpayable", + "type": "function" + }, + { + "inputs": [], + "name": "renounceOwnership", + "outputs": [], + "stateMutability": "nonpayable", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "uint256", + "name": "pairIndex", + "type": "uint256" + }, + { + "components": [ + { + "internalType": "uint256", + "name": "maxOI", + "type": "uint256" + }, + { + "internalType": "uint256", + "name": "maxNetOI", + "type": "uint256" + } + ], + "internalType": "struct PairInfos.InterestLimt", + "name": "_limit", + "type": "tuple" + } + ], + "name": "setInterestLimt", + "outputs": [], + "stateMutability": "nonpayable", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "address", + "name": "_keeper", + "type": "address" + } + ], + "name": "setKeeper", + "outputs": [], + "stateMutability": "nonpayable", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "uint256[]", + "name": "indices", + "type": "uint256[]" + }, + { + "components": [ + { + "internalType": "uint256", + "name": "onePercentDepthAbove", + "type": "uint256" + }, + { + "internalType": "uint256", + "name": "onePercentDepthBelow", + "type": "uint256" + }, + { + "internalType": "uint256", + "name": "rolloverFeePerBlockP", + "type": "uint256" + }, + { + "internalType": "uint256", + "name": "fundingFeePerBlockP", + "type": "uint256" + } + ], + "internalType": "struct PairInfos.PairParams[]", + "name": "values", + "type": "tuple[]" + } + ], + "name": "setPairParams", + "outputs": [], + "stateMutability": "nonpayable", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "contract ITradingStorage", + "name": "_tradingStorage", + "type": "address" + } + ], + "name": "setStorage", + "outputs": [], + "stateMutability": "nonpayable", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "uint256", + "name": "orderId", + "type": "uint256" + }, + { + "internalType": "uint256", + "name": "pairIndex", + "type": "uint256" + }, + { + "internalType": "bool", + "name": "long", + "type": "bool" + } + ], + "name": "storeTradeInitialAccFees", + "outputs": [], + "stateMutability": "nonpayable", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "uint256", + "name": "", + "type": "uint256" + } + ], + "name": "tradeInitialAccFees", + "outputs": [ + { + "internalType": "uint256", + "name": "rollover", + "type": "uint256" + }, + { + "internalType": "int256", + "name": "funding", + "type": "int256" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [], + "name": "trading", + "outputs": [ + { + "internalType": "address", + "name": "", + "type": "address" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [], + "name": "tradingStorage", + "outputs": [ + { + "internalType": "contract ITradingStorage", + "name": "", + "type": "address" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "address", + "name": "newOwner", + "type": "address" + } + ], + "name": "transferOwnership", + "outputs": [], + "stateMutability": "nonpayable", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "uint256", + "name": "_id", + "type": "uint256" + }, + { + "components": [ + { + "internalType": "string", + "name": "name", + "type": "string" + }, + { + "internalType": "uint256", + "name": "openFeeP", + "type": "uint256" + }, + { + "internalType": "uint256", + "name": "closeFeeP", + "type": "uint256" + } + ], + "internalType": "struct PairInfos.Fee", + "name": "_fee", + "type": "tuple" + } + ], + "name": "updateFee", + "outputs": [], + "stateMutability": "nonpayable", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "uint256", + "name": "_id", + "type": "uint256" + }, + { + "components": [ + { + "internalType": "string", + "name": "name", + "type": "string" + }, + { + "internalType": "bool", + "name": "guaranteedSlEnable", + "type": "bool" + }, + { + "internalType": "uint256", + "name": "maxCollateralP", + "type": "uint256" + } + ], + "internalType": "struct PairInfos.Group", + "name": "_group", + "type": "tuple" + } + ], + "name": "updateGroup", + "outputs": [], + "stateMutability": "nonpayable", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "uint256", + "name": "_pairIndex", + "type": "uint256" + }, + { + "internalType": "uint256", + "name": "_collateral", + "type": "uint256" + }, + { + "internalType": "bool", + "name": "_long", + "type": "bool" + }, + { + "internalType": "bool", + "name": "_increase", + "type": "bool" + } + ], + "name": "updateGroupCollateral", + "outputs": [], + "stateMutability": "nonpayable", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "uint256", + "name": "_pairIndex", + "type": "uint256" + }, + { + "internalType": "uint256", + "name": "_amount", + "type": "uint256" + }, + { + "internalType": "bool", + "name": "_long", + "type": "bool" + }, + { + "internalType": "bool", + "name": "_increase", + "type": "bool" + } + ], + "name": "updateOpenInterest", + "outputs": [], + "stateMutability": "nonpayable", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "uint256", + "name": "_pairIndex", + "type": "uint256" + }, + { + "components": [ + { + "internalType": "string", + "name": "pairName", + "type": "string" + }, + { + "internalType": "bytes32", + "name": "feedId", + "type": "bytes32" + }, + { + "internalType": "uint256", + "name": "spreadP", + "type": "uint256" + }, + { + "internalType": "uint256", + "name": "feeIndex", + "type": "uint256" + }, + { + "internalType": "uint256", + "name": "groupIndex", + "type": "uint256" + }, + { + "internalType": "uint256", + "name": "maxSpread", + "type": "uint256" + }, + { + "internalType": "uint256", + "name": "minLeverage", + "type": "uint256" + }, + { + "internalType": "uint256", + "name": "maxLeverage", + "type": "uint256" + }, + { + "internalType": "uint256", + "name": "minMagin", + "type": "uint256" + }, + { + "internalType": "uint256", + "name": "minLevPos", + "type": "uint256" + }, + { + "internalType": "uint256", + "name": "maxLevPos", + "type": "uint256" + }, + { + "internalType": "bool", + "name": "priceRollover", + "type": "bool" + } + ], + "internalType": "struct PairInfos.Pair", + "name": "_pair", + "type": "tuple" + } + ], + "name": "updatePair", + "outputs": [], + "stateMutability": "nonpayable", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "uint256", + "name": "pairIndex", + "type": "uint256" + }, + { + "internalType": "enum TradeType", + "name": "tradeType", + "type": "uint8" + }, + { + "internalType": "uint256", + "name": "limit", + "type": "uint256" + }, + { + "internalType": "uint256", + "name": "current", + "type": "uint256" + }, + { + "internalType": "bool", + "name": "isLong", + "type": "bool" + } + ], + "name": "withinMaxSpread", + "outputs": [ + { + "internalType": "bool", + "name": "result", + "type": "bool" + } + ], + "stateMutability": "view", + "type": "function" + } +] +; + + +export default PairInfoABI; diff --git a/adapters/rollie/src/abi/TradingVault.ts b/adapters/rollie/src/abi/TradingVault.ts new file mode 100644 index 0000000..7a8ff7b --- /dev/null +++ b/adapters/rollie/src/abi/TradingVault.ts @@ -0,0 +1,1009 @@ +const TradingVaultABI = [ + { + "inputs": [ + { + "internalType": "string", + "name": "_name", + "type": "string" + }, + { + "internalType": "string", + "name": "_symbol", + "type": "string" + } + ], + "stateMutability": "nonpayable", + "type": "constructor" + }, + { + "anonymous": false, + "inputs": [ + { + "indexed": true, + "internalType": "address", + "name": "_allowed", + "type": "address" + }, + { + "indexed": false, + "internalType": "bool", + "name": "status", + "type": "bool" + } + ], + "name": "AllowedSet", + "type": "event" + }, + { + "anonymous": false, + "inputs": [ + { + "indexed": true, + "internalType": "address", + "name": "sender", + "type": "address" + }, + { + "indexed": false, + "internalType": "bool", + "name": "status", + "type": "bool" + } + ], + "name": "AllowedToInteractSet", + "type": "event" + }, + { + "anonymous": false, + "inputs": [ + { + "indexed": true, + "internalType": "address", + "name": "owner", + "type": "address" + }, + { + "indexed": true, + "internalType": "address", + "name": "spender", + "type": "address" + }, + { + "indexed": false, + "internalType": "uint256", + "name": "value", + "type": "uint256" + } + ], + "name": "Approval", + "type": "event" + }, + { + "anonymous": false, + "inputs": [ + { + "indexed": false, + "internalType": "address", + "name": "trader", + "type": "address" + }, + { + "indexed": false, + "internalType": "uint256", + "name": "amount", + "type": "uint256" + }, + { + "indexed": false, + "internalType": "uint256", + "name": "newCurrentBalance", + "type": "uint256" + } + ], + "name": "Claimed", + "type": "event" + }, + { + "anonymous": false, + "inputs": [ + { + "indexed": false, + "internalType": "address", + "name": "caller", + "type": "address" + }, + { + "indexed": false, + "internalType": "uint256", + "name": "amount", + "type": "uint256" + }, + { + "indexed": false, + "internalType": "uint256", + "name": "newCurrentBalance", + "type": "uint256" + }, + { + "indexed": false, + "internalType": "uint256", + "name": "shares", + "type": "uint256" + } + ], + "name": "Deposited", + "type": "event" + }, + { + "anonymous": false, + "inputs": [ + { + "indexed": false, + "internalType": "uint256", + "name": "_amount", + "type": "uint256" + } + ], + "name": "DistributeReward", + "type": "event" + }, + { + "anonymous": false, + "inputs": [ + { + "indexed": true, + "internalType": "address", + "name": "previousOwner", + "type": "address" + }, + { + "indexed": true, + "internalType": "address", + "name": "newOwner", + "type": "address" + } + ], + "name": "OwnershipTransferStarted", + "type": "event" + }, + { + "anonymous": false, + "inputs": [ + { + "indexed": true, + "internalType": "address", + "name": "previousOwner", + "type": "address" + }, + { + "indexed": true, + "internalType": "address", + "name": "newOwner", + "type": "address" + } + ], + "name": "OwnershipTransferred", + "type": "event" + }, + { + "anonymous": false, + "inputs": [ + { + "indexed": false, + "internalType": "address", + "name": "caller", + "type": "address" + }, + { + "indexed": false, + "internalType": "address", + "name": "trader", + "type": "address" + }, + { + "indexed": false, + "internalType": "uint256", + "name": "UstandardTokenAmount", + "type": "uint256" + }, + { + "indexed": false, + "internalType": "uint256", + "name": "vaultFeeUSDT", + "type": "uint256" + }, + { + "indexed": false, + "internalType": "uint256", + "name": "newCurrentBalance", + "type": "uint256" + } + ], + "name": "ReceivedFromTrader", + "type": "event" + }, + { + "anonymous": false, + "inputs": [ + { + "indexed": false, + "internalType": "address", + "name": "caller", + "type": "address" + }, + { + "indexed": false, + "internalType": "address", + "name": "trader", + "type": "address" + }, + { + "indexed": false, + "internalType": "uint256", + "name": "amount", + "type": "uint256" + }, + { + "indexed": false, + "internalType": "uint256", + "name": "newCurrentBalance", + "type": "uint256" + } + ], + "name": "Sent", + "type": "event" + }, + { + "anonymous": false, + "inputs": [ + { + "indexed": false, + "internalType": "address", + "name": "_pairInfo", + "type": "address" + } + ], + "name": "SetPairInfo", + "type": "event" + }, + { + "anonymous": false, + "inputs": [ + { + "indexed": true, + "internalType": "address", + "name": "usdt", + "type": "address" + } + ], + "name": "StandardTokenSet", + "type": "event" + }, + { + "anonymous": false, + "inputs": [ + { + "indexed": false, + "internalType": "address", + "name": "caller", + "type": "address" + }, + { + "indexed": false, + "internalType": "address", + "name": "trader", + "type": "address" + }, + { + "indexed": false, + "internalType": "uint256", + "name": "amount", + "type": "uint256" + }, + { + "indexed": false, + "internalType": "uint256", + "name": "currentBalance", + "type": "uint256" + } + ], + "name": "ToClaim", + "type": "event" + }, + { + "anonymous": false, + "inputs": [ + { + "indexed": true, + "internalType": "address", + "name": "from", + "type": "address" + }, + { + "indexed": true, + "internalType": "address", + "name": "to", + "type": "address" + }, + { + "indexed": false, + "internalType": "uint256", + "name": "value", + "type": "uint256" + } + ], + "name": "Transfer", + "type": "event" + }, + { + "anonymous": false, + "inputs": [ + { + "indexed": true, + "internalType": "uint256", + "name": "_withdrawTimelock", + "type": "uint256" + } + ], + "name": "WithdrawTimelockSet", + "type": "event" + }, + { + "anonymous": false, + "inputs": [ + { + "indexed": false, + "internalType": "address", + "name": "caller", + "type": "address" + }, + { + "indexed": false, + "internalType": "uint256", + "name": "amount", + "type": "uint256" + }, + { + "indexed": false, + "internalType": "uint256", + "name": "newCurrentBalance", + "type": "uint256" + }, + { + "indexed": false, + "internalType": "uint256", + "name": "shares", + "type": "uint256" + } + ], + "name": "Withdrawn", + "type": "event" + }, + { + "inputs": [], + "name": "acceptOwnership", + "outputs": [], + "stateMutability": "nonpayable", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "address", + "name": "owner", + "type": "address" + }, + { + "internalType": "address", + "name": "spender", + "type": "address" + } + ], + "name": "allowance", + "outputs": [ + { + "internalType": "uint256", + "name": "", + "type": "uint256" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "address", + "name": "", + "type": "address" + } + ], + "name": "allowed", + "outputs": [ + { + "internalType": "bool", + "name": "", + "type": "bool" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "address", + "name": "", + "type": "address" + } + ], + "name": "allowedToInteract", + "outputs": [ + { + "internalType": "bool", + "name": "", + "type": "bool" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "address", + "name": "spender", + "type": "address" + }, + { + "internalType": "uint256", + "name": "amount", + "type": "uint256" + } + ], + "name": "approve", + "outputs": [ + { + "internalType": "bool", + "name": "", + "type": "bool" + } + ], + "stateMutability": "nonpayable", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "address", + "name": "account", + "type": "address" + } + ], + "name": "balanceOf", + "outputs": [ + { + "internalType": "uint256", + "name": "", + "type": "uint256" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [], + "name": "claim", + "outputs": [], + "stateMutability": "nonpayable", + "type": "function" + }, + { + "inputs": [], + "name": "currentBalance", + "outputs": [ + { + "internalType": "uint256", + "name": "", + "type": "uint256" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [], + "name": "decimals", + "outputs": [ + { + "internalType": "uint8", + "name": "", + "type": "uint8" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "address", + "name": "spender", + "type": "address" + }, + { + "internalType": "uint256", + "name": "subtractedValue", + "type": "uint256" + } + ], + "name": "decreaseAllowance", + "outputs": [ + { + "internalType": "bool", + "name": "", + "type": "bool" + } + ], + "stateMutability": "nonpayable", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "uint256", + "name": "_amount", + "type": "uint256" + }, + { + "internalType": "address", + "name": "_user", + "type": "address" + } + ], + "name": "deposit", + "outputs": [], + "stateMutability": "nonpayable", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "address", + "name": "_user", + "type": "address" + } + ], + "name": "depositAll", + "outputs": [], + "stateMutability": "nonpayable", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "uint256", + "name": "_amount", + "type": "uint256" + }, + { + "internalType": "bool", + "name": "_send", + "type": "bool" + } + ], + "name": "distributeReward", + "outputs": [], + "stateMutability": "nonpayable", + "type": "function" + }, + { + "inputs": [], + "name": "getPricePerFullShare", + "outputs": [ + { + "internalType": "uint256", + "name": "", + "type": "uint256" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "address", + "name": "spender", + "type": "address" + }, + { + "internalType": "uint256", + "name": "addedValue", + "type": "uint256" + } + ], + "name": "increaseAllowance", + "outputs": [ + { + "internalType": "bool", + "name": "", + "type": "bool" + } + ], + "stateMutability": "nonpayable", + "type": "function" + }, + { + "inputs": [], + "name": "name", + "outputs": [ + { + "internalType": "string", + "name": "", + "type": "string" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [], + "name": "owner", + "outputs": [ + { + "internalType": "address", + "name": "", + "type": "address" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [], + "name": "pairInfo", + "outputs": [ + { + "internalType": "contract IPairInfos", + "name": "", + "type": "address" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [], + "name": "pendingOwner", + "outputs": [ + { + "internalType": "address", + "name": "", + "type": "address" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "address", + "name": "_trader", + "type": "address" + }, + { + "internalType": "uint256", + "name": "_amount", + "type": "uint256" + }, + { + "internalType": "uint256", + "name": "_vaultFee", + "type": "uint256" + }, + { + "internalType": "bool", + "name": "_send", + "type": "bool" + } + ], + "name": "receiveFromTrader", + "outputs": [], + "stateMutability": "nonpayable", + "type": "function" + }, + { + "inputs": [], + "name": "renounceOwnership", + "outputs": [], + "stateMutability": "nonpayable", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "address", + "name": "_trader", + "type": "address" + }, + { + "internalType": "uint256", + "name": "_amount", + "type": "uint256" + } + ], + "name": "sendToTrader", + "outputs": [], + "stateMutability": "nonpayable", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "address", + "name": "_allowed", + "type": "address" + }, + { + "internalType": "bool", + "name": "_status", + "type": "bool" + } + ], + "name": "setAllowed", + "outputs": [], + "stateMutability": "nonpayable", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "address", + "name": "_sender", + "type": "address" + }, + { + "internalType": "bool", + "name": "_status", + "type": "bool" + } + ], + "name": "setAllowedToInteract", + "outputs": [], + "stateMutability": "nonpayable", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "address", + "name": "_pairInfo", + "type": "address" + } + ], + "name": "setPairInfo", + "outputs": [], + "stateMutability": "nonpayable", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "address", + "name": "_standardToken", + "type": "address" + } + ], + "name": "setStandardToken", + "outputs": [], + "stateMutability": "nonpayable", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "uint256", + "name": "_withdrawTimelock", + "type": "uint256" + } + ], + "name": "setWithdrawTimelock", + "outputs": [], + "stateMutability": "nonpayable", + "type": "function" + }, + { + "inputs": [], + "name": "standardToken", + "outputs": [ + { + "internalType": "address", + "name": "", + "type": "address" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "address", + "name": "", + "type": "address" + } + ], + "name": "standardTokenToClaim", + "outputs": [ + { + "internalType": "uint256", + "name": "", + "type": "uint256" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [], + "name": "symbol", + "outputs": [ + { + "internalType": "string", + "name": "", + "type": "string" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [], + "name": "totalRewardsDistributed", + "outputs": [ + { + "internalType": "uint256", + "name": "", + "type": "uint256" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [], + "name": "totalSupply", + "outputs": [ + { + "internalType": "uint256", + "name": "", + "type": "uint256" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "address", + "name": "to", + "type": "address" + }, + { + "internalType": "uint256", + "name": "amount", + "type": "uint256" + } + ], + "name": "transfer", + "outputs": [ + { + "internalType": "bool", + "name": "", + "type": "bool" + } + ], + "stateMutability": "nonpayable", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "address", + "name": "from", + "type": "address" + }, + { + "internalType": "address", + "name": "to", + "type": "address" + }, + { + "internalType": "uint256", + "name": "amount", + "type": "uint256" + } + ], + "name": "transferFrom", + "outputs": [ + { + "internalType": "bool", + "name": "", + "type": "bool" + } + ], + "stateMutability": "nonpayable", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "address", + "name": "newOwner", + "type": "address" + } + ], + "name": "transferOwnership", + "outputs": [], + "stateMutability": "nonpayable", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "address", + "name": "", + "type": "address" + } + ], + "name": "userLastDepositTime", + "outputs": [ + { + "internalType": "uint256", + "name": "", + "type": "uint256" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "uint256", + "name": "_shares", + "type": "uint256" + } + ], + "name": "withdraw", + "outputs": [], + "stateMutability": "nonpayable", + "type": "function" + }, + { + "inputs": [], + "name": "withdrawAll", + "outputs": [], + "stateMutability": "nonpayable", + "type": "function" + }, + { + "inputs": [], + "name": "withdrawTimelock", + "outputs": [ + { + "internalType": "uint256", + "name": "", + "type": "uint256" + } + ], + "stateMutability": "view", + "type": "function" + } +] +; + +export default TradingVaultABI; diff --git a/adapters/rollie/src/index.ts b/adapters/rollie/src/index.ts new file mode 100644 index 0000000..f4f191e --- /dev/null +++ b/adapters/rollie/src/index.ts @@ -0,0 +1,511 @@ +import PairInfoABI from "./abi/PairInfo"; +import {scroll} from "viem/chains"; +import {createPublicClient, http, formatUnits, Abi, Address, fallback} from "viem"; +import {chunk} from "lodash"; +import {QueryAllUsers, QueryClosedTrades, QueryOpenTrades} from "./queries"; +import * as fs from "fs"; +import csv from "csv-parser"; +import { format } from 'fast-csv'; +import { write } from 'fast-csv'; +import { pipeline as streamPipeline } from 'stream'; +import * as converter from "json-2-csv"; + +console.log("converter", converter) + + +import TradingVaultABI from "./abi/TradingVault"; +import {containsNodeError} from "viem/utils"; +import {resolve} from "path"; +import convert from "lodash/fp/convert"; +const PairInfoAddress = "0xA852CE8B0BF3Bcd9191D6140a4627E7823c84848"; +const TradingVaultAddress = "0xA79E00e68549e91e5f0c27048F453b3D87ef6E3D" +const SubgrapnUrl = "https://api.studio.thegraph.com/query/76203/rollie-finance/0.0.2"; +const RPCs = [ + "https://scroll.blockpi.network/v1/rpc/public", + "https://scroll-mainnet.chainstacklabs.com", + "https://scroll.drpc.org" +]; +const USDC_DECIMALS = 6; +const RLP_DECIMALS = 6; +const PPS_DECIMALS = 18; +const LEVERAGE_DECIMALS = 18; +const BLOCK_TIME = 3000; +const FEE_DECIMALS = 10; + + + + +const blockCountPerHour = 3600 * 1000 / BLOCK_TIME; + +interface BlockData { + blockNumber: number; + blockTimestamp: number; +} + +const readBlocksFromCSV = async (filePath: string): Promise => { + const blocks: BlockData[] = []; + + await new Promise((resolve, reject) => { + fs.createReadStream(filePath) + .pipe(csv({ separator: ',' })) // Specify the separator as '\t' for TSV files + .on('data', (row) => { + console.log(row, 'row') + const blockNumber = parseInt(row.number, 10); + const blockTimestamp = parseInt(row.timestamp, 10); + if (!isNaN(blockNumber) && blockTimestamp) { + blocks.push({ blockNumber: blockNumber, blockTimestamp }); + } + }) + .on('end', () => { + resolve(); + }) + .on('error', (err) => { + reject(err); + }); + }); + + return blocks; +}; + + +const client = createPublicClient({ + chain: { + ...scroll, + contracts: { + multicall3: { + address: "0xcA11bde05977b3631167028862bE2a173976CA11" + } + } + }, + transport: fallback(RPCs.map((rpc) => http(rpc))), + batch: { + multicall: true, + } +}) + +const PairInfoContract = { + abi: PairInfoABI as any, + address: PairInfoAddress as Address, +} + +const TradingVaultContract = { + abi: TradingVaultABI as any, + address: TradingVaultAddress as Address, +} + +interface Pair { + funding_rate: { + long: string, + short: string, + }, + asset: string, +} + +async function getPairs(block: BlockData): Promise { + console.log("Start load pairs ...") + return await client.readContract({ + ...PairInfoContract, + functionName: "pairsCount", + args: [], + blockNumber: BigInt(block.blockNumber) + }).then((pairCount: any) => { + const calls = []; + for (let i = 0; i < parseInt(pairCount.toString()); i++) { + calls.push({ + ...PairInfoContract, + functionName: "pairs", + args: [i], + }); + calls.push({ + ...PairInfoContract, + functionName: "openInterests", + args: [i], + }) + calls.push({ + ...PairInfoContract, + functionName: "pairParams", + args: [i], + }) + } + return client.multicall({ + contracts: calls, + allowFailure: false, + blockNumber: BigInt(block.blockNumber) + }).then((results) => { + const pairInfos = chunk(results, 3); + console.log("Finish load pairs ...") + return pairInfos.map((pairInfo, index) => { + const info = pairInfo[0] as any; + const interests = pairInfo[1] as any; + const params = pairInfo[2] as any; + + const longOI = interests[0] as bigint; + const shortOI = interests[1] as bigint; + const fundingFeePerBlockPercent = params[3]; + + const longFundingFeePerHour = longOI === 0n + ? "0" + : formatUnits( + (longOI - shortOI) * fundingFeePerBlockPercent * BigInt(blockCountPerHour) / longOI * 100n, + FEE_DECIMALS, + ) + + const shortFundingFeePerHour = shortOI === 0n + ? "0" + : formatUnits( + (shortOI - longOI) * fundingFeePerBlockPercent * BigInt(blockCountPerHour) / shortOI * 100n, + FEE_DECIMALS, + ) + return { + funding_rate: { + long: longFundingFeePerHour, + short: shortFundingFeePerHour, + }, + asset: info[0] as string + } + }) + }) + }) +} + +interface User { + tradePrincipal: string + tradeCountGainRate: string + totalVolume: string + totalTrade: string + tinderCloseCount: string + profitAmount: string + openPrincipal: string + openPositionCount: string + openPosition: string + openCount: string + liquidationCount: string + liquidationAmount: string + limitCount: string + id: string + gainTradeCount: string + deposited: string + withdrawed: string + closeCount: string + amountGainRate: string + staked: string, +} + +async function getAllUser(lastRequest: User[], page: number = 0, block: BlockData): Promise { + console.log("Start load users ..."); + const res = await fetch(SubgrapnUrl, { + method: "POST", + headers: { + "Content-Type": "application/json" + }, + body: JSON.stringify({ + query: QueryAllUsers, + variables: { + skip: page * 1000, + block: block.blockNumber + } + }) + }) + const data = await res.json(); + const users = data.data.users || []; + + if (users.length < 1000) { + const allUsers = [...lastRequest, ...users] + console.log("Finish load users ..."); + // get all staked value + const calls = allUsers.map((item: User) => { + return { + ...TradingVaultContract, + functionName: "balanceOf", + args: [item.id], + } + }) + calls.push({ + ...TradingVaultContract, + functionName: "getPricePerFullShare", + args: [], + }) + console.log("Start load user tvls ..."); + const result = await client.multicall({ + contracts: calls, + allowFailure: false, + blockNumber: BigInt(block.blockNumber) + }) as unknown as bigint[]; + console.log("Finish load user tvls ..."); + const pps = result[result.length - 1]; + return allUsers.map((item, index) => { + const staked = formatUnits( + (result[index] * pps ) / (10n ** BigInt(PPS_DECIMALS)), + RLP_DECIMALS + ); + return { + ...item, + staked, + } + }); + } + return await getAllUser([...lastRequest, ...users], page + 1, block); +} + +interface OpenOrder { + rolloverFee: string; + openTime: string; + pairIndex: string; + openFee: string; + long: boolean; + leverage: string; + id: string; + fundingFee: string; + trader: string; + leftMargin: string; +} + +interface ClosedOrder { + closeFee: string; + closeMargin: string; + closePrice: string; + closeTime: string; + fundingFee: string; + id: string; + leverage: string; + long: boolean; + openPrice: string; + orderId: string; + pairIndex: string; + profit: string; + rolloverFee: string; + user: { + id: string; + } +} + +interface Result { + protocol: string; + block_number: string; + timestamp: string; + user_address: string; + market: string; + trade_pair_symbol: string; + daily_user_volume_usd: number; + funding_rate: string; + supplied_amount_usd: number; + open_shorts_usd: number; + open_longs_usd: number; + protocol_fees_usd: number; + users_fees_usd: number; + etl_timestamp: number; +} + +// const endTime = Math.ceil(Date.now() / 1000); +// const startTime = endTime - 24 * 3600; + +async function getLast24HOpenTrades(lastRequest: OpenOrder[], page: number = 0, block: BlockData): Promise { + const endTime = block.blockTimestamp; + const startTime = endTime - 24 * 3600; + console.log("Start load open trades ...") + const res = await fetch(SubgrapnUrl, { + method: "POST", + headers: { + "Content-Type": "application/json" + }, + body: JSON.stringify({ + query: QueryOpenTrades, + variables: { + skip: page * 1000, + start: startTime, + end: endTime, + } + }) + }) + const data = await res.json(); + const openTrades = data.data.openTrades || []; + + if (openTrades.length < 1000) { + console.log("Finish load open trades ...") + return [...lastRequest, ...openTrades]; + } + return await getLast24HOpenTrades([...lastRequest, ...openTrades], page + 1, block); +} + +async function getLast24HClosedTrades(lastRequest: ClosedOrder[], page: number = 0, block: BlockData): Promise { + const endTime = block.blockTimestamp; + const startTime = endTime - 24 * 3600; + console.log("Start load closed trades ...") + const res = await fetch(SubgrapnUrl, { + method: "POST", + headers: { + "Content-Type": "application/json" + }, + body: JSON.stringify({ + query: QueryClosedTrades, + variables: { + skip: page * 1000, + start: startTime, + end: endTime, + } + }) + }) + const data = await res.json(); + const closedOrders = data.data.closeHistories || []; + + if (closedOrders.length < 1000) { + console.log("Finish load closed trades ...") + return [...lastRequest, ...closedOrders]; + } + return await getLast24HClosedTrades([...lastRequest, ...closedOrders], page + 1, block); + +} + +const runDate = Date.now(); + +function generateSinglePairData(openOrders: OpenOrder[], closedOrders: ClosedOrder[]) { + let open_shorts = 0n; + let open_longs = 0n; + let protocol_fees = 0n; + let users_fees = 0n; + openOrders.forEach((order) => { + if (order.long) { + open_longs += BigInt(order.leftMargin) * BigInt(order.leverage); + } else { + open_shorts += BigInt(order.leftMargin) * BigInt(order.leverage); + } + protocol_fees += BigInt(order.openFee) * 300n / 1000n; + users_fees += BigInt(order.openFee) * 350n / 1000n; + }) + + closedOrders.forEach((order) => { + if (order.long) { + open_longs += BigInt(order.closeMargin) * BigInt(order.leverage); + } else { + open_shorts += BigInt(order.closeMargin) * BigInt(order.leverage); + } + protocol_fees += BigInt(order.closeFee) * 300n / 1000n; + users_fees += BigInt(order.closeFee) * 350n / 1000n + BigInt(order.rolloverFee) - BigInt(order.fundingFee); + }) + return { + open_shorts: Number(formatUnits(open_shorts, USDC_DECIMALS + LEVERAGE_DECIMALS)), + open_longs: Number(formatUnits(open_longs, USDC_DECIMALS + LEVERAGE_DECIMALS)), + protocol_fees: Number(formatUnits(protocol_fees, USDC_DECIMALS)), + users_fees: Number(formatUnits(users_fees, USDC_DECIMALS)), + volumes: Number(formatUnits(open_shorts + open_longs, USDC_DECIMALS + LEVERAGE_DECIMALS)), + } +} + +function generateUserData(user: User, openOrders: OpenOrder[], closedOrders: ClosedOrder[], pairs: Pair[], block: BlockData): Result[] { + const protocol = "rollie"; + const date = runDate; + const market = PairInfoAddress; + const trader_address = user.id; + return pairs.map((pair, index) => { + const pairData = generateSinglePairData( + openOrders.filter((order) => Number(order.pairIndex) === index), + closedOrders.filter((order) => Number(order.pairIndex) === index), + ) + return { + protocol, + etl_timestamp:date, + market, + trade_pair_symbol: pair.asset, + block_number: block.blockNumber.toString(10), + timestamp: block.blockTimestamp.toString(10), + user_address: trader_address, + daily_user_volume_usd: pairData.volumes, + funding_rate: JSON.stringify({ + long: pair.funding_rate.long, + short: pair.funding_rate.short, + }), + supplied_amount_usd: Number(user.staked), + open_shorts_usd: pairData.open_shorts, + open_longs_usd: pairData.open_longs, + protocol_fees_usd: pairData.protocol_fees, + users_fees_usd: pairData.users_fees, + } + }) +} + +async function getAllData(block: BlockData) { + return Promise.all([ + getPairs(block), + getAllUser([], undefined, block), + getLast24HOpenTrades([],undefined, block), + getLast24HClosedTrades([], undefined, block) + ]).then(([pairs, users, openTrades, closedTrades]) => { + const openOrderMap = openTrades.reduce((acc, cur) => { + const orders = acc[cur.trader] || []; + return { + ...acc, + [cur.trader]: [...orders, cur] + } + }, {} as { + [user: string]: OpenOrder[], + }) + const closedOrderMap = closedTrades.reduce((acc, cur) => { + const order = acc[cur.user.id] || []; + return { + ...acc, + [cur.user.id]: [...order, cur] + } + }, {} as { + [user: string]: ClosedOrder[], + }) + const result = users.reduce((acc, user) => { + const openOrders = openOrderMap[user.id] || []; + const closedOrders = closedOrderMap[user.id] || []; + const userData = generateUserData(user, openOrders, closedOrders, pairs, block); + return [ + ...acc, + ...userData, + ] + }, [] as Result[]); + // console.log("Start Write file ..."); + return result; + // fs.writeFile("./output/result.json", JSON.stringify(result), (err) => { + // if (!err) { + // console.log("Write file success, check result in ./output/result.json"); + // } else { + // console.log("Write Error!", err); + // } + // }); + }); +} + + +function main() { + readBlocksFromCSV(resolve("./") + "/hourly_blocks.csv").then(async (blocks) => { + let results: Result[] = []; + for (const block of blocks) { + try { + const blockRes = await getAllData(block); + results = results.concat(blockRes); + } catch (error) { + console.error(`An error occurred for block ${block}:`, error); + } + } + console.log("Start Write file ..."); + const csv = converter.json2csv(results); + const ws = fs.createWriteStream("outputData.csv"); + ws.on("finish", () => { + console.log("Write file success, check result in outputData.csv"); + }); + ws.on("error", (err) => { + console.log("Write Error!", err); + }); + ws.write(csv); + ws.end(); + // const ws = fs.createWriteStream(`outputData.csv`, { }); + // + // if (i % batchSize === 0 || i === blocks.length) { + // write(results, { headers: i === batchSize }) + // .pipe(ws) + // .on("finish", () => { + // console.log(`CSV file has been written.`); + // }); + // + // // Clear the accumulated CSV rows + // results.length = 0; + // } + // i += 1; + }); +} + +main(); diff --git a/adapters/rollie/src/queries.ts b/adapters/rollie/src/queries.ts new file mode 100644 index 0000000..05dc137 --- /dev/null +++ b/adapters/rollie/src/queries.ts @@ -0,0 +1,78 @@ + +export const QueryAllUsers = ` +query QueryAllUsers($skip: Int, $block: Int) { + users(first: 1000, skip: $skip, block: {number: $block}) { + tradePrincipal + tradeCountGainRate + totalVolume + totalTrade + tinderCloseCount + profitAmount + openPrincipal + openPositionCount + openPosition + openCount + liquidationCount + liquidationAmount + limitCount + id + gainTradeCount + deposited + withdrawed + closeCount + amountGainRate + } +} +` + +export const QueryOpenTrades = ` + query QueryOpenTrades($start: Int, $end: Int, $skip: Int) { + openTrades( + skip: $skip, + first: 1000, + where: { status: 0, tradeType: 0, openTime_gt: $start, openTime_lte: $end }, + orderBy: openTime, + orderDirection: desc + ) { + rolloverFee + openTime + pairIndex + openFee + long + leverage + id + fundingFee + trader + leftMargin + } + } +` + +export const QueryClosedTrades = ` + query fetchTradingHistory($skip: Int = 0, $start: Int, $end: Int) { + closeHistories( + where: {closeTime_gt: $start, closeTime_lte: $end}, + orderBy: closeTime + orderDirection: desc + skip: $skip + first: 1000 + ) { + closeFee + closeMargin + closePrice + closeTime + fundingFee + id + leverage + long + openPrice + orderId + pairIndex + profit + rolloverFee + user { + id + } + } + } +` diff --git a/adapters/rollie/tsconfig.json b/adapters/rollie/tsconfig.json new file mode 100644 index 0000000..3466896 --- /dev/null +++ b/adapters/rollie/tsconfig.json @@ -0,0 +1,109 @@ +{ + "compilerOptions": { + /* Visit https://aka.ms/tsconfig to read more about this file */ + + /* Projects */ + // "incremental": true, /* Save .tsbuildinfo files to allow for incremental compilation of projects. */ + // "composite": true, /* Enable constraints that allow a TypeScript project to be used with project references. */ + // "tsBuildInfoFile": "./.tsbuildinfo", /* Specify the path to .tsbuildinfo incremental compilation file. */ + // "disableSourceOfProjectReferenceRedirect": true, /* Disable preferring source files instead of declaration files when referencing composite projects. */ + // "disableSolutionSearching": true, /* Opt a project out of multi-project reference checking when editing. */ + // "disableReferencedProjectLoad": true, /* Reduce the number of projects loaded automatically by TypeScript. */ + + /* Language and Environment */ + "target": "es2020", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */ + // "lib": [], /* Specify a set of bundled library declaration files that describe the target runtime environment. */ + // "jsx": "preserve", /* Specify what JSX code is generated. */ + // "experimentalDecorators": true, /* Enable experimental support for legacy experimental decorators. */ + // "emitDecoratorMetadata": true, /* Emit design-type metadata for decorated declarations in source files. */ + // "jsxFactory": "", /* Specify the JSX factory function used when targeting React JSX emit, e.g. 'React.createElement' or 'h'. */ + // "jsxFragmentFactory": "", /* Specify the JSX Fragment reference used for fragments when targeting React JSX emit e.g. 'React.Fragment' or 'Fragment'. */ + // "jsxImportSource": "", /* Specify module specifier used to import the JSX factory functions when using 'jsx: react-jsx*'. */ + // "reactNamespace": "", /* Specify the object invoked for 'createElement'. This only applies when targeting 'react' JSX emit. */ + // "noLib": true, /* Disable including any library files, including the default lib.d.ts. */ + // "useDefineForClassFields": true, /* Emit ECMAScript-standard-compliant class fields. */ + // "moduleDetection": "auto", /* Control what method is used to detect module-format JS files. */ + + /* Modules */ + "module": "commonjs", /* Specify what module code is generated. */ + // "rootDir": "./", /* Specify the root folder within your source files. */ + // "moduleResolution": "node10", /* Specify how TypeScript looks up a file from a given module specifier. */ + // "baseUrl": "./", /* Specify the base directory to resolve non-relative module names. */ + // "paths": {}, /* Specify a set of entries that re-map imports to additional lookup locations. */ + // "rootDirs": [], /* Allow multiple folders to be treated as one when resolving modules. */ + // "typeRoots": [], /* Specify multiple folders that act like './node_modules/@types'. */ + // "types": [], /* Specify type package names to be included without being referenced in a source file. */ + // "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */ + // "moduleSuffixes": [], /* List of file name suffixes to search when resolving a module. */ + // "allowImportingTsExtensions": true, /* Allow imports to include TypeScript file extensions. Requires '--moduleResolution bundler' and either '--noEmit' or '--emitDeclarationOnly' to be set. */ + // "resolvePackageJsonExports": true, /* Use the package.json 'exports' field when resolving package imports. */ + // "resolvePackageJsonImports": true, /* Use the package.json 'imports' field when resolving imports. */ + // "customConditions": [], /* Conditions to set in addition to the resolver-specific defaults when resolving imports. */ + // "resolveJsonModule": true, /* Enable importing .json files. */ + // "allowArbitraryExtensions": true, /* Enable importing files with any extension, provided a declaration file is present. */ + // "noResolve": true, /* Disallow 'import's, 'require's or ''s from expanding the number of files TypeScript should add to a project. */ + + /* JavaScript Support */ + // "allowJs": true, /* Allow JavaScript files to be a part of your program. Use the 'checkJS' option to get errors from these files. */ + // "checkJs": true, /* Enable error reporting in type-checked JavaScript files. */ + // "maxNodeModuleJsDepth": 1, /* Specify the maximum folder depth used for checking JavaScript files from 'node_modules'. Only applicable with 'allowJs'. */ + + /* Emit */ + // "declaration": true, /* Generate .d.ts files from TypeScript and JavaScript files in your project. */ + // "declarationMap": true, /* Create sourcemaps for d.ts files. */ + // "emitDeclarationOnly": true, /* Only output d.ts files and not JavaScript files. */ + // "sourceMap": true, /* Create source map files for emitted JavaScript files. */ + // "inlineSourceMap": true, /* Include sourcemap files inside the emitted JavaScript. */ + // "outFile": "./", /* Specify a file that bundles all outputs into one JavaScript file. If 'declaration' is true, also designates a file that bundles all .d.ts output. */ + // "outDir": "./", /* Specify an output folder for all emitted files. */ + // "removeComments": true, /* Disable emitting comments. */ + // "noEmit": true, /* Disable emitting files from a compilation. */ + // "importHelpers": true, /* Allow importing helper functions from tslib once per project, instead of including them per-file. */ + // "importsNotUsedAsValues": "remove", /* Specify emit/checking behavior for imports that are only used for types. */ + // "downlevelIteration": true, /* Emit more compliant, but verbose and less performant JavaScript for iteration. */ + // "sourceRoot": "", /* Specify the root path for debuggers to find the reference source code. */ + // "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */ + // "inlineSources": true, /* Include source code in the sourcemaps inside the emitted JavaScript. */ + // "emitBOM": true, /* Emit a UTF-8 Byte Order Mark (BOM) in the beginning of output files. */ + // "newLine": "crlf", /* Set the newline character for emitting files. */ + // "stripInternal": true, /* Disable emitting declarations that have '@internal' in their JSDoc comments. */ + // "noEmitHelpers": true, /* Disable generating custom helper functions like '__extends' in compiled output. */ + // "noEmitOnError": true, /* Disable emitting files if any type checking errors are reported. */ + // "preserveConstEnums": true, /* Disable erasing 'const enum' declarations in generated code. */ + // "declarationDir": "./", /* Specify the output directory for generated declaration files. */ + // "preserveValueImports": true, /* Preserve unused imported values in the JavaScript output that would otherwise be removed. */ + + /* Interop Constraints */ + // "isolatedModules": true, /* Ensure that each file can be safely transpiled without relying on other imports. */ + // "verbatimModuleSyntax": true, /* Do not transform or elide any imports or exports not marked as type-only, ensuring they are written in the output file's format based on the 'module' setting. */ + // "allowSyntheticDefaultImports": true, /* Allow 'import x from y' when a module doesn't have a default export. */ + "esModuleInterop": true, /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */ + // "preserveSymlinks": true, /* Disable resolving symlinks to their realpath. This correlates to the same flag in node. */ + "forceConsistentCasingInFileNames": true, /* Ensure that casing is correct in imports. */ + + /* Type Checking */ + "strict": true, /* Enable all strict type-checking options. */ + // "noImplicitAny": true, /* Enable error reporting for expressions and declarations with an implied 'any' type. */ + // "strictNullChecks": true, /* When type checking, take into account 'null' and 'undefined'. */ + // "strictFunctionTypes": true, /* When assigning functions, check to ensure parameters and the return values are subtype-compatible. */ + // "strictBindCallApply": true, /* Check that the arguments for 'bind', 'call', and 'apply' methods match the original function. */ + // "strictPropertyInitialization": true, /* Check for class properties that are declared but not set in the constructor. */ + // "noImplicitThis": true, /* Enable error reporting when 'this' is given the type 'any'. */ + // "useUnknownInCatchVariables": true, /* Default catch clause variables as 'unknown' instead of 'any'. */ + // "alwaysStrict": true, /* Ensure 'use strict' is always emitted. */ + // "noUnusedLocals": true, /* Enable error reporting when local variables aren't read. */ + // "noUnusedParameters": true, /* Raise an error when a function parameter isn't read. */ + // "exactOptionalPropertyTypes": true, /* Interpret optional property types as written, rather than adding 'undefined'. */ + // "noImplicitReturns": true, /* Enable error reporting for codepaths that do not explicitly return in a function. */ + // "noFallthroughCasesInSwitch": true, /* Enable error reporting for fallthrough cases in switch statements. */ + // "noUncheckedIndexedAccess": true, /* Add 'undefined' to a type when accessed using an index. */ + // "noImplicitOverride": true, /* Ensure overriding members in derived classes are marked with an override modifier. */ + // "noPropertyAccessFromIndexSignature": true, /* Enforces using indexed accessors for keys declared using an indexed type. */ + // "allowUnusedLabels": true, /* Disable error reporting for unused labels. */ + // "allowUnreachableCode": true, /* Disable error reporting for unreachable code. */ + + /* Completeness */ + // "skipDefaultLibCheck": true, /* Skip type checking .d.ts files that are included with TypeScript. */ + "skipLibCheck": true /* Skip type checking all .d.ts files. */ + } +} From 475842ee901c71dc33dc307e70e9ec38161414f4 Mon Sep 17 00:00:00 2001 From: cheetah_rollie Date: Mon, 29 Jul 2024 17:17:33 +0800 Subject: [PATCH 2/2] update rollie: add user usdc balance data --- adapters/rollie/hourly_blocks.csv | 2 +- adapters/rollie/src/abi/Erc20.ts | 124 ++++++++++++++++++++++++++++++ adapters/rollie/src/index.ts | 38 ++++++--- 3 files changed, 153 insertions(+), 11 deletions(-) create mode 100644 adapters/rollie/src/abi/Erc20.ts diff --git a/adapters/rollie/hourly_blocks.csv b/adapters/rollie/hourly_blocks.csv index f0407f9..9233e54 100644 --- a/adapters/rollie/hourly_blocks.csv +++ b/adapters/rollie/hourly_blocks.csv @@ -1,2 +1,2 @@ number,timestamp -7043812,1719856798 +7784790,1721942082 diff --git a/adapters/rollie/src/abi/Erc20.ts b/adapters/rollie/src/abi/Erc20.ts new file mode 100644 index 0000000..e57ff45 --- /dev/null +++ b/adapters/rollie/src/abi/Erc20.ts @@ -0,0 +1,124 @@ +const Erc20Abi = [{ + "inputs": [{"internalType": "address", "name": "to", "type": "address"}, { + "internalType": "uint256", + "name": "initSupply", + "type": "uint256" + }], "stateMutability": "nonpayable", "type": "constructor" +}, { + "anonymous": false, + "inputs": [{"indexed": true, "internalType": "address", "name": "owner", "type": "address"}, { + "indexed": true, + "internalType": "address", + "name": "spender", + "type": "address" + }, {"indexed": false, "internalType": "uint256", "name": "value", "type": "uint256"}], + "name": "Approval", + "type": "event" +}, { + "anonymous": false, + "inputs": [{"indexed": true, "internalType": "address", "name": "from", "type": "address"}, { + "indexed": true, + "internalType": "address", + "name": "to", + "type": "address" + }, {"indexed": false, "internalType": "uint256", "name": "value", "type": "uint256"}], + "name": "Transfer", + "type": "event" +}, { + "inputs": [{"internalType": "address", "name": "owner", "type": "address"}, { + "internalType": "address", + "name": "spender", + "type": "address" + }], + "name": "allowance", + "outputs": [{"internalType": "uint256", "name": "", "type": "uint256"}], + "stateMutability": "view", + "type": "function" +}, { + "inputs": [{"internalType": "address", "name": "spender", "type": "address"}, { + "internalType": "uint256", + "name": "amount", + "type": "uint256" + }], + "name": "approve", + "outputs": [{"internalType": "bool", "name": "", "type": "bool"}], + "stateMutability": "nonpayable", + "type": "function" +}, { + "inputs": [{"internalType": "address", "name": "account", "type": "address"}], + "name": "balanceOf", + "outputs": [{"internalType": "uint256", "name": "", "type": "uint256"}], + "stateMutability": "view", + "type": "function" +}, { + "inputs": [{"internalType": "uint256", "name": "amount", "type": "uint256"}], + "name": "burn", + "outputs": [{"internalType": "bool", "name": "", "type": "bool"}], + "stateMutability": "nonpayable", + "type": "function" +}, { + "inputs": [], + "name": "decimals", + "outputs": [{"internalType": "uint8", "name": "", "type": "uint8"}], + "stateMutability": "view", + "type": "function" +}, { + "inputs": [{"internalType": "address", "name": "spender", "type": "address"}, { + "internalType": "uint256", + "name": "subtractedValue", + "type": "uint256" + }], + "name": "decreaseAllowance", + "outputs": [{"internalType": "bool", "name": "", "type": "bool"}], + "stateMutability": "nonpayable", + "type": "function" +}, { + "inputs": [{"internalType": "address", "name": "spender", "type": "address"}, { + "internalType": "uint256", + "name": "addedValue", + "type": "uint256" + }], + "name": "increaseAllowance", + "outputs": [{"internalType": "bool", "name": "", "type": "bool"}], + "stateMutability": "nonpayable", + "type": "function" +}, { + "inputs": [], + "name": "name", + "outputs": [{"internalType": "string", "name": "", "type": "string"}], + "stateMutability": "view", + "type": "function" +}, { + "inputs": [], + "name": "symbol", + "outputs": [{"internalType": "string", "name": "", "type": "string"}], + "stateMutability": "view", + "type": "function" +}, { + "inputs": [], + "name": "totalSupply", + "outputs": [{"internalType": "uint256", "name": "", "type": "uint256"}], + "stateMutability": "view", + "type": "function" +}, { + "inputs": [{"internalType": "address", "name": "to", "type": "address"}, { + "internalType": "uint256", + "name": "amount", + "type": "uint256" + }], + "name": "transfer", + "outputs": [{"internalType": "bool", "name": "", "type": "bool"}], + "stateMutability": "nonpayable", + "type": "function" +}, { + "inputs": [{"internalType": "address", "name": "from", "type": "address"}, { + "internalType": "address", + "name": "to", + "type": "address" + }, {"internalType": "uint256", "name": "amount", "type": "uint256"}], + "name": "transferFrom", + "outputs": [{"internalType": "bool", "name": "", "type": "bool"}], + "stateMutability": "nonpayable", + "type": "function" +}] +export default Erc20Abi; diff --git a/adapters/rollie/src/index.ts b/adapters/rollie/src/index.ts index f4f191e..b3624b0 100644 --- a/adapters/rollie/src/index.ts +++ b/adapters/rollie/src/index.ts @@ -10,20 +10,19 @@ import { write } from 'fast-csv'; import { pipeline as streamPipeline } from 'stream'; import * as converter from "json-2-csv"; -console.log("converter", converter) - import TradingVaultABI from "./abi/TradingVault"; -import {containsNodeError} from "viem/utils"; import {resolve} from "path"; -import convert from "lodash/fp/convert"; +import Erc20Abi from "./abi/Erc20"; const PairInfoAddress = "0xA852CE8B0BF3Bcd9191D6140a4627E7823c84848"; -const TradingVaultAddress = "0xA79E00e68549e91e5f0c27048F453b3D87ef6E3D" +const TradingVaultAddress = "0xA79E00e68549e91e5f0c27048F453b3D87ef6E3D"; +const USDCAddress = "0x06eFdBFf2a14a7c8E15944D1F4A48F9F95F663A4"; const SubgrapnUrl = "https://api.studio.thegraph.com/query/76203/rollie-finance/0.0.2"; const RPCs = [ + "https://scroll.drpc.org", "https://scroll.blockpi.network/v1/rpc/public", "https://scroll-mainnet.chainstacklabs.com", - "https://scroll.drpc.org" + ]; const USDC_DECIMALS = 6; const RLP_DECIMALS = 6; @@ -93,6 +92,11 @@ const TradingVaultContract = { address: TradingVaultAddress as Address, } +const USDCContract = { + abi: Erc20Abi as any, + address: USDCAddress as Address, +} + interface Pair { funding_rate: { long: string, @@ -189,6 +193,7 @@ interface User { closeCount: string amountGainRate: string staked: string, + user_balance_usdc: string, } async function getAllUser(lastRequest: User[], page: number = 0, block: BlockData): Promise { @@ -213,12 +218,18 @@ async function getAllUser(lastRequest: User[], page: number = 0, block: BlockDat const allUsers = [...lastRequest, ...users] console.log("Finish load users ..."); // get all staked value - const calls = allUsers.map((item: User) => { - return { + const calls: any[] = []; + allUsers.forEach((item: User) => { + calls.push({ ...TradingVaultContract, functionName: "balanceOf", args: [item.id], - } + }) + calls.push({ + ...USDCContract, + functionName: "balanceOf", + args: [item.id], + }) }) calls.push({ ...TradingVaultContract, @@ -235,12 +246,17 @@ async function getAllUser(lastRequest: User[], page: number = 0, block: BlockDat const pps = result[result.length - 1]; return allUsers.map((item, index) => { const staked = formatUnits( - (result[index] * pps ) / (10n ** BigInt(PPS_DECIMALS)), + (result[index * 2] * pps ) / (10n ** BigInt(PPS_DECIMALS)), RLP_DECIMALS ); + const user_balance_usdc = formatUnits( + (result[index * 2 + 1]) , + USDC_DECIMALS + ) return { ...item, staked, + user_balance_usdc } }); } @@ -294,6 +310,7 @@ interface Result { protocol_fees_usd: number; users_fees_usd: number; etl_timestamp: number; + user_balance_usdc: string; } // const endTime = Math.ceil(Date.now() / 1000); @@ -419,6 +436,7 @@ function generateUserData(user: User, openOrders: OpenOrder[], closedOrders: Clo open_longs_usd: pairData.open_longs, protocol_fees_usd: pairData.protocol_fees, users_fees_usd: pairData.users_fees, + user_balance_usdc: user.user_balance_usdc, } }) }