From 5abb13271b4a2d638a1f6c4d1b2c8ee6ccd4d874 Mon Sep 17 00:00:00 2001 From: alex Date: Mon, 28 Oct 2024 09:58:14 +0300 Subject: [PATCH 1/4] Update sacra subgraph strategy --- src/strategies/sacra-subgraph/README.md | 49 +++++++++ src/strategies/sacra-subgraph/index.ts | 132 +++++++++++++++++++----- 2 files changed, 153 insertions(+), 28 deletions(-) diff --git a/src/strategies/sacra-subgraph/README.md b/src/strategies/sacra-subgraph/README.md index dd63aaf38..c3c9f3f96 100644 --- a/src/strategies/sacra-subgraph/README.md +++ b/src/strategies/sacra-subgraph/README.md @@ -14,4 +14,53 @@ Calculates users power by hero score from Sacra subgraph. ) { score } + + itemEntities( + where:{ + user: $address + dead: false + } + orderBy: score + orderDirection: desc + first: 1000 + ) { + score + } + + + pawnshopPositionEntities( + where:{ + borrower: $address + open: true + collateralHero_not: null + } + orderBy: collateralHero__score + orderDirection: desc + first: 1000 + ) { + collateralHero { + score + } + borrower { + id + } + } + + pawnshopPositionEntities( + where:{ + borrower: $address + open: true + collateralItem_not: null + } + orderBy: collateralItem__score + orderDirection: desc + first: 1000 + ) { + collateralItem { + score + } + borrower { + id + } + } ``` \ No newline at end of file diff --git a/src/strategies/sacra-subgraph/index.ts b/src/strategies/sacra-subgraph/index.ts index b9d4d4d84..920f8b1c9 100644 --- a/src/strategies/sacra-subgraph/index.ts +++ b/src/strategies/sacra-subgraph/index.ts @@ -7,7 +7,7 @@ const SUBGRAPH_URL = { }; export const author = 'alexandersazonof'; -export const version = '0.0.1'; +export const version = '0.0.2'; export async function strategy( _space, @@ -20,52 +20,128 @@ export async function strategy( // initialize scores const scores = {}; - // If graph doesn't exist return - if (!SUBGRAPH_URL[network]) { - return scores; - } - - const params = { + const queryOne = { heroEntities: { __args: { where: { owner_in: addresses.map((address) => address.toLowerCase()), - id_gt: '', - dead: false + dead: false, + score_gt: 0 }, - orderBy: 'id', - orderDirection: 'asc', + orderBy: 'score', + orderDirection: 'desc', first: 1000 }, - id: true, score: true, owner: { id: true } + }, + itemEntities: { + __args: { + where: { + user_in: addresses.map((address) => address.toLowerCase()), + score_gt: 0 + }, + orderBy: 'score', + orderDirection: 'desc', + first: 1000 + }, + score: true, + user: { + id: true + } + }, + pawnshopPositionEntities: { + __args: { + where: { + borrower_in: addresses.map((address) => address.toLowerCase()), + open: true, + collateralItem_not: null + }, + orderBy: 'collateralItem__score', + orderDirection: 'desc', + first: 1000 + }, + collateralItem: { + score: true + }, + borrower: { + id: true + } } }; - if (snapshot !== 'latest') { - // @ts-ignore - params.heroEntities.__args.block = { number: snapshot }; - } + const queryTwo = { + pawnshopPositionEntities: { + __args: { + where: { + borrower_in: addresses.map((address) => address.toLowerCase()), + open: true, + collateralHero_not: null + }, + orderBy: 'collateralHero__score', + orderDirection: 'desc', + first: 1000 + }, + collateralHero: { + score: true + }, + borrower: { + id: true + } + } + }; - let hasNext = true; - while (hasNext) { - const result = await subgraphRequest(SUBGRAPH_URL[network], params); + const subgraphKeys = Object.keys(SUBGRAPH_URL); + for (let i = 0; i < subgraphKeys.length; i++) { + const queryResultOne = await subgraphRequest( + SUBGRAPH_URL[subgraphKeys[i]], + queryOne + ); - const heroEntities = - result && result.heroEntities ? result.heroEntities : []; - const latest = heroEntities[heroEntities.length - 1]; + const queryResultTwo = await subgraphRequest( + SUBGRAPH_URL[subgraphKeys[i]], + queryTwo + ); - for (const heroEntity of heroEntities) { - const userAddress = getAddress(heroEntity.owner.id); - const score = heroEntity.score; - scores[userAddress] = (scores[userAddress] ?? 0) + score; + if (queryResultOne) { + const heroEntities = queryResultOne.heroEntities + ? queryResultOne.heroEntities + : []; + for (const heroEntity of heroEntities) { + const userAddress = getAddress(heroEntity.owner.id); + const score = heroEntity.score; + scores[userAddress] = (scores[userAddress] ?? 0) + score; + } + const itemEntities = queryResultOne.itemEntities + ? queryResultOne.itemEntities + : []; + for (const itemEntity of itemEntities) { + const userAddress = getAddress(itemEntity.user.id); + const score = itemEntity.score; + scores[userAddress] = (scores[userAddress] ?? 0) + score; + } + const pawnshopItemEntities = queryResultOne.pawnshopPositionEntities + ? queryResultOne.pawnshopPositionEntities + : []; + for (const pawnshopItemEntity of pawnshopItemEntities) { + const userAddress = getAddress(pawnshopItemEntity.borrower.id); + const score = pawnshopItemEntity.collateralItem.score; + scores[userAddress] = (scores[userAddress] ?? 0) + score; + } } - hasNext = heroEntities.length === params.heroEntities.__args.first; - params.heroEntities.__args.where.id_gt = latest ? latest.id : ''; + if (queryResultTwo) { + const pawnshopHeroEntities = queryResultTwo.pawnshopPositionEntities + ? queryResultTwo.pawnshopPositionEntities + : []; + for (const pawnshopHeroEntity of pawnshopHeroEntities) { + const userAddress = getAddress(pawnshopHeroEntity.borrower.id); + const score = pawnshopHeroEntity.collateralHero.score; + scores[userAddress] = (scores[userAddress] ?? 0) + score; + } + } } return scores || {}; From 608e01e5f95bc935789c767eae1870c7d5cebd92 Mon Sep 17 00:00:00 2001 From: alex Date: Wed, 30 Oct 2024 09:49:33 +0300 Subject: [PATCH 2/4] Change sacra subgraph logic --- src/strategies/sacra-subgraph/index.ts | 99 +++++++++++++------------- 1 file changed, 49 insertions(+), 50 deletions(-) diff --git a/src/strategies/sacra-subgraph/index.ts b/src/strategies/sacra-subgraph/index.ts index 920f8b1c9..b5c6ba7c1 100644 --- a/src/strategies/sacra-subgraph/index.ts +++ b/src/strategies/sacra-subgraph/index.ts @@ -57,13 +57,13 @@ export async function strategy( where: { borrower_in: addresses.map((address) => address.toLowerCase()), open: true, - collateralItem_not: null + collateralHero_not: null }, - orderBy: 'collateralItem__score', + orderBy: 'collateralHero__score', orderDirection: 'desc', first: 1000 }, - collateralItem: { + collateralHero: { score: true }, borrower: { @@ -78,13 +78,13 @@ export async function strategy( where: { borrower_in: addresses.map((address) => address.toLowerCase()), open: true, - collateralHero_not: null + collateralItem_not: null }, - orderBy: 'collateralHero__score', + orderBy: 'collateralItem__score', orderDirection: 'desc', first: 1000 }, - collateralHero: { + collateralItem: { score: true }, borrower: { @@ -93,54 +93,53 @@ export async function strategy( } }; - const subgraphKeys = Object.keys(SUBGRAPH_URL); - for (let i = 0; i < subgraphKeys.length; i++) { - const queryResultOne = await subgraphRequest( - SUBGRAPH_URL[subgraphKeys[i]], - queryOne - ); + if (snapshot !== 'latest') { + // @ts-ignore + queryOne.heroEntities.__args.block = { number: snapshot }; + // @ts-ignore + queryOne.itemEntities.__args.block = { number: snapshot }; + // @ts-ignore + queryOne.pawnshopPositionEntities.__args.block = { number: snapshot }; + } - const queryResultTwo = await subgraphRequest( - SUBGRAPH_URL[subgraphKeys[i]], - queryTwo - ); + const queryResultOne = await subgraphRequest(SUBGRAPH_URL[network], queryOne); - if (queryResultOne) { - const heroEntities = queryResultOne.heroEntities - ? queryResultOne.heroEntities - : []; - for (const heroEntity of heroEntities) { - const userAddress = getAddress(heroEntity.owner.id); - const score = heroEntity.score; - scores[userAddress] = (scores[userAddress] ?? 0) + score; - } - const itemEntities = queryResultOne.itemEntities - ? queryResultOne.itemEntities - : []; - for (const itemEntity of itemEntities) { - const userAddress = getAddress(itemEntity.user.id); - const score = itemEntity.score; - scores[userAddress] = (scores[userAddress] ?? 0) + score; - } - const pawnshopItemEntities = queryResultOne.pawnshopPositionEntities - ? queryResultOne.pawnshopPositionEntities - : []; - for (const pawnshopItemEntity of pawnshopItemEntities) { - const userAddress = getAddress(pawnshopItemEntity.borrower.id); - const score = pawnshopItemEntity.collateralItem.score; - scores[userAddress] = (scores[userAddress] ?? 0) + score; - } + if (queryResultOne) { + const heroEntities = queryResultOne.heroEntities + ? queryResultOne.heroEntities + : []; + for (const heroEntity of heroEntities) { + const userAddress = getAddress(heroEntity.owner.id); + const score = heroEntity.score; + scores[userAddress] = (scores[userAddress] ?? 0) + score; + } + const itemEntities = queryResultOne.itemEntities + ? queryResultOne.itemEntities + : []; + for (const itemEntity of itemEntities) { + const userAddress = getAddress(itemEntity.user.id); + const score = itemEntity.score; + scores[userAddress] = (scores[userAddress] ?? 0) + score; + } + const pawnshopHeroEntities = queryResultOne.pawnshopPositionEntities + ? queryResultOne.pawnshopPositionEntities + : []; + for (const pawnshopHeroEntity of pawnshopHeroEntities) { + const userAddress = getAddress(pawnshopHeroEntity.borrower.id); + const score = pawnshopHeroEntity.collateralHero.score; + scores[userAddress] = (scores[userAddress] ?? 0) + score; } + } - if (queryResultTwo) { - const pawnshopHeroEntities = queryResultTwo.pawnshopPositionEntities - ? queryResultTwo.pawnshopPositionEntities - : []; - for (const pawnshopHeroEntity of pawnshopHeroEntities) { - const userAddress = getAddress(pawnshopHeroEntity.borrower.id); - const score = pawnshopHeroEntity.collateralHero.score; - scores[userAddress] = (scores[userAddress] ?? 0) + score; - } + const queryResultTwo = await subgraphRequest(SUBGRAPH_URL[network], queryTwo); + if (queryResultTwo) { + const pawnshopItemEntities = queryResultTwo.pawnshopPositionEntities + ? queryResultTwo.pawnshopPositionEntities + : []; + for (const pawnshopItemEntity of pawnshopItemEntities) { + const userAddress = getAddress(pawnshopItemEntity.borrower.id); + const score = pawnshopItemEntity.collateralItem.score; + scores[userAddress] = (scores[userAddress] ?? 0) + score; } } From e244dfa34efeae17bac2ef7e2173c6d824acac4c Mon Sep 17 00:00:00 2001 From: alex Date: Wed, 30 Oct 2024 13:06:02 +0300 Subject: [PATCH 3/4] Delete subgraph requests --- src/strategies/sacra-subgraph/README.md | 18 -------------- src/strategies/sacra-subgraph/index.ts | 33 ------------------------- 2 files changed, 51 deletions(-) diff --git a/src/strategies/sacra-subgraph/README.md b/src/strategies/sacra-subgraph/README.md index c3c9f3f96..0b5febaf5 100644 --- a/src/strategies/sacra-subgraph/README.md +++ b/src/strategies/sacra-subgraph/README.md @@ -45,22 +45,4 @@ Calculates users power by hero score from Sacra subgraph. id } } - - pawnshopPositionEntities( - where:{ - borrower: $address - open: true - collateralItem_not: null - } - orderBy: collateralItem__score - orderDirection: desc - first: 1000 - ) { - collateralItem { - score - } - borrower { - id - } - } ``` \ No newline at end of file diff --git a/src/strategies/sacra-subgraph/index.ts b/src/strategies/sacra-subgraph/index.ts index b5c6ba7c1..4217f492c 100644 --- a/src/strategies/sacra-subgraph/index.ts +++ b/src/strategies/sacra-subgraph/index.ts @@ -72,27 +72,6 @@ export async function strategy( } }; - const queryTwo = { - pawnshopPositionEntities: { - __args: { - where: { - borrower_in: addresses.map((address) => address.toLowerCase()), - open: true, - collateralItem_not: null - }, - orderBy: 'collateralItem__score', - orderDirection: 'desc', - first: 1000 - }, - collateralItem: { - score: true - }, - borrower: { - id: true - } - } - }; - if (snapshot !== 'latest') { // @ts-ignore queryOne.heroEntities.__args.block = { number: snapshot }; @@ -131,17 +110,5 @@ export async function strategy( } } - const queryResultTwo = await subgraphRequest(SUBGRAPH_URL[network], queryTwo); - if (queryResultTwo) { - const pawnshopItemEntities = queryResultTwo.pawnshopPositionEntities - ? queryResultTwo.pawnshopPositionEntities - : []; - for (const pawnshopItemEntity of pawnshopItemEntities) { - const userAddress = getAddress(pawnshopItemEntity.borrower.id); - const score = pawnshopItemEntity.collateralItem.score; - scores[userAddress] = (scores[userAddress] ?? 0) + score; - } - } - return scores || {}; } From 17b54b073352ebbd468b7a22f70baad68d7d1b10 Mon Sep 17 00:00:00 2001 From: alex Date: Thu, 31 Oct 2024 20:15:25 +0300 Subject: [PATCH 4/4] Remove pawnshop info from subgraph request --- src/strategies/sacra-subgraph/README.md | 19 ----------------- src/strategies/sacra-subgraph/index.ts | 28 ------------------------- 2 files changed, 47 deletions(-) diff --git a/src/strategies/sacra-subgraph/README.md b/src/strategies/sacra-subgraph/README.md index 0b5febaf5..52c35ced5 100644 --- a/src/strategies/sacra-subgraph/README.md +++ b/src/strategies/sacra-subgraph/README.md @@ -26,23 +26,4 @@ Calculates users power by hero score from Sacra subgraph. ) { score } - - - pawnshopPositionEntities( - where:{ - borrower: $address - open: true - collateralHero_not: null - } - orderBy: collateralHero__score - orderDirection: desc - first: 1000 - ) { - collateralHero { - score - } - borrower { - id - } - } ``` \ No newline at end of file diff --git a/src/strategies/sacra-subgraph/index.ts b/src/strategies/sacra-subgraph/index.ts index 4217f492c..cbc2e4435 100644 --- a/src/strategies/sacra-subgraph/index.ts +++ b/src/strategies/sacra-subgraph/index.ts @@ -51,24 +51,6 @@ export async function strategy( user: { id: true } - }, - pawnshopPositionEntities: { - __args: { - where: { - borrower_in: addresses.map((address) => address.toLowerCase()), - open: true, - collateralHero_not: null - }, - orderBy: 'collateralHero__score', - orderDirection: 'desc', - first: 1000 - }, - collateralHero: { - score: true - }, - borrower: { - id: true - } } }; @@ -77,8 +59,6 @@ export async function strategy( queryOne.heroEntities.__args.block = { number: snapshot }; // @ts-ignore queryOne.itemEntities.__args.block = { number: snapshot }; - // @ts-ignore - queryOne.pawnshopPositionEntities.__args.block = { number: snapshot }; } const queryResultOne = await subgraphRequest(SUBGRAPH_URL[network], queryOne); @@ -100,14 +80,6 @@ export async function strategy( const score = itemEntity.score; scores[userAddress] = (scores[userAddress] ?? 0) + score; } - const pawnshopHeroEntities = queryResultOne.pawnshopPositionEntities - ? queryResultOne.pawnshopPositionEntities - : []; - for (const pawnshopHeroEntity of pawnshopHeroEntities) { - const userAddress = getAddress(pawnshopHeroEntity.borrower.id); - const score = pawnshopHeroEntity.collateralHero.score; - scores[userAddress] = (scores[userAddress] ?? 0) + score; - } } return scores || {};