@gearbox-protocol/sdk 1.15.24 → 1.16.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/contracts/contractsRegister.d.ts +11 -0
- package/lib/contracts/contractsRegister.js +42 -29
- package/lib/core/transactions.d.ts +7 -1
- package/lib/core/transactions.js +25 -1
- package/lib/rewards/creditRewardParams.d.ts +6 -0
- package/lib/rewards/creditRewardParams.js +33 -0
- package/lib/rewards/creditRewards.d.ts +15 -0
- package/lib/rewards/creditRewards.js +206 -0
- package/lib/rewards/creditRewards.spec.d.ts +1 -0
- package/lib/rewards/creditRewards.spec.js +57 -0
- package/package.json +1 -1
|
@@ -1,4 +1,15 @@
|
|
|
1
1
|
import { NetworkType } from "../core/constants";
|
|
2
2
|
export declare const stEthPoolWrapper: Record<NetworkType, string>;
|
|
3
|
+
export declare const CREDIT_MANAGER_DAI_V2_MAINNET = "0x672461bfc20dd783444a830ad4c38b345ab6e2f7";
|
|
4
|
+
export declare const CREDIT_MANAGER_USDC_V2_MAINNET = "0x95357303f995e184A7998dA6C6eA35cC728A1900";
|
|
5
|
+
export declare const CREDIT_MANAGER_WETH_V2_MAINNET = "0x5887ad4cb2352e7f01527035faa3ae0ef2ce2b9b";
|
|
6
|
+
export declare const CREDIT_MANAGER_WSTETH_V2_MAINNET = "0xe0bce4460795281d39c91da9b0275bca968293de";
|
|
7
|
+
export declare const CREDIT_MANAGER_WBTC_V2_MAINNET = "0xc62bf8a7889adf1c5dc4665486c7683ae6e74e0f";
|
|
8
|
+
export declare const CREDIT_MANAGER_DAI_V2_GOERLI = "0xf7ba434952acaa2e12035a2c3643ca327914a470";
|
|
9
|
+
export declare const CREDIT_MANAGER_USDC_V2_GOERLI = "0x2144a7785baecbab32295188285717cad6c1a11c";
|
|
10
|
+
export declare const CREDIT_MANAGER_WETH_V2_GOERLI = "0x2ad4a2f1bdd815e285a22cdcc072fbb43818b09b";
|
|
11
|
+
export declare const CREDIT_MANAGER_WSTETH_V2_GOERLI = "0x40d542a5c15b2c0c65af047984c285c2c30847af";
|
|
12
|
+
export declare const CREDIT_MANAGER_WBTC_V2_GOERLI = "0x6546dd4e8d507e3e45bb924818c0bad2a3aa2c5f";
|
|
13
|
+
export declare type CreditManagersV2 = typeof CREDIT_MANAGER_DAI_V2_MAINNET | typeof CREDIT_MANAGER_USDC_V2_MAINNET | typeof CREDIT_MANAGER_WETH_V2_MAINNET | typeof CREDIT_MANAGER_WSTETH_V2_MAINNET | typeof CREDIT_MANAGER_WBTC_V2_MAINNET | typeof CREDIT_MANAGER_DAI_V2_GOERLI | typeof CREDIT_MANAGER_USDC_V2_GOERLI | typeof CREDIT_MANAGER_WETH_V2_GOERLI | typeof CREDIT_MANAGER_WSTETH_V2_GOERLI | typeof CREDIT_MANAGER_WBTC_V2_GOERLI;
|
|
3
14
|
export declare const deployedContracts: Record<string, string>;
|
|
4
15
|
export declare function getContractName(address: string): string;
|
|
@@ -10,8 +10,9 @@ var __assign = (this && this.__assign) || function () {
|
|
|
10
10
|
};
|
|
11
11
|
return __assign.apply(this, arguments);
|
|
12
12
|
};
|
|
13
|
+
var _a;
|
|
13
14
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
14
|
-
exports.getContractName = exports.deployedContracts = exports.stEthPoolWrapper = void 0;
|
|
15
|
+
exports.getContractName = exports.deployedContracts = exports.CREDIT_MANAGER_WBTC_V2_GOERLI = exports.CREDIT_MANAGER_WSTETH_V2_GOERLI = exports.CREDIT_MANAGER_WETH_V2_GOERLI = exports.CREDIT_MANAGER_USDC_V2_GOERLI = exports.CREDIT_MANAGER_DAI_V2_GOERLI = exports.CREDIT_MANAGER_WBTC_V2_MAINNET = exports.CREDIT_MANAGER_WSTETH_V2_MAINNET = exports.CREDIT_MANAGER_WETH_V2_MAINNET = exports.CREDIT_MANAGER_USDC_V2_MAINNET = exports.CREDIT_MANAGER_DAI_V2_MAINNET = exports.stEthPoolWrapper = void 0;
|
|
15
16
|
var formatter_1 = require("../utils/formatter");
|
|
16
17
|
var mappers_1 = require("../utils/mappers");
|
|
17
18
|
var contracts_1 = require("./contracts");
|
|
@@ -19,37 +20,49 @@ exports.stEthPoolWrapper = {
|
|
|
19
20
|
Mainnet: "0x5a97e3E43dCBFe620ccF7865739075f92E93F5E4",
|
|
20
21
|
Goerli: "0x3fe62a62C022A069952069b32C9d56718D30B7ec",
|
|
21
22
|
};
|
|
22
|
-
exports.
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
23
|
+
exports.CREDIT_MANAGER_DAI_V2_MAINNET = "0x672461bfc20dd783444a830ad4c38b345ab6e2f7";
|
|
24
|
+
exports.CREDIT_MANAGER_USDC_V2_MAINNET = "0x95357303f995e184A7998dA6C6eA35cC728A1900";
|
|
25
|
+
exports.CREDIT_MANAGER_WETH_V2_MAINNET = "0x5887ad4cb2352e7f01527035faa3ae0ef2ce2b9b";
|
|
26
|
+
exports.CREDIT_MANAGER_WSTETH_V2_MAINNET = "0xe0bce4460795281d39c91da9b0275bca968293de";
|
|
27
|
+
exports.CREDIT_MANAGER_WBTC_V2_MAINNET = "0xc62bf8a7889adf1c5dc4665486c7683ae6e74e0f";
|
|
28
|
+
// GOERLI
|
|
29
|
+
exports.CREDIT_MANAGER_DAI_V2_GOERLI = "0xf7ba434952acaa2e12035a2c3643ca327914a470";
|
|
30
|
+
exports.CREDIT_MANAGER_USDC_V2_GOERLI = "0x2144a7785baecbab32295188285717cad6c1a11c";
|
|
31
|
+
exports.CREDIT_MANAGER_WETH_V2_GOERLI = "0x2ad4a2f1bdd815e285a22cdcc072fbb43818b09b";
|
|
32
|
+
exports.CREDIT_MANAGER_WSTETH_V2_GOERLI = "0x40d542a5c15b2c0c65af047984c285c2c30847af";
|
|
33
|
+
exports.CREDIT_MANAGER_WBTC_V2_GOERLI = "0x6546dd4e8d507e3e45bb924818c0bad2a3aa2c5f";
|
|
34
|
+
exports.deployedContracts = (_a = {
|
|
35
|
+
// MAINNET
|
|
36
|
+
"0x777E23A2AcB2fCbB35f6ccF98272d03C722Ba6EB": "DAI",
|
|
37
|
+
"0x2664cc24CBAd28749B3Dd6fC97A6B402484De527": "USDC",
|
|
38
|
+
"0x968f9a68a98819E2e6Bb910466e191A7b6cf02F0": "WETH",
|
|
39
|
+
"0xC38478B0A4bAFE964C3526EEFF534d70E1E09017": "WBTC",
|
|
40
|
+
// MAINNET POOLS
|
|
41
|
+
"0x24946bCbBd028D5ABb62ad9B635EB1b1a67AF668": "DAI",
|
|
42
|
+
"0x86130bDD69143D8a4E5fc50bf4323D48049E98E4": "USDC",
|
|
43
|
+
"0xB03670c20F87f2169A7c4eBE35746007e9575901": "WETH",
|
|
44
|
+
"0xB8cf3Ed326bB0E51454361Fb37E9E8df6DC5C286": "wstETH",
|
|
45
|
+
"0xB2A015c71c17bCAC6af36645DEad8c572bA08A08": "WBTC"
|
|
46
|
+
},
|
|
34
47
|
// MAINNET V2 CM
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
48
|
+
_a[exports.CREDIT_MANAGER_DAI_V2_MAINNET] = "DAI",
|
|
49
|
+
_a[exports.CREDIT_MANAGER_USDC_V2_MAINNET] = "USDC",
|
|
50
|
+
_a[exports.CREDIT_MANAGER_WETH_V2_MAINNET] = "WETH",
|
|
51
|
+
_a[exports.CREDIT_MANAGER_WSTETH_V2_MAINNET] = "wstETH",
|
|
52
|
+
_a[exports.CREDIT_MANAGER_WBTC_V2_MAINNET] = "WBTC",
|
|
40
53
|
// GOERLI CM
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
54
|
+
_a[exports.CREDIT_MANAGER_DAI_V2_GOERLI] = "DAI",
|
|
55
|
+
_a[exports.CREDIT_MANAGER_USDC_V2_GOERLI] = "USDC",
|
|
56
|
+
_a[exports.CREDIT_MANAGER_WETH_V2_GOERLI] = "WETH",
|
|
57
|
+
_a[exports.CREDIT_MANAGER_WSTETH_V2_GOERLI] = "wstETH",
|
|
58
|
+
_a[exports.CREDIT_MANAGER_WBTC_V2_GOERLI] = "WBTC",
|
|
46
59
|
// GOERLI pools
|
|
47
|
-
"0xfb422e503f8b768184aba5e73587543d1c871a23"
|
|
48
|
-
"0x7c14c88f672d5513c8fa640fa48815f40c781a60"
|
|
49
|
-
"0x13376d637ded73a0df20d192f691a88c8af7dd0a"
|
|
50
|
-
"0x3f926a46893af659c2768f118d5f189190baa15c"
|
|
51
|
-
"0xbdf0395fa6f134b8de35ef30c61b2b9711cbd67a"
|
|
52
|
-
|
|
60
|
+
_a["0xfb422e503f8b768184aba5e73587543d1c871a23"] = "DAI",
|
|
61
|
+
_a["0x7c14c88f672d5513c8fa640fa48815f40c781a60"] = "USDC",
|
|
62
|
+
_a["0x13376d637ded73a0df20d192f691a88c8af7dd0a"] = "ETH/WETH",
|
|
63
|
+
_a["0x3f926a46893af659c2768f118d5f189190baa15c"] = "WBTC",
|
|
64
|
+
_a["0xbdf0395fa6f134b8de35ef30c61b2b9711cbd67a"] = "wstETH",
|
|
65
|
+
_a);
|
|
53
66
|
var contractNames = Object.entries(contracts_1.contractsByAddress).reduce(function (acc, _a) {
|
|
54
67
|
var _b;
|
|
55
68
|
var addr = _a[0], cSymbol = _a[1];
|
|
@@ -2,7 +2,7 @@ import { BigNumber } from "ethers";
|
|
|
2
2
|
import { SupportedContract } from "../contracts/contracts";
|
|
3
3
|
import { EVMTx, EVMTxProps } from "./eventOrTx";
|
|
4
4
|
export interface TxSerialized {
|
|
5
|
-
type: "TxAddLiquidity" | "TxRemoveLiquidity" | "TxSwap" | "TxAddCollateral" | "TxIncreaseBorrowAmount" | "TxOpenAccount" | "TxRepayAccount" | "TxCloseAccount" | "TxApprove" | "TxOpenMultitokenAccount" | "TxClaimReward";
|
|
5
|
+
type: "TxAddLiquidity" | "TxRemoveLiquidity" | "TxSwap" | "TxAddCollateral" | "TxIncreaseBorrowAmount" | "TxOpenAccount" | "TxRepayAccount" | "TxCloseAccount" | "TxApprove" | "TxOpenMultitokenAccount" | "TxClaimReward" | "TxClaimNFT";
|
|
6
6
|
content: string;
|
|
7
7
|
}
|
|
8
8
|
export declare class TxSerializer {
|
|
@@ -121,6 +121,12 @@ export declare class TxClaimReward extends EVMTx {
|
|
|
121
121
|
toString(): string;
|
|
122
122
|
serialize(): TxSerialized;
|
|
123
123
|
}
|
|
124
|
+
declare type TxClaimNFTProps = EVMTxProps;
|
|
125
|
+
export declare class TxClaimNFT extends EVMTx {
|
|
126
|
+
constructor(opts: TxClaimNFTProps);
|
|
127
|
+
toString(): string;
|
|
128
|
+
serialize(): TxSerialized;
|
|
129
|
+
}
|
|
124
130
|
interface RepayAccountProps extends EVMTxProps {
|
|
125
131
|
creditManager: string;
|
|
126
132
|
}
|
package/lib/core/transactions.js
CHANGED
|
@@ -15,7 +15,7 @@ var __extends = (this && this.__extends) || (function () {
|
|
|
15
15
|
};
|
|
16
16
|
})();
|
|
17
17
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
18
|
-
exports.TxApprove = exports.TxCloseAccount = exports.TxRepayAccount = exports.TxClaimReward = exports.TxOpenMultitokenAccount = exports.TxOpenAccount = exports.TxIncreaseBorrowAmount = exports.TxAddCollateral = exports.TXSwap = exports.TxRemoveLiquidity = exports.TxAddLiquidity = exports.TxSerializer = void 0;
|
|
18
|
+
exports.TxApprove = exports.TxCloseAccount = exports.TxRepayAccount = exports.TxClaimNFT = exports.TxClaimReward = exports.TxOpenMultitokenAccount = exports.TxOpenAccount = exports.TxIncreaseBorrowAmount = exports.TxAddCollateral = exports.TXSwap = exports.TxRemoveLiquidity = exports.TxAddLiquidity = exports.TxSerializer = void 0;
|
|
19
19
|
var ethers_1 = require("ethers");
|
|
20
20
|
var contracts_1 = require("../contracts/contracts");
|
|
21
21
|
var contractsRegister_1 = require("../contracts/contractsRegister");
|
|
@@ -55,6 +55,8 @@ var TxSerializer = /** @class */ (function () {
|
|
|
55
55
|
return new TxOpenMultitokenAccount(params);
|
|
56
56
|
case "TxClaimReward":
|
|
57
57
|
return new TxClaimReward(params);
|
|
58
|
+
case "TxClaimNFT":
|
|
59
|
+
return new TxClaimNFT(params);
|
|
58
60
|
default:
|
|
59
61
|
throw new Error("Unknown transaction for parsing: ".concat(e.type));
|
|
60
62
|
}
|
|
@@ -299,6 +301,28 @@ var TxClaimReward = /** @class */ (function (_super) {
|
|
|
299
301
|
return TxClaimReward;
|
|
300
302
|
}(eventOrTx_1.EVMTx));
|
|
301
303
|
exports.TxClaimReward = TxClaimReward;
|
|
304
|
+
var TxClaimNFT = /** @class */ (function (_super) {
|
|
305
|
+
__extends(TxClaimNFT, _super);
|
|
306
|
+
function TxClaimNFT(opts) {
|
|
307
|
+
return _super.call(this, {
|
|
308
|
+
block: opts.block,
|
|
309
|
+
txHash: opts.txHash,
|
|
310
|
+
txStatus: opts.txStatus,
|
|
311
|
+
timestamp: opts.timestamp,
|
|
312
|
+
}) || this;
|
|
313
|
+
}
|
|
314
|
+
TxClaimNFT.prototype.toString = function () {
|
|
315
|
+
return "NFT claimed";
|
|
316
|
+
};
|
|
317
|
+
TxClaimNFT.prototype.serialize = function () {
|
|
318
|
+
return {
|
|
319
|
+
type: "TxClaimNFT",
|
|
320
|
+
content: JSON.stringify(this),
|
|
321
|
+
};
|
|
322
|
+
};
|
|
323
|
+
return TxClaimNFT;
|
|
324
|
+
}(eventOrTx_1.EVMTx));
|
|
325
|
+
exports.TxClaimNFT = TxClaimNFT;
|
|
302
326
|
var TxRepayAccount = /** @class */ (function (_super) {
|
|
303
327
|
__extends(TxRepayAccount, _super);
|
|
304
328
|
function TxRepayAccount(opts) {
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { CreditManagersV2 } from "../contracts/contractsRegister";
|
|
2
|
+
import { DieselTokenTypes } from "../tokens/gear";
|
|
3
|
+
import { RangedValue } from "./range";
|
|
4
|
+
export declare const creditRewardsPerBlock: Record<CreditManagersV2, RangedValue>;
|
|
5
|
+
export declare const GEAR_PER_BLOCK: Record<DieselTokenTypes, number>;
|
|
6
|
+
export declare const GOERLI_BLOCK = 7694030;
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var _a;
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.GOERLI_BLOCK = exports.GEAR_PER_BLOCK = exports.creditRewardsPerBlock = void 0;
|
|
5
|
+
var ethers_1 = require("ethers");
|
|
6
|
+
var contractsRegister_1 = require("../contracts/contractsRegister");
|
|
7
|
+
var range_1 = require("./range");
|
|
8
|
+
exports.creditRewardsPerBlock = (_a = {},
|
|
9
|
+
_a[contractsRegister_1.CREDIT_MANAGER_DAI_V2_MAINNET] = new range_1.RangedValue(),
|
|
10
|
+
_a[contractsRegister_1.CREDIT_MANAGER_USDC_V2_MAINNET] = new range_1.RangedValue(),
|
|
11
|
+
_a[contractsRegister_1.CREDIT_MANAGER_WETH_V2_MAINNET] = new range_1.RangedValue(),
|
|
12
|
+
_a[contractsRegister_1.CREDIT_MANAGER_WSTETH_V2_MAINNET] = new range_1.RangedValue(),
|
|
13
|
+
_a[contractsRegister_1.CREDIT_MANAGER_WBTC_V2_MAINNET] = new range_1.RangedValue(),
|
|
14
|
+
// GOERLI CM
|
|
15
|
+
_a[contractsRegister_1.CREDIT_MANAGER_DAI_V2_GOERLI] = new range_1.RangedValue(),
|
|
16
|
+
_a[contractsRegister_1.CREDIT_MANAGER_USDC_V2_GOERLI] = new range_1.RangedValue(),
|
|
17
|
+
_a[contractsRegister_1.CREDIT_MANAGER_WETH_V2_GOERLI] = new range_1.RangedValue(),
|
|
18
|
+
_a[contractsRegister_1.CREDIT_MANAGER_WSTETH_V2_GOERLI] = new range_1.RangedValue(),
|
|
19
|
+
_a[contractsRegister_1.CREDIT_MANAGER_WBTC_V2_GOERLI] = new range_1.RangedValue(),
|
|
20
|
+
_a);
|
|
21
|
+
exports.GEAR_PER_BLOCK = {
|
|
22
|
+
dDAI: 2283,
|
|
23
|
+
dUSDC: 2283,
|
|
24
|
+
dWETH: 3196,
|
|
25
|
+
dWBTC: 913,
|
|
26
|
+
dwstETH: 1636,
|
|
27
|
+
};
|
|
28
|
+
exports.GOERLI_BLOCK = 7694030;
|
|
29
|
+
exports.creditRewardsPerBlock[contractsRegister_1.CREDIT_MANAGER_DAI_V2_GOERLI].addValue(exports.GOERLI_BLOCK, ethers_1.BigNumber.from(10).pow(18).mul(exports.GEAR_PER_BLOCK.dDAI).div(100));
|
|
30
|
+
exports.creditRewardsPerBlock[contractsRegister_1.CREDIT_MANAGER_USDC_V2_GOERLI].addValue(exports.GOERLI_BLOCK, ethers_1.BigNumber.from(10).pow(18).mul(exports.GEAR_PER_BLOCK.dUSDC).div(100));
|
|
31
|
+
exports.creditRewardsPerBlock[contractsRegister_1.CREDIT_MANAGER_WETH_V2_GOERLI].addValue(exports.GOERLI_BLOCK, ethers_1.BigNumber.from(10).pow(18).mul(exports.GEAR_PER_BLOCK.dWETH).div(100));
|
|
32
|
+
exports.creditRewardsPerBlock[contractsRegister_1.CREDIT_MANAGER_WSTETH_V2_GOERLI].addValue(exports.GOERLI_BLOCK, ethers_1.BigNumber.from(10).pow(18).mul(exports.GEAR_PER_BLOCK.dwstETH).div(100));
|
|
33
|
+
exports.creditRewardsPerBlock[contractsRegister_1.CREDIT_MANAGER_WBTC_V2_GOERLI].addValue(exports.GOERLI_BLOCK, ethers_1.BigNumber.from(10).pow(18).mul(exports.GEAR_PER_BLOCK.dWBTC).div(100));
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { BigNumber, providers } from "ethers";
|
|
2
|
+
import { CreditManagerData } from "../core/creditManager";
|
|
3
|
+
import { CloseCreditAccountEvent, DecreaseBorrowedAmountEvent, IncreaseBorrowedAmountEvent, LiquidateCreditAccountEvent, OpenCreditAccountEvent, TransferAccountEvent } from "../types/@gearbox-protocol/core-v2/contracts/interfaces/ICreditFacade.sol/ICreditFacade";
|
|
4
|
+
import { RangedValue } from "./range";
|
|
5
|
+
export interface Reward {
|
|
6
|
+
address: string;
|
|
7
|
+
amount: BigNumber;
|
|
8
|
+
}
|
|
9
|
+
export declare class CreditRewards {
|
|
10
|
+
static computeReward(creditManagerData: CreditManagerData, address: string, provider: providers.Provider, toBlock?: number): Promise<BigNumber>;
|
|
11
|
+
static computeAllRewards(creditManagerData: CreditManagerData, provider: providers.Provider, toBlock?: number): Promise<Array<Reward>>;
|
|
12
|
+
static computeRewardInt(toBlock: number, balance: RangedValue, totalSupply: RangedValue, rewardPerBlock: RangedValue): BigNumber;
|
|
13
|
+
protected static query(creditFacade: string, provider: providers.Provider, toBlock: number): Promise<Array<OpenCreditAccountEvent | CloseCreditAccountEvent | LiquidateCreditAccountEvent | IncreaseBorrowedAmountEvent | DecreaseBorrowedAmountEvent | TransferAccountEvent>>;
|
|
14
|
+
protected static getRewardsRange(creditManager: string): RangedValue;
|
|
15
|
+
}
|
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
12
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
13
|
+
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
14
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
15
|
+
function step(op) {
|
|
16
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
17
|
+
while (_) try {
|
|
18
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
19
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
20
|
+
switch (op[0]) {
|
|
21
|
+
case 0: case 1: t = op; break;
|
|
22
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
23
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
24
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
25
|
+
default:
|
|
26
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
27
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
28
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
29
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
30
|
+
if (t[2]) _.ops.pop();
|
|
31
|
+
_.trys.pop(); continue;
|
|
32
|
+
}
|
|
33
|
+
op = body.call(thisArg, _);
|
|
34
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
35
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
|
39
|
+
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
40
|
+
if (ar || !(i in from)) {
|
|
41
|
+
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
42
|
+
ar[i] = from[i];
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
return to.concat(ar || Array.prototype.slice.call(from));
|
|
46
|
+
};
|
|
47
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
48
|
+
exports.CreditRewards = void 0;
|
|
49
|
+
var ethers_1 = require("ethers");
|
|
50
|
+
var types_1 = require("../types");
|
|
51
|
+
var creditRewardParams_1 = require("./creditRewardParams");
|
|
52
|
+
var range_1 = require("./range");
|
|
53
|
+
var CreditRewards = /** @class */ (function () {
|
|
54
|
+
function CreditRewards() {
|
|
55
|
+
}
|
|
56
|
+
CreditRewards.computeReward = function (creditManagerData, address, provider, toBlock) {
|
|
57
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
58
|
+
var rewards, rewardToAddress;
|
|
59
|
+
return __generator(this, function (_a) {
|
|
60
|
+
switch (_a.label) {
|
|
61
|
+
case 0: return [4 /*yield*/, CreditRewards.computeAllRewards(creditManagerData, provider, toBlock)];
|
|
62
|
+
case 1:
|
|
63
|
+
rewards = _a.sent();
|
|
64
|
+
rewardToAddress = rewards.filter(function (r) { return r.address.toLowerCase() === address.toLowerCase(); });
|
|
65
|
+
return [2 /*return*/, rewards.length === 0 ? ethers_1.BigNumber.from(0) : rewardToAddress[0].amount];
|
|
66
|
+
}
|
|
67
|
+
});
|
|
68
|
+
});
|
|
69
|
+
};
|
|
70
|
+
CreditRewards.computeAllRewards = function (creditManagerData, provider, toBlock) {
|
|
71
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
72
|
+
var toBlockQuery, _a, query, borrowedRange, totalSupplyRange, rewardPerBlock, borrowed, totalBorrowed, cfi;
|
|
73
|
+
return __generator(this, function (_b) {
|
|
74
|
+
switch (_b.label) {
|
|
75
|
+
case 0:
|
|
76
|
+
_a = toBlock;
|
|
77
|
+
if (_a) return [3 /*break*/, 2];
|
|
78
|
+
return [4 /*yield*/, provider.getBlockNumber()];
|
|
79
|
+
case 1:
|
|
80
|
+
_a = (_b.sent());
|
|
81
|
+
_b.label = 2;
|
|
82
|
+
case 2:
|
|
83
|
+
toBlockQuery = _a;
|
|
84
|
+
return [4 /*yield*/, CreditRewards.query(creditManagerData.creditFacade, provider, toBlockQuery)];
|
|
85
|
+
case 3:
|
|
86
|
+
query = _b.sent();
|
|
87
|
+
borrowedRange = {};
|
|
88
|
+
totalSupplyRange = new range_1.RangedValue();
|
|
89
|
+
rewardPerBlock = CreditRewards.getRewardsRange(creditManagerData.creditFacade);
|
|
90
|
+
borrowed = {};
|
|
91
|
+
totalBorrowed = ethers_1.BigNumber.from(0);
|
|
92
|
+
cfi = types_1.ICreditFacade__factory.connect(creditManagerData.creditFacade, provider).interface;
|
|
93
|
+
query.forEach(function (e) {
|
|
94
|
+
switch (e.topics[0]) {
|
|
95
|
+
case cfi.getEventTopic("OpenCreditAccount"): {
|
|
96
|
+
var _a = e
|
|
97
|
+
.args, onBehalfOf = _a.onBehalfOf, borrowAmount = _a.borrowAmount;
|
|
98
|
+
totalBorrowed = totalBorrowed.add(borrowAmount);
|
|
99
|
+
borrowed[onBehalfOf] = borrowAmount;
|
|
100
|
+
if (!borrowedRange[onBehalfOf]) {
|
|
101
|
+
borrowedRange[onBehalfOf] = new range_1.RangedValue();
|
|
102
|
+
}
|
|
103
|
+
borrowedRange[onBehalfOf].addValue(e.blockNumber, borrowAmount);
|
|
104
|
+
break;
|
|
105
|
+
}
|
|
106
|
+
case cfi.getEventTopic("CloseCreditAccount"):
|
|
107
|
+
case cfi.getEventTopic("LiquidateCreditAccount"):
|
|
108
|
+
case cfi.getEventTopic("LiquidateExpiredCreditAccount"): {
|
|
109
|
+
// We need { borrower} only so, we can use any event to get it from args
|
|
110
|
+
var borrower = e.args.borrower;
|
|
111
|
+
totalBorrowed = totalBorrowed.sub(borrowed[borrower]);
|
|
112
|
+
borrowed[borrower] = ethers_1.BigNumber.from(0);
|
|
113
|
+
borrowedRange[borrower].addValue(e.blockNumber, ethers_1.BigNumber.from(0));
|
|
114
|
+
break;
|
|
115
|
+
}
|
|
116
|
+
case cfi.getEventTopic("IncreaseBorrowedAmount"): {
|
|
117
|
+
var _b = e.args, borrower = _b.borrower, amount = _b.amount;
|
|
118
|
+
totalBorrowed = totalBorrowed.add(amount);
|
|
119
|
+
borrowed[borrower] = borrowed[borrower].add(amount);
|
|
120
|
+
borrowedRange[borrower].addValue(e.blockNumber, borrowed[borrower]);
|
|
121
|
+
break;
|
|
122
|
+
}
|
|
123
|
+
case cfi.getEventTopic("DecreaseBorrowedAmount"): {
|
|
124
|
+
var _c = e.args, borrower = _c.borrower, amount = _c.amount;
|
|
125
|
+
totalBorrowed = totalBorrowed.sub(amount);
|
|
126
|
+
borrowed[borrower] = borrowed[borrower].sub(amount);
|
|
127
|
+
borrowedRange[borrower].addValue(e.blockNumber, borrowed[borrower]);
|
|
128
|
+
break;
|
|
129
|
+
}
|
|
130
|
+
case cfi.getEventTopic("TransferAccount"): {
|
|
131
|
+
var _d = e.args, newOwner = _d.newOwner, oldOwner = _d.oldOwner;
|
|
132
|
+
borrowed[newOwner] = borrowed[oldOwner];
|
|
133
|
+
if (!borrowed[newOwner]) {
|
|
134
|
+
borrowedRange[newOwner] = new range_1.RangedValue();
|
|
135
|
+
}
|
|
136
|
+
borrowed[oldOwner] = ethers_1.BigNumber.from(0);
|
|
137
|
+
borrowedRange[newOwner].addValue(e.blockNumber, borrowed[newOwner]);
|
|
138
|
+
borrowedRange[oldOwner].addValue(e.blockNumber, ethers_1.BigNumber.from(0));
|
|
139
|
+
break;
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
});
|
|
143
|
+
return [2 /*return*/, Object.keys(borrowed).map(function (address) { return ({
|
|
144
|
+
address: address,
|
|
145
|
+
amount: CreditRewards.computeRewardInt(toBlockQuery, borrowedRange[address], totalSupplyRange, rewardPerBlock),
|
|
146
|
+
}); })];
|
|
147
|
+
}
|
|
148
|
+
});
|
|
149
|
+
});
|
|
150
|
+
};
|
|
151
|
+
CreditRewards.computeRewardInt = function (toBlock, balance, totalSupply, rewardPerBlock) {
|
|
152
|
+
var keys = Array.from(new Set(__spreadArray(__spreadArray(__spreadArray(__spreadArray([], balance.keys, true), totalSupply.keys, true), rewardPerBlock.keys, true), [
|
|
153
|
+
toBlock,
|
|
154
|
+
], false))).sort(function (a, b) { return (a > b ? 1 : -1); });
|
|
155
|
+
var total = ethers_1.BigNumber.from(0);
|
|
156
|
+
var balancesArr = balance.getValues(keys);
|
|
157
|
+
var totalSupplyArr = totalSupply.getValues(keys);
|
|
158
|
+
var rewardsArr = rewardPerBlock.getValues(keys);
|
|
159
|
+
for (var i = 0; i < keys.length; i++) {
|
|
160
|
+
var curBlock = keys[i];
|
|
161
|
+
var nextBlock = i === keys.length - 1 ? toBlock : keys[i + 1];
|
|
162
|
+
if (!totalSupplyArr[i].isZero()) {
|
|
163
|
+
total = total.add(balancesArr[i]
|
|
164
|
+
.mul(nextBlock - curBlock)
|
|
165
|
+
.mul(rewardsArr[i])
|
|
166
|
+
.div(totalSupplyArr[i]));
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
return total;
|
|
170
|
+
};
|
|
171
|
+
CreditRewards.query = function (creditFacade, provider, toBlock) {
|
|
172
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
173
|
+
var cf, topics, logs;
|
|
174
|
+
return __generator(this, function (_a) {
|
|
175
|
+
switch (_a.label) {
|
|
176
|
+
case 0:
|
|
177
|
+
cf = types_1.ICreditFacade__factory.connect(creditFacade, provider);
|
|
178
|
+
topics = {
|
|
179
|
+
OpenCreditAccount: cf.interface.getEventTopic("OpenCreditAccount"),
|
|
180
|
+
CloseCreditAccount: cf.interface.getEventTopic("CloseCreditAccount"),
|
|
181
|
+
LiquidateCreditAccount: cf.interface.getEventTopic("LiquidateCreditAccount"),
|
|
182
|
+
LiquidateExpiredCreditAccount: cf.interface.getEventTopic("LiquidateExpiredCreditAccount"),
|
|
183
|
+
TransferAccount: cf.interface.getEventTopic("TransferAccount"),
|
|
184
|
+
IncreaseBorrowedAmount: cf.interface.getEventTopic("IncreaseBorrowedAmount"),
|
|
185
|
+
DecreaseBorrowedAmount: cf.interface.getEventTopic("DecreaseBorrowedAmount"),
|
|
186
|
+
};
|
|
187
|
+
return [4 /*yield*/, cf.queryFilter({
|
|
188
|
+
address: cf.address,
|
|
189
|
+
topics: [Object.values(topics)],
|
|
190
|
+
}, undefined, toBlock)];
|
|
191
|
+
case 1:
|
|
192
|
+
logs = _a.sent();
|
|
193
|
+
return [2 /*return*/, logs];
|
|
194
|
+
}
|
|
195
|
+
});
|
|
196
|
+
});
|
|
197
|
+
};
|
|
198
|
+
CreditRewards.getRewardsRange = function (creditManager) {
|
|
199
|
+
var rewardPerBlock = creditRewardParams_1.creditRewardsPerBlock[creditManager];
|
|
200
|
+
if (!rewardPerBlock)
|
|
201
|
+
throw new Error("Unknown credit manager token ".concat(creditManager));
|
|
202
|
+
return rewardPerBlock;
|
|
203
|
+
};
|
|
204
|
+
return CreditRewards;
|
|
205
|
+
}());
|
|
206
|
+
exports.CreditRewards = CreditRewards;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
var chai_1 = require("chai");
|
|
4
|
+
var ethers_1 = require("ethers");
|
|
5
|
+
var poolRewards_1 = require("./poolRewards");
|
|
6
|
+
var range_1 = require("./range");
|
|
7
|
+
describe("CreditRewards test", function () {
|
|
8
|
+
it("computeReward works correctly", function () {
|
|
9
|
+
var balances = new range_1.RangedValue();
|
|
10
|
+
balances.addValue(1000, ethers_1.BigNumber.from(100));
|
|
11
|
+
balances.addValue(2000, ethers_1.BigNumber.from(200));
|
|
12
|
+
balances.addValue(3000, ethers_1.BigNumber.from(0));
|
|
13
|
+
var totalSupply = new range_1.RangedValue();
|
|
14
|
+
totalSupply.addValue(500, ethers_1.BigNumber.from(100));
|
|
15
|
+
totalSupply.addValue(1000, ethers_1.BigNumber.from(200));
|
|
16
|
+
totalSupply.addValue(1500, ethers_1.BigNumber.from(250));
|
|
17
|
+
totalSupply.addValue(2000, ethers_1.BigNumber.from(400));
|
|
18
|
+
totalSupply.addValue(3000, ethers_1.BigNumber.from(100));
|
|
19
|
+
var rewards = new range_1.RangedValue();
|
|
20
|
+
rewards.addValue(500, ethers_1.BigNumber.from(20));
|
|
21
|
+
// Block: ----- 500 ------ 1000 ----- 1500 ----- 2000 ------ 2500 ----- 3000
|
|
22
|
+
//
|
|
23
|
+
// Balances: 0 100 ---------------- 200 ------------------ 0
|
|
24
|
+
// Total supply 100 200 -- 250 400 100
|
|
25
|
+
// Rewards: 20
|
|
26
|
+
// 10 ------ 8 10 -------------------- 0
|
|
27
|
+
// Total: 10 * 500 + 8*500 + 10 * 1000 = 19000
|
|
28
|
+
var result = poolRewards_1.PoolRewards.computeRewardInt(4000, balances, totalSupply, rewards);
|
|
29
|
+
(0, chai_1.expect)(result.toNumber()).to.be.eq(19000);
|
|
30
|
+
});
|
|
31
|
+
it("sum of rewards is equal to total distributed", function () {
|
|
32
|
+
var balances0 = new range_1.RangedValue();
|
|
33
|
+
var balances1 = new range_1.RangedValue();
|
|
34
|
+
balances0.addValue(1000, ethers_1.BigNumber.from(100));
|
|
35
|
+
balances0.addValue(2000, ethers_1.BigNumber.from(200));
|
|
36
|
+
balances0.addValue(3000, ethers_1.BigNumber.from(0));
|
|
37
|
+
balances1.addValue(500, ethers_1.BigNumber.from(100));
|
|
38
|
+
balances1.addValue(1500, ethers_1.BigNumber.from(300));
|
|
39
|
+
balances1.addValue(2500, ethers_1.BigNumber.from(200));
|
|
40
|
+
balances1.addValue(3000, ethers_1.BigNumber.from(100));
|
|
41
|
+
var totalSupply = new range_1.RangedValue();
|
|
42
|
+
totalSupply.addValue(500, ethers_1.BigNumber.from(100));
|
|
43
|
+
totalSupply.addValue(1000, ethers_1.BigNumber.from(200));
|
|
44
|
+
totalSupply.addValue(1500, ethers_1.BigNumber.from(400));
|
|
45
|
+
totalSupply.addValue(2000, ethers_1.BigNumber.from(500));
|
|
46
|
+
totalSupply.addValue(2500, ethers_1.BigNumber.from(400));
|
|
47
|
+
totalSupply.addValue(3000, ethers_1.BigNumber.from(100));
|
|
48
|
+
var rewards = new range_1.RangedValue();
|
|
49
|
+
rewards.addValue(500, ethers_1.BigNumber.from(20));
|
|
50
|
+
rewards.addValue(1500, ethers_1.BigNumber.from(10));
|
|
51
|
+
rewards.addValue(2000, ethers_1.BigNumber.from(0));
|
|
52
|
+
rewards.addValue(2500, ethers_1.BigNumber.from(5));
|
|
53
|
+
var result0 = poolRewards_1.PoolRewards.computeRewardInt(3000, balances0, totalSupply, rewards);
|
|
54
|
+
var result1 = poolRewards_1.PoolRewards.computeRewardInt(3000, balances1, totalSupply, rewards);
|
|
55
|
+
(0, chai_1.expect)(result0.toNumber() + result1.toNumber()).to.be.eq(20 * 1000 + 10 * 500 + 5 * 500);
|
|
56
|
+
});
|
|
57
|
+
});
|