forked from renproject/send-crypto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathETHHandler.ts
173 lines (153 loc) · 5.12 KB
/
ETHHandler.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
import BigNumber from "bignumber.js";
import { ethers, Overrides } from "ethers";
import { newPromiEvent, PromiEvent } from "../../lib/promiEvent";
import { Asset, Handler } from "../../types/types";
import {
getEndpoint,
getEthersSigner,
getNetwork,
getTransactionConfig,
Network,
} from "./ethUtils";
interface ConstructorOptions {
infuraKey?: string;
ethereumNode?: string;
}
interface AddressOptions {}
interface BalanceOptions extends AddressOptions {
address?: string;
// Note that this acts differently to BTC/BCH/ZEC. This returns the balance
// (confirmations - 1) blocks ago.
confirmations?: number; // defaults to 0
}
interface TxOptions extends Overrides {
subtractFee?: boolean; // defaults to false
}
export class ETHHandler
implements
Handler<ConstructorOptions, AddressOptions, BalanceOptions, TxOptions>
{
private readonly privateKey: string;
private readonly network: Network;
private readonly decimals = 18;
private readonly unlockedAddress: string;
private readonly sharedState: {
ethSigner: ethers.Signer;
};
constructor(
privateKey: string,
network: string,
options?: ConstructorOptions,
sharedState?: any
) {
this.network = getNetwork(network);
this.privateKey = privateKey;
const [ethSigner, address] = getEthersSigner(
this.privateKey,
getEndpoint(
this.network,
options && options.ethereumNode,
options && options.infuraKey
)
);
this.unlockedAddress = address;
sharedState.ethSigner = ethSigner;
this.sharedState = sharedState;
}
// Returns whether or not this can handle the asset
public readonly handlesAsset = (asset: Asset): boolean =>
typeof asset === "string" &&
["ETH", "ETHER", "ETHEREUM"].indexOf(asset.toUpperCase()) !== -1;
public readonly address = async (
asset: Asset,
options?: AddressOptions
): Promise<string> => this.unlockedAddress;
// Balance
public readonly getBalance = async (
asset: Asset,
options?: BalanceOptions
): Promise<BigNumber> =>
(await this.getBalanceInSats(asset, options)).dividedBy(
new BigNumber(10).exponentiatedBy(this.decimals)
);
public readonly getBalanceInSats = async (
asset: Asset,
options?: BalanceOptions
): Promise<BigNumber> => {
let atBlock;
if (options && options.confirmations && options.confirmations > 0) {
const currentBlock = new BigNumber(
await this.sharedState.ethSigner.provider!.getBlockNumber()
);
atBlock = currentBlock
.minus(options.confirmations)
.plus(1)
.toNumber();
}
const address =
(options && options.address) || (await this.address(asset));
return new BigNumber(
(
await this.sharedState.ethSigner.provider!.getBalance(
address,
atBlock
)
).toString()
);
};
// Transfer
public readonly send = (
to: string,
value: BigNumber,
asset: Asset,
options?: TxOptions
): PromiEvent<string> =>
this.sendSats(
to,
value.times(new BigNumber(10).exponentiatedBy(this.decimals)),
asset,
options
);
public readonly sendSats = (
to: string,
valueIn: BigNumber,
asset: Asset,
optionsIn?: TxOptions
): PromiEvent<string> => {
const promiEvent = newPromiEvent<string>();
(async () => {
const options = optionsIn || {};
let value = valueIn;
const txOptions = getTransactionConfig(options);
if (options.subtractFee) {
const gasPrice =
txOptions.gasPrice ||
(await this.sharedState.ethSigner.provider!.getGasPrice());
const gasPriceBN = new BigNumber(gasPrice.toString());
const gasLimit = txOptions.gasLimit || 21000;
const gasLimitBN = new BigNumber(gasLimit.toString());
const fee = gasPriceBN.times(gasLimitBN);
if (fee.gt(value)) {
throw new Error(
`Unable to include fee in value, fee exceeds value (${fee.toFixed()} > ${value.toFixed()})`
);
}
value = value.minus(fee);
}
const from: string = await this.address(asset);
const tx = await this.sharedState.ethSigner.sendTransaction({
from,
gasLimit: 21000,
...txOptions,
to,
value: value.toFixed(),
});
promiEvent.emit("transactionHash", tx.hash);
await tx.wait();
promiEvent.resolve(tx.hash);
})().catch((error) => {
promiEvent.reject(error);
});
return promiEvent;
};
}