|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const db = require('../models'); |
| 4 | +const { C } = require('../../../constants/runtime-constants'); |
| 5 | +const AlchemyUtil = require('../../../datasources/alchemy'); |
| 6 | +const PromiseUtil = require('../../../utils/async/promise'); |
| 7 | +const Contracts = require('../../../datasources/contracts/contracts'); |
| 8 | +const EnvUtil = require('../../../utils/env'); |
| 9 | +const { TOKEN_TABLE } = require('../../../constants/tables'); |
| 10 | + |
| 11 | +// This was copied from silo-tokens-base.js and modified to include only pintowsteth. |
| 12 | + |
| 13 | +/** @type {import('sequelize-cli').Migration} */ |
| 14 | +module.exports = { |
| 15 | + async up(queryInterface, Sequelize) { |
| 16 | + if (!EnvUtil.isChainEnabled('base')) { |
| 17 | + console.log(`Skipping seeder: chain 'base' is not enabled.`); |
| 18 | + return; |
| 19 | + } |
| 20 | + const c = C('base'); |
| 21 | + const tokens = [c.PINTOWSTETH]; |
| 22 | + |
| 23 | + // Add base tokens |
| 24 | + await AlchemyUtil.ready(c.CHAIN); |
| 25 | + const beanstalk = Contracts.getBeanstalk(c); |
| 26 | + |
| 27 | + // Gets tokens that have already been populated |
| 28 | + const existingTokens = await db.sequelize.models.Token.findAll({ |
| 29 | + where: { |
| 30 | + chain: c.CHAIN, |
| 31 | + address: { |
| 32 | + [Sequelize.Op.in]: tokens |
| 33 | + } |
| 34 | + }, |
| 35 | + attributes: ['address'] |
| 36 | + }); |
| 37 | + |
| 38 | + // Add new tokens only |
| 39 | + const newTokens = tokens.filter((token) => !existingTokens.some((t) => t.address === token)); |
| 40 | + if (newTokens.length > 0) { |
| 41 | + const rows = []; |
| 42 | + for (const token of newTokens) { |
| 43 | + const erc20 = Contracts.get(token, c); |
| 44 | + const [name, symbol, supply, decimals] = await Promise.all([ |
| 45 | + erc20.name(), |
| 46 | + erc20.symbol(), |
| 47 | + erc20.totalSupply(), |
| 48 | + (async () => Number(await erc20.decimals()))() |
| 49 | + ]); |
| 50 | + const [bdv, stalkEarnedPerSeason, stemTip, totalDeposited, totalDepositedBdv] = await Promise.all( |
| 51 | + [ |
| 52 | + PromiseUtil.defaultOnReject(1n)(beanstalk.bdv(token, BigInt(10 ** decimals))), |
| 53 | + (async () => { |
| 54 | + const tokenSettings = await beanstalk.tokenSettings(token); |
| 55 | + return tokenSettings.stalkEarnedPerSeason; |
| 56 | + })(), |
| 57 | + beanstalk.stemTipForToken(token), |
| 58 | + beanstalk.getTotalDeposited(token), |
| 59 | + beanstalk.getTotalDepositedBdv(token) |
| 60 | + // If any revert, they return null instead |
| 61 | + ].map(PromiseUtil.defaultOnReject(null)) |
| 62 | + ); |
| 63 | + rows.push({ |
| 64 | + address: token, |
| 65 | + chain: c.CHAIN, |
| 66 | + name, |
| 67 | + symbol, |
| 68 | + supply, |
| 69 | + decimals, |
| 70 | + isWhitelisted: true, |
| 71 | + bdv, |
| 72 | + stalkEarnedPerSeason, |
| 73 | + stemTip, |
| 74 | + totalDeposited, |
| 75 | + totalDepositedBdv, |
| 76 | + createdAt: new Date(), |
| 77 | + updatedAt: new Date() |
| 78 | + }); |
| 79 | + } |
| 80 | + |
| 81 | + await queryInterface.bulkInsert(TOKEN_TABLE.env, rows); |
| 82 | + } |
| 83 | + }, |
| 84 | + |
| 85 | + async down(queryInterface, Sequelize) { |
| 86 | + if (EnvUtil.isChainEnabled('base')) { |
| 87 | + const c = C('base'); |
| 88 | + const tokens = [c.PINTOWSTETH]; |
| 89 | + // Delete pinto tokens |
| 90 | + await queryInterface.bulkDelete(TOKEN_TABLE.env, { |
| 91 | + address: { |
| 92 | + [Sequelize.Op.in]: tokens |
| 93 | + } |
| 94 | + }); |
| 95 | + } |
| 96 | + } |
| 97 | +}; |
0 commit comments