/
ETHCurrency.ts
182 lines (164 loc) · 6.45 KB
/
ETHCurrency.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/*
* The MIT License (MIT)
* Copyright (c) 2018 Heat Ledger Ltd.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
* */
class ETHCurrency implements ICurrency {
private ethplorer: EthplorerService
public symbol = 'ETH'
public homePath
private pendingTransactions: EthereumPendingTransactionsService
private user: UserService
constructor(public secretPhrase: string, public address: string) {
this.ethplorer = heat.$inject.get('ethplorer')
this.user = heat.$inject.get('user')
this.homePath = `/ethereum-account/${this.address}`
this.pendingTransactions = heat.$inject.get('ethereumPendingTransactions')
}
/* Returns the currency balance, fraction is delimited with a period (.) */
getBalance(): angular.IPromise<string> {
return this.ethplorer.getBalance(this.address).then(
balance => {
return utils.commaFormat(Number(balance+"").toFixed(18))
}
)
}
/* Register a balance changed observer, unregister by calling the returned
unregister method */
subscribeBalanceChanged(handler: ()=>void): ()=>void {
return function () {}
}
/* Manually invoke the balance changed observers */
notifyBalanceChanged() {
}
/* Invoke SEND currency dialog */
invokeSendDialog($event) {
this.sendEther($event).then(
data => {
let address = this.user.account
let timestamp = new Date().getTime()
this.pendingTransactions.add(address, data.txHash, timestamp)
},
err => {
if (err) {
dialogs.alert($event, 'Send Ether Error', 'There was an error sending this transaction: '+JSON.stringify(err))
}
}
)
}
/* Invoke SEND token dialog */
invokeSendToken($event) {
}
sendEther($event) {
function DialogController2($scope: angular.IScope, $mdDialog: angular.material.IDialogService) {
$scope['vm'].cancelButtonClick = function () {
$mdDialog.cancel()
}
$scope['vm'].okButtonClick = function ($event) {
let user = <UserService> heat.$inject.get('user')
let web3 = <Web3Service> heat.$inject.get('web3')
let amountInWei = web3.web3.toWei($scope['vm'].data.amount.replace(',',''), 'ether')
let from = user.currency.address
let to = $scope['vm'].data.recipient
$scope['vm'].disableOKBtn = true
web3.sendEther(from, to, amountInWei).then(
data => {
$mdDialog.hide(data).then(() => {
dialogs.alert(event, 'Success', `TxHash: ${data.txHash}`);
})
},
err => {
$mdDialog.hide(null).then(() => {
dialogs.alert(event, 'Error', err.message);
})
}
)
}
$scope['vm'].disableOKBtn = false
$scope['vm'].data = {
amount: '',
recipient: '',
recipientInfo: '',
fee: '0.000420'
}
/* Lookup recipient info and display this in the dialog */
let lookup = utils.debounce(function () {
let ethplorer = <EthplorerService> heat.$inject.get('ethplorer')
ethplorer.getAddressInfo($scope['vm'].data.recipient).then(
info => {
$scope.$evalAsync(() => {
let balance = Number.parseFloat(info.ETH.balance).toFixed(18)
$scope['vm'].data.recipientInfo = `Balance: ${balance} ETH`
})
},
error => {
$scope.$evalAsync(() => {
$scope['vm'].data.recipientInfo = error.message||'Invalid'
})
}
)
}, 1000, false)
$scope['vm'].recipientChanged = function () {
$scope['vm'].data.recipientInfo = ''
lookup()
}
}
let $q = heat.$inject.get('$q')
let $mdDialog = <angular.material.IDialogService> heat.$inject.get('$mdDialog')
let deferred = $q.defer<{ txHash:string }>()
$mdDialog.show({
controller: DialogController2,
parent: angular.element(document.body),
targetEvent: $event,
clickOutsideToClose:false,
controllerAs: 'vm',
template: `
<md-dialog>
<form name="dialogForm">
<md-toolbar>
<div class="md-toolbar-tools"><h2>Send Ether</h2></div>
</md-toolbar>
<md-dialog-content style="min-width:500px;max-width:600px" layout="column" layout-padding>
<div flex layout="column">
<md-input-container flex >
<label>Recipient</label>
<input ng-model="vm.data.recipient" ng-change="vm.recipientChanged()" required name="recipient">
<span ng-if="vm.data.recipientInfo">{{vm.data.recipientInfo}}</span>
</md-input-container>
<md-input-container flex >
<label>Amount in ETH</label>
<input ng-model="vm.data.amount" required name="amount">
</md-input-container>
<p>Fee: {{vm.data.fee}} ETH</p>
</div>
</md-dialog-content>
<md-dialog-actions layout="row">
<span flex></span>
<md-button class="md-warn" ng-click="vm.cancelButtonClick()" aria-label="Cancel">Cancel</md-button>
<md-button ng-disabled="!vm.data.recipient || !vm.data.amount || vm.disableOKBtn"
class="md-primary" ng-click="vm.okButtonClick()" aria-label="OK">OK</md-button>
</md-dialog-actions>
</form>
</md-dialog>
`
}).then(deferred.resolve, deferred.reject);
return deferred.promise
}
}