Skip to content

Commit

Permalink
More robust product import
Browse files Browse the repository at this point in the history
  • Loading branch information
prevostc committed Oct 11, 2024
1 parent b580f4c commit bfe0d28
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 63 deletions.
48 changes: 27 additions & 21 deletions src/protocol/beefy/connector/boost-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,32 +85,38 @@ export function beefyBoostsFromGitHistory$(chain: Chain, allChainVaults: BeefyVa
return v2$.pipe(
// process the vaults in chronolical order and mark the eol date if found
Rx.reduce((acc, { fileVersion, boosts }) => {
// reset the foundInCurrentBatch flag
for (const boostAddress of Object.keys(acc)) {
acc[boostAddress].foundInCurrentBatch = false;
}
try {
// reset the foundInCurrentBatch flag
for (const boostAddress of Object.keys(acc)) {
acc[boostAddress].foundInCurrentBatch = false;
}

// add boosts to the accumulator
for (const boost of boosts) {
const boostAddress = normalizeAddressOrThrow(boost.earnContractAddress);
if (!acc[boostAddress]) {
const eolDate = boost.status === "closed" ? fileVersion.date : null;
acc[boostAddress] = { fileVersion, eolDate, boost, foundInCurrentBatch: true };
} else {
const eolDate = boost.status === "closed" ? acc[boostAddress].eolDate || fileVersion.date : null;
acc[boostAddress] = { boost, eolDate, foundInCurrentBatch: true, fileVersion };
// add boosts to the accumulator
for (const boost of boosts) {
const boostAddress = normalizeAddressOrThrow(boost.earnContractAddress);
if (!acc[boostAddress]) {
const eolDate = boost.status === "closed" ? fileVersion.date : null;
acc[boostAddress] = { fileVersion, eolDate, boost, foundInCurrentBatch: true };
} else {
const eolDate = boost.status === "closed" ? acc[boostAddress].eolDate || fileVersion.date : null;
acc[boostAddress] = { boost, eolDate, foundInCurrentBatch: true, fileVersion };
}
}
}

// mark all deleted vaults as eol if not already done
for (const boostAddress of Object.keys(acc)) {
if (!acc[boostAddress].foundInCurrentBatch) {
acc[boostAddress].boost.status = "closed";
acc[boostAddress].eolDate = acc[boostAddress].eolDate || fileVersion.date;
// mark all deleted vaults as eol if not already done
for (const boostAddress of Object.keys(acc)) {
if (!acc[boostAddress].foundInCurrentBatch) {
acc[boostAddress].boost.status = "closed";
acc[boostAddress].eolDate = acc[boostAddress].eolDate || fileVersion.date;
}
}
}

return acc;
return acc;
} catch (error) {
logger.error({ msg: "Could not process boost file", data: { fileVersion, boosts }, error });
logger.debug(error);
return acc;
}
}, {} as Record<string, { foundInCurrentBatch: boolean; fileVersion: GitFileVersion; eolDate: Date | null; boost: RawBeefyBoost }>),

// flatten the accumulator
Expand Down
102 changes: 60 additions & 42 deletions src/protocol/beefy/connector/vault-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,55 +166,65 @@ export function beefyVaultsFromGitHistory$(chain: Chain): Rx.Observable<BeefyVau
Rx.pipe(
// process the vaults in chronolical order and mark the eol date if found
Rx.reduce((acc, { fileVersion, vaults }) => {
// reset the foundInCurrentBatch flag
for (const vaultAddress of Object.keys(acc)) {
acc[vaultAddress].foundInCurrentBatch = false;
}
try {
// reset the foundInCurrentBatch flag
for (const vaultAddress of Object.keys(acc)) {
acc[vaultAddress].foundInCurrentBatch = false;
}

// add vaults to the accumulator
for (const vault of vaults) {
// ignore those without earned token address
if (!vault.earnedTokenAddress) {
const msg = { msg: "Could not find vault earned token address for vault", data: { vaultId: vault.id } };
if (vault?.id?.endsWith("-rp") || vault?.id === "bifi-pool") {
logger.debug(msg);
} else {
logger.error(msg);
}
logger.trace(vault);
continue;
}

// add vaults to the accumulator
for (const vault of vaults) {
// ignore those without earned token address
if (!vault.earnedTokenAddress) {
const msg = { msg: "Could not find vault earned token address for vault", data: { vaultId: vault.id } };
if (vault?.id?.endsWith("-rp") || vault?.id === "bifi-pool") {
logger.debug(msg);
// index by contract address since beefy's team changes ids when the vault is eol
if (!ethers.utils.isAddress(vault.earnContractAddress)) {
logger.error({
msg: "Vault earnContractAddress is invalid, ignoring",
data: { vaultId: vault.id, earnContractAddress: vault.earnContractAddress },
});
logger.trace(vault);
continue;
}
const vaultAddress = normalizeAddressOrThrow(vault.earnContractAddress);
if (!acc[vaultAddress]) {
const eolDate = vault.status === "eol" ? fileVersion.date : null;
acc[vaultAddress] = { fileVersion, eolDate, vault, foundInCurrentBatch: true };
} else {
logger.error(msg);
acc[vaultAddress].foundInCurrentBatch = true;

const eolDate = vault.status === "eol" ? acc[vaultAddress].eolDate || fileVersion.date : null;
acc[vaultAddress] = { vault, eolDate, foundInCurrentBatch: true, fileVersion };
}
logger.trace(vault);
continue;
}

// index by contract address since beefy's team changes ids when the vault is eol
if (!ethers.utils.isAddress(vault.earnContractAddress)) {
logger.error({
msg: "Vault earnContractAddress is invalid, ignoring",
data: { vaultId: vault.id, earnContractAddress: vault.earnContractAddress },
});
logger.trace(vault);
continue;
}
const vaultAddress = normalizeAddressOrThrow(vault.earnContractAddress);
if (!acc[vaultAddress]) {
const eolDate = vault.status === "eol" ? fileVersion.date : null;
acc[vaultAddress] = { fileVersion, eolDate, vault, foundInCurrentBatch: true };
} else {
acc[vaultAddress].foundInCurrentBatch = true;

const eolDate = vault.status === "eol" ? acc[vaultAddress].eolDate || fileVersion.date : null;
acc[vaultAddress] = { vault, eolDate, foundInCurrentBatch: true, fileVersion };
// mark all deleted vaults as eol if not already done
for (const vaultAddress of Object.keys(acc)) {
if (!acc[vaultAddress].foundInCurrentBatch) {
acc[vaultAddress].vault.status = "eol";
acc[vaultAddress].eolDate = acc[vaultAddress].eolDate || fileVersion.date;
}
}
}

// mark all deleted vaults as eol if not already done
for (const vaultAddress of Object.keys(acc)) {
if (!acc[vaultAddress].foundInCurrentBatch) {
acc[vaultAddress].vault.status = "eol";
acc[vaultAddress].eolDate = acc[vaultAddress].eolDate || fileVersion.date;
}
return acc;
} catch (error) {
logger.error({
msg: "Could not process vaults",
data: { fileVersion, vaults },
error,
});
logger.debug(error);
return acc;
}

return acc;
}, {} as Record<string, { foundInCurrentBatch: boolean; fileVersion: GitFileVersion; eolDate: Date | null; vault: RawBeefyVault }>),
// flatten the accumulator
Rx.map((acc) => Object.values(acc)),
Expand Down Expand Up @@ -250,7 +260,15 @@ export function beefyVaultsFromGitHistory$(chain: Chain): Rx.Observable<BeefyVau
}),

// just emit the vault
Rx.map(({ vault, eolDate }) => ({ vault: rawVaultToBeefyVault(chain, vault, eolDate), rawVault: vault })),
Rx.concatMap(({ vault, eolDate }) => {
try {
return Rx.of({ vault: rawVaultToBeefyVault(chain, vault, eolDate), rawVault: vault });
} catch (error) {
logger.error({ msg: "Could not map raw vault to expected format", data: { rawVault: vault }, error });
logger.debug(error);
return Rx.EMPTY;
}
}),
),

// ignore cowcentrated vaults, those are covered by thegraph
Expand Down

0 comments on commit bfe0d28

Please sign in to comment.