Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: enable Morpho Blue external positions #114

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion subgraphs/enzyme-core/contexts/arbitrum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export const arbitrum: Context<Variables> = {
theGraphStakingProxyAddress: '0x0000000000000000000000000000000000000000',
lusdAddress: '0x0000000000000000000000000000000000000000',
compAddress: '0x354a6da3fcde098f8389cad84b0182725c6c91de',
// morphoBlueAddress: "0x0000000000000000000000000000000000000000",
morphoBlueAddress: '0x0000000000000000000000000000000000000000',
aliceOrderManager: '0x0000000000000000000000000000000000000000',
},
persistent: {
Expand Down
2 changes: 1 addition & 1 deletion subgraphs/enzyme-core/contexts/ethereum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export const ethereum: Context<Variables> = {
theGraphStakingProxyAddress: suluDeployment.externalContracts.theGraphStakingProxy,
lusdAddress: '0x5f98805a4e8be255a32880fdec7f6728c6568ba0',
compAddress: '0xc00e94cb662c3520282e6f5717214004a7f26888',
// morphoBlueAddress: "0xbbbbbbbbbb9cc5e90e3b3af64bdaf62c37eeffcb",
morphoBlueAddress: '0xbbbbbbbbbb9cc5e90e3b3af64bdaf62c37eeffcb',
aliceOrderManager: '0x841473a19279e54a850e9083a3a57de9e6244d2e',
},
persistent: {
Expand Down
2 changes: 1 addition & 1 deletion subgraphs/enzyme-core/contexts/polygon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export const polygon: Context<Variables> = {
theGraphStakingProxyAddress: polygonV4Deployment.externalContracts.theGraphStakingProxy,
lusdAddress: '0x0000000000000000000000000000000000000000',
compAddress: '0x8505b9d2254a7ae468c0e9dd10ccea3a837aef5c',
// morphoBlueAddress: "0x0000000000000000000000000000000000000000",
morphoBlueAddress: '0x0000000000000000000000000000000000000000',
aliceOrderManager: '0x0000000000000000000000000000000000000000',
},
persistent: {
Expand Down
2 changes: 1 addition & 1 deletion subgraphs/enzyme-core/contexts/testnet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export const testnet: Context<Variables> = {
theGraphStakingProxyAddress: testnetV4Deployment.externalContracts.theGraphStakingProxy,
lusdAddress: '0x0000000000000000000000000000000000000000',
compAddress: '0x8505b9d2254a7ae468c0e9dd10ccea3a837aef5c',
// morphoBlueAddress: "0x0000000000000000000000000000000000000000",
morphoBlueAddress: '0x0000000000000000000000000000000000000000',
aliceOrderManager: '0x0000000000000000000000000000000000000000',
},
persistent: {
Expand Down
157 changes: 82 additions & 75 deletions subgraphs/enzyme-core/entities/MorphoBluePosition.ts
Original file line number Diff line number Diff line change
@@ -1,90 +1,97 @@
// import { Address, Bytes, ethereum } from '@graphprotocol/graph-ts';
// import { AssetAmount, ExternalPositionType, MorphoBlueMarket, MorphoBluePosition, MorphoBluePositionChange, Vault } from '../generated/schema';
// import { useVault } from './Vault';
// import { logCritical, uniqueEventId } from '@enzymefinance/subgraph-utils';
// import { getActivityCounter } from './Counter';
// import { ExternalSdk } from '../generated/contracts/ExternalSdk';
// import { morphoBlueAddress } from '../generated/addresses';
// import { ensureAsset } from './Asset';
import { Address, Bytes, ethereum } from '@graphprotocol/graph-ts';
import {
AssetAmount,
ExternalPositionType,
MorphoBlueMarket,
MorphoBluePosition,
MorphoBluePositionChange,
Vault,
} from '../generated/schema';
import { useVault } from './Vault';
import { logCritical, uniqueEventId } from '@enzymefinance/subgraph-utils';
import { getActivityCounter } from './Counter';
import { ExternalSdk } from '../generated/contracts/ExternalSdk';
import { morphoBlueAddress } from '../generated/addresses';
import { ensureAsset } from './Asset';

// function morphoBlueMarketId(morphoBluePosition: Address, marketId: Bytes): string {
// return morphoBluePosition.toHex() + '/' + marketId.toHex();
// }
function morphoBlueMarketId(morphoBluePosition: Address, marketId: Bytes): string {
return morphoBluePosition.toHex() + '/' + marketId.toHex();
}

// export function ensureMorphoBlueMarket(morphoBluePosition: Address, marketId: Bytes): MorphoBlueMarket {
// let id = morphoBlueMarketId(morphoBluePosition, marketId);
// let market = MorphoBlueMarket.load(id);
export function ensureMorphoBlueMarket(morphoBluePosition: Address, marketId: Bytes): MorphoBlueMarket {
let id = morphoBlueMarketId(morphoBluePosition, marketId);
let market = MorphoBlueMarket.load(id);

// if (market) {
// return market;
// }
if (market) {
return market;
}

// let contract = ExternalSdk.bind(morphoBlueAddress);
// let marketParams = contract.try_idToMarketParams(marketId);
let contract = ExternalSdk.bind(morphoBlueAddress);
let marketParams = contract.try_idToMarketParams(marketId);

// if (marketParams.reverted) {
// logCritical('Unable to obtain market params for market id {}',[marketId.toHex()])
// }
if (marketParams.reverted) {
logCritical('Unable to obtain market params for market id {}', [marketId.toHex()]);
}

// let loanToken = ensureAsset(marketParams.value.loanToken);
// let collateralToken = ensureAsset(marketParams.value.collateralToken)
let loanToken = ensureAsset(marketParams.value.loanToken);
let collateralToken = ensureAsset(marketParams.value.collateralToken);

// market = new MorphoBlueMarket(id);
// market.marketId = marketId;
// market.morpheBluePosition = morphoBluePosition.toHex();
// market.loanToken = loanToken.id;
// market.collateralToken = collateralToken.id;
// market.removed = false;
// market.save();
market = new MorphoBlueMarket(id);
market.marketId = marketId;
market.morphoBluePosition = morphoBluePosition.toHex();
market.loanToken = loanToken.id;
market.collateralToken = collateralToken.id;
market.removed = false;
market.save();

// return market;
// }
return market;
}

// export function createMorphoBluePosition(
// externalPositionAddress: Address,
// vaultAddress: Address,
// type: ExternalPositionType,
// ): MorphoBluePosition {
// let position = new MorphoBluePosition(externalPositionAddress.toHex());
// position.vault = useVault(vaultAddress.toHex()).id;
// position.active = true;
// position.type = type.id;
// position.save();
export function createMorphoBluePosition(
externalPositionAddress: Address,
vaultAddress: Address,
type: ExternalPositionType,
): MorphoBluePosition {
let position = new MorphoBluePosition(externalPositionAddress.toHex());
position.vault = useVault(vaultAddress.toHex()).id;
position.active = true;
position.type = type.id;
position.save();

// return position;
// }
return position;
}

// export function useMorphoBluePosition(id: string): MorphoBluePosition {
// let position = MorphoBluePosition.load(id);
// if (position == null) {
// logCritical('Failed to load MorphoBluePosition {}.', [id]);
// }
export function useMorphoBluePosition(id: string): MorphoBluePosition {
let position = MorphoBluePosition.load(id);
if (position == null) {
logCritical('Failed to load MorphoBluePosition {}.', [id]);
}

// return position as MorphoBluePosition;
// }
return position as MorphoBluePosition;
}

// export function createMorphoBluePositionChange(
// MorphoBluePositionAddress: Address,
// changeType: string,
// vault: Vault,
// market: MorphoBlueMarket,
// assetAmount: AssetAmount,
// event: ethereum.Event,
// ): MorphoBluePositionChange {
// let change = new MorphoBluePositionChange(uniqueEventId(event));
// change.morphoBluePositionChangeType = changeType;
// change.externalPosition = MorphoBluePositionAddress.toHex();
// change.vault = vault.id;
// change.timestamp = event.block.timestamp.toI32();
// change.market = market.id;
// change.assetAmount = assetAmount.id;
// change.activityCounter = getActivityCounter();
// change.activityCategories = ['Vault'];
// change.activityType = 'Trade';
// change.save();
export function createMorphoBluePositionChange(
morphoBluePositionAddress: Address,
changeType: string,
vault: Vault,
market: MorphoBlueMarket,
assetAmount: AssetAmount,
event: ethereum.Event,
): MorphoBluePositionChange {
let change = new MorphoBluePositionChange(uniqueEventId(event));
change.morphoBluePositionChangeType = changeType;
change.externalPosition = morphoBluePositionAddress.toHex();
change.vault = vault.id;
change.timestamp = event.block.timestamp.toI32();
change.market = market.id;
change.assetAmount = assetAmount.id;
change.activityCounter = getActivityCounter();
change.activityCategories = ['Vault'];
change.activityType = 'Trade';
change.save();

// vault.lastAssetUpdate = event.block.timestamp.toI32();
// vault.save();
vault.lastAssetUpdate = event.block.timestamp.toI32();
vault.save();

// return change;
// }
return change;
}
Loading