forked from renproject/send-crypto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ETHHandler.ts
163 lines (144 loc) · 4.9 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
import BigNumber from "bignumber.js";
import Web3 from "web3";
import { TransactionConfig } from "web3-core";
import { forwardEvents, newPromiEvent, PromiEvent } from "../../lib/promiEvent";
import { Asset, Handler } from "../../types/types";
import {
getEndpoint,
getNetwork,
getTransactionConfig,
getWeb3,
} 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 TransactionConfig {
subtractFee?: boolean; // defaults to false
}
export class ETHHandler
implements
Handler<ConstructorOptions, AddressOptions, BalanceOptions, TxOptions> {
private readonly privateKey: string;
private readonly network: string;
private readonly decimals = 18;
private readonly unlockedAddress: string;
private readonly sharedState: {
web3: Web3;
};
constructor(
privateKey: string,
network: string,
options?: ConstructorOptions,
sharedState?: any
) {
this.network = getNetwork(network);
this.privateKey = privateKey;
const [web3, address] = getWeb3(
this.privateKey,
getEndpoint(
this.network,
options && options.ethereumNode,
options && options.infuraKey
)
);
this.unlockedAddress = address;
sharedState.web3 = web3;
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;
// (await this.sharedState.web3.eth.getAccounts())[0];
// 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.web3.eth.getBlockNumber()
);
atBlock = currentBlock
.minus(options.confirmations)
.plus(1)
.toNumber();
}
const address =
(options && options.address) || (await this.address(asset));
return new BigNumber(
await this.sharedState.web3.eth.getBalance(address, atBlock as any)
);
};
// 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.web3.eth.getGasPrice());
const gasLimit = txOptions.gas || 21000;
const fee = new BigNumber(gasPrice.toString()).times(gasLimit);
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 web3PromiEvent = (this.sharedState.web3.eth.sendTransaction({
from: await this.address(asset),
gas: 21000,
...txOptions,
to,
value: value.toFixed(),
}) as unknown) as PromiEvent<string>;
forwardEvents(web3PromiEvent, promiEvent);
web3PromiEvent.then(promiEvent.resolve);
})().catch((error) => {
promiEvent.reject(error);
});
return promiEvent;
};
}