Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Alexey-N-Chernyshov/a2be64e76920b7b3ac0ed0b7b560587f to your computer and use it in GitHub Desktop.
Save Alexey-N-Chernyshov/a2be64e76920b7b3ac0ed0b7b560587f to your computer and use it in GitHub Desktop.
Web changes for Kensetsu configurable stables
async subscribeOnAverageCollateralPrices(context): Promise<void> {
const { commit, state } = vaultActionContext(context);
const { collaterals, averageCollateralPriceSubscriptions } = state;
const newIds: string[] = [];
const idsToRemove: string[] = [];
// Check if there are new collateral assets or some of them were removed
// TODO Please, check - stablecoins may be different as well as collaterals
for (const collateralId in collaterals) {
if (!(collateralId in averageCollateralPriceSubscriptions)) {
newIds.push(collateralId);
}
}
for (const priceSubId in averageCollateralPriceSubscriptions) {
if (!(priceSubId in collaterals)) {
idsToRemove.push(priceSubId);
}
}
// Unsubscribe from removed collateral assets
if (idsToRemove.length) {
commit.removeAverageCollateralPriceSubscriptions(idsToRemove);
}
if (!newIds.length) {
return;
}
// Subscribe on new collateral assets if they are not subscribed yet
const newSubscriptions: Record<string, Subscription> = {};
// TODO should be CollateralAssetId and StablecoinAssetId
for (const collateralAddress of newIds) {
// TODO get StablecoinInfo for StablecoinAssetId like:
// https://github.com/sora-xor/sora2-network/blob/master/pallets/kensetsu/src/lib.rs#L1176
// if StablecoinInfo pegged to Oracle (band), then
// 1. get collateral price in DAI (as it now with `getAveragePrice(collateralAddress, DAI, Sell)`
// (https://github.com/sora-xor/sora2-network/blob/master/pallets/kensetsu/src/lib.rs#L1184)
// 2. get PegAsset price from Band, it is in USD, StablecoinInfo contains Peg Symbol for Oracle
// (https://github.com/sora-xor/sora2-network/blob/master/pallets/kensetsu/src/lib.rs#L1190)
// 3. collateral_price = collateral_price_in_dai / oracle_price, simply divide (1) by (2)
// (https://github.com/sora-xor/sora2-network/blob/master/pallets/kensetsu/src/lib.rs#L1199)
// else StablecoinInfo pegged to Sora with PegAssetId, then
// StablecoinInfo contains PegAssetId
// getAveragePrice(collateralAddress, PegAssetId, Sell)
// (https://github.com/sora-xor/sora2-network/blob/master/pallets/kensetsu/src/lib.rs#L1199)
if (collateralAddress !== DaiAddress) {
const sub = api.swap.subscribeOnReserves(collateralAddress, DaiAddress)?.subscribe((payload) => {
try {
const averagePrice = getAveragePrice(collateralAddress, DaiAddress, PriceVariant.Sell, payload);
commit.setAverageCollateralPrice({ address: collateralAddress, price: averagePrice });
} catch (error) {
commit.setAverageCollateralPrice({ address: collateralAddress, price: null });
console.warn(`[Kensetsu] getAveragePrice with ${collateralAddress}`, error);
}
});
if (sub) {
newSubscriptions[collateralAddress] = sub;
}
}
}
commit.setAverageCollateralPriceSubscriptions(newSubscriptions);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment