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

fix(core-api): missing orderBy #2974

Merged
merged 1 commit into from
Sep 28, 2019
Merged
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
33 changes: 32 additions & 1 deletion __tests__/integration/core-api/handlers/locks.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ describe("API 2.0 - Locks", () => {
walletManager.findByAddress(Identities.Address.fromPassphrase("1")),
walletManager.findByAddress(Identities.Address.fromPassphrase("2")),
walletManager.findByAddress(Identities.Address.fromPassphrase("3")),
walletManager.findByAddress(Identities.Address.fromPassphrase("4")),
walletManager.findByAddress(Identities.Address.fromPassphrase("5")),
walletManager.findByAddress(Identities.Address.fromPassphrase("6")),
];

lockIds = [];
Expand All @@ -33,7 +36,7 @@ describe("API 2.0 - Locks", () => {
lockIds.push(transaction.id);

locks[transaction.id] = {
amount: Utils.BigNumber.make(10),
amount: Utils.BigNumber.make(10 * (j + 1)),
recipientId: wallet.address,
secretHash: transaction.id,
expiration: {
Expand Down Expand Up @@ -80,6 +83,34 @@ describe("API 2.0 - Locks", () => {
expect(response.data.data).not.toBeEmpty();
expect(response.data.data.every(lock => lock.expirationType === 2)).toBeTrue();
});

describe("orderBy", () => {
it("should be ordered by amount:desc", async () => {
const response = await utils.request("GET", "locks", { orderBy: "amount:desc", expirationType: 2 });
expect(response).toBeSuccessfulResponse();
expect(response.data.data).toBeArray();

for (let i = 0; i < response.data.data.length - 1; i++) {
const lockA = response.data.data[i];
const lockB = response.data.data[i + 1];

expect(Utils.BigNumber.make(lockA.amount).isGreaterThanOrEqualTo(lockB.amount)).toBeTrue();
}
});

it("should be ordered by amount:ascs", async () => {
const response = await utils.request("GET", "locks", { orderBy: "amount:asc", expirationType: 2 });
expect(response).toBeSuccessfulResponse();
expect(response.data.data).toBeArray();

for (let i = 0; i < response.data.data.length - 1; i++) {
const lockA = response.data.data[i];
const lockB = response.data.data[i + 1];

expect(Utils.BigNumber.make(lockA.amount).isLessThanOrEqualTo(lockB.amount)).toBeTrue();
}
});
});
});

describe("GET /locks/:id", () => {
Expand Down
75 changes: 75 additions & 0 deletions __tests__/integration/core-api/handlers/wallets.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import "../../../utils";

import { app } from "@arkecosystem/core-container";
import { Database, State } from "@arkecosystem/core-interfaces";
import { Identities, Utils } from "@arkecosystem/crypto";
import { genesisBlock } from "../../../utils/fixtures/testnet/block-model";
import { setUp, tearDown } from "../__support__/setup";
import { utils } from "../utils";

Expand Down Expand Up @@ -127,6 +131,77 @@ describe("API 2.0 - Wallets", () => {
});
});

describe("GET /wallets/:id/locks", () => {
let walletManager: State.IWalletManager;
let wallets;
let lockIds;

beforeAll(() => {
walletManager = app.resolvePlugin<Database.IDatabaseService>("database").walletManager;

wallets = [
walletManager.findByPublicKey(Identities.PublicKey.fromPassphrase("1")),
walletManager.findByPublicKey(Identities.PublicKey.fromPassphrase("2")),
walletManager.findByPublicKey(Identities.PublicKey.fromPassphrase("3")),
walletManager.findByPublicKey(Identities.PublicKey.fromPassphrase("4")),
walletManager.findByPublicKey(Identities.PublicKey.fromPassphrase("5")),
walletManager.findByPublicKey(Identities.PublicKey.fromPassphrase("6")),
];

lockIds = [];

for (let i = 0; i < wallets.length; i++) {
const wallet = wallets[i];
const transactions = genesisBlock.transactions.slice(i * 10, i * 10 + i + 1);

const locks = {};
for (let j = 0; j < transactions.length; j++) {
const transaction = transactions[j];
lockIds.push(transaction.id);

locks[transaction.id] = {
amount: Utils.BigNumber.make(10 * (j + 1)),
recipientId: wallet.address,
secretHash: transaction.id,
expiration: {
type: j % 2 === 0 ? 1 : 2,
value: 100 * (j + 1),
},
};
}

wallet.setAttribute("htlc.locks", locks);
}

walletManager.index(wallets);
});

it("should GET all locks for the given wallet by id", async () => {
const response = await utils.request("GET", `wallets/${wallets[0].address}/locks`);
expect(response).toBeSuccessfulResponse();
expect(response.data.data).toBeArray();
expect(response.data.data).toHaveLength(1);
utils.expectLock(response.data.data[0]);
});

it("should fail to GET locks for the given wallet if it doesn't exist", async () => {
utils.expectError(await utils.request("GET", "wallets/fake-address/locks"), 404);
});

it("should GET all locks for the given wallet in the given order", async () => {
const response = await utils.request("GET", `wallets/${wallets[5].address}/locks`, {
orderBy: "amount:desc",
});

for (let i = 0; i < response.data.data.length - 1; i++) {
const lockA = response.data.data[i];
const lockB = response.data.data[i + 1];

expect(Utils.BigNumber.make(lockA.amount).isGreaterThanOrEqualTo(lockB.amount)).toBeTrue();
}
});
});

describe("POST /wallets/search", () => {
it("should POST a search for wallets with the exact specified address", async () => {
const response = await utils.request("POST", "wallets/search", {
Expand Down
5 changes: 4 additions & 1 deletion packages/core-api/src/handlers/locks/transformer.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { Interfaces } from "@arkecosystem/crypto";

export const transformLock = (lock: Interfaces.IHtlcLock) => {
return lock;
return {
...lock,
amount: lock.amount.toFixed(),
};
};
1 change: 1 addition & 0 deletions packages/core-api/src/handlers/wallets/methods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ const locks = async request => {

const rows = databaseService.wallets.search(Database.SearchScope.Locks, {
...request.params,
...request.query,
...paginate(request),
senderPublicKey: wallet.publicKey,
});
Expand Down
11 changes: 11 additions & 0 deletions packages/core-api/src/handlers/wallets/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,9 @@ export const locks: object = {
},
query: {
...pagination,
...{
orderBy: Joi.string(),
},
},
};

Expand Down Expand Up @@ -227,5 +230,13 @@ export const search: object = {
.integer()
.min(0),
}),
lockedBalance: Joi.object().keys({
from: Joi.number()
.integer()
.min(0),
to: Joi.number()
.integer()
.min(0),
}),
},
};
3 changes: 3 additions & 0 deletions packages/core-api/src/handlers/wallets/transformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ export const transformWallet = (wallet: State.IWallet) => {
nonce: wallet.nonce.toFixed(),
secondPublicKey: wallet.getAttribute("secondPublicKey"),
balance: Utils.BigNumber.make(wallet.balance).toFixed(),
lockedBalance: wallet.hasAttribute("htlc.lockedBalance")
? wallet.getAttribute("htlc.lockedBalance").toFixed()
: undefined,
isDelegate: !!username,
isResigned: !!wallet.getAttribute("delegate.resigned"),
vote: wallet.getAttribute("vote"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export const sortEntries = <T extends Record<string, any>>(
): T[] => {
const [iteratee, order] = params.orderBy ? params.orderBy : defaultOrder;

if (["balance", "voteBalance"].includes(iteratee)) {
if (["balance", "voteBalance", "lockedBalance", "amount"].includes(iteratee)) {
return Object.values(entries).sort((a: T, b: T) => {
const iterateeA: Utils.BigNumber = getProperty(a, iteratee) || Utils.BigNumber.ZERO;
const iterateeB: Utils.BigNumber = getProperty(b, iteratee) || Utils.BigNumber.ZERO;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Database, State } from "@arkecosystem/core-interfaces";
import { delegateCalculator, hasSomeProperty } from "@arkecosystem/core-utils";
import { Interfaces } from "@arkecosystem/crypto";
import { Interfaces, Utils } from "@arkecosystem/crypto";
import { searchEntries } from "./utils/search-entries";

interface ISearchContext<T = any> {
Expand All @@ -12,15 +12,15 @@ interface ISearchContext<T = any> {
interface IUnwrappedHtlcLock {
lockId: string;
senderPublicKey: string;
amount: string;
amount: Utils.BigNumber;
recipientId: string;
secretHash: string;
expirationType: number;
expirationValue: number;
}

export class WalletsBusinessRepository implements Database.IWalletsBusinessRepository {
public constructor(private readonly databaseServiceProvider: () => Database.IDatabaseService) {}
public constructor(private readonly databaseServiceProvider: () => Database.IDatabaseService) { }

public search<T>(scope: Database.SearchScope, params: Database.IParameters = {}): Database.IRowsPaginated<T> {
let searchContext: ISearchContext;
Expand Down Expand Up @@ -90,7 +90,7 @@ export class WalletsBusinessRepository implements Database.IWalletsBusinessRepos
private searchWallets(params: Database.IParameters): ISearchContext<State.IWallet> {
const query: Record<string, string[]> = {
exact: ["address", "publicKey", "secondPublicKey", "username", "vote"],
between: ["balance", "voteBalance"],
between: ["balance", "voteBalance", "lockedBalance"],
};

if (params.addresses) {
Expand Down Expand Up @@ -194,7 +194,7 @@ export class WalletsBusinessRepository implements Database.IWalletsBusinessRepos
const lock: Interfaces.IHtlcLock = locks[lockId];
return {
lockId,
amount: lock.amount.toFixed(),
amount: lock.amount,
secretHash: lock.secretHash,
senderPublicKey: wallet.publicKey,
recipientId: lock.recipientId,
Expand Down