UNPKG

revos-sdk-js-mainnet

Version:
471 lines (405 loc) 16.1 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.deleteName = deleteName; exports.getBalance = getBalance; exports.getBlockByHash = getBlockByHash; exports.getBlockByHeight = getBlockByHeight; exports.getBlockTxsByHeight = getBlockTxsByHeight; exports.getLatestBlock = getLatestBlock; exports.getNeighbors = getNeighbors; exports.getNonce = getNonce; exports.getRDCBalance = getRDCBalance; exports.getRegistrant = getRegistrant; exports.getSubscribers = getSubscribers; exports.getSubscribersCount = getSubscribersCount; exports.getSubscription = getSubscription; exports.getTransaction = getTransaction; exports.getWsAddr = getWsAddr; exports.getWssAddr = getWssAddr; exports.registerName = registerName; exports.sendTransaction = sendTransaction; exports.subscribe = subscribe; exports.swap = swap; exports.swapRDC = swapRDC; exports.transferName = transferName; exports.transferRDCTo = transferRDCTo; exports.transferTo = transferTo; exports.unsubscribe = unsubscribe; var _axios = _interopRequireDefault(require("axios")); var _amount = _interopRequireDefault(require("./amount")); var address = _interopRequireWildcard(require("../wallet/address")); var transactionfunc = address; var errors = _interopRequireWildcard(require("./errors")); var transaction = _interopRequireWildcard(require("../wallet/transaction")); var util = _interopRequireWildcard(require("./util")); var transactionproto = _interopRequireWildcard(require("../common/pb/transaction_pb")); function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); } function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; } function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } const rpcTimeout = 10000; const methods = { getWsAddr: { method: "getwsaddr" }, getWssAddr: { method: "getwssaddr" }, getSubscribers: { method: "getsubscribers", defaultParams: { offset: 0, limit: 1000, meta: false, txPool: false } }, getSubscribersCount: { method: "getsubscriberscount" }, getSubscription: { method: "getsubscription" }, getBalanceByAddr: { method: "getbalancebyaddr" }, getNeighbor: { method: "getneighbor" }, getRDCBalanceByAddr: { method: "getbalancebyassetid" }, getNonceByAddr: { method: "getnoncebyaddr" }, getRegistrant: { method: "getregistrant" }, getLatestBlockHash: { method: "getlatestblockhash" }, sendRawTransaction: { method: "sendrawtransaction" }, getBlock: { method: "getblock" }, getBlockCount: { method: "getblockcount" }, getLatestBlockHeight: { method: "getlatestblockheight" }, getBlockTxsByHeight: { method: "getblocktxsbyheight" }, getTransaction: { method: "gettransaction" } }; var rpc = {}; for (let method in methods) { if (methods.hasOwnProperty(method)) { rpc[method] = (addr, params) => { params = util.assignDefined({}, methods[method].defaultParams, params); return rpcCall(addr, methods[method].method, params); }; } } async function rpcCall(addr, method, params = {}) { const source = _axios.default.CancelToken.source(); let response = null; setTimeout(() => { if (response === null) { source.cancel("rpc timeout"); } }, rpcTimeout); try { response = await (0, _axios.default)({ url: addr, method: "POST", timeout: rpcTimeout, cancelToken: source.token, data: { id: "nkn-sdk-js", jsonrpc: "2.0", method: method, params: params } }); } catch (e) { if (_axios.default.isCancel(e)) { throw new errors.RpcTimeoutError(e.message); } else { throw new errors.RpcError(e.message); } } let data = response.data; if (data.error) { throw new errors.ServerError(data.error); } if (data.result !== undefined) { return data.result; } throw new errors.InvalidResponseError("rpc response contains no result or error field"); } async function getWsAddr(address, options = {}) { return rpc.getWsAddr(options.rpcServerAddr, { address }); } async function getWssAddr(address, options = {}) { return rpc.getWssAddr(options.rpcServerAddr, { address }); } async function getLatestBlock(options = {}) { return rpc.getLatestBlockHash(options.rpcServerAddr); } async function getNeighbors(options = {}) { return rpc.getNeighbor(options.rpcServerAddr); } async function getBlockByHash(hash, options = {}) { return rpc.getBlock(options.rpcServerAddr, { hash }); } async function getBlockByHeight(height, options = {}) { return rpc.getBlock(options.rpcServerAddr, { height }); } async function getBlockTxsByHeight(height, options = {}) { return rpc.getBlockTxsByHeight(options.rpcServerAddr, { height }); } async function getTransaction(hash, options = {}) { const data = await rpc.getTransaction(options.rpcServerAddr, { hash }); const payLoad = data.payloadData; const txType = data.txType; let txData; let senderAddress; let amount; let recipientAddress; let decodedPayload; switch (txType) { case "COINBASE_TYPE": txData = transactionproto.Coinbase.deserializeBinary(util.hexToBytes(payLoad)); senderAddress = address.programHashStringToAddress(util.bytesToHex(txData.array[0])); const recipient_PoP_ChallengersAddress = address.programHashStringToAddress(util.bytesToHex(txData.array[1])); const recipient_PoP_ChallengessAddress = address.programHashStringToAddress(util.bytesToHex(txData.array[2])); const recipient_Revos_Token_Corporate_TreasuryAddress = address.programHashStringToAddress(util.bytesToHex(txData.array[3])); const recipient_Revos_Token_SalesAddress = address.programHashStringToAddress(util.bytesToHex(txData.array[4])); const recipient_PoN_Connectivity_UtilizationAddress = address.programHashStringToAddress(util.bytesToHex(txData.array[5])); const recipient_PoN_Compute_UtilizationAddress = address.programHashStringToAddress(util.bytesToHex(txData.array[6])); const recipient_PoN_Storage_UtilizationAddress = address.programHashStringToAddress(util.bytesToHex(txData.array[7])); amount = txData.array[8]; const amount_PoP_Challengers = txData.array[9]; const amount_PoP_Challengess = txData.array[10]; const amount_Revos_Token_Corporate_Treasury = txData.array[11]; const amount_Revos_Token_Sales = txData.array[12]; const amount_PoN_Connectivity_Utilization = txData.array[13]; const amount_PoN_Compute_Utilization = txData.array[14]; const amount_PoN_Storage_Utilization = txData.array[15]; decodedPayload = { sender: senderAddress, recipient_PoP_Challengers: recipient_PoP_ChallengersAddress, recipient_PoP_Challengess: recipient_PoP_ChallengessAddress, recipient_Revos_Token_Corporate_Treasury: recipient_Revos_Token_Corporate_TreasuryAddress, recipient_Revos_Token_Sales: recipient_Revos_Token_SalesAddress, recipient_PoN_Connectivity_Utilization: recipient_PoN_Connectivity_UtilizationAddress, recipient_PoN_Compute_Utilization: recipient_PoN_Compute_UtilizationAddress, recipient_PoN_Storage_Utilization: recipient_PoN_Storage_UtilizationAddress, amount, amount_PoP_Challengers, amount_PoP_Challengess, amount_Revos_Token_Corporate_Treasury, amount_Revos_Token_Sales, amount_PoN_Connectivity_Utilization, amount_PoN_Compute_Utilization, amount_PoN_Storage_Utilization }; data.decodedPayload = decodedPayload; break; case "TRANSFER_ASSET_TYPE": case "TRANSFER_RDC_ASSET_TYPE": txData = transactionproto.TransferAsset.deserializeBinary(util.hexToBytes(payLoad)); senderAddress = address.programHashStringToAddress(util.bytesToHex(txData.array[0])); recipientAddress = address.programHashStringToAddress(util.bytesToHex(txData.array[1])); amount = txData.array[2]; decodedPayload = { sender: senderAddress, recipient: recipientAddress, amount }; data.decodedPayload = decodedPayload; break; case "SWAP_ASSET_TYPE": case "SWAP_RDC_ASSET_TYPE": txData = transactionproto.SwapAsset.deserializeBinary(util.hexToBytes(payLoad)); senderAddress = address.programHashStringToAddress(util.bytesToHex(txData.array[0])); recipientAddress = address.programHashStringToAddress(util.bytesToHex(txData.array[1])); amount = txData.array[2]; decodedPayload = { sender: senderAddress, recipient: recipientAddress, amount }; data.decodedPayload = decodedPayload; break; default: break; } return data; } async function getRegistrant(name, options = {}) { return rpc.getRegistrant(options.rpcServerAddr, { name }); } async function getSubscribers(topic, options = {}) { return rpc.getSubscribers(options.rpcServerAddr, { topic, offset: options.offset, limit: options.limit, meta: options.meta, txPool: options.txPool }); } async function getSubscribersCount(topic, options = {}) { return rpc.getSubscribersCount(options.rpcServerAddr, { topic }); } async function getSubscription(topic, subscriber, options = {}) { return rpc.getSubscription(options.rpcServerAddr, { topic, subscriber }); } async function getBalance(address, options = {}) { if (!address) { throw new errors.InvalidArgumentError("address is empty"); } let data = await rpc.getBalanceByAddr(options.rpcServerAddr, { address }); if (!data.amount) { throw new errors.InvalidResponseError("amount is empty"); } return new _amount.default(data.amount); } async function getRDCBalance(address, options = {}) { if (!address) { throw new errors.InvalidArgumentError("address is empty"); } let data = await rpc.getRDCBalanceByAddr(options.rpcServerAddr, { address }); if (!data.amount) { throw new errors.InvalidResponseError("amount is empty"); } return new _amount.default(data.amount); } async function getNonce(address, options = {}) { if (!address) { throw new errors.InvalidArgumentError("address is empty"); } options = util.assignDefined({ txPool: true }, options); let data = await rpc.getNonceByAddr(options.rpcServerAddr, { address }); if (typeof data.nonce !== "number") { throw new errors.InvalidResponseError("nonce is not a number"); } let nonce = data.nonce; if (options.txPool && data.nonceInTxPool && data.nonceInTxPool > nonce) { nonce = data.nonceInTxPool; } return nonce; } async function sendTransaction(txn, options = {}) { return rpc.sendRawTransaction(options.rpcServerAddr, { tx: util.bytesToHex(txn.serializeBinary()) }); } async function transferTo(toAddress, amount, options = {}) { if (!address.verifyAddress(toAddress)) { throw new errors.InvalidAddressError("invalid recipient address"); } let nonce = options.nonce || (await this.getNonce()); let signatureRedeem = address.publicKeyToSignatureRedeem(this.getPublicKey()); let programHash = address.hexStringToProgramHash(signatureRedeem); let pld = transaction.newTransferPayload(programHash, address.addressStringToProgramHash(toAddress), amount); let txn = await this.createTransaction(pld, nonce, options); return options.buildOnly ? txn : await this.sendTransaction(txn); } async function transferRDCTo(toAddress, amount, options = {}) { if (!address.verifyAddress(toAddress)) { throw new errors.InvalidAddressError("invalid recipient address"); } let nonce = options.nonce || (await this.getNonce()); let signatureRedeem = address.publicKeyToSignatureRedeem(this.getPublicKey()); let programHash = address.hexStringToProgramHash(signatureRedeem); let pld = transaction.newRDCTransferPayload(programHash, address.addressStringToProgramHash(toAddress), amount); let txn = await this.createTransaction(pld, nonce, options); return options.buildOnly ? txn : await this.sendTransaction(txn); } async function swap(toAddress, amount, options = {}) { if (!address.verifyAddress(toAddress)) { throw new errors.InvalidAddressError("invalid recipient address"); } let nonce = options.nonce || (await this.getNonce()); let signatureRedeem = address.publicKeyToSignatureRedeem(this.getPublicKey()); let programHash = address.hexStringToProgramHash(signatureRedeem); let pld = transaction.newSwapPayload(programHash, address.addressStringToProgramHash(toAddress), amount); let txn = await this.createTransaction(pld, nonce, options); return options.buildOnly ? txn : await this.sendTransaction(txn); } async function swapRDC(toAddress, amount, options = {}) { if (!address.verifyAddress(toAddress)) { throw new errors.InvalidAddressError("invalid recipient address"); } let nonce = options.nonce || (await this.getNonce()); let signatureRedeem = address.publicKeyToSignatureRedeem(this.getPublicKey()); let programHash = address.hexStringToProgramHash(signatureRedeem); let pld = transaction.newRDCSwapPayload(programHash, address.addressStringToProgramHash(toAddress), amount); let txn = await this.createTransaction(pld, nonce, options); return options.buildOnly ? txn : await this.sendTransaction(txn); } async function registerName(name, options = {}) { let nonce = options.nonce || (await this.getNonce()); let pld = transaction.newRegisterNamePayload(this.getPublicKey(), name); let txn = await this.createTransaction(pld, nonce, options); return options.buildOnly ? txn : await this.sendTransaction(txn); } async function transferName(name, recipient, options = {}) { let nonce = options.nonce || (await this.getNonce()); let pld = transaction.newTransferNamePayload(name, this.getPublicKey(), recipient); let txn = await this.createTransaction(pld, nonce, options); return options.buildOnly ? txn : await this.sendTransaction(txn); } async function deleteName(name, options = {}) { let nonce = options.nonce || (await this.getNonce()); let pld = transaction.newDeleteNamePayload(this.getPublicKey(), name); let txn = await this.createTransaction(pld, nonce, options); return options.buildOnly ? txn : await this.sendTransaction(txn); } async function subscribe(topic, duration, identifier, meta, options = {}) { let nonce = options.nonce || (await this.getNonce()); let pld = transaction.newSubscribePayload(this.getPublicKey(), identifier, topic, duration, meta); let txn = await this.createTransaction(pld, nonce, options); return options.buildOnly ? txn : await this.sendTransaction(txn); } async function unsubscribe(topic, identifier, options = {}) { let nonce = options.nonce || (await this.getNonce()); let pld = transaction.newUnsubscribePayload(this.getPublicKey(), identifier, topic); let txn = await this.createTransaction(pld, nonce, options); return options.buildOnly ? txn : await this.sendTransaction(txn); }