Created
November 25, 2020 16:19
-
-
Save l3wi/db73c566ba8f637f409d855251330aaf to your computer and use it in GitHub Desktop.
A snippet of code you can use to find how many coupons will be available at the next epoch
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
const epochPeriod = 28800 | |
const epochStart = 1602201600 | |
const epochOffset = 105 | |
const getWithdrawals = async (delay) => { | |
let dao = new ethers.Contract(data.DAO_ADDRESS, data.DAO_ABI, provider) | |
const epoch = (await dao.epoch()).toNumber() | |
console.log('Getting next epoch LP unlocks') | |
const lpStart = await getBlockByTimestamp( | |
(epoch - epochOffset - (5 + delay)) * epochPeriod + epochStart | |
) | |
const lpEnd = await getBlockByTimestamp( | |
(epoch - epochOffset - (4 + delay)) * epochPeriod + epochStart | |
) | |
let pool = new ethers.Contract(data.POOL_ADDRESS, data.POOL_ABI, provider) | |
const filter = pool.filters.Unbond() | |
const lpUnbonds = await pool.queryFilter(filter, lpStart, lpEnd) | |
console.log(lpUnbonds) | |
let lpUnbonded = await getESDfromLP( | |
lpUnbonds | |
.map((tx) => { | |
return parseFloat(ethers.utils.formatUnits(tx.args['value'], 18)) | |
}) | |
.reduce((a, c) => a + c) | |
) | |
console.log('Getting next epoch DAO unlocks') | |
const daoStart = await getBlockByTimestamp( | |
(epoch - epochOffset - (15 + delay)) * epochPeriod + epochStart | |
) | |
const daoEnd = await getBlockByTimestamp( | |
(epoch - epochOffset - (14 + delay)) * epochPeriod + epochStart | |
) | |
const daoContract = new ethers.Contract( | |
data.DAO_ADDRESS, | |
data.DAO_ABI, | |
provider | |
) | |
const daoFilter = dao.filters.Unbond() | |
const daoUnbonds = await dao.queryFilter(daoFilter, daoStart, daoEnd) | |
let daoUnbonded = daoUnbonds | |
.map((tx) => { | |
return parseFloat(ethers.utils.formatUnits(tx.args['value'], 18)) | |
}) | |
.reduce((a, c) => a + c) | |
console.log('Fetched unlocks for next epoch:', lpUnbonded + daoUnbonded) | |
console.log('DAO:', daoUnbonded) | |
console.log('LP:', lpUnbonded) | |
return lpUnbonded + daoUnbonded | |
} | |
const getBlockByTimestamp = async (timestamp) => { | |
// get current time | |
const now = Date.now() / 1000 | |
// get time distance to timestamp | |
const difference = now - timestamp | |
// overestimate blocktime | |
const avgBlocktime = 13 | |
const estimatedBlockDistance = parseInt(difference / avgBlocktime) | |
// get inital block below timestamp | |
const currentBlock = await provider.getBlock() | |
let block = await provider.getBlock( | |
currentBlock.number - estimatedBlockDistance | |
) | |
let blockNumber = block.number | |
let requestsMade = 0 | |
const roughSearch = 30 | |
/// Start rough search | |
console.log('Starting Rough Search') | |
while (timestamp > block.timestamp) { | |
blockNumber = blockNumber + roughSearch | |
requestsMade++ | |
block = await provider.getBlock(blockNumber) | |
} | |
// Start Fine Search | |
console.log('Starting Fine Search') | |
block = await provider.getBlock(blockNumber - roughSearch) | |
blockNumber = blockNumber - roughSearch | |
while (timestamp > block.timestamp) { | |
blockNumber++ | |
requestsMade++ | |
block = await provider.getBlock(blockNumber) | |
} | |
console.log('Blocks searched:', requestsMade) | |
return block.number | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment