UNPKG

@gateway.fm/gtw-dvf-client-js

Version:

DVF client js lib with gateway.fm rpc endpoints

129 lines (104 loc) 3.72 kB
const nock = require('nock') const instance = require('../test/helpers/instance') const mockGetConf = require('../test/fixtures/getConf') jest.mock('../../lib/ledger/selectTransport') const mockEth = require('@ledgerhq/hw-app-eth') const mockSelector = require('../../lib/ledger/selectTransport') const { createTransportReplayer } = require('@ledgerhq/hw-transport-mocker') jest.mock('@ledgerhq/hw-app-eth', () => { return { default: jest.fn(() => mockEth), getAddress: jest.fn(() => { return { address: '0x341e46a49f15785373ede443df0220dea6a41bbc' } }), starkGetPublicKey: jest.fn(() => '0401841559c5a886771644573dbb6dba210a1a7a0834afcf6bb3cbba1565ae7b3202f0f543d1b6666fa1e093b5d03feb90f0e68ab007baf587b6285d425d8a34dc'), provideERC20TokenInformation: jest.fn(() => true), starkProvideQuantum: jest.fn(() => true), starkSignTransfer: jest.fn(() => { return { r: '06519b47cc1c5a2731420d824cce3a1a42fcbe3a4b0614187603474255a7332c', s: '01f1ae85334d2a2aea2e3ec2e79f42c6397dd97fc72332a0b1e948cb82e2d3ef' } }) } }) const Transport = createTransportReplayer() let dvf describe.skip('dvf.withdraw', () => { beforeAll(async () => { mockGetConf() dvf = await instance() }) it('Withdraws ETH to users vault', async () => { mockGetConf() const path = '44\'/60\'/0\'/0\'/0' const token = 'ETH' const amount = 1.117 const starkPublicKey = { x: '01841559c5a886771644573dbb6dba210a1a7a0834afcf6bb3cbba1565ae7b32', y: '02f0f543d1b6666fa1e093b5d03feb90f0e68ab007baf587b6285d425d8a34dc' } const apiResponse = { token, amount, starkPublicKey } mockSelector.mockImplementation(() => { return Transport }) const payloadValidator = jest.fn(body => { expect(body).toMatchObject(apiResponse) expect(typeof body.nonce).toBe('number') expect(body.starkSignature.r).toMatch(/[\da-f]/i) expect(body.starkSignature.s).toMatch(/[\da-f]/i) expect(typeof body.starkVaultId).toBe('number') expect(typeof body.expireTime).toBe('number') return true }) nock(dvf.config.api) .post('/v1/trading/w/withdraw', payloadValidator) .reply(200, apiResponse) const starkWithdrawal = await dvf.stark.ledger.createWithdrawalData( path, token, amount ) await dvf.ledger.withdraw(token, amount, starkWithdrawal) expect(payloadValidator).toBeCalled() }) it('Withdraws ERC20 token to user\'s vault', async () => { mockGetConf() const path = '44\'/60\'/0\'/0\'/0' const amount = 193 const token = 'USDT' const starkPublicKey = { x: '01841559c5a886771644573dbb6dba210a1a7a0834afcf6bb3cbba1565ae7b32', y: '02f0f543d1b6666fa1e093b5d03feb90f0e68ab007baf587b6285d425d8a34dc' } const apiResponse = { token, amount, starkPublicKey } mockSelector.mockImplementation(() => { return Transport }) const payloadValidator = jest.fn(body => { expect(body).toMatchObject(apiResponse) expect(typeof body.nonce).toBe('number') expect(body.starkSignature.r).toMatch(/[\da-f]/i) expect(body.starkSignature.s).toMatch(/[\da-f]/i) expect(typeof body.starkVaultId).toBe('number') expect(typeof body.expireTime).toBe('number') return true }) nock(dvf.config.api) .post('/v1/trading/w/withdraw', payloadValidator) .reply(200, apiResponse) const starkWithdrawal = await dvf.stark.ledger.createWithdrawalData( path, token, amount ) await dvf.ledger.withdraw(token, amount, starkWithdrawal) expect(payloadValidator).toBeCalled() }) })