@exodus/ethereum-api 6.2.3 → 6.2.5

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@exodus/ethereum-api",
3
- "version": "6.2.3",
3
+ "version": "6.2.5",
4
4
  "description": "Ethereum Api",
5
5
  "main": "src/index.js",
6
6
  "files": [
@@ -16,7 +16,7 @@
16
16
  "dependencies": {
17
17
  "@exodus/asset-lib": "^3.7.1",
18
18
  "@exodus/crypto": "^1.0.0-rc.0",
19
- "@exodus/ethereum-lib": "^3.1.0",
19
+ "@exodus/ethereum-lib": "^3.3.0",
20
20
  "@exodus/ethereumjs-util": "^7.1.0-exodus.6",
21
21
  "@exodus/fetch": "^1.2.1",
22
22
  "@exodus/simple-retry": "^0.0.6",
@@ -36,5 +36,5 @@
36
36
  "devDependencies": {
37
37
  "@exodus/models": "^8.10.4"
38
38
  },
39
- "gitHead": "6df51df0f2625839988bd554c83e86660ba488c8"
39
+ "gitHead": "70cc3abc876af2d29d08586bf7cbbca056cee69b"
40
40
  }
@@ -1,15 +1,20 @@
1
+ import BN from 'bn.js'
1
2
  import { createContract } from '@exodus/ethereum-lib'
3
+ import { asset as polygon } from '@exodus/matic-meta'
2
4
  import { getServerByName } from '../exodus-eth-server'
3
5
  import { retry } from '@exodus/simple-retry'
4
6
  import { bufferToHex } from '@exodus/ethereumjs-util'
5
7
 
6
- export const EVERSTAKE_VALIDATOR_ADDR = '0xf30cf4ed712d3734161fdaab5b1dbb49fd2d0e5c'
7
- export const STAKING_MANAGER_ADDR = '0x5e3ef299fddf15eaa0432e6e66473ace8c13d908'
8
+ const EVERSTAKE_VALIDATOR_CONTRACT_ADDR = '0xf30cf4ed712d3734161fdaab5b1dbb49fd2d0e5c'
9
+ const STAKING_MANAGER_ADDR = '0x5e3ef299fddf15eaa0432e6e66473ace8c13d908'
8
10
 
9
11
  const RETRY_DELAYS = ['10s']
10
12
 
11
13
  export class MaticStaking {
12
- constructor(validatorId = EVERSTAKE_VALIDATOR_ADDR, stakeManagerAddr = STAKING_MANAGER_ADDR) {
14
+ constructor(
15
+ validatorId = EVERSTAKE_VALIDATOR_CONTRACT_ADDR,
16
+ stakeManagerAddr = STAKING_MANAGER_ADDR
17
+ ) {
13
18
  this.validatorShareContract = createContract(validatorId, 'maticValidatorShare')
14
19
  this.stakingManagerContract = createContract(stakeManagerAddr, 'maticStakingManager')
15
20
  }
@@ -19,7 +24,7 @@ export class MaticStaking {
19
24
  return txData
20
25
  }
21
26
 
22
- processRead = (contract, method, ...args) => {
27
+ callReadFunctionContract = (contract, method, ...args) => {
23
28
  const callData = this.buildTxData(contract, method, ...args)
24
29
  const data = {
25
30
  data: bufferToHex(callData),
@@ -27,29 +32,22 @@ export class MaticStaking {
27
32
  tag: 'latest',
28
33
  }
29
34
 
30
- // TODO: why 'ethereum' if this is matic staking?
31
35
  const eth = getServerByName('ethereum')
32
36
  return retry(eth.ethCall, { delayTimesMs: RETRY_DELAYS })(data)
33
37
  }
34
38
 
35
39
  /**
36
- * Get current checkpoint
37
- * @returns {number}
40
+ * A checkpoint is an epoch value that increments in the contract, every epoch is
41
+ * stored in a list and points the events that happened in that epoch. (timeline)
38
42
  */
39
43
  getCurrentCheckpoint = async () => {
40
- const currentEpoch = await this.processRead(this.stakingManagerContract, 'epoch')
41
- return parseInt(currentEpoch, 16)
42
- }
43
-
44
- getCurrentUnbondNonce = async (address) => {
45
- const result = await this.processRead(this.validatorShareContract, 'unbondNonces', address)
46
- return parseInt(result, 16)
44
+ const currentEpoch = await this.callReadFunctionContract(this.stakingManagerContract, 'epoch')
45
+ return toBN(currentEpoch)
47
46
  }
48
47
 
49
48
  /**
50
- * Get unbond info by supplying unbondNonces
51
- * @param {string} address
52
- * @param {number} nonce
49
+ * Unbond is a struct in the staking contract that holds the number of shares and
50
+ * the withdrawEpoch for a delegator (used when delegators unstaked their tokens)
53
51
  * @returns {number}
54
52
  */
55
53
  getUnboundInfo = async (address, nonce) => {
@@ -58,104 +56,95 @@ export class MaticStaking {
58
56
  _nonce = await this.getCurrentUnbondNonce(address)
59
57
  }
60
58
 
61
- const unboundInfo = await this.processRead(
59
+ const unboundInfo = await this.callReadFunctionContract(
62
60
  this.validatorShareContract,
63
61
  'unbonds_new',
64
62
  address,
65
63
  _nonce
66
64
  )
67
- const [withdrawEpoch, shares] = this._removeHexPrefix(unboundInfo).match(/.{1,64}/g) || []
65
+
66
+ const [withdrawEpoch, shares] = splitIn32BytesArray(unboundInfo)
68
67
  return {
69
- withdrawEpoch: parseInt(withdrawEpoch, 16),
70
- shares: parseInt(shares, 16),
68
+ withdrawEpoch: toBN(withdrawEpoch, 16),
69
+ shares: toBN(shares, 16),
71
70
  }
72
71
  }
73
72
 
74
73
  /**
75
- * Get current Liquid rewards
76
- * @param {string} address
77
- * @returns {number}
74
+ * UnbondNonce is a counter stored in the contract that tracks each time a delegator unstakes
75
+ * @param address delegator address
76
+ * @return current unbonded nonce
78
77
  */
78
+ getCurrentUnbondNonce = async (address) => {
79
+ const unbondNonce = await this.callReadFunctionContract(
80
+ this.validatorShareContract,
81
+ 'unbondNonces',
82
+ address
83
+ )
84
+ return parseInt(unbondNonce, 16)
85
+ }
86
+
79
87
  getLiquidRewards = async (address) => {
80
- const currentEpoch = await this.processRead(
88
+ const liquidRewards = await this.callReadFunctionContract(
81
89
  this.validatorShareContract,
82
90
  'getLiquidRewards',
83
91
  address
84
92
  )
85
- return parseInt(currentEpoch, 16)
93
+ return polygon.currency.baseUnit(liquidRewards)
86
94
  }
87
95
 
88
- /**
89
- * Get total staked amount
90
- * @param {string} address
91
- * @returns {number}
92
- */
93
96
  getTotalStake = async (address) => {
94
- const stakeInfo = await this.processRead(this.validatorShareContract, 'getTotalStake', address)
95
- const [amount] = this._removeHexPrefix(stakeInfo).match(/.{1,64}/g) || []
96
- return parseInt(amount, 16)
97
+ const stakeInfo = await this.callReadFunctionContract(
98
+ this.validatorShareContract,
99
+ 'getTotalStake',
100
+ address
101
+ )
102
+ const [amount] = splitIn32BytesArray(stakeInfo)
103
+ return polygon.currency.baseUnit(toBN(amount).toString())
97
104
  }
98
105
 
99
- /**
100
- * Get tx data of method to restake the earned rewards
101
- */
102
106
  restakeReward = () => {
103
107
  return this.buildTxData(this.validatorShareContract, 'restake')
104
108
  }
105
109
 
106
- /**
107
- * Get tx data of method to withdraw earned rewards
108
- */
109
110
  withdrawRewards = () => {
110
111
  return this.buildTxData(this.validatorShareContract, 'withdrawRewards')
111
112
  }
112
113
 
113
- /**
114
- * Get tx data of method to delegate (stake) amount to validator
115
- * internally it calls method - **buyVoucher**
116
- * @param {string} param.amount
117
- */
118
- delegateAmount = ({ amount }) => {
119
- return this.buildTxData(this.validatorShareContract, 'buyVoucher', amount, 0)
114
+ delegate = ({ amount }) => {
115
+ return this.buildTxData(this.validatorShareContract, 'buyVoucher', amount.toBaseString(), 0)
120
116
  }
121
117
 
122
- /**
123
- * Get tx data of method to unstake delegated amount
124
- * internally it calls - **sellVoucher_new**
125
- * @param {string} param.amount
126
- * @param {string} param.maximumSharesToBurn
127
- * @returns {string}
128
- */
129
- unbond = ({ amount, maximumSharesToBurn }) => {
118
+ undelegate = ({ amount, maximumSharesToBurn }) => {
130
119
  const _maximumSharesToBurn = maximumSharesToBurn || amount
131
120
  return this.buildTxData(
132
121
  this.validatorShareContract,
133
122
  'sellVoucher_new',
134
- amount,
135
- _maximumSharesToBurn
123
+ amount.toBaseString(),
124
+ _maximumSharesToBurn.toBaseString()
136
125
  )
137
126
  }
138
127
 
139
128
  /**
140
- * Get tx data of method to claim the delegated amount by supplying nonce
141
- * internally it calls method - **unstakeClaimTokens_new**
142
- * @param {number} nonce
143
- * @returns {string}
129
+ * @param {number} nonce the unbond nonce from where delegator claim its staked tokens
144
130
  */
145
- claimStake = (nonce) => {
131
+ claimStake = ({ nonce }) => {
146
132
  return this.buildTxData(this.validatorShareContract, 'unstakeClaimTokens_new', nonce)
147
133
  }
134
+ }
148
135
 
149
- /**
150
- * Remove 0x prefix of a hex string
151
- * @param {string} str
152
- * @returns {string}
153
- */
154
- _removeHexPrefix = (str) => {
155
- if (typeof str !== 'string' || str === '') {
156
- return str
157
- }
136
+ // see arguments encoding standard (padded 32 bytes)
137
+ // https://docs.soliditylang.org/en/v0.8.15/abi-spec.html?highlight=abi.encode#function-selector-and-argument-encoding
138
+ const splitIn32BytesArray = (output) => removeHexPrefix(output).match(/[0-9a-f]{1,64}/gi) || []
139
+
140
+ const toBN = (str) => new BN(removeLeadingZeroes(removeHexPrefix(str)), 16)
158
141
 
159
- return str.startsWith('0x') ? str.slice(2) : str
142
+ const removeLeadingZeroes = (str) => str.replace(/^0+/, '')
143
+
144
+ const removeHexPrefix = (str) => {
145
+ if (typeof str !== 'string' || str === '') {
146
+ return str
160
147
  }
148
+
149
+ return str.startsWith('0x') ? str.slice(2) : str
161
150
  }