Skip to content

Commit

Permalink
Merge pull request #11 from Pusher-Labs/implement-missing-btc-items
Browse files Browse the repository at this point in the history
Implement missing btc items
  • Loading branch information
Stuart authored Oct 14, 2020
2 parents d81cdac + e4a6b4c commit a66aeb0
Show file tree
Hide file tree
Showing 14 changed files with 43,431 additions and 34,270 deletions.
2 changes: 1 addition & 1 deletion packages/asgardex-binance/src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,5 @@ export const getTxType = (t: BinanceTxType): TxType => {
if (t === 'TRANSFER' || t === 'DEPOSIT') return 'transfer'
if (t === 'FREEZE_TOKEN') return 'freeze'
if (t === 'UN_FREEZE_TOKEN') return 'unfreeze'
return 'unkown'
return 'unknown'
}
49 changes: 45 additions & 4 deletions packages/asgardex-bitcoin/__tests__/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,17 +159,17 @@ describe('BitcoinClient Test', () => {
btcClient.setNetwork('testnet')
btcClient.setPhrase(phraseOne)
const estimates = await btcClient.getFees()
expect(estimates.fast).toEqual(expect.any(Number))
expect(estimates.fastest).toEqual(expect.any(Number))
expect(estimates.average).toEqual(expect.any(Number))
expect(estimates.fast).toBeDefined()
expect(estimates.fastest).toBeDefined()
expect(estimates.average).toBeDefined()
})

it('should return estimated fees of a vault tx that are more expensive than a normal tx', async () => {
btcClient.setNetwork('testnet')
btcClient.setPhrase(phraseOne)
const normalTx = await btcClient.getFees()
const vaultTx = await btcClient.getFeesWithMemo(MEMO)
expect(vaultTx.fast!).toBeGreaterThan(normalTx.fast!)
expect(vaultTx.fast!.amount().isGreaterThan(normalTx.fast!.amount())).toBeTruthy()
})

it('should error when an invalid address is used in getting balance', () => {
Expand All @@ -192,4 +192,45 @@ describe('BitcoinClient Test', () => {
btcClient.transfer({ asset: AssetBTC, recipient: invalidAddress, amount, feeRate: 1 }),
).rejects.toThrow(expectedError)
})


it('should get address transactions', async () => {
btcClient.setNetwork('testnet')

const txPages = await btcClient.getTransactions({ address:addyThree, limit: 4 })
expect(txPages.total).toEqual(1)
expect(txPages.txs[0].asset).toEqual(AssetBTC)
expect(txPages.txs[0].date).toEqual(new Date("2020-10-08T01:28:33.000Z"))
expect(txPages.txs[0].hash).toEqual('63fe21150a285bd4c4266e6e3aafe5d99cc76eafb8e92a0c0b2896d3c3a21c78')
expect(txPages.txs[0].type).toEqual('transfer')
expect(txPages.txs[0].to.length).toEqual(2)
expect(txPages.txs[0].from.length).toEqual(1)
})

it('should get address transactions by offset', async () => {
btcClient.setNetwork('testnet')
// Offset should work
const txPages = await btcClient.getTransactions({ address:addyThree, offset: 1, limit: 1 })
expect(txPages.total).toEqual(1)
expect(txPages.txs.length).toEqual(0) //coz addyThree only has one tx so offsetting should give 0
})

it('should not get address transactions when offset too high', async () => {
btcClient.setNetwork('testnet')
// Offset max should work
return expect(btcClient.getTransactions({ address:addyThree, offset: 9000000 })).rejects.toThrow('Max offset allowed 1000000')
})

it('should get address transactions with limit', async () => {
btcClient.setNetwork('testnet')
// Limit should work
const txPages = await btcClient.getTransactions({ address:addyThree, limit: 1 })
return expect(txPages.total).toEqual(1)
})

it('should not get address transactions when limit too high', async () => {
btcClient.setNetwork('testnet')
// Limit max should work
return expect(btcClient.getTransactions({ address:addyThree, limit: 9000000 })).rejects.toThrow('Max limit allowed 10000')
})
})
30 changes: 30 additions & 0 deletions packages/asgardex-bitcoin/lib/blockchair-api.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { BtcAddressDTO, Transactions, RawTxsBTC, ChainStatsBtc } from './types/blockchair-api-types';
/**
* https://blockchair.com/api/docs#link_200
* @param chain
* @param hash
*/
export declare const getTx: (baseUrl: string, hash: string, apiKey?: string | undefined) => Promise<Transactions>;
/**
* https://blockchair.com/api/docs#link_201
* @param chain
* @param hash
*/
export declare const getRawTx: (baseUrl: string, hash: string, apiKey?: string | undefined) => Promise<RawTxsBTC>;
/**
* https://blockchair.com/api/docs#link_300
* @param chain
* @param address
*/
export declare const getAddress: (baseUrl: string, address: string, apiKey?: string | undefined, limit?: number | undefined, offset?: number | undefined) => Promise<BtcAddressDTO>;
/**
* https://blockchair.com/api/docs#link_202
* @param chain
* @param txHex
*/
export declare const broadcastTx: (baseUrl: string, txHex: string, apiKey?: string | undefined) => Promise<string>;
/**
* https://blockchair.com/api/docs#link_001
* @param chain
*/
export declare const bitcoinStats: (baseUrl: string, apiKey?: string | undefined) => Promise<ChainStatsBtc>;
55 changes: 25 additions & 30 deletions packages/asgardex-bitcoin/lib/client.d.ts
Original file line number Diff line number Diff line change
@@ -1,53 +1,48 @@
import * as Bitcoin from 'bitcoinjs-lib';
import * as Utils from './utils';
import { Txs } from './types/electrs-api-types';
import { FeeOptions } from './types/client-types';
import { AsgardexClient, TxParams } from '@asgardex-clients/core';
import { TxHistoryParams, TxsPage, Address, AsgardexClient, TxParams, TxHash, Balance, Network, Fees, AsgardexClientParams } from '@asgardex-clients/asgardex-client';
/**
* Class variables accessed across functions
*/
declare enum Network {
TEST = "testnet",
MAIN = "mainnet"
}
/**
* BitcoinClient Interface. Potentially to become AsgardClient
* BitcoinClient Interface
*/
interface BitcoinClient {
purgeClient(): void;
setNetwork(net: Network): void;
getNetwork(net: Network): Bitcoin.networks.Network;
setBaseUrl(endpoint: string): void;
getAddress(): string;
validateAddress(address: string): boolean;
scanUTXOs(): Promise<void>;
calcFees(addressTo: string, memo?: string): Promise<FeeOptions>;
}
declare type BitcoinClientParams = AsgardexClientParams & {
nodeUrl?: string;
nodeApiKey?: string;
};
/**
* Implements Client declared above
*/
declare class Client implements BitcoinClient, AsgardexClient {
net: Network;
phrase: string;
electrsAPI: string;
utxos: Utils.UTXO[];
constructor(_net?: Network, _electrsAPI?: string, _phrase?: string);
nodeUrl: string;
nodeApiKey: string;
constructor({ network, nodeUrl, nodeApiKey, phrase }: BitcoinClientParams);
setNodeURL(url: string): void;
setNodeAPIKey(key: string): void;
generatePhrase: () => string;
setPhrase: (phrase?: string | undefined) => void;
setPhrase: (phrase: string) => Address;
purgeClient: () => void;
setNetwork(_net: Network): void;
getNetwork(net: Network): Bitcoin.networks.Network;
setBaseUrl(endpoint: string): void;
getExplorerUrl: () => string;
getAddress: () => string;
getNetwork(): Network;
getExplorerAddressUrl(address: Address): string;
getExplorerTxUrl(txID: string): string;
getAddress: () => Address;
private getBtcKeys;
validateAddress: (address: string) => boolean;
scanUTXOs: () => Promise<void>;
getBalance: () => Promise<number>;
getBalanceForAddress: (address: string) => Promise<number>;
getBalance: (address?: string | undefined) => Promise<Balance[]>;
private getChange;
getTransactions: (address: string) => Promise<Txs>;
calcFees: (memo?: string | undefined) => Promise<FeeOptions>;
transfer: ({ amount, recipient, memo, feeRate }: TxParams) => Promise<string>;
getTransactions: (params?: TxHistoryParams | undefined) => Promise<TxsPage>;
/**
* getFees
*/
getFees(): Promise<Fees>;
getFeesWithMemo(memo: string): Promise<Fees>;
deposit({ asset, amount, recipient, memo, feeRate }: TxParams): Promise<TxHash>;
transfer({ asset, amount, recipient, memo, feeRate }: TxParams): Promise<TxHash>;
}
export { Client, Network };
Loading

0 comments on commit a66aeb0

Please sign in to comment.