source get data agoran

 avatar
user_9696827045
javascript
2 years ago
2.8 kB
4
Indexable
async function getData() {
	let dataCount = 0
	let offsetCurrent = 0
	let result = []
	console.log('💫 Bắt đầu lấy data...');
	do {
		await fetch("https://backend.agoran.io/query", {
		  "headers": {
		    "accept": "*/*",
		    "content-type": "application/json",
		  },
		  "body": `{\"operationName\":\"getOverleagues\",\"variables\":{\"offset\":${offsetCurrent},\"limit\":50,\"filters\":{\"property\":{\"andConditions\":[{\"inArrayExpr\":{\"name\":\"Metadata.Part\",\"array\":[\"Wheel\",\"Body\",\"Rear\",\"Engine\"]}}]},\"userAddress\":\"\",\"unclaimed\":true},\"sort\":{\"field\":\"listed\",\"direction\":\"desc\"}},\"query\":\"query getOverleagues($offset: Int, $limit: Int, $filters: NftFilter, $sort: Sort) {\\n  ovlItems(\\n    blockchainID: bsc\\n    filter: $filters\\n    limit: $limit\\n    offset: $offset\\n    sort: $sort\\n  ) {\\n    count\\n    data {\\n      name\\n      description\\n      image\\n      cachedImage\\n      nftItem {\\n        id\\n        name\\n        blockChainId\\n        tokenId\\n        smcAddress\\n        orderItem {\\n          id\\n          chainId\\n          makerAddress\\n          takerAddress\\n          expirationTimeSeconds\\n          erc20TokenAddress\\n          erc20TokenAmount\\n          erc721TokenAddress\\n          erc721TokenID\\n          saleType\\n          totalAmount\\n          orderInfo {\\n            status\\n            __typename\\n          }\\n          __typename\\n        }\\n        nftOwnershipItem {\\n          amount\\n          ownerAddress\\n          __typename\\n        }\\n        likeCount\\n        isLikedByUser\\n        __typename\\n      }\\n      metadata {\\n        assetID\\n        car\\n        rarity\\n        part\\n        upgrade\\n        handling\\n        speed\\n        acceleration\\n        nitro\\n        __typename\\n      }\\n      isClaimedReward\\n      __typename\\n    }\\n    __typename\\n  }\\n}\"}`,
		  "method": "POST",
		})
		.then(res => res.json())
		.then(json => {
			const { count, data } = mapData(json)
			dataCount = count
			result = [...result, ...data]
			offsetCurrent += data.length
		});
		console.log('count:', dataCount);
		console.log('offset current:', offsetCurrent);
		console.log('count current:', result.length)
		// delay 1s
		await new Promise(resolve => setTimeout(() => resolve(true), 1000))
	} while (offsetCurrent < dataCount);
	console.log('⛔ Kết thúc lấy data...');
	console.log("🤑 :", result);
}

function mapData(json) {
	const items = json.data.ovlItems.data
	const count = json.data.ovlItems.count
	return {
		count,
		data: items.map(item => [item.metadata.assetID, WeiToEther(item.nftItem.orderItem.totalAmount)])
	}
}

function WeiToEther(value) {
	return parseFloat(value)/1000000000000000000
}