Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/css/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,6 @@ body {
.selector-ul > li span {
@apply font-normal block truncate;
}



20 changes: 17 additions & 3 deletions src/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,12 @@ <h1 class="text-3xl tracking-tight font-extrabold text-white sm:text-5xl md:text
if(window.getAllMaterialsPriceFailed){
materialsPriceProm = getAllMaterialsPrice()
}

const results = await Promise.all([getPlayerResults(), materialsPriceProm])
this.resources = results[0]
this.prices = results[1]
console.log(this.prices)

this.total = getTotal(this.resources, this.prices, this.selectedDate, this?.movrPrice ?? 0, this?.samaPrice ?? 0)

if(this.resources.error){
Expand Down Expand Up @@ -195,23 +198,34 @@ <h1 class="text-3xl tracking-tight font-extrabold text-white sm:text-5xl md:text

</div>
<div class="flex-1 text-right">
<div class="font-bold">Buy</div>
<div class="font-bold ">Buy</div>
<template x-for="(resource, index) in getValidResources(resources)" :key="resource">
<div :class="{'bg-gray-800': getValidResources(resources).indexOf(resource) % 2}" class="font-mono pl-2 flex-1 text-right" x-text="getPrice(resource, prices, this.selectedDate) ? parseFloat(getPrice(resource, prices, this.selectedDate).highestBuy).toFixed(3) : '-'"></div>
<div :class="{'bg-gray-800': getValidResources(resources).indexOf(resource) % 2}" class="font-mono pl-2 flex-1 text-right" x-text="getPrice(resource, prices, selectedDate) ? parseFloat(getPrice(resource, prices, selectedDate).highestBuy).toFixed(3) : '-'"></div>
</template>
<div class="font-mono pl-2 ml-1 flex-1 text-right border-t-2" x-text="total.totalBuyMovr"></div>
<div class="font-mono pl-2 flex-1 text-right" x-text="total.totalBuySama"></div>
<div class="font-mono pl-2 flex-1 text-right" x-text="`$${numberWithCommas(total.totalBuyUsd)}`"></div>
</div>

<div class="flex-1 text-right">
<div class="font-bold pr-1">Sell</div>
<template x-for="(resource, index) in getValidResources(resources)" :key="resource">
<div :class="{'bg-gray-800': getValidResources(resources).indexOf(resource) % 2}" class="font-mono pl-2 pr-1 flex-1 text-right" x-text="getPrice(resource, prices, this.selectedDate) ? parseFloat(getPrice(resource, prices, this.selectedDate).lowestSell).toFixed(3) : '-'"></div>
<div :class="{'bg-gray-800': getValidResources(resources).indexOf(resource) % 2}" class="font-mono pl-2 pr-1 flex-1 text-right" x-text="getPrice(resource, prices, selectedDate) ? parseFloat(getPrice(resource, prices, selectedDate).lowestSell).toFixed(3) : '-'"></div>
</template>
<div class="font-mono pl-2 pr-1 flex-1 text-right border-t-2" x-text="total.totalSellMovr"></div>
<div class="font-mono pl-2 pr-1 flex-1 text-right" x-text="total.totalSellSama"></div>
<div class="font-mono pl-2 pr-1 flex-1 text-right" x-text="`$${numberWithCommas(total.totalSellUsd)}`"></div>
</div>

<div class="flex-1 text-right">
<div class="font-bold pr-1">KhaosDex</div>
<template x-for="(resource, index) in getValidResources(resources)" :key="resource">
<div :class="{'bg-gray-800': getValidResources(resources).indexOf(resource) % 2}" class="font-mono pl-2 pr-1 flex-1 text-right" x-text="getPrice(resource, prices, selectedDate) ? parseFloat(getPrice(resource, prices, selectedDate).priceOfRssKhaos).toFixed(3) : '-'"></div>
</template>
<div class="font-mono pl-2 pr-1 flex-1 text-right border-t-2" x-text="total.totalSellMovr"></div>
<div class="font-mono pl-2 pr-1 flex-1 text-right" x-text="total.totalSellKhaosSama"></div>
<div class="font-mono pl-2 pr-1 flex-1 text-right" x-text="`$${numberWithCommas(total.totalSellKhaosSamaUsd)}`"></div>
</div>
</div>

</template>
Expand Down
80 changes: 48 additions & 32 deletions src/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,24 @@ import Alpine from 'alpinejs'

//carnage resources
const materials = [
{ "chainId": 1285, "assetAddress": "0x1b30a3b5744e733d8d2f19f0812e3f79152a8777", "tokenId": 1, "name": "wood" },
{ "chainId": 1285, "assetAddress": "0x1b30a3b5744e733d8d2f19f0812e3f79152a8777", "tokenId": 2, "name": "stone" },
{ "chainId": 1285, "assetAddress": "0x1b30a3b5744e733d8d2f19f0812e3f79152a8777", "tokenId": 3, "name": "iron" },
{ "chainId": 1285, "assetAddress": "0x1b30a3b5744e733d8d2f19f0812e3f79152a8777", "tokenId": 4, "name": "gold" },
{ "chainId": 1285, "assetAddress": "0x1b30a3b5744e733d8d2f19f0812e3f79152a8777", "tokenId": 5, "name": "experience" },
{ "chainId": 1285, "assetAddress": "0x1b30a3b5744e733d8d2f19f0812e3f79152a8777", "tokenId": 10, "name": "grain" },
{ "chainId": 1285, "assetAddress": "0x1b30a3b5744e733d8d2f19f0812e3f79152a8777", "tokenId": 12, "name": "string" },
{ "chainId": 1285, "assetAddress": "0x1b30a3b5744e733d8d2f19f0812e3f79152a8777", "tokenId": 13, "name": "fish_specimen" },
{ "chainId": 1285, "assetAddress": "0x1b30a3b5744e733d8d2f19f0812e3f79152a8777", "tokenId": 16, "name": "moonstone" },

{ "chainId": 2109, "assetAddress": "0x95370351df734b6a712ba18848b47574d3e90e61", "tokenId": 1, "name": "pumpkins" },
{ "chainId": 2109, "assetAddress": "0x95370351df734b6a712ba18848b47574d3e90e61", "tokenId": 2, "name": "blood_crystals" },
{ "chainId": 2109, "assetAddress": "0x95370351df734b6a712ba18848b47574d3e90e61", "tokenId": 3, "name": "dna" },
{ "chainId": 2109, "assetAddress": "0x95370351df734b6a712ba18848b47574d3e90e61", "tokenId": 4, "name": "mobidium" },
{ "chainId": 2109, "assetAddress": "0x95370351df734b6a712ba18848b47574d3e90e61", "tokenId": 5, "name": "wood" },
{ "chainId": 2109, "assetAddress": "0x95370351df734b6a712ba18848b47574d3e90e61", "tokenId": 6, "name": "stone" },
{ "chainId": 2109, "assetAddress": "0x95370351df734b6a712ba18848b47574d3e90e61", "tokenId": 7, "name": "iron" },
{ "chainId": 2109, "assetAddress": "0x95370351df734b6a712ba18848b47574d3e90e61", "tokenId": 8, "name": "gold" }
{ "chainId": 1285, "assetAddress": "0x1b30a3b5744e733d8d2f19f0812e3f79152a8777", "tokenId": 1, "name": "wood", "erc20Address": "0x8ce2bdc6e0319cea87337d027382f09b715c9601"},
{ "chainId": 1285, "assetAddress": "0x1b30a3b5744e733d8d2f19f0812e3f79152a8777", "tokenId": 2, "name": "stone", "erc20Address": "0x77709c42d43f2e53c24b8fa623a207abdc89857c"},
{ "chainId": 1285, "assetAddress": "0x1b30a3b5744e733d8d2f19f0812e3f79152a8777", "tokenId": 3, "name": "iron", "erc20Address": "0x9e403aa2dfef9ba2a2b82286d13864a64d90bf36"},
{ "chainId": 1285, "assetAddress": "0x1b30a3b5744e733d8d2f19f0812e3f79152a8777", "tokenId": 4, "name": "gold", "erc20Address": "0x088fe6e0e1caca1ee45e8de96abe79e4e139f4ab"},
{ "chainId": 1285, "assetAddress": "0x1b30a3b5744e733d8d2f19f0812e3f79152a8777", "tokenId": 5, "name": "experience", "erc20Address": "0x138a90f246abb23a157da7a1d9db19dcf1691362"},
{ "chainId": 1285, "assetAddress": "0x1b30a3b5744e733d8d2f19f0812e3f79152a8777", "tokenId": 10, "name": "grain", "erc20Address": "" },
{ "chainId": 1285, "assetAddress": "0x1b30a3b5744e733d8d2f19f0812e3f79152a8777", "tokenId": 12, "name": "string", "erc20Address": "0x0c0e3977c00ab4121d52ac8c6aeef9421d174458" },
{ "chainId": 1285, "assetAddress": "0x1b30a3b5744e733d8d2f19f0812e3f79152a8777", "tokenId": 13, "name": "fish_specimen", "erc20Address": ""},
{ "chainId": 1285, "assetAddress": "0x1b30a3b5744e733d8d2f19f0812e3f79152a8777", "tokenId": 16, "name": "moonstone", "erc20Address": "0xbe0b5b87ceb3eb1b05d11dc83332409291b35746"},

{ "chainId": 2109, "assetAddress": "0x95370351df734b6a712ba18848b47574d3e90e61", "tokenId": 1, "name": "pumpkins", "erc20Address": "0xb296335b6d4ab9f5e5dc4bf115621c14ab70136c" },
{ "chainId": 2109, "assetAddress": "0x95370351df734b6a712ba18848b47574d3e90e61", "tokenId": 2, "name": "blood_crystals", "erc20Address": "0x76ba6821a834e2d30a699312829f8204830ccf5b" },
{ "chainId": 2109, "assetAddress": "0x95370351df734b6a712ba18848b47574d3e90e61", "tokenId": 3, "name": "dna", "erc20Address": "0xff6ca2a78e4dd4871256cc5b63dc8dbf02a36f3d" },
{ "chainId": 2109, "assetAddress": "0x95370351df734b6a712ba18848b47574d3e90e61", "tokenId": 4, "name": "mobidium", "erc20Address": "0xa1caaaa65fd4e144c4650f87933705b9a065008e" },
{ "chainId": 2109, "assetAddress": "0x95370351df734b6a712ba18848b47574d3e90e61", "tokenId": 5, "name": "wood", "erc20Address": "0x0528ae6c997ba4ffc4d6add898a2cc9f26d4a872"},
{ "chainId": 2109, "assetAddress": "0x95370351df734b6a712ba18848b47574d3e90e61", "tokenId": 6, "name": "stone", "erc20Address": "0x122ebe2b679cf54bc8a6e89c1009714b354e2d10" },
{ "chainId": 2109, "assetAddress": "0x95370351df734b6a712ba18848b47574d3e90e61", "tokenId": 7, "name": "iron", "erc20Address": "0x829191fec0f25a27c5bdcdbbd0e37e6342b39945" },
{ "chainId": 2109, "assetAddress": "0x95370351df734b6a712ba18848b47574d3e90e61", "tokenId": 8, "name": "gold", "erc20Address": "0x549ab43056e3489335b47927ffcd4825ce484ba3" }
]

/**
Expand All @@ -36,15 +36,15 @@ const materials = [
* @returns {Promise<MaterialResult[]>}
*/
async function getAllMaterialsPrice(){
let results = await Promise.all(materials.map(async ({chainId, name, tokenId, assetAddress}) => {
let results = await Promise.all(materials.map(async ({chainId, name, tokenId, assetAddress, erc20Address}) => {
let result
if(chainId === 1285){
result = await getMaterialMovr(assetAddress, tokenId)
}else if(chainId === 2109){
result = await getMaterialSama(assetAddress, tokenId)
result = await getMaterialSama(assetAddress, tokenId, erc20Address)
}
if(!!result){
return {chainId, name, tokenId, lowestSell: result.lowestSell, highestBuy: result.highestBuy }
return {chainId, name, tokenId, lowestSell: result.lowestSell, highestBuy: result.highestBuy, priceOfRssKhaos: result.priceOfRssKhaos }
}
}))

Expand Down Expand Up @@ -113,7 +113,8 @@ async function getMaterialMovr(assetAddress, tokenId){
* @param {number} tokenId
* @returns {Promise<{lowestSell: number, highestBuy: number}>}
*/
async function getMaterialSama(assetAddress, tokenId){
async function getMaterialSama(assetAddress, tokenId, erc20Address){
/** Get price of resource from Moonsama Marketplace */
const graphqlQuery =
`query getAssetOrders {
sells: orders(where: {active: true, sellAsset: "${assetAddress}-${tokenId}", onlyTo: "0x0000000000000000000000000000000000000000"}, orderBy: pricePerUnit, orderDirection: asc, skip: 0, first: 1) {
Expand All @@ -131,7 +132,6 @@ async function getMaterialSama(assetAddress, tokenId){
askPerUnitDenominator
}
}`

const fetchUrl = "https://exosama-subgraph.moonsama.com/subgraphs/name/moonsama/marketplacev8"
const response = await fetch(fetchUrl, {
method: 'POST',
Expand All @@ -148,19 +148,27 @@ async function getMaterialSama(assetAddress, tokenId){
const responseJson = await response.json()
const buys = responseJson.data.buys.map(buy => buy.askPerUnitDenominator/buy.askPerUnitNominator).sort((a, b) => a-b)
const sells = responseJson.data.sells.map(sell => sell.askPerUnitNominator/sell.askPerUnitDenominator).sort((a, b) => a-b)

let lowestSell = sells.shift()
let highestBuy = buys.pop()

if(isNaN(lowestSell)){
lowestSell = 0
}
if(isNaN(highestBuy)){
highestBuy = 0
}
return {lowestSell, highestBuy}
}

/** Get price of resource from Khaos Dex */
const priceOfRssKhaosResponse = await fetch(`https://moonsama-serverside-priceticker.vercel.app/api/getpriceofrssinsama/${erc20Address}`, {
method: 'GET',
mode: 'cors',
headers: {
},
redirect: 'follow'
})
const priceOfRssKhaosJsonResponse = await priceOfRssKhaosResponse.json();
const priceOfRssKhaos = priceOfRssKhaosJsonResponse.price;
return {lowestSell, highestBuy, priceOfRssKhaos}
}

/**
* Returns the total USD value of all resources
Expand All @@ -179,6 +187,8 @@ function getTotal(resources, prices, gameDate, movrPrice, samaPrice){
let totalBuySama = 0
let totalSellSama = 0

let totalSellKhaosSama = 0;

for(const {chainId, tokenId, name} of materials){
if(resources.hasOwnProperty(name) && !isNaN(parseFloat(resources[name]))){
const matchingPrice = prices.find(p=> p.chainId === chainId && p.tokenId === tokenId)
Expand All @@ -201,14 +211,17 @@ function getTotal(resources, prices, gameDate, movrPrice, samaPrice){
if(!!matchingPrice){
totalBuySama+= resources[name] * matchingPrice.highestBuy
totalSellSama+= resources[name] * matchingPrice.lowestSell
totalSellKhaosSama+= resources[name] * matchingPrice.priceOfRssKhaos
}
}
}
}

const totalBuyUsd = totalBuyMovr * movrPrice + totalBuySama * samaPrice
const totalSellUsd = totalSellMovr * movrPrice + totalSellSama * samaPrice
return {totalBuyMovr: totalBuyMovr.toFixed(3), totalBuySama: totalBuySama.toFixed(3), totalBuyUsd: totalBuyUsd.toFixed(2), totalSellMovr: totalSellMovr.toFixed(3), totalSellSama: totalSellSama.toFixed(3), totalSellUsd: totalSellUsd.toFixed(2)}
const totalSellKhaosSamaUsd = totalSellKhaosSama * samaPrice + totalBuyMovr * movrPrice
return {totalBuyMovr: totalBuyMovr.toFixed(3), totalBuySama: totalBuySama.toFixed(3), totalBuyUsd: totalBuyUsd.toFixed(2), totalSellMovr: totalSellMovr.toFixed(3), totalSellSama: totalSellSama.toFixed(3), totalSellUsd: totalSellUsd.toFixed(2),
totalSellKhaosSama: totalSellKhaosSama.toFixed(3), totalSellKhaosSamaUsd: totalSellKhaosSamaUsd.toFixed(2)}
}


Expand All @@ -220,17 +233,15 @@ function getTotal(resources, prices, gameDate, movrPrice, samaPrice){
* @returns {lowestSell: number, highestBuy: number} | undefined
*/
function getPrice(resource, prices, gameDate){

const betaResource = (gameDate > 1664609731000) && ["wood", "stone", "iron", "gold"].includes(resource)

let matchingPrice
if(betaResource){
matchingPrice = prices.find(p=> p.chainId === 2109 && p.name === resource)
}else{
matchingPrice = prices.find(p=> p.name === resource)
}
if(!!matchingPrice){
return {lowestSell: matchingPrice.lowestSell, highestBuy: matchingPrice.highestBuy}
return {lowestSell: matchingPrice.lowestSell, highestBuy: matchingPrice.highestBuy, priceOfRssKhaos: matchingPrice.priceOfRssKhaos}
}
}

Expand Down Expand Up @@ -269,6 +280,10 @@ function prettifyResource(resource){
res = "fish"
}

if(res === "blood_crystals"){
res = "blood"
}

return res.trim().toLowerCase().replace(/\w\S*/g, (w) => (w.replace(/^\w/, (c) => c.toUpperCase())))
}

Expand Down Expand Up @@ -323,7 +338,8 @@ window.getAllMaterialsPrice = getAllMaterialsPrice
//load material quote on page load
window.getAllMaterialsPriceFailed = false
const getAllMaterialsPricePromise = getAllMaterialsPrice()
getAllMaterialsPricePromise.catch(()=>{
getAllMaterialsPricePromise.catch((e)=>{
console.log(e)
window.getAllMaterialsPricePromise = true
})
window.getAllMaterialsPricePromise = getAllMaterialsPricePromise
Expand Down