-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #23 from BeanstalkFarms/lambda
Database stores detailed info on each deposit
- Loading branch information
Showing
94 changed files
with
4,256 additions
and
534 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
const { allToBigInt } = require('../../utils/number'); | ||
const retryable = require('../../utils/async/retryable'); | ||
|
||
// Proxy wrapper to: | ||
// (1) built in retry support for failed requests | ||
// (2) transform all BigNumber -> BigInt | ||
class SuperContract { | ||
constructor(contract) { | ||
const proxyHandler = { | ||
get: (target, property, receiver) => { | ||
if (['interface', 'provider', 'filters', 'address'].includes(property)) { | ||
return contract[property]; | ||
} | ||
|
||
if (property === 'then') { | ||
return undefined; | ||
} | ||
|
||
return async (...args) => { | ||
const rawResult = await retryable(() => contract[property](...args)); | ||
return SuperContract._transformAll(rawResult); | ||
}; | ||
} | ||
}; | ||
|
||
return new Proxy(this, proxyHandler); | ||
} | ||
|
||
// Transforms everything in this result to be a BigInt. | ||
// Handles arrays/tuples (with named fields), and single values | ||
static _transformAll(rawResult) { | ||
if (!Array.isArray(rawResult)) { | ||
return allToBigInt(rawResult); | ||
} else { | ||
// The raw result is frozen | ||
const transformed = allToBigInt(JSON.parse(JSON.stringify(rawResult))); | ||
// Re assign the convenience names if the return value was tuple | ||
const namedKeys = Object.keys(rawResult).filter(isNaN); | ||
if (namedKeys.length > 0) { | ||
const retval = {}; | ||
for (let i = 0; i < namedKeys.length; ++i) { | ||
retval[namedKeys[i]] = transformed[i]; | ||
} | ||
return retval; | ||
} else { | ||
// expected result is an array | ||
return transformed; | ||
} | ||
} | ||
} | ||
} | ||
|
||
module.exports = SuperContract; |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
const AlchemyUtil = require('../alchemy'); | ||
const FilterLogs = require('./filter-logs'); | ||
|
||
const DEPOSIT_EVENTS = ['AddDeposit', 'RemoveDeposit', 'RemoveDeposits']; | ||
|
||
class DepositEvents { | ||
// Returns a summary of add/remove deposit events. Collapses RemoveDeposits out of its array form | ||
static async getSiloDepositEvents(fromBlock, toBlock = 'latest') { | ||
const rawEvents = await FilterLogs.getBeanstalkEvents(DEPOSIT_EVENTS, fromBlock, toBlock); | ||
const collapsed = []; | ||
for (const event of rawEvents) { | ||
if (event.name === 'RemoveDeposits') { | ||
for (let i = 0; i < event.args.stems.length; ++i) { | ||
collapsed.push({ | ||
type: -1, | ||
account: event.args.account.toLowerCase(), | ||
token: event.args.token.toLowerCase(), | ||
stem: BigInt(event.args.stems[i]), | ||
amount: BigInt(event.args.amounts[i]), | ||
bdv: BigInt(event.args.bdvs[i]) | ||
}); | ||
} | ||
} else { | ||
collapsed.push({ | ||
type: event.name === 'AddDeposit' ? 1 : -1, | ||
account: event.args.account.toLowerCase(), | ||
token: event.args.token.toLowerCase(), | ||
stem: BigInt(event.args.stem), | ||
amount: BigInt(event.args.amount), | ||
bdv: BigInt(event.args.bdv) | ||
}); | ||
} | ||
} | ||
return collapsed; | ||
} | ||
|
||
// Returns condensed info from StalkBalanceChanged | ||
static async getStalkBalanceChangedEvents(fromBlock, toBlock = 'latest') { | ||
const rawEvents = await FilterLogs.getBeanstalkEvents(['StalkBalanceChanged'], fromBlock, toBlock); | ||
const summary = []; | ||
for (const event of rawEvents) { | ||
summary.push({ | ||
account: event.args.account.toLowerCase(), | ||
deltaStalk: BigInt(event.args.delta), | ||
blockNumber: event.rawLog.blockNumber | ||
}); | ||
} | ||
return summary; | ||
} | ||
} | ||
module.exports = DepositEvents; | ||
|
||
if (require.main === module) { | ||
(async () => { | ||
await AlchemyUtil.ready('arb'); | ||
// const logs = await DepositEvents.getSiloDepositEvents(264547404); | ||
// console.log(logs.filter((l) => l.name === 'AddDeposit')[0]); | ||
// console.log(logs.filter((l) => l.name === 'RemoveDeposit')[0]); | ||
// console.log(logs.filter((l) => l.name === 'RemoveDeposits')[0].args.stems); | ||
// console.log(await DepositEvents.getSiloDepositEvents(264547404)); | ||
console.log(await DepositEvents.getStalkBalanceChangedEvents(264547404)); | ||
})(); | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
const { C } = require('../../constants/runtime-constants'); | ||
const Contracts = require('../contracts/contracts'); | ||
|
||
class FilterLogs { | ||
// Retrieves beanstalk events matching the requested names | ||
static async getBeanstalkEvents(eventNames, fromBlock, toBlock, c = C()) { | ||
const iBeanstalk = Contracts.getBeanstalk(c).interface; | ||
const topics = eventNames.map((n) => iBeanstalk.getEventTopic(n)); | ||
|
||
const filter = { | ||
address: c.BEANSTALK, | ||
topics: [topics], | ||
fromBlock, | ||
toBlock | ||
}; | ||
|
||
const logs = await c.RPC.getLogs(filter); | ||
const events = logs.map((log) => { | ||
const parsed = iBeanstalk.parseLog(log); | ||
parsed.rawLog = log; | ||
return parsed; | ||
}); | ||
return events; | ||
} | ||
} | ||
module.exports = FilterLogs; |
Oops, something went wrong.