|
| 1 | +import BaseController, { BaseConfig, BaseState } from './BaseController'; |
| 2 | + |
| 3 | +/** |
| 4 | + * @type ShapeShiftTransaction |
| 5 | + * |
| 6 | + * ShapeShift transaction object |
| 7 | + * |
| 8 | + * @property depositAddress - Address where coins should be deposited |
| 9 | + * @property depositType - Abbreviation of the type of crypto currency to be deposited |
| 10 | + * @property key - Unique string to identify this transaction as a ShapeShift transaction |
| 11 | + * @property response - Populated with a ShapeShiftResponse object upon transaction completion |
| 12 | + * @property time - Timestamp when this transction was last updated |
| 13 | + */ |
| 14 | +export interface ShapeShiftTransaction { |
| 15 | + depositAddress: string; |
| 16 | + depositType: string; |
| 17 | + key: string; |
| 18 | + response?: ShapeShiftResponse; |
| 19 | + time: number; |
| 20 | +} |
| 21 | + |
| 22 | +/** |
| 23 | + * @type ShapeShiftResponse |
| 24 | + * |
| 25 | + * ShapeShift transaction response object |
| 26 | + * |
| 27 | + * @property status - String indicating transactional status |
| 28 | + */ |
| 29 | +export interface ShapeShiftResponse { |
| 30 | + status: 'complete' | 'failed' | 'no_deposits' | 'received'; |
| 31 | +} |
| 32 | + |
| 33 | +/** |
| 34 | + * @type ShapeShiftConfig |
| 35 | + * |
| 36 | + * ShapeShift controller configuration |
| 37 | + * |
| 38 | + * @property interval - Polling interval used to fetch ShapeShift transactions |
| 39 | + */ |
| 40 | +export interface ShapeShiftConfig extends BaseConfig { |
| 41 | + interval: number; |
| 42 | +} |
| 43 | + |
| 44 | +/** |
| 45 | + * @type ShapeShiftState |
| 46 | + * |
| 47 | + * ShapeShift controller state |
| 48 | + * |
| 49 | + * @property shapeShiftTxList - List of ShapeShift transactions |
| 50 | + */ |
| 51 | +export interface ShapeShiftState extends BaseState { |
| 52 | + shapeShiftTxList: ShapeShiftTransaction[]; |
| 53 | +} |
| 54 | + |
| 55 | +/** |
| 56 | + * Controller that passively polls on a set interval for ShapeShift transactions |
| 57 | + */ |
| 58 | +export class ShapeShiftController extends BaseController<ShapeShiftState, ShapeShiftConfig> { |
| 59 | + private handle?: NodeJS.Timer; |
| 60 | + |
| 61 | + private getPendingTransactions() { |
| 62 | + return this.state.shapeShiftTxList.filter((tx) => !tx.response || tx.response.status !== 'complete'); |
| 63 | + } |
| 64 | + |
| 65 | + private getUpdateURL(transaction: ShapeShiftTransaction) { |
| 66 | + return `https://shapeshift.io/txStat/${transaction.depositAddress}`; |
| 67 | + } |
| 68 | + |
| 69 | + private async updateTransaction(transaction: ShapeShiftTransaction) { |
| 70 | + try { |
| 71 | + const response = await fetch(this.getUpdateURL(transaction)); |
| 72 | + transaction.response = await response.json(); |
| 73 | + if (transaction.response && transaction.response.status === 'complete') { |
| 74 | + transaction.time = Date.now(); |
| 75 | + } |
| 76 | + return transaction; |
| 77 | + } catch (error) { |
| 78 | + /* tslint:disable-next-line:no-empty */ |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * Creates a ShapeShiftController instance |
| 84 | + * |
| 85 | + * @param state - Initial state to set on this controller |
| 86 | + * @param config - Initial options used to configure this controller |
| 87 | + */ |
| 88 | + constructor(state?: Partial<ShapeShiftState>, config?: Partial<ShapeShiftConfig>) { |
| 89 | + super(state, config); |
| 90 | + this.defaultConfig = { interval: 3000 }; |
| 91 | + this.defaultState = { shapeShiftTxList: [] }; |
| 92 | + this.initialize(); |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * Sets a new polling interval |
| 97 | + * |
| 98 | + * @param interval - Polling interval used to fetch new ShapeShift transactions |
| 99 | + */ |
| 100 | + set interval(interval: number) { |
| 101 | + this.handle && clearInterval(this.handle); |
| 102 | + this.updateTransactionList(); |
| 103 | + this.handle = setInterval(() => { |
| 104 | + this.updateTransactionList(); |
| 105 | + }, interval); |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * Creates a new ShapeShift transaction |
| 110 | + * |
| 111 | + * @param depositAddress - Address where coins should be deposited |
| 112 | + * @param depositType - Abbreviation of the type of crypto currency to be deposited |
| 113 | + */ |
| 114 | + createTransaction(depositAddress: string, depositType: string) { |
| 115 | + const { shapeShiftTxList } = this.state; |
| 116 | + const transaction = { |
| 117 | + depositAddress, |
| 118 | + depositType, |
| 119 | + key: 'shapeshift', |
| 120 | + response: undefined, |
| 121 | + time: Date.now() |
| 122 | + }; |
| 123 | + |
| 124 | + shapeShiftTxList.push(transaction); |
| 125 | + this.update({ shapeShiftTxList }); |
| 126 | + } |
| 127 | + |
| 128 | + /** |
| 129 | + * Updates list of ShapeShift transactions |
| 130 | + */ |
| 131 | + async updateTransactionList() { |
| 132 | + const { shapeShiftTxList } = this.state; |
| 133 | + const pendingTx = this.getPendingTransactions(); |
| 134 | + |
| 135 | + if (this.disabled || pendingTx.length === 0) { |
| 136 | + return; |
| 137 | + } |
| 138 | + await Promise.all(pendingTx.map((tx) => this.updateTransaction(tx))); |
| 139 | + this.update({ shapeShiftTxList }); |
| 140 | + } |
| 141 | +} |
| 142 | + |
| 143 | +export default ShapeShiftController; |
0 commit comments