This commit is contained in:
Richa-iitr 2022-10-27 15:04:23 +05:30
parent 5f94d4bd88
commit e79e9c4841
6 changed files with 3995 additions and 70 deletions

3
.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
node_modules
generated
build

View File

@ -1,9 +1,21 @@
[
{
"inputs": [
{ "internalType": "address", "name": "_logic", "type": "address" },
{ "internalType": "address", "name": "admin_", "type": "address" },
{ "internalType": "bytes", "name": "_data", "type": "bytes" }
{
"internalType": "address",
"name": "_logic",
"type": "address"
},
{
"internalType": "address",
"name": "admin_",
"type": "address"
},
{
"internalType": "bytes",
"name": "_data",
"type": "bytes"
}
],
"stateMutability": "nonpayable",
"type": "constructor"
@ -53,19 +65,30 @@
"name": "Upgraded",
"type": "event"
},
{ "stateMutability": "payable", "type": "fallback" },
{
"stateMutability": "payable",
"type": "fallback"
},
{
"inputs": [],
"name": "admin",
"outputs": [
{ "internalType": "address", "name": "admin_", "type": "address" }
{
"internalType": "address",
"name": "admin_",
"type": "address"
}
],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{ "internalType": "address", "name": "newAdmin", "type": "address" }
{
"internalType": "address",
"name": "newAdmin",
"type": "address"
}
],
"name": "changeAdmin",
"outputs": [],
@ -105,12 +128,402 @@
"name": "newImplementation",
"type": "address"
},
{ "internalType": "bytes", "name": "data", "type": "bytes" }
{
"internalType": "bytes",
"name": "data",
"type": "bytes"
}
],
"name": "upgradeToAndCall",
"outputs": [],
"stateMutability": "payable",
"type": "function"
},
{ "stateMutability": "payable", "type": "receive" }
{
"stateMutability": "payable",
"type": "receive"
},
{
"anonymous": false,
"inputs": [
{
"indexed": true,
"internalType": "address",
"name": "account",
"type": "address"
},
{
"indexed": true,
"internalType": "uint256",
"name": "route",
"type": "uint256"
},
{
"indexed": false,
"internalType": "address[]",
"name": "tokens",
"type": "address[]"
},
{
"indexed": false,
"internalType": "uint256[]",
"name": "amounts",
"type": "uint256[]"
}
],
"name": "LogFlashloan",
"type": "event"
},
{
"anonymous": false,
"inputs": [
{
"indexed": true,
"internalType": "address",
"name": "oldOwner",
"type": "address"
},
{
"indexed": true,
"internalType": "address",
"name": "newOwner",
"type": "address"
}
],
"name": "updateOwnerLog",
"type": "event"
},
{
"anonymous": false,
"inputs": [
{
"indexed": true,
"internalType": "address",
"name": "account",
"type": "address"
},
{
"indexed": true,
"internalType": "bool",
"name": "isWhitelisted_",
"type": "bool"
}
],
"name": "updateWhitelistLog",
"type": "event"
},
{
"inputs": [],
"name": "InstaFeeBPS",
"outputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
"internalType": "address[]",
"name": "_cTokens",
"type": "address[]"
}
],
"name": "addTokenToCToken",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
"internalType": "uint256",
"name": "_route",
"type": "uint256"
},
{
"internalType": "address",
"name": "account_",
"type": "address"
}
],
"name": "calculateFeeBPS",
"outputs": [
{
"internalType": "uint256",
"name": "BPS_",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
"internalType": "address[]",
"name": "_assets",
"type": "address[]"
},
{
"internalType": "uint256[]",
"name": "_amounts",
"type": "uint256[]"
},
{
"internalType": "uint256[]",
"name": "_premiums",
"type": "uint256[]"
},
{
"internalType": "address",
"name": "_initiator",
"type": "address"
},
{
"internalType": "bytes",
"name": "_data",
"type": "bytes"
}
],
"name": "executeOperation",
"outputs": [
{
"internalType": "bool",
"name": "",
"type": "bool"
}
],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
"internalType": "address[]",
"name": "_tokens",
"type": "address[]"
},
{
"internalType": "uint256[]",
"name": "_amounts",
"type": "uint256[]"
},
{
"internalType": "uint256",
"name": "_route",
"type": "uint256"
},
{
"internalType": "bytes",
"name": "_data",
"type": "bytes"
},
{
"internalType": "bytes",
"name": "",
"type": "bytes"
}
],
"name": "flashLoan",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [],
"name": "getRoutes",
"outputs": [
{
"internalType": "uint16[]",
"name": "routes_",
"type": "uint16[]"
}
],
"stateMutability": "pure",
"type": "function"
},
{
"inputs": [
{
"internalType": "address",
"name": "owner_",
"type": "address"
}
],
"name": "initialize",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
"internalType": "address",
"name": "",
"type": "address"
}
],
"name": "isWhitelisted",
"outputs": [
{
"internalType": "bool",
"name": "",
"type": "bool"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
"internalType": "address",
"name": "_initiator",
"type": "address"
},
{
"internalType": "address",
"name": "",
"type": "address"
},
{
"internalType": "uint256",
"name": "_amount",
"type": "uint256"
},
{
"internalType": "uint256",
"name": "_fee",
"type": "uint256"
},
{
"internalType": "bytes",
"name": "_data",
"type": "bytes"
}
],
"name": "onFlashLoan",
"outputs": [
{
"internalType": "bytes32",
"name": "",
"type": "bytes32"
}
],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [],
"name": "owner",
"outputs": [
{
"internalType": "address",
"name": "",
"type": "address"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
"internalType": "contract IERC20[]",
"name": "",
"type": "address[]"
},
{
"internalType": "uint256[]",
"name": "_amounts",
"type": "uint256[]"
},
{
"internalType": "uint256[]",
"name": "_fees",
"type": "uint256[]"
},
{
"internalType": "bytes",
"name": "_data",
"type": "bytes"
}
],
"name": "receiveFlashLoan",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
"internalType": "address",
"name": "",
"type": "address"
}
],
"name": "tokenToCToken",
"outputs": [
{
"internalType": "address",
"name": "",
"type": "address"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
"internalType": "address[]",
"name": "_tokens",
"type": "address[]"
}
],
"name": "transferFeeToTreasury",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
"internalType": "address",
"name": "newOwner_",
"type": "address"
}
],
"name": "updateOwner",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
"internalType": "address",
"name": "account_",
"type": "address"
},
{
"internalType": "bool",
"name": "whitelist_",
"type": "bool"
}
],
"name": "updateWhitelist",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"stateMutability": "payable",
"type": "receive"
}
]

View File

@ -1,6 +1,8 @@
type ExampleEntity @entity {
type FeeData @entity {
# user + '#' + token address
id: ID!
count: BigInt!
previousAdmin: Bytes! # address
newAdmin: Bytes! # address
user: Bytes!
token: Bytes!
flaFee: Int!
routeFee: Int!
}

View File

@ -1,55 +1,113 @@
import { BigInt } from "@graphprotocol/graph-ts"
import { Address, BigInt, ethereum } from "@graphprotocol/graph-ts";
import {
ExecuteOperationCall,
FLA,
AdminChanged,
BeaconUpgraded,
Upgraded
} from "../generated/FLA/FLA"
import { ExampleEntity } from "../generated/schema"
OnFlashLoanCall,
ReceiveFlashLoanCall,
} from "../generated/FLA/FLA";
import { FeeData } from "../generated/schema";
export function handleAdminChanged(event: AdminChanged): void {
// Entities can be loaded from the store using a string ID; this ID
// needs to be unique across all entities of the same type
let entity = ExampleEntity.load(event.transaction.from.toHex())
// const DAI = new Address(0x6b175474e89094c44da98b954eedeac495271d0f);
export function handleExecuteOperation(call: ExecuteOperationCall): void {
// let assets = call.inputs._assets;
// let amounts = call.inputs._amounts;
// let user = call.inputs._initiator;
let fees = call.inputs._premiums;
let length = fees.length;
let data_ = call.inputs._data;
// Entities only exist after they have been saved to the store;
// `null` checks allow to create entities on demand
if (!entity) {
entity = new ExampleEntity(event.transaction.from.toHex())
let decoded = ethereum.decode("(uint256,address[],uint256[],address,bytes)", data_)?.toTuple();
if (decoded != undefined) {
let tokens = decoded[1].toArray();
let amounts = decoded[2].toArray();
let user = decoded[3].toAddress();
// Entity fields can be set using simple assignments
entity.count = BigInt.fromI32(0)
for (let i = 0; i < length; i++) {
let id = user.toHexString() + "#" + tokens[i].toAddress().toHexString();
let data = createOrLoadFeeData(id);
data.user = user;
data.token = tokens[i].toAddress();
let instaAmt_ = amounts[i]
.toBigInt()
.times(BigInt.fromI32(5))
.div(BigInt.fromI32(1e4));
if (instaAmt_ > fees[i]) {
data.flaFee += instaAmt_.minus(fees[i]).toI32();
}
data.routeFee += fees[i].toI32();
data.save();
}
}
// BigInt and BigDecimal math are supported
entity.count = entity.count + BigInt.fromI32(1)
// Entity fields can be set based on event parameters
entity.previousAdmin = event.params.previousAdmin
entity.newAdmin = event.params.newAdmin
// Entities can be written to the store with `.save()`
entity.save()
// Note: If a handler doesn't require existing field values, it is faster
// _not_ to load the entity from the store. Instead, create it fresh with
// `new Entity(...)`, set the fields that should be updated and save the
// entity back to the store. Fields that were not set or unset remain
// unchanged, allowing for partial updates to be applied.
// It is also possible to access smart contracts from mappings. For
// example, the contract that has emitted the event can be connected to
// with:
//
// let contract = Contract.bind(event.address)
//
// The following functions can then be called on this contract to access
// state variables and other data:
//
// - contract.admin(...)
// - contract.implementation(...)
}
export function handleBeaconUpgraded(event: BeaconUpgraded): void {}
export function handleOnFlashloan(call: OnFlashLoanCall): void {
// let amount = call.inputs._amount;
let fee = call.inputs._fee;
let data_ = call.inputs._data;
let decoded = ethereum.decode("(uint256,address[],uint256[],address,bytes)", data_)?.toTuple();
if (decoded != undefined) {
let tokens = decoded[1].toArray();
let amounts = decoded[2].toArray();
let user = decoded[3].toAddress();
export function handleUpgraded(event: Upgraded): void {}
for (let i = 0; i < tokens.length; i++) {
let id = user.toHexString() + "#" + tokens[i].toAddress().toHexString();
let data = createOrLoadFeeData(id);
data.user = user;
data.token = tokens[i].toAddress();
let instaAmt_ = amounts[i]
.toBigInt()
.times(BigInt.fromI32(5))
.div(BigInt.fromI32(1e4));
if (instaAmt_ > fee) {
data.flaFee += instaAmt_.minus(fee).toI32();
}
data.routeFee += fee.toI32();
data.save();
}
}
}
export function handleReceiveFlashloan(call: ReceiveFlashLoanCall): void {
let fees = call.inputs._fees;
let length = fees.length;
let data_ = call.inputs._data;
let decoded = ethereum.decode("(uint256,address[],uint256[],address,bytes)", data_)?.toTuple();
if (decoded != undefined) {
let tokens = decoded[1].toArray();
let amounts = decoded[2].toArray();
let user = decoded[3].toAddress();
for (let i = 0; i < length; i++) {
let id = user.toHexString() + "#" + tokens[i].toAddress().toHexString();
let data = createOrLoadFeeData(id);
data.user = user;
data.token = tokens[i].toAddress();
let instaAmt_ = amounts[i]
.toBigInt()
.times(BigInt.fromI32(5))
.div(BigInt.fromI32(1e4));
if (instaAmt_ > fees[i]) {
data.flaFee += instaAmt_.minus(fees[i]).toI32();
}
data.routeFee += fees[i].toI32();
data.save();
}
}
}
export function createOrLoadFeeData(id: string): FeeData {
let data = FeeData.load(id);
if (data == null) {
data = new FeeData(id);
data.user = new Address(0);
data.token = new Address(0);
data.flaFee = 0;
data.routeFee = 0;
}
return data;
}

View File

@ -1,4 +1,4 @@
specVersion: 0.0.1
specVersion: 0.0.4
schema:
file: ./schema.graphql
dataSources:
@ -10,20 +10,18 @@ dataSources:
abi: FLA
mapping:
kind: ethereum/events
apiVersion: 0.0.5
apiVersion: 0.0.6
language: wasm/assemblyscript
entities:
- AdminChanged
- BeaconUpgraded
- Upgraded
- FeeData
abis:
- name: FLA
file: ./abis/FLA.json
eventHandlers:
- event: AdminChanged(address,address)
handler: handleAdminChanged
- event: BeaconUpgraded(indexed address)
handler: handleBeaconUpgraded
- event: Upgraded(indexed address)
handler: handleUpgraded
callHandlers:
- function: executeOperation(address[],uint256[],uint256[],address,bytes)
handler: handleExecuteOperation
- function: onFlashLoan(address,address,uint256,uint256,bytes)
handler: handleOnFlashloan
- function: receiveFlashLoan(address[],uint256[],uint256[],bytes)
handler: handleReceiveFlashloan
file: ./src/fla.ts

3451
yarn.lock Normal file

File diff suppressed because it is too large Load Diff