diff --git a/lib/FIOSDK.js b/lib/FIOSDK.js index eaa29eb9..e96239e3 100644 --- a/lib/FIOSDK.js +++ b/lib/FIOSDK.js @@ -42,12 +42,14 @@ class FIOSDK { * @param technologyProviderId Default FIO Address of the wallet which generates transactions. */ constructor(privateKey, publicKey, baseUrl, fetchjson, registerMockUrl = '', technologyProviderId = '', returnPreparedTrx = false) { + this.baseUrl = ''; + this.abiMap = new Map(); /** * Defines whether SignedTransaction would execute or return prepared transaction */ this.returnPreparedTrx = false; this.transactions = new Transactions_1.Transactions(); - Transactions_1.Transactions.baseUrl = baseUrl; + this.baseUrl = baseUrl; Transactions_1.Transactions.FioProvider = fiojs_1.Fio; Transactions_1.Transactions.fetchJson = fetchjson; this.registerMockUrl = registerMockUrl; @@ -58,7 +60,7 @@ class FIOSDK { for (const accountName of constants_1.Constants.rawAbiAccountName) { this.getAbi(accountName) .then((response) => { - Transactions_1.Transactions.abiMap.set(response.account_name, response); + this.abiMap.set(response.account_name, response); }) .catch((error) => { throw error; @@ -271,7 +273,7 @@ class FIOSDK { */ registerFioAddress(fioAddress, maxFee, technologyProviderId = null) { const registerFioAddress = new SignedTransactions.RegisterFioAddress(fioAddress, null, maxFee, this.getTechnologyProviderId(technologyProviderId)); - return registerFioAddress.execute(this.privateKey, this.publicKey, this.returnPreparedTrx); + return registerFioAddress.execute(this.baseUrl, this.abiMap, this.privateKey, this.publicKey, this.returnPreparedTrx); } /** * Registers a Fio Address on behalf of the owner FIO Public key parameter. Owner FIO Public key owns the FIO address @@ -283,7 +285,7 @@ class FIOSDK { */ registerOwnerFioAddress(fioAddress, ownerPublicKey, maxFee, technologyProviderId = null) { const registerFioAddress = new SignedTransactions.RegisterFioAddress(fioAddress, ownerPublicKey, maxFee, this.getTechnologyProviderId(technologyProviderId)); - return registerFioAddress.execute(this.privateKey, this.publicKey, this.returnPreparedTrx); + return registerFioAddress.execute(this.baseUrl, this.abiMap, this.privateKey, this.publicKey, this.returnPreparedTrx); } /** * Registers a FIO Domain on the FIO blockchain. @@ -294,7 +296,7 @@ class FIOSDK { */ registerFioDomain(fioDomain, maxFee, technologyProviderId = null) { const registerFioDomain = new SignedTransactions.RegisterFioDomain(fioDomain, null, maxFee, this.getTechnologyProviderId(technologyProviderId)); - return registerFioDomain.execute(this.privateKey, this.publicKey, this.returnPreparedTrx); + return registerFioDomain.execute(this.baseUrl, this.abiMap, this.privateKey, this.publicKey, this.returnPreparedTrx); } /** * Registers a FIO Domain on behalf of the owner FIO Public key parameter. Owner FIO Public key owns the FIO domain. @@ -306,7 +308,7 @@ class FIOSDK { */ registerOwnerFioDomain(fioDomain, ownerPublicKey, maxFee, technologyProviderId = null) { const registerFioDomain = new SignedTransactions.RegisterFioDomain(fioDomain, ownerPublicKey, maxFee, this.getTechnologyProviderId(technologyProviderId)); - return registerFioDomain.execute(this.privateKey, this.publicKey, this.returnPreparedTrx); + return registerFioDomain.execute(this.baseUrl, this.abiMap, this.privateKey, this.publicKey, this.returnPreparedTrx); } /** * Burns a FIO Address on the FIO blockchain. @@ -317,7 +319,7 @@ class FIOSDK { */ burnFioAddress(fioAddress, maxFee, technologyProviderId = null) { const burnFioAddress = new SignedTransactions.BurnFioAddress(fioAddress, maxFee, this.getTechnologyProviderId(technologyProviderId)); - return burnFioAddress.execute(this.privateKey, this.publicKey, this.returnPreparedTrx); + return burnFioAddress.execute(this.baseUrl, this.abiMap, this.privateKey, this.publicKey, this.returnPreparedTrx); } /** * Transfers a FIO Domain on the FIO blockchain. @@ -329,7 +331,7 @@ class FIOSDK { */ transferFioDomain(fioDomain, newOwnerKey, maxFee, technologyProviderId = null) { const transferFioDomain = new SignedTransactions.TransferFioDomain(fioDomain, newOwnerKey, maxFee, this.getTechnologyProviderId(technologyProviderId)); - return transferFioDomain.execute(this.privateKey, this.publicKey, this.returnPreparedTrx); + return transferFioDomain.execute(this.baseUrl, this.abiMap, this.privateKey, this.publicKey, this.returnPreparedTrx); } /** * Transfers a FIO Address on the FIO blockchain. @@ -341,7 +343,7 @@ class FIOSDK { */ transferFioAddress(fioAddress, newOwnerKey, maxFee, technologyProviderId = null) { const transferFioAddress = new SignedTransactions.TransferFioAddress(fioAddress, newOwnerKey, maxFee, this.getTechnologyProviderId(technologyProviderId)); - return transferFioAddress.execute(this.privateKey, this.publicKey, this.returnPreparedTrx); + return transferFioAddress.execute(this.baseUrl, this.abiMap, this.privateKey, this.publicKey, this.returnPreparedTrx); } /** * Adds bundles of transactions to FIO Address. @@ -353,7 +355,7 @@ class FIOSDK { */ addBundledTransactions(fioAddress, bundleSets, maxFee, technologyProviderId = null) { const addBundledTransactions = new SignedTransactions.AddBundledTransactions(fioAddress, bundleSets, maxFee, this.getTechnologyProviderId(technologyProviderId)); - return addBundledTransactions.execute(this.privateKey, this.publicKey, this.returnPreparedTrx); + return addBundledTransactions.execute(this.baseUrl, this.abiMap, this.privateKey, this.publicKey, this.returnPreparedTrx); } /** * Renew a FIO Address on the FIO blockchain. @@ -364,7 +366,7 @@ class FIOSDK { */ renewFioAddress(fioAddress, maxFee, technologyProviderId = null) { const renewFioAddress = new SignedTransactions.RenewFioAddress(fioAddress, maxFee, this.getTechnologyProviderId(technologyProviderId)); - return renewFioAddress.execute(this.privateKey, this.publicKey, this.returnPreparedTrx); + return renewFioAddress.execute(this.baseUrl, this.abiMap, this.privateKey, this.publicKey, this.returnPreparedTrx); } /** * Renew a FIO Domain on the FIO blockchain. @@ -375,7 +377,7 @@ class FIOSDK { */ renewFioDomain(fioDomain, maxFee, technologyProviderId = null) { const renewFioDomain = new SignedTransactions.RenewFioDomain(fioDomain, maxFee, this.getTechnologyProviderId(technologyProviderId)); - return renewFioDomain.execute(this.privateKey, this.publicKey, this.returnPreparedTrx); + return renewFioDomain.execute(this.baseUrl, this.abiMap, this.privateKey, this.publicKey, this.returnPreparedTrx); } /** * This call allows a public address of the specific blockchain type to be added to the FIO Address. @@ -393,7 +395,7 @@ class FIOSDK { token_code: tokenCode, public_address: publicAddress }], maxFee, this.getTechnologyProviderId(technologyProviderId)); - return addPublicAddress.execute(this.privateKey, this.publicKey, this.returnPreparedTrx); + return addPublicAddress.execute(this.baseUrl, this.abiMap, this.privateKey, this.publicKey, this.returnPreparedTrx); } /** * This call cancels the specified fio funds request.. @@ -404,7 +406,7 @@ class FIOSDK { */ cancelFundsRequest(fioRequestId, maxFee, technologyProviderId = null) { const cancelFundsRequest = new SignedTransactions.CancelFundsRequest(fioRequestId, maxFee, this.getTechnologyProviderId(technologyProviderId)); - return cancelFundsRequest.execute(this.privateKey, this.publicKey, this.returnPreparedTrx); + return cancelFundsRequest.execute(this.baseUrl, this.abiMap, this.privateKey, this.publicKey, this.returnPreparedTrx); } /** * This call allows a any number of public addresses matching the blockchain code, the token code and the public address to be removed from the FIO Address. @@ -416,7 +418,7 @@ class FIOSDK { */ removePublicAddresses(fioAddress, publicAddresses, maxFee, technologyProviderId = null) { const removePublicAddresses = new SignedTransactions.RemovePublicAddresses(fioAddress, publicAddresses, maxFee, this.getTechnologyProviderId(technologyProviderId)); - return removePublicAddresses.execute(this.privateKey, this.publicKey, this.returnPreparedTrx); + return removePublicAddresses.execute(this.baseUrl, this.abiMap, this.privateKey, this.publicKey, this.returnPreparedTrx); } /** * This call allows a user to transfer locked tokens to the specified fio public key @@ -430,7 +432,7 @@ class FIOSDK { */ transferLockedTokens(payeePublicKey, canVote, periods, amount, maxFee, technologyProviderId = null) { const transferLockedTokens = new SignedTransactions.TransferLockedTokens(payeePublicKey, canVote, periods, amount, maxFee, this.getTechnologyProviderId(technologyProviderId)); - return transferLockedTokens.execute(this.privateKey, this.publicKey); + return transferLockedTokens.execute(this.baseUrl, this.abiMap, this.privateKey, this.publicKey); } /** * This call allows a user to remove all addresses from the specified FIO Address, all addresses except the FIO address will be removed. @@ -441,7 +443,7 @@ class FIOSDK { */ removeAllPublicAddresses(fioAddress, maxFee, technologyProviderId = null) { const removeAllPublicAddresses = new SignedTransactions.RemoveAllPublicAddresses(fioAddress, maxFee, this.getTechnologyProviderId(technologyProviderId)); - return removeAllPublicAddresses.execute(this.privateKey, this.publicKey, this.returnPreparedTrx); + return removeAllPublicAddresses.execute(this.baseUrl, this.abiMap, this.privateKey, this.publicKey, this.returnPreparedTrx); } /** * This call allows a public addresses of the specific blockchain type to be added to the FIO Address. @@ -453,7 +455,7 @@ class FIOSDK { */ addPublicAddresses(fioAddress, publicAddresses, maxFee, technologyProviderId = null) { const addPublicAddress = new SignedTransactions.AddPublicAddress(fioAddress, publicAddresses, maxFee, this.getTechnologyProviderId(technologyProviderId)); - return addPublicAddress.execute(this.privateKey, this.publicKey, this.returnPreparedTrx); + return addPublicAddress.execute(this.baseUrl, this.abiMap, this.privateKey, this.publicKey, this.returnPreparedTrx); } /** * By default all FIO Domains are non-public, meaning only the owner can register FIO Addresses on that domain. Setting them to public allows anyone to register a FIO Address on that domain. @@ -465,7 +467,7 @@ class FIOSDK { */ setFioDomainVisibility(fioDomain, isPublic, maxFee, technologyProviderId = null) { const SetFioDomainVisibility = new SignedTransactions.SetFioDomainVisibility(fioDomain, isPublic, maxFee, this.getTechnologyProviderId(technologyProviderId)); - return SetFioDomainVisibility.execute(this.privateKey, this.publicKey, this.returnPreparedTrx); + return SetFioDomainVisibility.execute(this.baseUrl, this.abiMap, this.privateKey, this.publicKey, this.returnPreparedTrx); } /** * @@ -499,7 +501,7 @@ class FIOSDK { payeeKey.public_address = payeeFioPublicKey; } const recordObtData = new SignedTransactions.RecordObtData(fioRequestId, payerFioAddress, payeeFioAddress, payerTokenPublicAddress, payeeTokenPublicAddress, amount, chainCode, tokenCode, obtId, maxFee, status, this.getTechnologyProviderId(technologyProviderId), payeeKey.public_address, memo, hash, offLineUrl); - return recordObtData.execute(this.privateKey, this.publicKey, this.returnPreparedTrx); + return recordObtData.execute(this.baseUrl, this.abiMap, this.privateKey, this.publicKey, this.returnPreparedTrx); }); } /** @@ -511,7 +513,7 @@ class FIOSDK { */ getObtData(limit, offset, tokenCode) { const getObtDataRequest = new queries.GetObtData(this.publicKey, limit, offset, tokenCode); - return getObtDataRequest.execute(this.publicKey, this.privateKey); + return getObtDataRequest.execute(this.baseUrl, this.publicKey, this.privateKey); } /** * Reject funds request. @@ -522,7 +524,7 @@ class FIOSDK { */ rejectFundsRequest(fioRequestId, maxFee, technologyProviderId = null) { const rejectFundsRequest = new SignedTransactions.RejectFundsRequest(fioRequestId, maxFee, this.getTechnologyProviderId(technologyProviderId)); - return rejectFundsRequest.execute(this.privateKey, this.publicKey, this.returnPreparedTrx); + return rejectFundsRequest.execute(this.baseUrl, this.abiMap, this.privateKey, this.publicKey, this.returnPreparedTrx); } /** * Create a new funds request on the FIO chain. @@ -550,7 +552,7 @@ class FIOSDK { payerKey.public_address = payerFioPublicKey; } const requestNewFunds = new SignedTransactions.RequestNewFunds(payerFioAddress, payerKey.public_address, payeeFioAddress, this.getTechnologyProviderId(technologyProviderId), maxFee, payeeTokenPublicAddress, amount, chainCode, tokenCode, memo, hash, offlineUrl); - return requestNewFunds.execute(this.privateKey, this.publicKey, this.returnPreparedTrx); + return requestNewFunds.execute(this.baseUrl, this.abiMap, this.privateKey, this.publicKey, this.returnPreparedTrx); }); } /** @@ -560,7 +562,7 @@ class FIOSDK { */ getLocks(fioPublicKey) { const getLocks = new queries.GetLocks(fioPublicKey); - return getLocks.execute(this.publicKey); + return getLocks.execute(this.baseUrl, this.publicKey); } /* * Retrieves info on account for this actor @@ -569,7 +571,7 @@ class FIOSDK { */ getAccount(actor) { const getAccount = new queries.GetAccount(actor); - return getAccount.execute(this.publicKey); + return getAccount.execute(this.baseUrl, this.publicKey); } /** * Checks if a FIO Address or FIO Domain is available for registration. @@ -578,7 +580,7 @@ class FIOSDK { */ isAvailable(fioName) { const availabilityCheck = new queries.AvailabilityCheck(fioName); - return availabilityCheck.execute(this.publicKey); + return availabilityCheck.execute(this.baseUrl, this.publicKey); } /** * Retrieves balance of FIO tokens @@ -587,7 +589,7 @@ class FIOSDK { */ getFioBalance(fioPublicKey) { const getFioBalance = new queries.GetFioBalance(fioPublicKey); - return getFioBalance.execute(this.publicKey); + return getFioBalance.execute(this.baseUrl, this.publicKey); } /** * Returns FIO Addresses and FIO Domains owned by this public key. @@ -596,7 +598,7 @@ class FIOSDK { */ getFioNames(fioPublicKey) { const getNames = new queries.GetNames(fioPublicKey); - return getNames.execute(this.publicKey); + return getNames.execute(this.baseUrl, this.publicKey); } /** * Returns FIO Addresses owned by this public key. @@ -607,7 +609,7 @@ class FIOSDK { */ getFioAddresses(fioPublicKey, limit, offset) { const getNames = new queries.GetAddresses(fioPublicKey, limit, offset); - return getNames.execute(this.publicKey); + return getNames.execute(this.baseUrl, this.publicKey); } /** * Returns FIO domains owned by this public key. @@ -618,7 +620,7 @@ class FIOSDK { */ getFioDomains(fioPublicKey, limit, offset) { const getNames = new queries.GetDomains(fioPublicKey, limit, offset); - return getNames.execute(this.publicKey); + return getNames.execute(this.baseUrl, this.publicKey); } /** * Polls for any pending requests sent to public key associated with the FIO SDK instance. @@ -628,7 +630,7 @@ class FIOSDK { */ getPendingFioRequests(limit, offset) { const pendingFioRequests = new queries.PendingFioRequests(this.publicKey, limit, offset); - return pendingFioRequests.execute(this.publicKey, this.privateKey); + return pendingFioRequests.execute(this.baseUrl, this.publicKey, this.privateKey); } /** * Polls for any sent requests sent by public key associated with the FIO SDK instance. @@ -638,7 +640,7 @@ class FIOSDK { */ getSentFioRequests(limit, offset) { const sentFioRequest = new queries.SentFioRequests(this.publicKey, limit, offset); - return sentFioRequest.execute(this.publicKey, this.privateKey); + return sentFioRequest.execute(this.baseUrl, this.publicKey, this.privateKey); } /** * Polls for any cancelled requests sent by public key associated with the FIO SDK instance. @@ -648,7 +650,7 @@ class FIOSDK { */ getCancelledFioRequests(limit, offset) { const cancelledFioRequest = new queries.CancelledFioRequests(this.publicKey, limit, offset); - return cancelledFioRequest.execute(this.publicKey, this.privateKey); + return cancelledFioRequest.execute(this.baseUrl, this.publicKey, this.privateKey); } /** * Returns a token public address for specified token code and FIO Address. @@ -659,7 +661,7 @@ class FIOSDK { */ getPublicAddress(fioAddress, chainCode, tokenCode) { const publicAddressLookUp = new queries.GetPublicAddress(fioAddress, chainCode, tokenCode); - return publicAddressLookUp.execute(this.publicKey); + return publicAddressLookUp.execute(this.baseUrl, this.publicKey); } /** * Returns all public addresses for specified FIO Address. @@ -670,7 +672,7 @@ class FIOSDK { */ getPublicAddresses(fioAddress, limit, offset) { const publicAddressesLookUp = new queries.GetPublicAddresses(fioAddress, limit, offset); - return publicAddressesLookUp.execute(this.publicKey); + return publicAddressesLookUp.execute(this.baseUrl, this.publicKey); } /** * Returns the FIO token public address for specified FIO Address. @@ -692,7 +694,7 @@ class FIOSDK { */ transferTokens(payeeFioPublicKey, amount, maxFee, technologyProviderId = null) { const transferTokens = new SignedTransactions.TransferTokens(payeeFioPublicKey, amount, maxFee, this.getTechnologyProviderId(technologyProviderId)); - return transferTokens.execute(this.privateKey, this.publicKey, this.returnPreparedTrx); + return transferTokens.execute(this.baseUrl, this.abiMap, this.privateKey, this.publicKey, this.returnPreparedTrx); } /** * Compute and return fee amount for specific call and specific user @@ -718,7 +720,7 @@ class FIOSDK { */ getFee(endPoint, fioAddress = '') { const fioFee = new queries.GetFee(endPoint, fioAddress); - return fioFee.execute(this.publicKey); + return fioFee.execute(this.baseUrl, this.publicKey); } /** * Compute and return fee amount for specific call and specific user @@ -832,7 +834,7 @@ class FIOSDK { pushTransaction(account, action, data) { data.tpid = this.getTechnologyProviderId(data.tpid); const pushTransaction = new SignedTransactions.PushTransaction(action, account, data); - return pushTransaction.execute(this.privateKey, this.publicKey, this.returnPreparedTrx); + return pushTransaction.execute(this.baseUrl, this.abiMap, this.privateKey, this.publicKey, this.returnPreparedTrx); } /** * @ignore @@ -956,7 +958,7 @@ class FIOSDK { */ getAbi(accountName) { const abi = new queries.GetAbi(accountName); - return abi.execute(this.publicKey); + return abi.execute(this.baseUrl, this.publicKey); } } exports.FIOSDK = FIOSDK; diff --git a/lib/transactions/Transactions.js b/lib/transactions/Transactions.js index cf9b05b9..8b8b5a05 100644 --- a/lib/transactions/Transactions.js +++ b/lib/transactions/Transactions.js @@ -18,6 +18,8 @@ const textEncoder = new text_encoding_1.TextEncoder(); const textDecoder = new text_encoding_1.TextDecoder(); class Transactions { constructor() { + this.abiMap = new Map(); + this.baseUrl = ''; this.publicKey = ''; this.privateKey = ''; this.serilizeEndpoint = 'chain/serialize_json'; @@ -37,7 +39,7 @@ class Transactions { 'Content-Type': 'application/json', }, }; - const res = yield Transactions.fetchJson(Transactions.baseUrl + 'chain/get_info', options); + const res = yield Transactions.fetchJson(this.baseUrl + 'chain/get_info', options); return yield res.json(); }); } @@ -49,7 +51,7 @@ class Transactions { if (chain.last_irreversible_block_num == undefined) { throw new Error('chain.last_irreversible_block_num undefined'); } - const res = yield Transactions.fetchJson(Transactions.baseUrl + 'chain/get_block', { + const res = yield Transactions.fetchJson(this.baseUrl + 'chain/get_block', { method: 'POST', headers: { 'Accept': 'application/json', @@ -93,13 +95,13 @@ class Transactions { transaction.expiration = expirationStr.substr(0, expirationStr.length - 1); if (dryRun) { return Transactions.FioProvider.prepareTransaction({ - transaction, chainId: chain.chain_id, privateKeys: privky, abiMap: Transactions.abiMap, + transaction, chainId: chain.chain_id, privateKeys: privky, abiMap: this.abiMap, textDecoder: new text_encoding_1.TextDecoder(), textEncoder: new text_encoding_1.TextEncoder(), }); } else { const signedTransaction = yield Transactions.FioProvider.prepareTransaction({ - transaction, chainId: chain.chain_id, privateKeys: privky, abiMap: Transactions.abiMap, + transaction, chainId: chain.chain_id, privateKeys: privky, abiMap: this.abiMap, textDecoder: new text_encoding_1.TextDecoder(), textEncoder: new text_encoding_1.TextEncoder(), }); return this.executeCall(endpoint, JSON.stringify(signedTransaction)); @@ -127,9 +129,9 @@ class Transactions { }; } try { - const res = yield Transactions.fetchJson(Transactions.baseUrl + endPoint, options); + const res = yield Transactions.fetchJson(this.baseUrl + endPoint, options); if (!res.ok) { - const error = new Error(`Error ${res.status} while fetching ${Transactions.baseUrl + endPoint}`); + const error = new Error(`Error ${res.status} while fetching ${this.baseUrl + endPoint}`); try { error.json = yield res.json(); } @@ -166,4 +168,3 @@ class Transactions { } } exports.Transactions = Transactions; -Transactions.abiMap = new Map(); diff --git a/lib/transactions/queries/Query.js b/lib/transactions/queries/Query.js index 4311fdc5..a2c8cbde 100644 --- a/lib/transactions/queries/Query.js +++ b/lib/transactions/queries/Query.js @@ -18,10 +18,11 @@ class Query extends Transactions_1.Transactions { } decrypt(result) { } - execute(publicKey, privateKey = '') { + execute(baseUrl, publicKey, privateKey = '') { return __awaiter(this, void 0, void 0, function* () { this.publicKey = publicKey; this.privateKey = privateKey; + this.baseUrl = baseUrl; if (!this.isEncrypted) { return this.executeCall(this.getEndPoint(), JSON.stringify(this.getData())); } diff --git a/lib/transactions/signed/SignedTransaction.js b/lib/transactions/signed/SignedTransaction.js index 31f6296e..f982e421 100644 --- a/lib/transactions/signed/SignedTransaction.js +++ b/lib/transactions/signed/SignedTransaction.js @@ -15,10 +15,12 @@ const RawAction_1 = require("../../entities/RawAction"); const RawTransaction_1 = require("../../entities/RawTransaction"); const Transactions_1 = require("../Transactions"); class SignedTransaction extends Transactions_1.Transactions { - execute(privateKey, publicKey, dryRun = false) { + execute(baseUrl, abiMap, privateKey, publicKey, dryRun = false) { return __awaiter(this, void 0, void 0, function* () { this.privateKey = privateKey; this.publicKey = publicKey; + this.baseUrl = baseUrl; + this.abiMap = abiMap; const rawTransaction = new RawTransaction_1.RawTransaction(); const rawaction = new RawAction_1.RawAction(); rawaction.account = this.getAccount(); diff --git a/src/FIOSDK.ts b/src/FIOSDK.ts index cd1fad9b..6ab07edf 100644 --- a/src/FIOSDK.ts +++ b/src/FIOSDK.ts @@ -54,6 +54,8 @@ const { Ecc } = require('@fioprotocol/fiojs') type FetchJson = (uri: string, opts?: object) => Promise export class FIOSDK { + private baseUrl: string = '' + private abiMap: Map = new Map() /** * @ignore */ @@ -302,7 +304,7 @@ export class FIOSDK { returnPreparedTrx: boolean = false, ) { this.transactions = new Transactions() - Transactions.baseUrl = baseUrl + this.baseUrl = baseUrl Transactions.FioProvider = Fio Transactions.fetchJson = fetchjson this.registerMockUrl = registerMockUrl @@ -314,7 +316,7 @@ export class FIOSDK { for (const accountName of Constants.rawAbiAccountName) { this.getAbi(accountName) .then((response) => { - Transactions.abiMap.set(response.account_name, response) + this.abiMap.set(response.account_name, response) }) .catch((error) => { throw error @@ -375,7 +377,7 @@ export class FIOSDK { maxFee, this.getTechnologyProviderId(technologyProviderId), ) - return registerFioAddress.execute(this.privateKey, this.publicKey, this.returnPreparedTrx) + return registerFioAddress.execute(this.baseUrl, this.abiMap, this.privateKey, this.publicKey, this.returnPreparedTrx) } /** @@ -398,7 +400,7 @@ export class FIOSDK { maxFee, this.getTechnologyProviderId(technologyProviderId), ) - return registerFioAddress.execute(this.privateKey, this.publicKey, this.returnPreparedTrx) + return registerFioAddress.execute(this.baseUrl, this.abiMap, this.privateKey, this.publicKey, this.returnPreparedTrx) } /** @@ -419,7 +421,7 @@ export class FIOSDK { maxFee, this.getTechnologyProviderId(technologyProviderId), ) - return registerFioDomain.execute(this.privateKey, this.publicKey, this.returnPreparedTrx) + return registerFioDomain.execute(this.baseUrl, this.abiMap, this.privateKey, this.publicKey, this.returnPreparedTrx) } /** @@ -442,7 +444,7 @@ export class FIOSDK { maxFee, this.getTechnologyProviderId(technologyProviderId), ) - return registerFioDomain.execute(this.privateKey, this.publicKey, this.returnPreparedTrx) + return registerFioDomain.execute(this.baseUrl, this.abiMap, this.privateKey, this.publicKey, this.returnPreparedTrx) } /** @@ -462,7 +464,7 @@ export class FIOSDK { maxFee, this.getTechnologyProviderId(technologyProviderId), ) - return burnFioAddress.execute(this.privateKey, this.publicKey, this.returnPreparedTrx) + return burnFioAddress.execute(this.baseUrl, this.abiMap, this.privateKey, this.publicKey, this.returnPreparedTrx) } /** @@ -485,7 +487,7 @@ export class FIOSDK { maxFee, this.getTechnologyProviderId(technologyProviderId), ) - return transferFioDomain.execute(this.privateKey, this.publicKey, this.returnPreparedTrx) + return transferFioDomain.execute(this.baseUrl, this.abiMap, this.privateKey, this.publicKey, this.returnPreparedTrx) } /** @@ -508,7 +510,7 @@ export class FIOSDK { maxFee, this.getTechnologyProviderId(technologyProviderId), ) - return transferFioAddress.execute(this.privateKey, this.publicKey, this.returnPreparedTrx) + return transferFioAddress.execute(this.baseUrl, this.abiMap, this.privateKey, this.publicKey, this.returnPreparedTrx) } /** @@ -531,7 +533,7 @@ export class FIOSDK { maxFee, this.getTechnologyProviderId(technologyProviderId), ) - return addBundledTransactions.execute(this.privateKey, this.publicKey, this.returnPreparedTrx) + return addBundledTransactions.execute(this.baseUrl, this.abiMap, this.privateKey, this.publicKey, this.returnPreparedTrx) } /** @@ -551,7 +553,7 @@ export class FIOSDK { maxFee, this.getTechnologyProviderId(technologyProviderId), ) - return renewFioAddress.execute(this.privateKey, this.publicKey, this.returnPreparedTrx) + return renewFioAddress.execute(this.baseUrl, this.abiMap, this.privateKey, this.publicKey, this.returnPreparedTrx) } /** @@ -571,7 +573,7 @@ export class FIOSDK { maxFee, this.getTechnologyProviderId(technologyProviderId), ) - return renewFioDomain.execute(this.privateKey, this.publicKey, this.returnPreparedTrx) + return renewFioDomain.execute(this.baseUrl, this.abiMap, this.privateKey, this.publicKey, this.returnPreparedTrx) } /** @@ -602,7 +604,7 @@ export class FIOSDK { maxFee, this.getTechnologyProviderId(technologyProviderId), ) - return addPublicAddress.execute(this.privateKey, this.publicKey, this.returnPreparedTrx) + return addPublicAddress.execute(this.baseUrl, this.abiMap, this.privateKey, this.publicKey, this.returnPreparedTrx) } /** @@ -622,7 +624,7 @@ export class FIOSDK { maxFee, this.getTechnologyProviderId(technologyProviderId), ) - return cancelFundsRequest.execute(this.privateKey, this.publicKey, this.returnPreparedTrx) + return cancelFundsRequest.execute(this.baseUrl, this.abiMap, this.privateKey, this.publicKey, this.returnPreparedTrx) } @@ -646,7 +648,7 @@ export class FIOSDK { maxFee, this.getTechnologyProviderId(technologyProviderId), ) - return removePublicAddresses.execute(this.privateKey, this.publicKey, this.returnPreparedTrx) + return removePublicAddresses.execute(this.baseUrl, this.abiMap, this.privateKey, this.publicKey, this.returnPreparedTrx) } /** @@ -675,7 +677,7 @@ export class FIOSDK { maxFee, this.getTechnologyProviderId(technologyProviderId), ) - return transferLockedTokens.execute(this.privateKey, this.publicKey) + return transferLockedTokens.execute(this.baseUrl, this.abiMap, this.privateKey, this.publicKey) } @@ -697,7 +699,7 @@ export class FIOSDK { maxFee, this.getTechnologyProviderId(technologyProviderId), ) - return removeAllPublicAddresses.execute(this.privateKey, this.publicKey, this.returnPreparedTrx) + return removeAllPublicAddresses.execute(this.baseUrl, this.abiMap, this.privateKey, this.publicKey, this.returnPreparedTrx) } @@ -721,7 +723,7 @@ export class FIOSDK { maxFee, this.getTechnologyProviderId(technologyProviderId), ) - return addPublicAddress.execute(this.privateKey, this.publicKey, this.returnPreparedTrx) + return addPublicAddress.execute(this.baseUrl, this.abiMap, this.privateKey, this.publicKey, this.returnPreparedTrx) } /** @@ -744,7 +746,7 @@ export class FIOSDK { maxFee, this.getTechnologyProviderId(technologyProviderId), ) - return SetFioDomainVisibility.execute(this.privateKey, this.publicKey, this.returnPreparedTrx) + return SetFioDomainVisibility.execute(this.baseUrl, this.abiMap, this.privateKey, this.publicKey, this.returnPreparedTrx) } /** @@ -811,7 +813,7 @@ export class FIOSDK { hash, offLineUrl, ) - return recordObtData.execute(this.privateKey, this.publicKey, this.returnPreparedTrx) + return recordObtData.execute(this.baseUrl, this.abiMap, this.privateKey, this.publicKey, this.returnPreparedTrx) } /** @@ -823,7 +825,7 @@ export class FIOSDK { */ public getObtData(limit?: number, offset?: number, tokenCode?: string): Promise { const getObtDataRequest = new queries.GetObtData(this.publicKey, limit, offset, tokenCode) - return getObtDataRequest.execute(this.publicKey, this.privateKey) + return getObtDataRequest.execute(this.baseUrl, this.publicKey, this.privateKey) } /** @@ -843,7 +845,7 @@ export class FIOSDK { maxFee, this.getTechnologyProviderId(technologyProviderId), ) - return rejectFundsRequest.execute(this.privateKey, this.publicKey, this.returnPreparedTrx) + return rejectFundsRequest.execute(this.baseUrl, this.abiMap, this.privateKey, this.publicKey, this.returnPreparedTrx) } /** @@ -896,7 +898,7 @@ export class FIOSDK { hash, offlineUrl, ) - return requestNewFunds.execute(this.privateKey, this.publicKey, this.returnPreparedTrx) + return requestNewFunds.execute(this.baseUrl, this.abiMap, this.privateKey, this.publicKey, this.returnPreparedTrx) } /** @@ -906,7 +908,7 @@ export class FIOSDK { */ public getLocks(fioPublicKey: string): Promise { const getLocks = new queries.GetLocks(fioPublicKey) - return getLocks.execute(this.publicKey) + return getLocks.execute(this.baseUrl, this.publicKey) } /* @@ -916,7 +918,7 @@ export class FIOSDK { */ public getAccount(actor: string): Promise { const getAccount = new queries.GetAccount(actor) - return getAccount.execute(this.publicKey) + return getAccount.execute(this.baseUrl, this.publicKey) } /** @@ -926,7 +928,7 @@ export class FIOSDK { */ public isAvailable(fioName: string): Promise { const availabilityCheck = new queries.AvailabilityCheck(fioName) - return availabilityCheck.execute(this.publicKey) + return availabilityCheck.execute(this.baseUrl, this.publicKey) } /** @@ -936,7 +938,7 @@ export class FIOSDK { */ public getFioBalance(fioPublicKey?: string): Promise { const getFioBalance = new queries.GetFioBalance(fioPublicKey) - return getFioBalance.execute(this.publicKey) + return getFioBalance.execute(this.baseUrl, this.publicKey) } /** @@ -946,7 +948,7 @@ export class FIOSDK { */ public getFioNames(fioPublicKey: string): Promise { const getNames = new queries.GetNames(fioPublicKey) - return getNames.execute(this.publicKey) + return getNames.execute(this.baseUrl, this.publicKey) } /** @@ -958,7 +960,7 @@ export class FIOSDK { */ public getFioAddresses(fioPublicKey: string, limit?: number, offset?: number): Promise { const getNames = new queries.GetAddresses(fioPublicKey, limit, offset) - return getNames.execute(this.publicKey) + return getNames.execute(this.baseUrl, this.publicKey) } /** @@ -970,7 +972,7 @@ export class FIOSDK { */ public getFioDomains(fioPublicKey: string, limit?: number, offset?: number): Promise { const getNames = new queries.GetDomains(fioPublicKey, limit, offset) - return getNames.execute(this.publicKey) + return getNames.execute(this.baseUrl, this.publicKey) } /** @@ -981,7 +983,7 @@ export class FIOSDK { */ public getPendingFioRequests(limit?: number, offset?: number): Promise { const pendingFioRequests = new queries.PendingFioRequests(this.publicKey, limit, offset) - return pendingFioRequests.execute(this.publicKey, this.privateKey) + return pendingFioRequests.execute(this.baseUrl, this.publicKey, this.privateKey) } /** @@ -992,7 +994,7 @@ export class FIOSDK { */ public getSentFioRequests(limit?: number, offset?: number): Promise { const sentFioRequest = new queries.SentFioRequests(this.publicKey, limit, offset) - return sentFioRequest.execute(this.publicKey, this.privateKey) + return sentFioRequest.execute(this.baseUrl, this.publicKey, this.privateKey) } /** @@ -1003,7 +1005,7 @@ export class FIOSDK { */ public getCancelledFioRequests(limit?: number, offset?: number): Promise { const cancelledFioRequest = new queries.CancelledFioRequests(this.publicKey, limit, offset) - return cancelledFioRequest.execute(this.publicKey, this.privateKey) + return cancelledFioRequest.execute(this.baseUrl, this.publicKey, this.privateKey) } /** @@ -1023,7 +1025,7 @@ export class FIOSDK { chainCode, tokenCode, ) - return publicAddressLookUp.execute(this.publicKey) + return publicAddressLookUp.execute(this.baseUrl, this.publicKey) } /** @@ -1043,7 +1045,7 @@ export class FIOSDK { limit, offset, ) - return publicAddressesLookUp.execute(this.publicKey) + return publicAddressesLookUp.execute(this.baseUrl, this.publicKey) } /** @@ -1077,7 +1079,7 @@ export class FIOSDK { maxFee, this.getTechnologyProviderId(technologyProviderId), ) - return transferTokens.execute(this.privateKey, this.publicKey, this.returnPreparedTrx) + return transferTokens.execute(this.baseUrl, this.abiMap, this.privateKey, this.publicKey, this.returnPreparedTrx) } /** @@ -1106,7 +1108,7 @@ export class FIOSDK { */ public getFee(endPoint: EndPoint, fioAddress: string = ''): Promise { const fioFee = new queries.GetFee(endPoint, fioAddress) - return fioFee.execute(this.publicKey) + return fioFee.execute(this.baseUrl, this.publicKey) } /** @@ -1242,7 +1244,7 @@ export class FIOSDK { account, data, ) - return pushTransaction.execute(this.privateKey, this.publicKey, this.returnPreparedTrx) + return pushTransaction.execute(this.baseUrl, this.abiMap, this.privateKey, this.publicKey, this.returnPreparedTrx) } /** @@ -1492,6 +1494,6 @@ export class FIOSDK { */ private getAbi(accountName: string): Promise { const abi = new queries.GetAbi(accountName) - return abi.execute(this.publicKey) + return abi.execute(this.baseUrl, this.publicKey) } } diff --git a/src/transactions/Transactions.ts b/src/transactions/Transactions.ts index f4ec1183..63a8eeeb 100644 --- a/src/transactions/Transactions.ts +++ b/src/transactions/Transactions.ts @@ -10,13 +10,13 @@ const textEncoder: TextEncoder = new TextEncoder() const textDecoder: TextDecoder = new TextDecoder() export class Transactions { - public static baseUrl: string - public static abiMap: Map = new Map() public static FioProvider: { prepareTransaction(param: any): Promise; accountHash(pubkey: string): string } + public abiMap: Map = new Map() + public baseUrl: string = '' public static fetchJson: FetchJson public publicKey: string = '' public privateKey: string = '' @@ -38,7 +38,7 @@ export class Transactions { 'Content-Type': 'application/json', }, } - const res = await Transactions.fetchJson(Transactions.baseUrl + 'chain/get_info', options) + const res = await Transactions.fetchJson(this.baseUrl + 'chain/get_info', options) return await res.json() } @@ -49,7 +49,7 @@ export class Transactions { if (chain.last_irreversible_block_num == undefined) { throw new Error('chain.last_irreversible_block_num undefined') } - const res = await Transactions.fetchJson(Transactions.baseUrl + 'chain/get_block', { + const res = await Transactions.fetchJson(this.baseUrl + 'chain/get_block', { method: 'POST', headers: { 'Accept': 'application/json', @@ -90,12 +90,12 @@ export class Transactions { transaction.expiration = expirationStr.substr(0, expirationStr.length - 1) if (dryRun) { return Transactions.FioProvider.prepareTransaction({ - transaction, chainId: chain.chain_id, privateKeys: privky, abiMap: Transactions.abiMap, + transaction, chainId: chain.chain_id, privateKeys: privky, abiMap: this.abiMap, textDecoder: new TextDecoder(), textEncoder: new TextEncoder(), }) } else { const signedTransaction = await Transactions.FioProvider.prepareTransaction({ - transaction, chainId: chain.chain_id, privateKeys: privky, abiMap: Transactions.abiMap, + transaction, chainId: chain.chain_id, privateKeys: privky, abiMap: this.abiMap, textDecoder: new TextDecoder(), textEncoder: new TextEncoder(), }) return this.executeCall(endpoint, JSON.stringify(signedTransaction)) @@ -122,9 +122,9 @@ export class Transactions { } } try { - const res = await Transactions.fetchJson(Transactions.baseUrl + endPoint, options) + const res = await Transactions.fetchJson(this.baseUrl + endPoint, options) if (!res.ok) { - const error: Error & { json?: Object, errorCode?: string, requestParams?: { endPoint: string, body: string, fetchOptions?: any } } = new Error(`Error ${res.status} while fetching ${Transactions.baseUrl + endPoint}`) + const error: Error & { json?: Object, errorCode?: string, requestParams?: { endPoint: string, body: string, fetchOptions?: any } } = new Error(`Error ${res.status} while fetching ${this.baseUrl + endPoint}`) try { error.json = await res.json() } catch (e) { diff --git a/src/transactions/queries/Query.ts b/src/transactions/queries/Query.ts index d01cf8ca..1db83968 100644 --- a/src/transactions/queries/Query.ts +++ b/src/transactions/queries/Query.ts @@ -10,9 +10,10 @@ export abstract class Query extends Transactions { isEncrypted = false - async execute(publicKey: string, privateKey: string = ''): Promise { + async execute(baseUrl: string, publicKey: string, privateKey: string = ''): Promise { this.publicKey = publicKey this.privateKey = privateKey + this.baseUrl = baseUrl if (!this.isEncrypted) { return this.executeCall(this.getEndPoint(), JSON.stringify(this.getData())) } else { diff --git a/src/transactions/signed/SignedTransaction.ts b/src/transactions/signed/SignedTransaction.ts index ce5c1961..0ca67685 100644 --- a/src/transactions/signed/SignedTransaction.ts +++ b/src/transactions/signed/SignedTransaction.ts @@ -2,6 +2,7 @@ import { Autorization } from '../../entities/Autorization' import { RawAction } from '../../entities/RawAction' import { RawTransaction } from '../../entities/RawTransaction' import { Transactions } from '../Transactions' +import { AbiResponse } from "../../entities/AbiResponse"; export abstract class SignedTransaction extends Transactions { public abstract ENDPOINT: string @@ -10,9 +11,11 @@ export abstract class SignedTransaction extends Transactions { public abstract getData(): any - public async execute(privateKey: string, publicKey: string, dryRun = false): Promise { + public async execute(baseUrl: string, abiMap: Map, privateKey: string, publicKey: string, dryRun = false): Promise { this.privateKey = privateKey this.publicKey = publicKey + this.baseUrl = baseUrl + this.abiMap = abiMap const rawTransaction = new RawTransaction() const rawaction = new RawAction()