UNPKG

archethic

Version:
282 lines 7.91 kB
import fetch from "cross-fetch"; import absinthe from "./api/absinthe.js"; import { maybeUint8ArrayToHex } from "./utils.js"; export async function rawGraphQLQuery(query, endpoint) { const url = new URL("/api", endpoint); return fetch(url, { method: "POST", headers: { "Content-Type": "application/json", Accept: "application/json", }, body: JSON.stringify({ query: query, }), }) .then(handleResponse) .then((res) => { if (res.errors) { return null; } else { return res.data; } }); } export async function getNearestEndpoints(endpoint) { const url = new URL("/api", endpoint); return fetch(url, { method: "POST", headers: { "Content-Type": "application/json", Accept: "application/json", }, body: JSON.stringify({ query: `query { nearestEndpoints { ip, port } }`, }), }) .then(handleResponse) .then((res) => { if (res.errors || res.data.nearestEndpoints == null) { return []; } else { return res.data.nearestEndpoints; } }); } export async function getTransactionIndex(address, endpoint) { address = maybeUint8ArrayToHex(address); const url = new URL("/api", endpoint); return fetch(url, { method: "POST", headers: { "Content-Type": "application/json", Accept: "application/json", }, body: JSON.stringify({ query: `query { lastTransaction(address: "${address}") { chainLength } }`, }), }) .then(handleResponse) .then((res) => { if (res.errors || res.data.lastTransaction == null) { return 0; } else { return res.data.lastTransaction.chainLength; } }); } export async function getStorageNoncePublicKey(endpoint) { const url = new URL("/api", endpoint); return fetch(url, { method: "POST", headers: { "Content-Type": "application/json", Accept: "application/json", }, body: JSON.stringify({ query: `query { sharedSecrets { storageNoncePublicKey } }`, }), }) .then(handleResponse) .then((res) => { if (res.errors || res.data.sharedSecrets == null) { return ""; } else { return res.data.sharedSecrets.storageNoncePublicKey; } }); } export async function getTransactionOwnerships(address, endpoint, last = false) { address = maybeUint8ArrayToHex(address); const url = new URL("/api", endpoint); const field = last ? "lastTransaction" : "transaction"; return fetch(url, { method: "POST", headers: { "Content-Type": "application/json", Accept: "application/json", }, body: JSON.stringify({ query: `query { ${field}(address: "${address}") { data { ownerships { secret, authorizedPublicKeys { encryptedSecretKey, publicKey } } } } }`, }), }) .then(handleResponse) .then((res) => { if (res.errors || res.data == null) { return []; } else { return res.data[field].data.ownerships; } }); } export async function getToken(tokenAddress, endpoint) { tokenAddress = maybeUint8ArrayToHex(tokenAddress); const url = new URL("/api", endpoint); return fetch(url, { method: "POST", headers: { "Content-Type": "application/json", Accept: "application/json", }, body: JSON.stringify({ query: `query { token(address: "${tokenAddress}") { genesis, name, symbol, supply, type properties, collection, id, decimals } }`, }), }) .then(handleResponse) .then((res) => { if (res.errors || res.data == null) { return {}; } else { return res.data.token; } }); } export async function getOracleData(endpoint, timestamp = undefined) { let query; if (timestamp === undefined) { query = `query { oracleData { timestamp, services { uco { eur, usd } } } }`; } else { query = `query { oracleData(timestamp: ${timestamp}) { services { uco { eur, usd } } } }`; } const url = new URL("/api", endpoint); return fetch(url, { method: "POST", headers: { "Content-Type": "application/json", Accept: "application/json", }, body: JSON.stringify({ query: query, }), }) .then(handleResponse) .then((res) => { if (res.data.oracleData == null) { return { services: {} }; } else { return res.data.oracleData; } }); } export async function subscribeToOracleUpdates(endpoint, handler) { const { host, protocol } = new URL(endpoint); const ws_protocol = protocol == "https:" ? "wss" : "ws"; const absintheSocket = absinthe.create(`${ws_protocol}://${host}/socket`); const operation = ` subscription { oracleUpdate { timestamp, services { uco { eur, usd } } } } `; const notifier = absinthe.send(absintheSocket, operation); return absinthe.observe(absintheSocket, notifier, (result) => { handler(result.data.oracleUpdate); }); } export async function getBalance(address, endpoint) { address = maybeUint8ArrayToHex(address); const url = new URL("/api", endpoint); return fetch(url, { method: "POST", headers: { "Content-Type": "application/json", Accept: "application/json", }, body: JSON.stringify({ query: `query { balance(address: "${address}") { uco, token { address, amount, tokenId } } }`, }), }) .then(handleResponse) .then((res) => { if (res.errors || res.data == null) { return { uco: 0, token: [] }; } else { return res.data.balance; } }); } async function handleResponse(response) { return new Promise(function (resolve, reject) { if (response.status >= 200 && response.status <= 299) { response.json().then(resolve); } else { reject(response.statusText); } }); } //# sourceMappingURL=api.js.map