Created
February 1, 2023 05:08
-
-
Save simplemachine92/081ab9d3b2ae0b8c4d2db3a8624359ed to your computer and use it in GitHub Desktop.
cachooooor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
async updatecache(lastBlock: number, reserves: Reserve[]) { | |
let aaveOrigin = lastBlock; | |
const current = await this.provider.getBlockNumber(); | |
// 3000 block range for RPC limits | |
const iterations = Math.floor((current - aaveOrigin) / 3000); | |
const promises = []; | |
let allUsers: Player[] = []; | |
let userData: Users = { | |
currentToBlock: 0, | |
users: [], | |
}; | |
let newUserData: Users = { | |
currentToBlock: 0, | |
users: [], | |
}; | |
let oldList: Users = uniques; | |
for (let i = 0; i < iterations; i++) { | |
aaveOrigin += 3000; | |
if (aaveOrigin >= current) { | |
aaveOrigin == current; | |
} | |
promises.push(this.Cache(aaveOrigin, aaveOrigin + 3000, reserves)); | |
} | |
await Promise.all(promises) | |
.then((res) => { | |
res.forEach((array) => { | |
for (let user of array) { | |
allUsers.push(user); | |
newUserData.users.push(user); | |
} | |
}); | |
}) | |
.then(() => { | |
var uniques: Player[] = _(allUsers) | |
.orderBy(["loanAmountEth", "loanBlock"], ["desc", "desc"]) | |
.uniqBy("address") | |
.value(); | |
let allUniques: Player[]; | |
for (let e of uniques) { | |
oldList.users.push(e); | |
} | |
allUniques = _(oldList.users) | |
.orderBy(["loanAmountEth", "loanBlock"], ["desc", "desc"]) | |
.uniqBy("address") | |
.value(); | |
userData = { currentToBlock: current, users: allUniques }; | |
}); | |
return { userData, newUserData }; | |
} | |
async Cache(fromBlock: number, toBlock: number, reserves: Reserve[]) { | |
// Event filter | |
const bFilter = this.aavepool.filters.Borrow(); | |
let users: Player[] = []; | |
// Borrow events that occured in this block range. | |
const borrows = await this.aavepool.queryFilter( | |
bFilter, | |
fromBlock, | |
toBlock | |
); | |
borrows.forEach( | |
async (loan: { | |
args: { reserve: any; amount: any; user: any }; | |
blockNumber: any; | |
}) => { | |
let token: Reserve = _.find(reserves, { | |
address: loan.args.reserve, | |
}); | |
if (token == undefined) { | |
return; | |
} | |
const loanAmount = loan.args.amount; | |
const numTokensLoaned = ethers.utils.formatUnits( | |
loanAmount, | |
token.decimals | |
); | |
const ethPriceDecimal = ethers.utils.formatEther(token.quote); | |
const numTokensFloat = parseFloat(numTokensLoaned); | |
const ethPriceFloat = parseFloat(ethPriceDecimal); | |
const ethTotal = numTokensFloat * ethPriceFloat; | |
const threshhold = parseFloat("0.1"); | |
const player: Player = { | |
address: loan.args.user, | |
loanAmountEth: ethTotal, | |
loanBlock: loan.blockNumber, | |
}; | |
if (ethTotal >= threshhold) { | |
users.push(player); | |
} | |
} | |
); | |
return users; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment