|
| 1 | +const signale = require('signale'); |
| 2 | + |
| 3 | +const { TOKEN_TYPE } = require('../constants'); |
| 4 | +const Fill = require('../model/fill'); |
| 5 | +const Token = require('../model/token'); |
| 6 | + |
| 7 | +const logger = signale.scope('backfill token types'); |
| 8 | + |
| 9 | +const getTokenType = assetProxyId => { |
| 10 | + return { |
| 11 | + '0xf47261b0': TOKEN_TYPE.ERC20, |
| 12 | + '0x02571792': TOKEN_TYPE.ERC721, |
| 13 | + }[assetProxyId]; |
| 14 | +}; |
| 15 | + |
| 16 | +const backfillTokenTypes = async () => { |
| 17 | + const tokens = await Token.find({ type: null }).limit(100); |
| 18 | + |
| 19 | + logger.info(`found ${tokens.length} without their type set`); |
| 20 | + |
| 21 | + if (tokens.length === 0) { |
| 22 | + return; |
| 23 | + } |
| 24 | + |
| 25 | + await Promise.all( |
| 26 | + tokens.map(async token => { |
| 27 | + const fill = await Fill.findOne({ 'assets.tokenAddress': token.address }); |
| 28 | + |
| 29 | + if (fill === null) { |
| 30 | + logger.warn(`unable to find fill matching ${token.address}`); |
| 31 | + return; |
| 32 | + } |
| 33 | + |
| 34 | + let tokenType; |
| 35 | + |
| 36 | + if ( |
| 37 | + fill.protocolVersion === 1 || |
| 38 | + fill.protocolVersion === undefined || |
| 39 | + fill.protocolVersion === null |
| 40 | + ) { |
| 41 | + tokenType = TOKEN_TYPE.ERC20; |
| 42 | + } else if (fill.makerAsset.tokenAddress === token.address) { |
| 43 | + tokenType = getTokenType(fill.makerAsset.assetProxyId); |
| 44 | + } else { |
| 45 | + tokenType = getTokenType(fill.takerAsset.assetProxyId); |
| 46 | + } |
| 47 | + |
| 48 | + console.log(tokenType); |
| 49 | + |
| 50 | + token.set({ type: tokenType }); |
| 51 | + await token.save(); |
| 52 | + |
| 53 | + logger.success(`set token type for ${token.address}`); |
| 54 | + }), |
| 55 | + ); |
| 56 | +}; |
| 57 | + |
| 58 | +module.exports = backfillTokenTypes; |
0 commit comments