-
Notifications
You must be signed in to change notification settings - Fork 76
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1012 from InjectiveLabs/dev
#PRMaster
- Loading branch information
Showing
578 changed files
with
27,256 additions
and
12,491 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
app/client/streams/data-integrity/strategies/BaseDataIntegrityStrategy.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export abstract class BaseDataIntegrityStrategy<T> { | ||
public args: T | ||
|
||
constructor(args: T) { | ||
this.args = args | ||
} | ||
} |
67 changes: 67 additions & 0 deletions
67
app/client/streams/data-integrity/strategies/account/BankBalance.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import { Coin } from '@injectivelabs/ts-types' | ||
import { ConcreteDataIntegrityStrategy } from '@/app/client/streams/data-integrity/types' | ||
import { BaseDataIntegrityStrategy } from '@/app/client/streams/data-integrity/strategies' | ||
import { indexerAccountPortfolioApi } from '@/app/Services' | ||
|
||
export class BankBalanceIntegrityStrategy | ||
extends BaseDataIntegrityStrategy<void> | ||
implements ConcreteDataIntegrityStrategy<void, Coin[]> | ||
{ | ||
static make(): BankBalanceIntegrityStrategy { | ||
return new BankBalanceIntegrityStrategy() | ||
} | ||
|
||
async validate(): Promise<void> { | ||
const walletStore = useWalletStore() | ||
const accountStore = useAccountStore() | ||
|
||
if (!walletStore.isUserWalletConnected) { | ||
return | ||
} | ||
|
||
const latestBankBalances = await this.fetchData() | ||
|
||
if (latestBankBalances.length === 0) { | ||
return | ||
} | ||
|
||
const existingBankBalances = [...accountStore.bankBalances] | ||
const isDataValid = this.verifyData( | ||
existingBankBalances, | ||
latestBankBalances | ||
) | ||
|
||
if (!isDataValid) { | ||
accountStore.cancelBankBalanceStream() | ||
accountStore.$patch({ bankBalances: await this.fetchData() }) | ||
accountStore.streamBankBalance() | ||
} | ||
} | ||
|
||
verifyData( | ||
existingBankBalances: Coin[], | ||
latestBankBalances: Coin[] | ||
): boolean { | ||
if (existingBankBalances.length !== latestBankBalances.length) { | ||
return false | ||
} | ||
|
||
return latestBankBalances.every((latestBalance) => { | ||
const existingBalance = existingBankBalances.find( | ||
(b) => b.denom === latestBalance.denom | ||
) | ||
return existingBalance && existingBalance.amount === latestBalance.amount | ||
}) | ||
} | ||
|
||
async fetchData(): Promise<Coin[]> { | ||
const walletStore = useWalletStore() | ||
|
||
const { bankBalancesList } = | ||
await indexerAccountPortfolioApi.fetchAccountPortfolio( | ||
walletStore.authZOrInjectiveAddress | ||
) | ||
|
||
return bankBalancesList || [] | ||
} | ||
} |
109 changes: 109 additions & 0 deletions
109
app/client/streams/data-integrity/strategies/account/SubaccountBalance.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
import { ConcreteDataIntegrityStrategy } from '@/app/client/streams/data-integrity/types' | ||
import { BaseDataIntegrityStrategy } from '@/app/client/streams/data-integrity/strategies' | ||
import { SubaccountBalance } from '@/types' | ||
import { indexerAccountPortfolioApi } from '@/app/Services' | ||
import { | ||
getDefaultAccountBalances, | ||
getNonDefaultSubaccountBalances | ||
} from '@/app/client/utils/account' | ||
|
||
export class SubaccountBalanceIntegrityStrategy | ||
extends BaseDataIntegrityStrategy<void> | ||
implements | ||
ConcreteDataIntegrityStrategy<void, Record<string, SubaccountBalance[]>> | ||
{ | ||
static make(): SubaccountBalanceIntegrityStrategy { | ||
return new SubaccountBalanceIntegrityStrategy() | ||
} | ||
|
||
async validate(): Promise<void> { | ||
const walletStore = useWalletStore() | ||
const accountStore = useAccountStore() | ||
|
||
if (!walletStore.isUserWalletConnected) { | ||
return | ||
} | ||
|
||
if (walletStore.authZOrDefaultSubaccountId === accountStore.subaccountId) { | ||
return | ||
} | ||
|
||
const latestSubaccountBalancesMap = await this.fetchData() | ||
|
||
const existingSubaccountBalancesMap = { | ||
...accountStore.subaccountBalancesMap | ||
} | ||
|
||
const isDataValid = this.verifyData( | ||
existingSubaccountBalancesMap, | ||
latestSubaccountBalancesMap | ||
) | ||
|
||
if (!isDataValid) { | ||
accountStore.cancelSubaccountBalanceStream() | ||
accountStore.$patch({ | ||
subaccountBalancesMap: await this.fetchData() | ||
}) | ||
accountStore.streamSubaccountBalance() | ||
} | ||
} | ||
|
||
verifyData( | ||
existingMap: Record<string, SubaccountBalance[]>, | ||
latestMap: Record<string, SubaccountBalance[]> | ||
): boolean { | ||
const existingKeys = Object.keys(existingMap) | ||
const latestKeys = Object.keys(latestMap) | ||
if ( | ||
existingKeys.length !== latestKeys.length || | ||
!existingKeys.every((key) => latestKeys.includes(key)) | ||
) { | ||
return false | ||
} | ||
|
||
return latestKeys.every((subaccountId) => { | ||
const existingBalances = existingMap[subaccountId] || [] | ||
const latestBalances = latestMap[subaccountId] || [] | ||
|
||
if (existingBalances.length !== latestBalances.length) { | ||
return false | ||
} | ||
|
||
return latestBalances.every((latestBalance) => { | ||
const existingBalance = existingBalances.find( | ||
(b) => b.denom === latestBalance.denom | ||
) | ||
|
||
return ( | ||
existingBalance && | ||
existingBalance.availableBalance === latestBalance.availableBalance && | ||
existingBalance.totalBalance === latestBalance.totalBalance | ||
) | ||
}) | ||
}) | ||
} | ||
|
||
async fetchData(): Promise<Record<string, SubaccountBalance[]>> { | ||
const walletStore = useWalletStore() | ||
|
||
const accountPortfolio = | ||
await indexerAccountPortfolioApi.fetchAccountPortfolio( | ||
walletStore.authZOrInjectiveAddress | ||
) | ||
|
||
const defaultAccountBalances = getDefaultAccountBalances( | ||
accountPortfolio.subaccountsList, | ||
walletStore.authZOrDefaultSubaccountId | ||
) | ||
|
||
const nonDefaultSubaccounts = getNonDefaultSubaccountBalances( | ||
accountPortfolio.subaccountsList, | ||
walletStore.authZOrDefaultSubaccountId | ||
) | ||
|
||
return { | ||
[walletStore.authZOrDefaultSubaccountId]: defaultAccountBalances, | ||
...nonDefaultSubaccounts | ||
} | ||
} | ||
} |
2 changes: 2 additions & 0 deletions
2
app/client/streams/data-integrity/strategies/account/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './BankBalance' | ||
export * from './SubaccountBalance' |
113 changes: 113 additions & 0 deletions
113
app/client/streams/data-integrity/strategies/derivative/OraclePrices.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
import { | ||
MarketType, | ||
UiPerpetualMarketWithToken, | ||
UiBinaryOptionsMarketWithToken | ||
} from '@injectivelabs/sdk-ui-ts' | ||
import { | ||
MarketIdsArgs, | ||
ConcreteDataIntegrityStrategy | ||
} from '@/app/client/streams/data-integrity/types' | ||
import { BaseDataIntegrityStrategy } from '@/app/client/streams/data-integrity/strategies' | ||
import { MarketMarkPriceMap } from '@/types' | ||
import { indexerOracleApi } from '@/app/Services' | ||
|
||
export class DerivativeOraclePriceIntegrityStrategy | ||
extends BaseDataIntegrityStrategy<MarketIdsArgs> | ||
implements ConcreteDataIntegrityStrategy<MarketIdsArgs, MarketMarkPriceMap> | ||
{ | ||
static make( | ||
marketIds: MarketIdsArgs | ||
): DerivativeOraclePriceIntegrityStrategy { | ||
return new DerivativeOraclePriceIntegrityStrategy(marketIds) | ||
} | ||
|
||
async validate(): Promise<void> { | ||
const { args: marketIds } = this | ||
|
||
if (!marketIds) { | ||
return | ||
} | ||
|
||
const latestMarketPrices = await this.fetchData() | ||
|
||
if (!latestMarketPrices) { | ||
return | ||
} | ||
|
||
if (Object.keys(latestMarketPrices).length === 0) { | ||
return | ||
} | ||
|
||
const derivativeStore = useDerivativeStore() | ||
|
||
const existingMarketPrices = { ...derivativeStore.marketMarkPriceMap } | ||
const isDataValid = this.verifyData( | ||
existingMarketPrices, | ||
latestMarketPrices | ||
) | ||
|
||
if (!isDataValid) { | ||
derivativeStore.cancelMarketsMarkPrices() | ||
derivativeStore.$patch({ marketMarkPriceMap: await this.fetchData() }) | ||
derivativeStore.streamMarketsMarkPrices() | ||
} | ||
} | ||
|
||
verifyData( | ||
existingMarketPrices: MarketMarkPriceMap, | ||
latestMarketPrices: MarketMarkPriceMap | ||
): boolean { | ||
return Object.entries(latestMarketPrices).every( | ||
([marketId, latestPrice]) => { | ||
const existingPrice = existingMarketPrices[marketId] | ||
|
||
return existingPrice && existingPrice.price === latestPrice.price | ||
} | ||
) | ||
} | ||
|
||
async fetchData(): Promise<MarketMarkPriceMap> { | ||
const { args: marketIds } = this | ||
|
||
if (!marketIds) { | ||
return {} | ||
} | ||
|
||
const derivativeStore = useDerivativeStore() | ||
const markets = derivativeStore.markets.filter((market) => | ||
marketIds.includes(market.marketId) | ||
) | ||
|
||
const pricePromises = markets.map((market) => | ||
(market.subType !== MarketType.BinaryOptions | ||
? indexerOracleApi.fetchOraclePrice({ | ||
oracleType: market.oracleType, | ||
baseSymbol: (market as UiPerpetualMarketWithToken).oracleBase, | ||
quoteSymbol: (market as UiPerpetualMarketWithToken).oracleQuote | ||
}) | ||
: indexerOracleApi.fetchOraclePriceNoThrow({ | ||
baseSymbol: (market as UiBinaryOptionsMarketWithToken).oracleSymbol, | ||
quoteSymbol: (market as UiBinaryOptionsMarketWithToken) | ||
.oracleProvider, | ||
oracleType: market.oracleType | ||
}) | ||
).then((oraclePrice) => ({ | ||
marketId: market.marketId, | ||
price: oraclePrice.price | ||
})) | ||
) | ||
|
||
const marketPricesResults = await Promise.all(pricePromises) | ||
|
||
return marketPricesResults.reduce( | ||
(accumulatedMarketPrices, marketPrice) => { | ||
if (marketPrice) { | ||
accumulatedMarketPrices[marketPrice.marketId] = marketPrice | ||
} | ||
|
||
return accumulatedMarketPrices | ||
}, | ||
{} as MarketMarkPriceMap | ||
) | ||
} | ||
} |
Oops, something went wrong.