Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: added testnet url to wallet service and using it on axiosIn… #248

Draft
wants to merge 4 commits into
base: old-dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -224,9 +224,10 @@ export const TX_MINING_TESTNET_URL = 'https://txmining.testnet.hathor.network/';
export const SELECT_OUTPUTS_TIMEOUT = 1000 * 60;

/**
* Wallet service URL
* Wallet service URLs
*/
export const WALLET_SERVICE_BASE_URL = 'https://wallet-service.testnet.hathor.network/';
export const WALLET_SERVICE_BASE_URL = 'https://wallet-service.hathor.network/';
export const WALLET_SERVICE_TESTNET_BASE_URL = 'https://wallet-service.testnet.hathor.network/';

/**
* Size in bytes of a transaction hash (32 bytes)
Expand Down
30 changes: 15 additions & 15 deletions src/wallet/api/walletApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,47 +16,47 @@ import Input from '../../models/input';
*/

const walletApi = {
getWalletStatus(id: string): Promise<any> {
getWalletStatus(id: string, network = 'mainnet'): Promise<any> {
const data = { params: { id } }
return axiosInstance().get('wallet', data);
return axiosInstance(network).get('wallet', data);
},

createWallet(xpubkey: string): Promise<any> {
createWallet(xpubkey: string, network = 'mainnet'): Promise<any> {
const data = { xpubkey };
return axiosInstance().post('wallet', data);
return axiosInstance(network).post('wallet', data);
},

getAddresses(id: string): Promise<any> {
getAddresses(id: string, network = 'mainnet'): Promise<any> {
const data = { params: { id } }
return axiosInstance().get('addresses', data);
return axiosInstance(network).get('addresses', data);
},

getBalances(id: string, token: string | null = null): Promise<any> {
getBalances(id: string, token: string | null = null, network = 'mainnet'): Promise<any> {
const data = { params: { id } }
if (token) {
data['params']['token_id'] = token;
}
return axiosInstance().get('balances', data);
return axiosInstance(network).get('balances', data);
},

getHistory(id: string, token: string | null = null): Promise<any> {
getHistory(id: string, token: string | null = null, network = 'mainnet'): Promise<any> {
// TODO add pagination parameters
const data = { params: { id } }
if (token) {
data['params']['token_id'] = token;
}
return axiosInstance().get('txhistory', data);
return axiosInstance(network).get('txhistory', data);
},

createTxProposal(id: string, outputs: Output[], inputs: Input[]): Promise<any> {
createTxProposal(id: string, outputs: Output[], inputs: Input[], network = 'mainnet'): Promise<any> {
const data = { id, outputs, inputs };
return axiosInstance().post('txproposals', data);
return axiosInstance(network).post('txproposals', data);
},

updateTxProposal(id: string, timestamp: number, nonce: number, weight: number, parents: string[], inputsData: string[]): Promise<any> {
updateTxProposal(id: string, timestamp: number, nonce: number, weight: number, parents: string[], inputsData: string[], network = 'mainnet'): Promise<any> {
const data = { timestamp, nonce, weight, parents, inputsSignatures: inputsData };
return axiosInstance().put(`txproposals/${id}`, data);
return axiosInstance(network).put(`txproposals/${id}`, data);
},
};

export default walletApi;
export default walletApi;
28 changes: 23 additions & 5 deletions src/wallet/api/walletServiceAxios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@
*/

import axios from 'axios';
import { WALLET_SERVICE_BASE_URL, TIMEOUT } from '../../constants';
import {
WALLET_SERVICE_BASE_URL,
WALLET_SERVICE_TESTNET_BASE_URL,
TIMEOUT,
} from '../../constants';

/**
* Method that creates an axios instance
Expand All @@ -19,11 +23,12 @@ import { WALLET_SERVICE_BASE_URL, TIMEOUT } from '../../constants';
*
* @param {number} timeout Timeout in milliseconds for the request
*/
export const axiosInstance = (timeout: number = TIMEOUT) => {
// TODO make base URL customizable
export const axiosInstance = (network: string = 'mainnet', timeout: number = TIMEOUT) => {
const baseUrl = getBaseUrl(network);

// TODO How to allow 'Retry' request?
const defaultOptions = {
baseURL: WALLET_SERVICE_BASE_URL,
baseURL: baseUrl,
timeout: timeout,
headers: {
'Content-Type': 'application/json',
Expand All @@ -33,4 +38,17 @@ export const axiosInstance = (timeout: number = TIMEOUT) => {
return axios.create(defaultOptions);
}

export default axiosInstance;
/**
* Returns the correct base url constant for wallet service based on the network
*
* @param {string} network The network, can be either mainnet or testnet but will default to testnet
*/
const getBaseUrl = (network: string): string => {
if (network === 'mainnet') {
return WALLET_SERVICE_BASE_URL;
} else {
return WALLET_SERVICE_TESTNET_BASE_URL
}
};

export default axiosInstance;
14 changes: 7 additions & 7 deletions src/wallet/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ class HathorWalletServiceWallet extends EventEmitter {
}
}
try {
const res = await walletApi.createWallet(xpub);
const res = await walletApi.createWallet(xpub, this.network.name);
const data = res.data;
if (res.status === 200 && data.success) {
await handleCreate(data.status);
Expand Down Expand Up @@ -118,7 +118,7 @@ class HathorWalletServiceWallet extends EventEmitter {
*/
async startPollingStatus() {
try {
const res = await walletApi.getWalletStatus(this.walletId!);
const res = await walletApi.getWalletStatus(this.walletId!, this.network.name);
const data = res.data;
if (res.status === 200 && data.success) {
if (data.status.status === 'creating') {
Expand Down Expand Up @@ -147,7 +147,7 @@ class HathorWalletServiceWallet extends EventEmitter {
* @inner
*/
async getAllAddresses(): Promise<string[]> {
const response = await walletApi.getAddresses(this.walletId!);
const response = await walletApi.getAddresses(this.walletId!, this.network.name);
let addresses = [];
if (response.status === 200 && response.data.success === true) {
addresses = response.data.addresses;
Expand All @@ -164,7 +164,7 @@ class HathorWalletServiceWallet extends EventEmitter {
* @inner
*/
async getBalance(token: string | null = null) {
const response = await walletApi.getBalances(this.walletId!, token);
const response = await walletApi.getBalances(this.walletId!, token, this.network.name);
let balance = null;
if (response.status === 200 && response.data.success === true) {
balance = response.data.balances;
Expand All @@ -184,7 +184,7 @@ class HathorWalletServiceWallet extends EventEmitter {
// TODO Add pagination parameters
const requestOptions = Object.assign({ token: null }, options);
const { token } = requestOptions;
const response = await walletApi.getHistory(this.walletId!, token);
const response = await walletApi.getHistory(this.walletId!, token, this.network.name);
let history = []
if (response.status === 200 && response.data.success === true) {
history = response.data.history;
Expand Down Expand Up @@ -232,7 +232,7 @@ class HathorWalletServiceWallet extends EventEmitter {
changeAddress: null
}, options);
const { inputs, changeAddress } = newOptions;
const response = await walletApi.createTxProposal(this.walletId!, outputs, inputs);
const response = await walletApi.createTxProposal(this.walletId!, outputs, inputs, this.network.name);
if (response.status === 201) {
const responseData = response.data;
this.txProposalId = responseData.txProposalId;
Expand Down Expand Up @@ -302,7 +302,7 @@ class HathorWalletServiceWallet extends EventEmitter {
inputsData.push(input.data!.toString('base64'));
}

return await walletApi.updateTxProposal(this.txProposalId!, data.timestamp, data.nonce, data.weight, data.parents, inputsData);
return await walletApi.updateTxProposal(this.txProposalId!, data.timestamp, data.nonce, data.weight, data.parents, inputsData, this.network.name);
}

/**
Expand Down