Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
2.0 kB
3
Indexable
Never
self.addGraphQLResolvers({
  "Tokens.rate": getCurrentRate,
  "Mutation.deleteStakedAssetByWallet": deleteStakedAssetByWallet,
});

async function deleteStakedAssetByWallet({ args, graphql }) {
   const getselectedAssets = await graphql(`subscription queryWallet($address: String!, $asset_id: Int!) {
        queryWallet(filter: {address: {eq: $address}}) {
            id
            stakedassets(filter: {asset_id: {eq: $asset_id}}) {
              id
            }
        }
    }`, {"address": args.address, "asset_id": args.asset_id})

  //console.log("queryWallet: ", getselectedAssets)
  const walletAssetIds = getselectedAssets.data.queryWallet[0].stakedassets.map(
    (asa) => asa.id
  );

  const deleteQuery = `
    mutation {
      deleteStakedAssets(filter: { id: ["${walletAssetIds.join('","')}"] }) {
        msg
        numUids
      }
    }
  `;

  const result = await graphql(deleteQuery);
  return result.data.deleteStakedAssets;
  
}

async function getRate(asset_id) {
  let url = `https://free-api.vestige.fi/asset/${asset_id}/price?currency=ALGO&provider=T3`;
  let response = await fetch(url);
  let data = await response.json();
  return (data)? data.price : 0;
}

async function getCurrentRate({ parent }) {
  const { asset_id } = parent;
  var rate = getRate(asset_id);
  return rate;
}

async function getNFD(seller_wallet) {
  if(seller_wallet == null) {
    return null
  }
  let url = `https://api.nf.domains/nfd/address?address=${seller_wallet}`;
  let response = await fetch(url);
  
  if(response.status == 200) {
    let data = await response.json();
    //console.log("NFD",data[0].name)
    return (data[0].name)? data[0].name : null;
  }
  if(response.status == 404) {
    return null
  }
  //console.log("test37")
  //console.log(response)
  return null
}

async function getCurrentNFD({ parent }) {
  const { seller_wallet } = parent;
  var nfd = getNFD(seller_wallet);
  return nfd;
}