|
1 | 1 | import { Op, QueryTypes, Sequelize } from 'sequelize'
|
2 | 2 |
|
3 | 3 | import {
|
4 |
| - BankBalance, |
5 | 4 | BankDenomBalance,
|
6 | 5 | BankStateEvent,
|
7 | 6 | Block,
|
@@ -1066,80 +1065,23 @@ export const getEnv = ({
|
1066 | 1065 | throw new Error('Incorrect event type.')
|
1067 | 1066 | }
|
1068 | 1067 |
|
1069 |
| - // Call hook. |
1070 |
| - if (bankDenomBalance) { |
1071 |
| - await onFetch?.([bankDenomBalance]) |
1072 |
| - } |
1073 |
| - |
1074 | 1068 | // Cache bank balance denom, null if nonexistent.
|
1075 | 1069 | if (cachedBalanceDenom === undefined) {
|
1076 | 1070 | cache.events[bankDenomBalanceDependentKey] = bankDenomBalance
|
1077 | 1071 | ? [bankDenomBalance]
|
1078 | 1072 | : null
|
1079 | 1073 | }
|
1080 | 1074 |
|
1081 |
| - // Use BankBalance if exists. |
1082 |
| - const bankBalanceDependentKey = getDependentKey( |
1083 |
| - BankBalance.dependentKeyNamespace, |
1084 |
| - address |
1085 |
| - ) |
1086 |
| - |
1087 |
| - const cachedBalance = cache.events[bankBalanceDependentKey] |
1088 |
| - // Only one bank balance exists per address, since it represents the latest |
1089 |
| - // set of all balances. Apply the block height filter to the denom to ensure |
1090 |
| - // we don't access future balances if we're querying a past block height. |
1091 |
| - const bankBalance = |
1092 |
| - // If undefined, we haven't tried to fetch it yet. If not undefined, |
1093 |
| - // either it exists or it doesn't (null). |
1094 |
| - cachedBalance !== undefined |
1095 |
| - ? cachedBalance?.[0] |
1096 |
| - : await BankBalance.findOne({ |
1097 |
| - where: { |
1098 |
| - address, |
1099 |
| - balances: { |
1100 |
| - [denom]: { |
1101 |
| - [Op.not]: null, |
1102 |
| - }, |
1103 |
| - }, |
1104 |
| - denomUpdateBlockHeights: { |
1105 |
| - [denom + '::bigint']: blockHeightFilter, |
1106 |
| - }, |
1107 |
| - }, |
1108 |
| - }) |
1109 |
| - |
1110 |
| - if (bankBalance && !(bankBalance instanceof BankBalance)) { |
1111 |
| - throw new Error('Incorrect event type.') |
1112 |
| - } |
1113 |
| - |
1114 |
| - // Cache bank balance, null if nonexistent. |
1115 |
| - if (cachedBalance === undefined) { |
1116 |
| - cache.events[bankBalanceDependentKey] = bankBalance ? [bankBalance] : null |
1117 |
| - } |
1118 |
| - |
1119 |
| - if (bankBalance) { |
| 1075 | + if (bankDenomBalance) { |
1120 | 1076 | // Call hook.
|
1121 |
| - await onFetch?.([bankBalance]) |
1122 |
| - } |
1123 |
| - |
1124 |
| - // Choose the latest balance. |
1125 |
| - if (bankDenomBalance || bankBalance) { |
1126 |
| - const balance = |
1127 |
| - bankDenomBalance && |
1128 |
| - bankBalance?.balances?.[denom] && |
1129 |
| - bankBalance.denomUpdateBlockHeights?.[denom] |
1130 |
| - ? BigInt(bankDenomBalance.blockHeight) > |
1131 |
| - BigInt(bankBalance.denomUpdateBlockHeights[denom]) |
1132 |
| - ? bankDenomBalance.balance |
1133 |
| - : bankBalance.balances[denom] |
1134 |
| - : bankDenomBalance?.balance || bankBalance?.balances[denom] |
1135 |
| - |
1136 |
| - if (balance) { |
1137 |
| - return balance |
1138 |
| - } |
| 1077 | + await onFetch?.([bankDenomBalance]) |
| 1078 | + |
| 1079 | + // Return balance. |
| 1080 | + return bankDenomBalance.balance |
1139 | 1081 | }
|
1140 | 1082 |
|
1141 |
| - // Use BankStateEvent if no BankBalance/BankDenomBalance and this address is |
1142 |
| - // a contract that we keep history for. |
| 1083 | + // Use BankStateEvent if no BankDenomBalance and this address is a contract |
| 1084 | + // that we keep history for. |
1143 | 1085 | const historyExists = await contractMatchesCodeIdKeys(
|
1144 | 1086 | address,
|
1145 | 1087 | ...BANK_HISTORY_CODE_IDS_KEYS
|
@@ -1197,42 +1139,6 @@ export const getEnv = ({
|
1197 | 1139 | }
|
1198 | 1140 |
|
1199 | 1141 | const getBalances: FormulaBalancesGetter = async (address) => {
|
1200 |
| - // Use BankBalance and BankDenomBalance if exists. |
1201 |
| - const bankBalanceDependentKey = getDependentKey( |
1202 |
| - BankBalance.dependentKeyNamespace, |
1203 |
| - address |
1204 |
| - ) |
1205 |
| - const cachedBalance = cache.events[bankBalanceDependentKey] |
1206 |
| - |
1207 |
| - // Only one bank balance exists per address, since it represents the latest |
1208 |
| - // set of all balances. |
1209 |
| - const bankBalance: BankBalance | undefined | null = |
1210 |
| - // If undefined, we haven't tried to fetch it yet. If not undefined, |
1211 |
| - // either it exists or it doesn't (null). |
1212 |
| - cachedBalance !== undefined |
1213 |
| - ? (cachedBalance?.[0] as BankBalance | null | undefined) |
1214 |
| - : await BankBalance.findOne({ |
1215 |
| - where: { |
1216 |
| - address, |
1217 |
| - }, |
1218 |
| - }) |
1219 |
| - |
1220 |
| - if (bankBalance && !(bankBalance instanceof BankBalance)) { |
1221 |
| - throw new Error('Incorrect event type.') |
1222 |
| - } |
1223 |
| - |
1224 |
| - // Cache bank balance, null if nonexistent. |
1225 |
| - if (cachedBalance === undefined) { |
1226 |
| - cache.events[bankBalanceDependentKey] = bankBalance ? [bankBalance] : null |
1227 |
| - } |
1228 |
| - |
1229 |
| - // If bank balance found, return balance. Otherwise fall back to |
1230 |
| - // BankStateEvent. |
1231 |
| - if (bankBalance && bankBalance instanceof BankBalance) { |
1232 |
| - // Call hook. |
1233 |
| - await onFetch?.([bankBalance]) |
1234 |
| - } |
1235 |
| - |
1236 | 1142 | // Use BankDenomBalance if exists.
|
1237 | 1143 | const bankDenomBalanceDependentKey = getDependentKey(
|
1238 | 1144 | BankDenomBalance.dependentKeyNamespace,
|
@@ -1287,58 +1193,23 @@ export const getEnv = ({
|
1287 | 1193 | : null
|
1288 | 1194 | }
|
1289 | 1195 |
|
1290 |
| - // Call hook. |
| 1196 | + // If bank denom balances found, return them. If none, fall back to |
| 1197 | + // BankStateEvent. |
1291 | 1198 | if (bankDenomBalances.length) {
|
| 1199 | + // Call hook. |
1292 | 1200 | await onFetch?.(bankDenomBalances)
|
1293 |
| - } |
1294 | 1201 |
|
1295 |
| - // If bank balance or bank denom balances found, combine and return. If |
1296 |
| - // neither exist, fall back to BankStateEvent if this address is a contract |
1297 |
| - // that we keep history for. |
1298 |
| - if (bankBalance || bankDenomBalances.length) { |
1299 |
| - const uniqueDenoms = Array.from( |
1300 |
| - new Set([ |
1301 |
| - ...Object.keys(bankBalance?.balances ?? {}), |
1302 |
| - ...bankDenomBalances.map(({ denom }) => denom), |
1303 |
| - ]) |
| 1202 | + return bankDenomBalances.reduce( |
| 1203 | + (acc, denomBalance) => ({ |
| 1204 | + ...acc, |
| 1205 | + [denomBalance.denom]: denomBalance.balance, |
| 1206 | + }), |
| 1207 | + {} as Record<string, string> |
1304 | 1208 | )
|
| 1209 | + } |
1305 | 1210 |
|
1306 |
| - // Merge and choose latest balance for each denom. |
1307 |
| - return uniqueDenoms.reduce((acc, denom) => { |
1308 |
| - const balanceFromAll = |
1309 |
| - bankBalance?.balances?.[denom] && |
1310 |
| - bankBalance.denomUpdateBlockHeights?.[denom] && |
1311 |
| - // Filter out denoms that are not valid for the current block height. |
1312 |
| - BigInt(bankBalance.denomUpdateBlockHeights[denom]) <= block.height |
1313 |
| - ? { |
1314 |
| - balance: bankBalance.balances[denom], |
1315 |
| - blockHeight: bankBalance.denomUpdateBlockHeights[denom], |
1316 |
| - } |
1317 |
| - : undefined |
1318 |
| - |
1319 |
| - const denomBalance = bankDenomBalances.find( |
1320 |
| - ({ denom: d }) => d === denom |
1321 |
| - ) |
1322 |
| - |
1323 |
| - const balance = |
1324 |
| - balanceFromAll && denomBalance |
1325 |
| - ? BigInt(denomBalance.blockHeight) > |
1326 |
| - BigInt(balanceFromAll.blockHeight) |
1327 |
| - ? denomBalance.balance |
1328 |
| - : balanceFromAll.balance |
1329 |
| - : denomBalance?.balance || balanceFromAll?.balance |
1330 |
| - |
1331 |
| - return balance |
1332 |
| - ? { |
1333 |
| - ...acc, |
1334 |
| - [denom]: balance, |
1335 |
| - } |
1336 |
| - : acc |
1337 |
| - }, {} as Record<string, string>) |
1338 |
| - } |
1339 |
| - |
1340 |
| - // Use BankStateEvent if no BankBalance and this address is a contract that |
1341 |
| - // we keep history for. |
| 1211 | + // Use BankStateEvent if no BankDenomBalance and this address is a contract |
| 1212 | + // that we keep history for. |
1342 | 1213 | const historyExists = await contractMatchesCodeIdKeys(
|
1343 | 1214 | address,
|
1344 | 1215 | ...BANK_HISTORY_CODE_IDS_KEYS
|
|
0 commit comments