@human-protocol/sdk 0.0.10

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/src/utils.ts ADDED
@@ -0,0 +1,164 @@
1
+ import { BigNumber, ethers } from 'ethers';
2
+
3
+ import {
4
+ Escrow,
5
+ Escrow__factory,
6
+ EscrowFactory,
7
+ EscrowFactory__factory,
8
+ HMToken,
9
+ HMToken__factory,
10
+ Staking,
11
+ Staking__factory,
12
+ RewardPool,
13
+ RewardPool__factory,
14
+ } from '@human-protocol/core/typechain-types';
15
+
16
+ /**
17
+ * **Get HMToken contract instance at given address**
18
+ *
19
+ * @param {string} hmTokenAddr HMToken contract address
20
+ * @param {ethers.Signer | undefined} signer Deployer signer
21
+ * @returns {Promise<HMToken>} Attached contract instance
22
+ */
23
+ export const getHmToken = async (
24
+ hmTokenAddr: string,
25
+ signer?: ethers.Signer
26
+ ): Promise<HMToken> => {
27
+ const factory = new HMToken__factory(signer);
28
+
29
+ const contract = await factory.attach(hmTokenAddr);
30
+
31
+ return contract;
32
+ };
33
+
34
+ /**
35
+ * **Deploy EscrowFactory contract**
36
+ *
37
+ * @param {string} hmTokenAddr HMToken address
38
+ * @param {string} stakingAddr Staking address
39
+ * @param {ethers.Signer | undefined} signer Deployer signer
40
+ * @returns {Promise<EscrowFactory>} Deployed contract instance
41
+ */
42
+ export const deployEscrowFactory = async (
43
+ hmTokenAddr: string,
44
+ stakingAddr: string,
45
+ signer?: ethers.Signer
46
+ ): Promise<EscrowFactory> => {
47
+ const factory = new EscrowFactory__factory(signer);
48
+
49
+ const contract = await factory.deploy(hmTokenAddr, stakingAddr);
50
+
51
+ return contract;
52
+ };
53
+
54
+ /**
55
+ * **Get EscrowFactory contract instance at given address**
56
+ *
57
+ * @param {string} factoryAddr EscrowFactory contract address
58
+ * @param {ethers.Signer | undefined} signer Deployer signer
59
+ * @returns {Promise<EscrowFactory>} Attached contract instance
60
+ */
61
+ export const getEscrowFactory = async (
62
+ factoryAddr: string,
63
+ signer?: ethers.Signer
64
+ ): Promise<EscrowFactory> => {
65
+ const factory = new EscrowFactory__factory(signer);
66
+
67
+ const contract = await factory.attach(factoryAddr);
68
+
69
+ return contract;
70
+ };
71
+
72
+ /**
73
+ * **Get Escrow contract instance at given address**
74
+ *
75
+ * @param {string} escrowAddr Escrow contract address
76
+ * @param {ethers.Signer | undefined} signer Deployer signer
77
+ * @returns {Promise<Escrow>} Attached contract instance
78
+ */
79
+ export const getEscrow = async (
80
+ escrowAddr: string,
81
+ signer?: ethers.Signer
82
+ ): Promise<Escrow> => {
83
+ const factory = new Escrow__factory(signer);
84
+
85
+ const contract = await factory.attach(escrowAddr);
86
+
87
+ return contract;
88
+ };
89
+
90
+ /**
91
+ * **Deploy Staking contract**
92
+ *
93
+ * @param {string} hmTokenAddr HMToken address
94
+ * @param {number} minimumStake Minimum amount to stake
95
+ * @param {number} lockPeriod Lock period after unstake
96
+ * @param {ethers.Signer | undefined} signer Deployer signer
97
+ * @returns {Promise<Staking>} Deployed contract instance
98
+ */
99
+ export const deployStaking = async (
100
+ hmTokenAddr: string,
101
+ minimumStake: number,
102
+ lockPeriod: number,
103
+ signer?: ethers.Signer
104
+ ): Promise<Staking> => {
105
+ const staking = new Staking__factory(signer);
106
+ const contract = await staking.deploy(hmTokenAddr, minimumStake, lockPeriod);
107
+
108
+ return contract;
109
+ };
110
+
111
+ /**
112
+ * **Get Staking contract instance at given address**
113
+ *
114
+ * @param {string} stakingAddr Staking contract address
115
+ * @param {ethers.Signer | undefined} signer Deployer signer
116
+ * @returns {Promise<Staking>} Attached contract instance
117
+ */
118
+ export const getStaking = async (
119
+ stakingAddr: string,
120
+ signer?: ethers.Signer
121
+ ): Promise<Staking> => {
122
+ const factory = new Staking__factory(signer);
123
+
124
+ const contract = await factory.attach(stakingAddr);
125
+
126
+ return contract;
127
+ };
128
+
129
+ /**
130
+ * **Deploy RewardPool contract**
131
+ *
132
+ * @param {string} hmTokenAddr HMToken address
133
+ * @param {string} stakingAddr Staking address
134
+ * @param {number} fee Reward fee of the protocol
135
+ * @param {ethers.Signer | undefined} signer Deployer signer
136
+ * @returns {Promise<Staking>} Deployed contract instance
137
+ */
138
+ export const deployRewardPool = async (
139
+ hmTokenAddr: string,
140
+ stakingAddr: string,
141
+ fee: number,
142
+ signer?: ethers.Signer
143
+ ): Promise<RewardPool> => {
144
+ const rewardPool = new RewardPool__factory(signer);
145
+ const contract = await rewardPool.deploy(hmTokenAddr, stakingAddr, fee);
146
+
147
+ return contract;
148
+ };
149
+
150
+ /**
151
+ * **Get specific amount representation in given decimals**
152
+ *
153
+ * Apply given decimals to the specified amount.
154
+ *
155
+ * @param {string | number} amount - Amount to convert
156
+ * @param {number} decimals - Decimal to convert
157
+ * @returns {BigNumber} Converted amount
158
+ */
159
+ export const toFullDigit = (
160
+ amount: number | string,
161
+ decimals = 18
162
+ ): BigNumber => {
163
+ return BigNumber.from(amount).mul(BigNumber.from(10).pow(decimals));
164
+ };