-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
242 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { http, HttpResponse } from "msw"; | ||
import { setupServer } from "msw/node"; | ||
|
||
import { buildNodeConfiguration } from "../utils/public"; | ||
import { getAddress } from "./getAddress"; | ||
|
||
const server = setupServer(); | ||
|
||
describe("Function: getAddress", () => { | ||
// MSW Setup | ||
beforeAll(() => server.listen()); | ||
afterEach(() => server.resetHandlers()); | ||
afterAll(() => server.close()); | ||
|
||
it("returns address for valid ", async () => { | ||
// ARRANGE | ||
const nodeConfiguration = buildNodeConfiguration({ | ||
url: "http://localhost", | ||
}); | ||
|
||
const mockResponse = { | ||
status: "ok", | ||
data: "2z2jCebUcMDWxsvVXxSjGCEspg3sobCoQQQJRZY3g6985uhLJKEAJ3ofcUeHU9rpN9dWYirNVGmneiaiYwb7Jxh", | ||
coin: "arionum", | ||
}; | ||
|
||
let requestUrl = ""; | ||
|
||
server.use( | ||
http.get(`http://localhost/api.php`, (info) => { | ||
requestUrl = info.request.url; | ||
return HttpResponse.json(mockResponse); | ||
}), | ||
); | ||
|
||
// ACT | ||
const response = await getAddress(nodeConfiguration, { | ||
publicKey: | ||
"PZ8Tyr4Nx8MHsRAGMpZmZ6TWY63dXWSCy82Bs7eNrGVcNW1p9cgZMe954tCXagAhmKqd1VtjRzwBYCFdh8KuwXqDiX58nRhU4CGGXcRDzFEf9ttUMQjGFjzv", | ||
}); | ||
|
||
// ASSERT | ||
expect(requestUrl).toContain( | ||
"public_key=PZ8Tyr4Nx8MHsRAGMpZmZ6TWY63dXWSCy82Bs7eNrGVcNW1p9cgZMe954tCXagAhmKqd1VtjRzwBYCFdh8KuwXqDiX58nRhU4CGGXcRDzFEf9ttUMQjGFjzv", | ||
); | ||
|
||
expect(response).toEqual( | ||
"2z2jCebUcMDWxsvVXxSjGCEspg3sobCoQQQJRZY3g6985uhLJKEAJ3ofcUeHU9rpN9dWYirNVGmneiaiYwb7Jxh", | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { NodeConfiguration } from "../utils/public"; | ||
import { buildRequestUrl } from "../utils/internal"; | ||
import { call } from "../utils/internal"; | ||
import { AssetBalance, assetBalanceFromApi } from "./models"; | ||
|
||
export const getAssetBalance = async ( | ||
nodeConfiguration: NodeConfiguration, | ||
payload: { address?: string; asset?: string; publicKey?: string }, | ||
): Promise<Array<AssetBalance>> => { | ||
const { address, asset, publicKey } = payload; | ||
|
||
const queryParams: Record<string, number | string> = { | ||
q: "asset-orders", | ||
}; | ||
|
||
if (publicKey) { | ||
queryParams.public_key = publicKey; | ||
} | ||
|
||
if (address) { | ||
queryParams.account = address; | ||
} | ||
|
||
if (asset) { | ||
queryParams.asset = asset; | ||
} | ||
|
||
const url = buildRequestUrl(nodeConfiguration.url, "/api.php", queryParams); | ||
|
||
return (await call<Array<any>>({ url })).map((data: any) => | ||
assetBalanceFromApi(data), | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { NodeConfiguration } from "../utils/public"; | ||
import { buildRequestUrl } from "../utils/internal"; | ||
import { call } from "../utils/internal"; | ||
import { AssetOrder, assetOrderFromApi } from "./models"; | ||
|
||
export const getAssetOrders = async ( | ||
nodeConfiguration: NodeConfiguration, | ||
payload: { address?: string; asset?: string }, | ||
): Promise<Array<AssetOrder>> => { | ||
const { address, asset } = payload; | ||
|
||
const queryParams: Record<string, number | string> = { | ||
q: "asset-orders", | ||
}; | ||
|
||
if (address) { | ||
queryParams.account = address; | ||
} | ||
|
||
if (asset) { | ||
queryParams.asset = asset; | ||
} | ||
|
||
const url = buildRequestUrl(nodeConfiguration.url, "/api.php", queryParams); | ||
|
||
return (await call<Array<any>>({ url })).map((data: any) => | ||
assetOrderFromApi(data), | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { NodeConfiguration } from "../utils/public"; | ||
import { buildRequestUrl } from "../utils/internal"; | ||
import { call } from "../utils/internal"; | ||
import { Asset, assetFromApi } from "./models"; | ||
|
||
export const getAssets = async ( | ||
nodeConfiguration: NodeConfiguration, | ||
payload: { asset?: string }, | ||
): Promise<Array<Asset>> => { | ||
const { asset } = payload; | ||
|
||
const queryParams: Record<string, number | string> = { | ||
q: "assets", | ||
}; | ||
|
||
if (asset) { | ||
queryParams.asset = asset; | ||
} | ||
|
||
const url = buildRequestUrl(nodeConfiguration.url, "/api.php", queryParams); | ||
|
||
return (await call<Array<any>>({ url })).map((data: any) => | ||
assetFromApi(data), | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
export interface AssetBalance { | ||
asset: string; | ||
alias: string; | ||
address: string; | ||
balance: number; | ||
} | ||
|
||
export const assetBalanceFromApi = (data: any): AssetBalance => { | ||
return { | ||
asset: data.asset, | ||
alias: data.alias, | ||
address: data.account, | ||
balance: data.balance, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
export type AssetOrderType = "bid"; | ||
|
||
export interface AssetOrder { | ||
id: string; | ||
address: string; | ||
asset: string; | ||
price: number; | ||
date: number; | ||
status: boolean; | ||
type: AssetOrderType; | ||
value: number; | ||
valueDone: number; | ||
cancelable: boolean; | ||
} | ||
|
||
export const assetOrderFromApi = (data: any): AssetOrder => { | ||
return { | ||
id: data.id, | ||
address: data.account, | ||
asset: data.asset, | ||
price: data.price, | ||
date: data.date, | ||
status: data.status, | ||
type: data.type, | ||
value: data.val, | ||
valueDone: data.val_done, | ||
cancelable: data.cancelable, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
export interface Asset { | ||
id: string; | ||
maxSupply: number; | ||
tradeable: boolean; | ||
price: number; | ||
dividendOnly: boolean; | ||
autoDividend: boolean; | ||
allowBids: boolean; | ||
blockHeight: number; | ||
alias: string | null; | ||
balance: number; | ||
} | ||
|
||
export const assetFromApi = (data: any): Asset => { | ||
return { | ||
id: data.id, | ||
maxSupply: data.max_supply, | ||
tradeable: data.tradable, | ||
price: data.price, | ||
dividendOnly: data.dividend_only, | ||
autoDividend: data.auto_dividend, | ||
allowBids: data.allow_bid, | ||
blockHeight: data.height, | ||
alias: data.alias, | ||
balance: data.balance, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters