Skip to content

Commit

Permalink
Merge pull request #2401 from zeitgeistpm/tr-more-asset-refactor
Browse files Browse the repository at this point in the history
More asset system stuff
  • Loading branch information
Robiquet authored May 13, 2024
2 parents 331cec0 + e61a086 commit 6b4f274
Show file tree
Hide file tree
Showing 15 changed files with 75 additions and 78 deletions.
3 changes: 3 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,6 @@ IPFS_NODE_BASIC_AUTH_PASSWORD=xxxxx
NEXT_PUBLIC_SHOW_AIRDROP=true


NEXT_PUBLIC_LAST_MARKET_ID_BEFORE_ASSET_MIGRATION=874


Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ const AssetTradingButtons = ({
{tradeItem && (
<Modal open={isOpen} onClose={() => setIsOpen(false)}>
<Dialog.Panel className="w-full max-w-[564px] rounded-[10px] bg-white">
{market?.scoringRule === ScoringRule.Lmsr ? (
{market?.scoringRule === ScoringRule.AmmCdaHybrid ? (
<Amm2TradeForm
marketId={marketId}
initialAsset={assetId}
Expand Down
3 changes: 2 additions & 1 deletion components/liquidity/MarketLiquiditySection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ export const MarketLiquiditySection = ({
}) => {
const marketHasPool =
(market?.scoringRule === ScoringRule.Cpmm && market.pool != null) ||
(market?.scoringRule === ScoringRule.Lmsr && market.neoPool != null);
(market?.scoringRule === ScoringRule.AmmCdaHybrid &&
market.neoPool != null);

return (
<>
Expand Down
5 changes: 3 additions & 2 deletions lib/constants/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,5 +115,6 @@ export const endpointsStaging = getEndpointOptions("staging");
export const endpointOptions =
environment === "production" ? endpointsProduction : endpointsStaging;

export const LAST_MARKET_ID_BEFORE_ASSET_MIGRATION =
environment === "production" ? 9999 : 9999;
export const LAST_MARKET_ID_BEFORE_ASSET_MIGRATION = Number(
process.env.NEXT_PUBLIC_LAST_MARKET_ID_BEFORE_ASSET_MIGRATION,
);
2 changes: 1 addition & 1 deletion lib/gql/trending-markets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ const getTrendingMarkets = async (
order: MarketOrderByInput.IdDesc,
where: {
status_eq: MarketStatus.Active,
scoringRule_eq: ScoringRule.Lmsr,
scoringRule_eq: ScoringRule.AmmCdaHybrid,
...marketMetaFilter,
},
});
Expand Down
41 changes: 16 additions & 25 deletions lib/hooks/queries/useAccountTokenPositions.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { useQuery } from "@tanstack/react-query";
import { isRpcSdk } from "@zeitgeistpm/sdk";
import { isIndexedSdk } from "@zeitgeistpm/sdk";
import { parseAssetIdString } from "lib/util/parse-asset-id";
import { useSdkv2 } from "../useSdkv2";

export const positionsRootKey = "account-token-positions";
Expand All @@ -10,36 +11,26 @@ export const useAccountTokenPositions = (address?: string) => {
return useQuery(
[id, positionsRootKey, address],
async () => {
if (sdk && isRpcSdk(sdk) && address) {
// todo: we may wish to add this back and stitch the two data sources together
// to get back the fast initial load along with faster updates issue #1945
// const { accountBalances } = await sdk.indexer.accountBalances({
// where: {
// account: {
// accountId_eq: address,
// },
// balance_gt: 0,
// },
// });
if (sdk && isIndexedSdk(sdk) && address) {
const { accountBalances } = await sdk.indexer.accountBalances({
where: {
account: {
accountId_eq: address,
},
balance_gt: 0,
},
});

const accounts = await sdk.api.query.tokens.accounts.entries(address);

const accountBalances = accounts
.map((account) => {
const assetId = account[0].args[1];
const balance = account[1].free.toString();

return { assetId, balance };
})
.filter((account) => Number(account.balance) > 0);

return accountBalances;
return accountBalances.map(({ assetId, balance }) => ({
assetId: parseAssetIdString(assetId)!,
balance,
}));
}
return null;
},
{
keepPreviousData: true,
enabled: Boolean(sdk && isRpcSdk(sdk) && address),
enabled: Boolean(sdk && isIndexedSdk(sdk) && address),
staleTime: 10_000,
},
);
Expand Down
4 changes: 2 additions & 2 deletions lib/hooks/queries/useBalance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export const fetchAssetBalance = async (
} else if (IOCurrencyAsset.is(assetId)) {
if (
IOMarketOutcomeAssetId.is(assetId) &&
// new market assets need queried with marketAssets.account
// new market assets need to be queried with marketAssets.account
getMarketIdOf(assetId) > LAST_MARKET_ID_BEFORE_ASSET_MIGRATION
) {
const balance = await api.query.marketAssets.account(assetId, address);
Expand All @@ -72,7 +72,7 @@ export const fetchAssetBalance = async (
);
return new Decimal(balance.unwrap().balance.toString());
} else {
const balance = await api.query.campaignAssets.account(
const balance = await api.query.customAssets.account(
assetId.CustomAsset,
address,
);
Expand Down
2 changes: 1 addition & 1 deletion lib/hooks/queries/useInfiniteMarkets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ export const useInfiniteMarkets = (
: {}),
},
{
scoringRule_eq: ScoringRule.Lmsr,
scoringRule_eq: ScoringRule.AmmCdaHybrid,
neoPool_isNull: withLiquidityOnly ? false : undefined,
},
],
Expand Down
2 changes: 1 addition & 1 deletion lib/hooks/queries/useMarketSpotPrices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export const useMarketSpotPrices = (
if (!enabled) return;
const spotPrices: MarketPrices =
market?.status !== "Resolved"
? market.scoringRule === ScoringRule.Lmsr
? market.scoringRule === ScoringRule.AmmCdaHybrid
? calcMarketPricesAmm2(amm2Pool!)
: calcMarketPrices(market, basePoolBalance!, balances!)
: calcResolvedMarketPrices(market);
Expand Down
4 changes: 2 additions & 2 deletions lib/hooks/queries/usePortfolioPositions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ export const usePortfolioPositions = (
const pools = usePoolsByIds(filter);
const markets = useMarketsByIds(filter);
const amm2MarketIds = markets.data
?.filter((market) => market.scoringRule === ScoringRule.Lmsr)
?.filter((market) => market.scoringRule === ScoringRule.AmmCdaHybrid)
.map((m) => m.marketId);

const { data: amm2SpotPrices } = useAmm2MarketSpotPrices(amm2MarketIds);
Expand Down Expand Up @@ -318,7 +318,7 @@ export const usePortfolioPositions = (
price = calcResolvedMarketPrices(market).get(getIndexOf(assetId));
price24HoursAgo = price;
} else {
if (market.scoringRule === ScoringRule.Lmsr) {
if (market.scoringRule === ScoringRule.AmmCdaHybrid) {
price = lookupAssetPrice(assetId, amm2SpotPrices);

price24HoursAgo = lookupAssetPrice(
Expand Down
4 changes: 2 additions & 2 deletions lib/util/amm2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ export const isValidBuyAmount = (
assetReserve,
amountInMinusFees,
liquidityParameter,
).lessThanOrEqualTo(lsmrConstant)
).lessThanOrEqualTo(lmsrConstant)
) {
return { isValid: false, message: "Amount in too low" };
} else {
Expand Down Expand Up @@ -187,7 +187,7 @@ export const calculateReserveAfterSell = (
return term1.plus(term2).minus(1).ln().mul(liquidity).plus(assetReserve);
};

const lsmrConstant = 0.1;
const lmsrConstant = 0.1;

const calculatePoolNumericalThreshold = (liquidityParameter: Decimal) =>
liquidityParameter.mul(10);
Expand Down
2 changes: 1 addition & 1 deletion lib/util/calc-resolved-market-prices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export const calcResolvedMarketPrices = (
market: FullMarketFragment,
): MarketPrices => {
const assetIds = (
market.scoringRule === ScoringRule.Lmsr
market.scoringRule === ScoringRule.AmmCdaHybrid
? market.neoPool?.account.balances.map((b) =>
parseAssetIdString(b.assetId),
)
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,12 @@
"@web3auth/openlogin-adapter": "^8.0.1",
"@yornaath/batshit": "^0.8.0",
"@yornaath/batshit-devtools-react": "^0.5.4",
"@zeitgeistpm/augment-api": "3.6.2",
"@zeitgeistpm/augment-api": "3.7.0",
"@zeitgeistpm/avatara-nft-sdk": "^1.3.1",
"@zeitgeistpm/avatara-react": "^1.3.2",
"@zeitgeistpm/avatara-util": "^1.2.0",
"@zeitgeistpm/sdk": "3.6.2",
"@zeitgeistpm/utility": "3.6.2",
"@zeitgeistpm/sdk": "3.7.0",
"@zeitgeistpm/utility": "3.7.0",
"axios": "^0.21.4",
"boring-avatars": "^1.6.1",
"decimal.js": "^10.4.3",
Expand Down
3 changes: 2 additions & 1 deletion pages/markets/[marketid].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,8 @@ const Market: NextPage<MarketPageProps> = ({
(market?.scoringRule === ScoringRule.Cpmm &&
poolId != null &&
poolIdLoading === false) ||
(market?.scoringRule === ScoringRule.Lmsr && market.neoPool != null);
(market?.scoringRule === ScoringRule.AmmCdaHybrid &&
market.neoPool != null);

const poolCreationDate = new Date(
indexedMarket.pool?.createdAt ?? indexedMarket.neoPool?.createdAt ?? "",
Expand Down
70 changes: 35 additions & 35 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4866,14 +4866,14 @@ __metadata:
languageName: node
linkType: hard

"@zeitgeistpm/augment-api@npm:3.6.2, @zeitgeistpm/augment-api@npm:^3.6.2":
version: 3.6.2
resolution: "@zeitgeistpm/augment-api@npm:3.6.2"
"@zeitgeistpm/augment-api@npm:3.7.0, @zeitgeistpm/augment-api@npm:^3.7.0":
version: 3.7.0
resolution: "@zeitgeistpm/augment-api@npm:3.7.0"
peerDependencies:
"@polkadot/api-base": "*"
"@polkadot/rpc-core": "*"
"@polkadot/types": "*"
checksum: c5f7c6a1dd2d27afdee9ad178e83863b3d114786919b63a3f2c4fcd0f0e2e47a9bf4f4839d616efdec141ff0c31c8602756207e20b333b6ebd3365cec78b9d3b
checksum: ca2658b680406a10c548c159edad4a41fec8f77cad356df243718a3ef779a718c92596243e7161da77931c7563de9f28deaacd13f680143884087279b2ab1f19
languageName: node
linkType: hard

Expand Down Expand Up @@ -4943,40 +4943,40 @@ __metadata:
languageName: node
linkType: hard

"@zeitgeistpm/indexer@npm:^4.6.2":
version: 4.6.2
resolution: "@zeitgeistpm/indexer@npm:4.6.2"
"@zeitgeistpm/indexer@npm:^4.7.0":
version: 4.7.0
resolution: "@zeitgeistpm/indexer@npm:4.7.0"
dependencies:
graphql: ^16.6.0
graphql-request: ^5.0.0
graphql-tag: ^2.12.6
checksum: 0bf8348922fe75b366ebb41557165ac69e0a6ee89d943e7cf11a0f2153b36656552effc01d002eec8e1525052af381c36db7bb2514802b6fcac2bf0cc993928a
checksum: 03c44382e856bc07f2e145dc784a16fbd0a4792c58de83ba289485283b1048904468b8ddfc347675f75af6b5e63bee08828fa4b70421fcc804826f6b28d44851
languageName: node
linkType: hard

"@zeitgeistpm/rpc@npm:^3.6.2":
version: 3.6.2
resolution: "@zeitgeistpm/rpc@npm:3.6.2"
"@zeitgeistpm/rpc@npm:^3.7.0":
version: 3.7.0
resolution: "@zeitgeistpm/rpc@npm:3.7.0"
dependencies:
"@zeitgeistpm/augment-api": ^3.6.2
"@zeitgeistpm/utility": ^3.6.2
"@zeitgeistpm/augment-api": ^3.7.0
"@zeitgeistpm/utility": ^3.7.0
peerDependencies:
"@polkadot/api": "*"
"@polkadot/keyring": "*"
"@polkadot/types": "*"
checksum: bfb9513a44329426d6b607fb62dcb8186a1d2f35cf52316bf85bd2a3c542b03c1b386957253bac6e770d4b58cca0e60d8c88d2bb697084ffd060b73f1af67649
checksum: 7caf29450310ca8224f1afbd17184836564aa4e62a8dee85d05235d252720f43d0ed99481d5011bda226f870ac0f7db9aa0a2629644876aae63812170d9f1d12
languageName: node
linkType: hard

"@zeitgeistpm/sdk@npm:3.6.2":
version: 3.6.2
resolution: "@zeitgeistpm/sdk@npm:3.6.2"
"@zeitgeistpm/sdk@npm:3.7.0":
version: 3.7.0
resolution: "@zeitgeistpm/sdk@npm:3.7.0"
dependencies:
"@zeitgeistpm/augment-api": ^3.6.2
"@zeitgeistpm/indexer": ^4.6.2
"@zeitgeistpm/rpc": ^3.6.2
"@zeitgeistpm/utility": ^3.6.2
"@zeitgeistpm/web3.storage": ^3.6.2
"@zeitgeistpm/augment-api": ^3.7.0
"@zeitgeistpm/indexer": ^4.7.0
"@zeitgeistpm/rpc": ^3.7.0
"@zeitgeistpm/utility": ^3.7.0
"@zeitgeistpm/web3.storage": ^3.7.0
cids: ^1.1.9
decimal.js: ^10.4.3
human-object-diff: ^3.0.0
Expand All @@ -4990,7 +4990,7 @@ __metadata:
"@polkadot/api": "*"
"@polkadot/types": "*"
"@polkadot/util": "*"
checksum: 8ccb96b2008fd25923741b046419ec346d6a5fb731d2b38f820171006c6d1bb56482af33d1d322bca2d59bdd527d15b0dd08b237c976bd06d0a457258ca62dfc
checksum: 82f332edf3b4f7c711c88573d9d7013c265945520f0749a469e4101fd28480566da2acfd11ddf4359cde44f5fb061f310cacf298314db83e0fb9003008ffbbd5
languageName: node
linkType: hard

Expand Down Expand Up @@ -5054,12 +5054,12 @@ __metadata:
"@web3auth/openlogin-adapter": ^8.0.1
"@yornaath/batshit": ^0.8.0
"@yornaath/batshit-devtools-react": ^0.5.4
"@zeitgeistpm/augment-api": 3.6.2
"@zeitgeistpm/augment-api": 3.7.0
"@zeitgeistpm/avatara-nft-sdk": ^1.3.1
"@zeitgeistpm/avatara-react": ^1.3.2
"@zeitgeistpm/avatara-util": ^1.2.0
"@zeitgeistpm/sdk": 3.6.2
"@zeitgeistpm/utility": 3.6.2
"@zeitgeistpm/sdk": 3.7.0
"@zeitgeistpm/utility": 3.7.0
autoprefixer: 10.2.5
axios: ^0.21.4
boring-avatars: ^1.6.1
Expand Down Expand Up @@ -5134,9 +5134,9 @@ __metadata:
languageName: unknown
linkType: soft

"@zeitgeistpm/utility@npm:3.6.2, @zeitgeistpm/utility@npm:^3.6.2":
version: 3.6.2
resolution: "@zeitgeistpm/utility@npm:3.6.2"
"@zeitgeistpm/utility@npm:3.7.0, @zeitgeistpm/utility@npm:^3.7.0":
version: 3.7.0
resolution: "@zeitgeistpm/utility@npm:3.7.0"
dependencies:
decimal.js: ^10.4.3
lodash.omit: ^4.5.0
Expand All @@ -5146,16 +5146,16 @@ __metadata:
"@polkadot/api": "*"
"@polkadot/types": "*"
"@polkadot/util": "*"
checksum: 589bf9bf38672a2bc38676990160bc660a7ef4f8a238927257f826a1c7016f44be176b2be746405ac3e9140a46054df2043ed4edaf4cc4aadd4dd7792b7c1fdd
checksum: b2a8cf5a4caa6e8b8bba8f45494278cd2424c6676691e750ee0294a01cd90683ea4c6c0a78d99a3450aa26e0889413771811410fc441a3113b2e226769b3be5d
languageName: node
linkType: hard

"@zeitgeistpm/web3.storage@npm:^3.6.2":
version: 3.6.2
resolution: "@zeitgeistpm/web3.storage@npm:3.6.2"
"@zeitgeistpm/web3.storage@npm:^3.7.0":
version: 3.7.0
resolution: "@zeitgeistpm/web3.storage@npm:3.7.0"
dependencies:
"@multiformats/sha3": ^3.0.2
"@zeitgeistpm/utility": ^3.6.2
"@zeitgeistpm/utility": ^3.7.0
cids: ^1.1.9
ipfs-http-client: ^60.0.0
ipfs-only-hash: ^4.0.0
Expand All @@ -5164,7 +5164,7 @@ __metadata:
up: ^1.0.2
peerDependencies:
"@polkadot/util": "*"
checksum: 2a9ff3d1276d5a650b1e20ab904ab24b4b806c685b3c2e6e2ebc5e6d1b16c31d80ee15af1fb89e2932391462369ce4ebe125f9dd497e3d95e9d33b21dba041c8
checksum: 220f412a6940298d985a0944fbe695e951ca48d36c5955eccc05af4cc2cae2240d60abcae7aaf36822e0cd2c30320085f5fa4a7af41de29d48d8bc6c160ace53
languageName: node
linkType: hard

Expand Down

0 comments on commit 6b4f274

Please sign in to comment.