UNPKG

aion-gql-webcomponents

Version:
146 lines (145 loc) 4.54 kB
export default class AionPayService { constructor(gqlUrl) { this.NONCE_NRG_QUERY = ` query nonceAndNrg($address: String!, $txArgs: TxArgsInput!) { chainApi { nonce(address: $address) } txnApi { estimateNrgByTxArgs(txArgs: $txArgs) } }`; this.BALANCE_QUERY = ` query nonce($address: String!) { chainApi { balance(address: $address) } txnApi { nrgPrice } }`; this.SEND_RAWTXN_QUERY = ` mutation sendRawTransaction($encodedTx: String!) { txnApi { sendRawTransaction(encodedTx: $encodedTx) { status msgHash txHash txResult txDeploy error } } }`; this.gqlUrl = gqlUrl; } fetchNonceNrg(address, txn) { let txArgs = { to: txn.to, from: address, value: txn.value, data: txn.data, }; console.log(txArgs); return fetch(this.gqlUrl, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ "query": this.NONCE_NRG_QUERY, "variables": { "address": address, "txArgs": txArgs } }), }) .then(res => res.json()) .then(res => { console.log("Nonce & NrgPrice fetched"); let error = this.getError(res); if (error) { throw new Error(error); } let nonceData = ((res["data"])["chainApi"])["nonce"]; let estimatedNrg = null; try { estimatedNrg = ((res["data"])["txnApi"])["estimateNrgByTxArgs"]; } catch (e) { console.log("Error getting estimated energy"); } try { let nonce = nonceData; return [nonce, estimatedNrg]; } catch (e) { console.log(e); throw new Error("Unable to get nonce or estimated nrg"); } }); } fetchBalance(address) { return fetch(this.gqlUrl, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ "query": this.BALANCE_QUERY, "variables": { "address": address } }), }) .then(res => res.json()) .then(res => { console.log("Balance fetched"); let error = this.getError(res); if (error) { throw new Error(error); } let data = ((res["data"])["chainApi"])["balance"]; let nrgPriceData = null; try { nrgPriceData = ((res["data"])["txnApi"])["nrgPrice"]; } catch (e) { console.log("Error getting current nrg price"); } console.log("Nrg price fetched is " + nrgPriceData); if (data) { let balance = data; if (nrgPriceData) nrgPriceData = nrgPriceData; return [balance, nrgPriceData]; } else return null; }); } sendRawTransaction(encodedTx) { return fetch(this.gqlUrl, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ "query": this.SEND_RAWTXN_QUERY, "variables": { "encodedTx": encodedTx } }), }) .then(res => res.json()) .then(res => { let error = this.getError(res); if (error) { throw new Error(error); } let data = ((res["data"])["txnApi"])["sendRawTransaction"]; if (data) { let txnResponse = data; return txnResponse; } else return null; }); } getError(res) { let errors = this._getErrors(res); if (errors.length > 0) { let errorMsg = ''; errors.forEach(e => errorMsg += e + ", "); return errorMsg; } else return null; } _getErrors(res) { let errors = res["errors"]; if (errors && errors.length > 0) { return errors.map(error => error["message"]); } else return []; } }