-
Notifications
You must be signed in to change notification settings - Fork 0
/
fees-claimant.js
180 lines (154 loc) · 7.17 KB
/
fees-claimant.js
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
'use strict';
const TokenHolderRevenueFundContractsEnsemble = require('./token-holder-revenue-fund-contracts-ensemble');
const NestedError = require('../nested-error');
const {ethers: {utils: {formatUnits}}} = require('ethers');
const _tokenHolderRevenueFundContractEnsemble = new WeakMap();
const _provider = new WeakMap();
async function getTokenInfo(currency) {
return _provider.get(this).getTokenInfo(currency.ct.toString(), true);
}
/**
* @class FeesClaimant
* A class for the claiming and withdrawal of accrued fees
* @alias module:nahmii-sdk
*/
class FeesClaimant {
constructor(provider, abstractionNames) {
_tokenHolderRevenueFundContractEnsemble.set(
this, new TokenHolderRevenueFundContractsEnsemble(provider, abstractionNames)
);
_provider.set(this, provider);
}
/**
* Get claimable accruals
* @param {Wallet} wallet - The claimer nahmii wallet
* @param {Currency} currency - The currency
* @param {number} [firstAccrual] - An optional lower accrual index boundary
* @param {number} [lastAccrual] - An optional upper accrual index boundary
* @returns {Promise} A promise that resolves into an array of claimable accruals.
*/
async claimableAccruals(wallet, currency, firstAccrual, lastAccrual) {
const ensemble = _tokenHolderRevenueFundContractEnsemble.get(this);
firstAccrual = firstAccrual || 0;
lastAccrual = lastAccrual || (await ensemble.closedAccrualsCount(currency)) - 1;
const accruals = [...Array(lastAccrual - firstAccrual + 1).keys()]
.map(o => firstAccrual + o);
const accrualClaimableStats = (await Promise.all(
accruals.map(accrual => ensemble.fullyClaimed(wallet, currency, accrual))
))
.map((fullyClaimed, index) => ({
accrual: accruals[index],
claimable: !fullyClaimed
}));
const claimableAccruals = accrualClaimableStats
.filter(s => s.claimable)
.map(s => s.accrual);
return claimableAccruals;
}
/**
* Get claimable amount of fees for a span of accruals
* @param {Wallet} wallet - The claimer nahmii wallet
* @param {Currency} currency - The currency
* @param {number} firstAccrual - The lower accrual index boundary
* @param {number} lastAccrual - The upper accrual index boundary
* @returns {Promise} A promise that resolves into a string value representing the claimable amount of fees.
*/
async claimableFeesForAccruals(wallet, currency, firstAccrual, lastAccrual) {
const ensemble = _tokenHolderRevenueFundContractEnsemble.get(this);
const claimableAmount = await ensemble.claimableAmountByAccruals(
wallet, currency, firstAccrual, lastAccrual
);
const tokenInfo = await getTokenInfo.call(this, currency);
return tokenInfo ? formatUnits(claimableAmount, tokenInfo.decimals) : claimableAmount.toString();
}
/**
* Claim fees for a span of accruals
* @param {Wallet} wallet - The claimer nahmii wallet
* @param {Currency} currency - The currency
* @param {number} firstAccrual - The lower accrual index boundary
* @param {number} lastAccrual - The upper accrual index boundary
* @param [options] - An optional object containing the parameters for gasLimit and gasPrice
* @returns {Promise} A promise that resolves into an array of transaction hashes.
*/
async claimFeesForAccruals(wallet, currency, firstAccrual, lastAccrual, options) {
try {
const ensemble = _tokenHolderRevenueFundContractEnsemble.get(this);
const txs = ensemble.claimAndStageByAccruals(
wallet, currency, firstAccrual, lastAccrual, options
);
return txs;
}
catch (error) {
throw new NestedError(error, 'Unable to claim fees for accruals.');
}
}
/**
* Get claimable amount of fees for a span of block numbers
* @param {Wallet} wallet - The claimer nahmii wallet
* @param {Currency} currency - The currency
* @param {number} firstBlock - The lower block number boundary
* @param {number} lastBlock - The upper block number boundary
* @returns {Promise} A promise that resolves into a string value representing the claimable amount of fees.
*/
async claimableFeesForBlocks(wallet, currency, firstBlock, lastBlock) {
const ensemble = _tokenHolderRevenueFundContractEnsemble.get(this);
const claimableAmount = await ensemble.claimableAmountByBlockNumbers(
wallet, currency, firstBlock, lastBlock
);
const tokenInfo = await getTokenInfo.call(this, currency);
return tokenInfo ? formatUnits(claimableAmount, tokenInfo.decimals) : claimableAmount.toString();
}
/**
* Claim fees for a span of block numbers
* @param {Wallet} wallet - The claimer nahmii wallet
* @param {Currency} currency - The currency
* @param {number} firstBlock - The lower block number boundary
* @param {number} lastBlock - The upper block number boundary
* @param [options] - An optional object containing the parameters for gasLimit and gasPrice
* @returns {Promise} A promise that resolves into an array of transaction hashes.
*/
async claimFeesForBlocks(wallet, currency, firstBlock, lastBlock, options) {
try {
const ensemble = _tokenHolderRevenueFundContractEnsemble.get(this);
const txs = ensemble.claimAndStageByBlockNumbers(
wallet, currency, firstBlock, lastBlock, options
);
return txs;
}
catch (error) {
throw new NestedError(error, 'Unable to claim fees for blocks.');
}
}
/**
* Get the withdrawable amount of fees
* @param {Wallet} wallet - The claimer nahmii wallet
* @param {Currency} currency - The currency
* @returns {Promise} A promise that resolves into a string value representing the withdrawable amount of fees.
*/
async withdrawableFees(wallet, currency) {
const ensemble = _tokenHolderRevenueFundContractEnsemble.get(this);
const withdrawableFees = await ensemble.stagedBalance(wallet, currency);
const tokenInfo = await getTokenInfo.call(this, currency);
return tokenInfo ? formatUnits(withdrawableFees, tokenInfo.decimals) : withdrawableFees.toString();
}
/**
* Withdraw the given amount of fees
* @param {Wallet} wallet - The claimer nahmii wallet
* @param {MonetaryAmount} monetaryAmount - The monetary amount
* @param [options] - An optional object containing the parameters for gasLimit and gasPrice
* @returns {Promise} A promise that resolves into an array of transaction hashes.
*/
async withdrawFees(wallet, monetaryAmount, options) {
try {
const ensemble = _tokenHolderRevenueFundContractEnsemble.get(this);
const txs = ensemble.withdraw(
wallet, monetaryAmount, 'ERC20', options
);
return txs;
}
catch (error) {
throw new NestedError(error, 'Unable to withdraw fees.');
}
}
}
module.exports = FeesClaimant;