Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/ExchangeUnion/xud into or…
Browse files Browse the repository at this point in the history
…der-invalidation-fix
  • Loading branch information
sercan.ozdemir committed Sep 5, 2020
2 parents b5cc88c + 1b397db commit 5dee5c6
Show file tree
Hide file tree
Showing 4 changed files with 145 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/connextclient/ConnextClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -723,7 +723,7 @@ class ConnextClient extends SwapClient {
if (all) {
if (currency === 'ETH') {
// TODO: query Ether balance, subtract gas price times 21000 (gas usage of transferring Ether), and set that as amount
throw Error('withdrawing all ETH is not supported yet');
throw new Error('withdrawing all ETH is not supported yet');
}
units = freeBalanceOnChain;
} else if (argAmount) {
Expand All @@ -732,7 +732,7 @@ class ConnextClient extends SwapClient {
amount: argAmount,
});
if (Number(freeBalanceOnChain) < argUnits) {
throw new Error('amount cannot be greater than wallet balance');
throw errors.INSUFFICIENT_BALANCE;
}
units = argUnits.toString();
}
Expand Down
2 changes: 2 additions & 0 deletions lib/grpc/getGrpcError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { errorCodes as orderErrorCodes } from '../orderbook/errors';
import { errorCodes as p2pErrorCodes } from '../p2p/errors';
import { errorCodes as serviceErrorCodes } from '../service/errors';
import { errorCodes as swapErrorCodes } from '../swaps/errors';
import { errorCodes as connextErrorCodes } from '../connextclient/errors';

/**
* Convert an internal xud error type into a gRPC error.
Expand Down Expand Up @@ -38,6 +39,7 @@ const getGrpcError = (err: any) => {
case orderErrorCodes.PAIR_ALREADY_EXISTS:
code = status.ALREADY_EXISTS;
break;
case connextErrorCodes.INSUFFICIENT_BALANCE:
case p2pErrorCodes.NOT_CONNECTED:
case p2pErrorCodes.NODE_NOT_BANNED:
case p2pErrorCodes.NODE_IS_BANNED:
Expand Down
130 changes: 130 additions & 0 deletions test/jest/Connext.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@ import { CurrencyInstance } from '../../lib/db/types';
import { PaymentState } from '../../lib/swaps/SwapClient';
import errors from '../../lib/connextclient/errors';

const MOCK_TX_HASH = '0x5544332211';
jest.mock('../../lib/utils/utils', () => {
return {
parseResponseBody: () => {
return { txhash: MOCK_TX_HASH };
},
};
});
jest.mock('../../lib/Logger');
const mockedLogger = <jest.Mock<Logger>>(<any>Logger);

Expand All @@ -27,6 +35,7 @@ jest.mock('http', () => {
});

const ETH_ASSET_ID = '0x0000000000000000000000000000000000000000';
const USDT_ASSET_ID = '0xdAC17F958D2ee523a2206206994597C13D831ec7';

describe('ConnextClient', () => {
let connext: ConnextClient;
Expand All @@ -48,6 +57,11 @@ describe('ConnextClient', () => {
tokenAddress: ETH_ASSET_ID,
swapClient: SwapClientType.Connext,
},
{
id: 'USDT',
tokenAddress: USDT_ASSET_ID,
swapClient: SwapClientType.Connext,
},
] as CurrencyInstance[];
connext = new ConnextClient({
config,
Expand All @@ -57,6 +71,122 @@ describe('ConnextClient', () => {
});
});

describe('withdraw', () => {
const MOCK_FREE_BALANCE_ON_CHAIN = 10000;
const DESTINATION_ADDRESS = '0x12345';

beforeEach(() => {
connext['getBalance'] = jest.fn().mockReturnValue({
freeBalanceOnChain: MOCK_FREE_BALANCE_ON_CHAIN,
});
connext['sendRequest'] = jest.fn();
});

afterEach(() => {
jest.clearAllMocks();
}),

it('fails with custom fee', async () => {
expect.assertions(1);
try {
await connext.withdraw({
currency: 'ETH',
destination: DESTINATION_ADDRESS,
amount: 123,
fee: 1,
});
} catch (e) {
expect(e).toMatchSnapshot();
}
});

it('fails to withdraw all ETH', async () => {
expect.assertions(1);
try {
await connext.withdraw({
currency: 'ETH',
destination: DESTINATION_ADDRESS,
all: true,
});
} catch (e) {
expect(e).toMatchSnapshot();
}
});

it('fails when amount bigger than wallet balance', async () => {
expect.assertions(1);
try {
await connext.withdraw({
currency: 'ETH',
destination: DESTINATION_ADDRESS,
amount: 0.0000011,
});
} catch (e) {
expect(e).toMatchSnapshot();
}
});

it('withdraws all USDT', async () => {
expect.assertions(3);
const txhash = await connext.withdraw({
currency: 'USDT',
destination: DESTINATION_ADDRESS,
all: true,
});
expect(connext['sendRequest']).toHaveBeenCalledTimes(1);
expect(connext['sendRequest']).toHaveBeenCalledWith(
'/onchain-transfer',
'POST',
expect.objectContaining({
assetId: USDT_ASSET_ID,
amount: MOCK_FREE_BALANCE_ON_CHAIN,
recipient: DESTINATION_ADDRESS,
}),
);
expect(txhash).toEqual(MOCK_TX_HASH);
});

it('withdraws 5000 USDT amount', async () => {
expect.assertions(3);
const txhash = await connext.withdraw({
currency: 'USDT',
destination: DESTINATION_ADDRESS,
amount: 5000,
});
expect(connext['sendRequest']).toHaveBeenCalledTimes(1);
expect(connext['sendRequest']).toHaveBeenCalledWith(
'/onchain-transfer',
'POST',
expect.objectContaining({
assetId: USDT_ASSET_ID,
amount: '50',
recipient: DESTINATION_ADDRESS,
}),
);
expect(txhash).toEqual(MOCK_TX_HASH);
});

it('withdraws 0.000001 ETH amount', async () => {
expect.assertions(3);
const txhash = await connext.withdraw({
currency: 'ETH',
destination: DESTINATION_ADDRESS,
amount: 0.0000005,
});
expect(connext['sendRequest']).toHaveBeenCalledTimes(1);
expect(connext['sendRequest']).toHaveBeenCalledWith(
'/onchain-transfer',
'POST',
expect.objectContaining({
assetId: ETH_ASSET_ID,
amount: '5000',
recipient: DESTINATION_ADDRESS,
}),
);
expect(txhash).toEqual(MOCK_TX_HASH);
});
});

describe('sendRequest', () => {
it('deposit fails with 404', async () => {
expect.assertions(1);
Expand Down
11 changes: 11 additions & 0 deletions test/jest/__snapshots__/Connext.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,14 @@ Object {
"message": "connext payment not found",
}
`;

exports[`ConnextClient withdraw fails to withdraw all ETH 1`] = `[Error: withdrawing all ETH is not supported yet]`;

exports[`ConnextClient withdraw fails when amount bigger than wallet balance 1`] = `
Object {
"code": "8.2",
"message": "insufficient balance to perform request",
}
`;

exports[`ConnextClient withdraw fails with custom fee 1`] = `[Error: setting fee for Ethereum withdrawals is not supported yet]`;

0 comments on commit 5dee5c6

Please sign in to comment.