@exodus/ethereum-api 2.13.1 → 2.14.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/package.json +3 -3
- package/src/index.js +1 -0
- package/src/staking/fantom-staking.js +115 -0
- package/src/staking/index.js +2 -0
- package/src/staking/matic-staking.js +159 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@exodus/ethereum-api",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.14.0",
|
|
4
4
|
"description": "Ethereum Api",
|
|
5
5
|
"main": "src/index.js",
|
|
6
6
|
"author": "Exodus Movement, Inc.",
|
|
@@ -12,7 +12,7 @@
|
|
|
12
12
|
"dependencies": {
|
|
13
13
|
"@exodus/asset-lib": "^3.5.4",
|
|
14
14
|
"@exodus/crypto": "^1.0.0-rc.0",
|
|
15
|
-
"@exodus/ethereum-lib": "^2.
|
|
15
|
+
"@exodus/ethereum-lib": "^2.14.0",
|
|
16
16
|
"@exodus/ethereumjs-util": "^7.1.0-exodus.6",
|
|
17
17
|
"@exodus/simple-retry": "^0.0.6",
|
|
18
18
|
"fetchival": "0.3.3",
|
|
@@ -28,5 +28,5 @@
|
|
|
28
28
|
"@exodus/assets-base": "^8.0.136",
|
|
29
29
|
"@exodus/models": "^8.7.2"
|
|
30
30
|
},
|
|
31
|
-
"gitHead": "
|
|
31
|
+
"gitHead": "f898e71ce1f19bbb5cd35327f4708f55a7bcb72c"
|
|
32
32
|
}
|
package/src/index.js
CHANGED
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import { createContract } from '@exodus/ethereum-lib'
|
|
2
|
+
|
|
3
|
+
export const SFC_ADDRESS = '0xfc00face00000000000000000000000000000000'
|
|
4
|
+
|
|
5
|
+
export class FantomStaking {
|
|
6
|
+
constructor() {
|
|
7
|
+
this.sfcContract = createContract(SFC_ADDRESS, 'fantomSfc')
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
buildTxData = (contract, method, ...args) => {
|
|
11
|
+
return contract[method].build(...args)
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Get tx data of method to delegate (stake) amount to validator
|
|
16
|
+
*/
|
|
17
|
+
delegate = ({ validatorId }) => {
|
|
18
|
+
this.#validateValidatorId(validatorId)
|
|
19
|
+
return this.buildTxData(this.sfcContract, 'delegate', validatorId)
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* undelegate creates a transaction preparing part of the delegations
|
|
24
|
+
* to be withdrawn.
|
|
25
|
+
*
|
|
26
|
+
* Note: The amount must be lower than the total delegation amount for that account. Also,
|
|
27
|
+
* the requestId value has to be unique and previously unused numeric identifier of the new
|
|
28
|
+
* withdrawal. The actual withdraw execution, available after a lock period, will use the same
|
|
29
|
+
* request id to process the prepared withdrawal.
|
|
30
|
+
*/
|
|
31
|
+
undelegate = ({ requestId, validatorId, amount }) => {
|
|
32
|
+
this.#validateRequestId(requestId)
|
|
33
|
+
this.#validateValidatorId(validatorId)
|
|
34
|
+
return this.buildTxData(this.sfcContract, 'undelegate', validatorId, requestId, amount)
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* withdraw creates a transaction executing partial withdraw for the given
|
|
39
|
+
* prepared withdraw request.
|
|
40
|
+
*
|
|
41
|
+
* Note: The request id has to exist and has to be prepared for the withdraw to execute
|
|
42
|
+
* correctly.
|
|
43
|
+
*/
|
|
44
|
+
withdraw = ({ requestId, validatorId }) => {
|
|
45
|
+
this.#validateRequestId(requestId)
|
|
46
|
+
this.#validateValidatorId(validatorId)
|
|
47
|
+
return this.buildTxData(this.sfcContract, 'withdraw', validatorId, requestId)
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* claimRewards creates a new delegator rewards claiming transaction.
|
|
52
|
+
* The call transfers all the rewards from SFC back to the stake in single transaction.
|
|
53
|
+
*/
|
|
54
|
+
claimRewards = ({ validatorId }) => {
|
|
55
|
+
this.#validateValidatorId(validatorId)
|
|
56
|
+
return this.buildTxData(this.sfcContract, 'claimRewards', validatorId)
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* restakeRewards creates a new delegator rewards claiming transaction.
|
|
61
|
+
* The call transfers all the rewards from SFC back to the stake in single transaction.
|
|
62
|
+
*/
|
|
63
|
+
restakeRewards = ({ validatorId }) => {
|
|
64
|
+
this.#validateValidatorId(validatorId)
|
|
65
|
+
return this.buildTxData(this.sfcContract, 'restakeRewards', validatorId)
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* lockStake creates a transaction for locking delegation.
|
|
70
|
+
*/
|
|
71
|
+
lockStake = ({ validatorId, amount, durationSeconds }) => {
|
|
72
|
+
this.#validateValidatorId(validatorId)
|
|
73
|
+
this.#validateDuration(durationSeconds)
|
|
74
|
+
return this.buildTxData(this.sfcContract, 'lockStake', validatorId, durationSeconds, amount)
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* relockStake creates a transaction for re-locking delegation.
|
|
79
|
+
*/
|
|
80
|
+
relockStake = ({ validatorId, amount, durationSeconds }) => {
|
|
81
|
+
this.#validateValidatorId(validatorId)
|
|
82
|
+
this.#validateDuration(durationSeconds)
|
|
83
|
+
return this.buildTxData(this.sfcContract, 'relockStake', validatorId, durationSeconds, amount)
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* unlockStake creates a transaction for unlocking delegation.
|
|
88
|
+
*/
|
|
89
|
+
unlockStake = ({ validatorId, amount }) => {
|
|
90
|
+
this.#validateValidatorId(validatorId)
|
|
91
|
+
return this.buildTxData(this.sfcContract, 'unlockStake', validatorId, amount)
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
#validateValidatorId = (validatorId) => {
|
|
95
|
+
if (!Number.isInteger(validatorId) || validatorId <= 0) {
|
|
96
|
+
throw new Error('Validator id must be positive unsigned integer value.')
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
#validateRequestId = (requestId) => {
|
|
101
|
+
if (!Number.isInteger(requestId) || requestId <= 0) {
|
|
102
|
+
throw new Error('Request id must be a valid numeric identifier.')
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
#validateDuration = (duration) => {
|
|
107
|
+
if (!Number.isInteger(duration)) {
|
|
108
|
+
throw new Error('The lock duration must be an integer.')
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
if (duration > 365 * 86400) {
|
|
112
|
+
throw new Error('The lock duration must be at most 365 days.')
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
}
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
import { createContract } from '@exodus/ethereum-lib'
|
|
2
|
+
import { eth } from '../exodus-eth-server'
|
|
3
|
+
import { retry } from '@exodus/simple-retry'
|
|
4
|
+
import { bufferToHex } from '@exodus/ethereumjs-util'
|
|
5
|
+
|
|
6
|
+
export const EVERSTAKE_VALIDATOR_ADDR = '0xf30cf4ed712d3734161fdaab5b1dbb49fd2d0e5c'
|
|
7
|
+
export const STAKING_MANAGER_ADDR = '0x5e3ef299fddf15eaa0432e6e66473ace8c13d908'
|
|
8
|
+
|
|
9
|
+
const RETRY_DELAYS = ['10s']
|
|
10
|
+
|
|
11
|
+
export class MaticStaking {
|
|
12
|
+
constructor(validatorId = EVERSTAKE_VALIDATOR_ADDR, stakeManagerAddr = STAKING_MANAGER_ADDR) {
|
|
13
|
+
this.validatorShareContract = createContract(validatorId, 'maticValidatorShare')
|
|
14
|
+
this.stakingManagerContract = createContract(stakeManagerAddr, 'maticStakingManager')
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
buildTxData = (contract, method, ...args) => {
|
|
18
|
+
const txData = contract[method].build(...args)
|
|
19
|
+
return txData
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
processRead = (contract, method, ...args) => {
|
|
23
|
+
const callData = this.buildTxData(contract, method, ...args)
|
|
24
|
+
const data = {
|
|
25
|
+
data: bufferToHex(callData),
|
|
26
|
+
to: contract.address,
|
|
27
|
+
tag: 'latest',
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
return retry(eth.ethCall, { delayTimesMs: RETRY_DELAYS })(data)
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Get current checkpoint
|
|
35
|
+
* @returns {number}
|
|
36
|
+
*/
|
|
37
|
+
getCurrentCheckpoint = async () => {
|
|
38
|
+
const currentEpoch = await this.processRead(this.stakingManagerContract, 'epoch')
|
|
39
|
+
return parseInt(currentEpoch, 16)
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
getCurrentUnbondNonce = async (address) => {
|
|
43
|
+
const result = await this.processRead(this.validatorShareContract, 'unbondNonces', address)
|
|
44
|
+
return parseInt(result, 16)
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Get unbond info by supplying unbondNonces
|
|
49
|
+
* @param {string} address
|
|
50
|
+
* @param {number} nonce
|
|
51
|
+
* @returns {number}
|
|
52
|
+
*/
|
|
53
|
+
getUnboundInfo = async (address, nonce) => {
|
|
54
|
+
let _nonce = nonce
|
|
55
|
+
if (!Number.isInteger(_nonce)) {
|
|
56
|
+
_nonce = await this.getCurrentUnbondNonce(address)
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const unboundInfo = await this.processRead(
|
|
60
|
+
this.validatorShareContract,
|
|
61
|
+
'unbonds_new',
|
|
62
|
+
address,
|
|
63
|
+
_nonce
|
|
64
|
+
)
|
|
65
|
+
const [withdrawEpoch, shares] = this._removeHexPrefix(unboundInfo).match(/.{1,64}/g) || []
|
|
66
|
+
return {
|
|
67
|
+
withdrawEpoch: parseInt(withdrawEpoch, 16),
|
|
68
|
+
shares: parseInt(shares, 16),
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Get current Liquid rewards
|
|
74
|
+
* @param {string} address
|
|
75
|
+
* @returns {number}
|
|
76
|
+
*/
|
|
77
|
+
getLiquidRewards = async (address) => {
|
|
78
|
+
const currentEpoch = await this.processRead(
|
|
79
|
+
this.validatorShareContract,
|
|
80
|
+
'getLiquidRewards',
|
|
81
|
+
address
|
|
82
|
+
)
|
|
83
|
+
return parseInt(currentEpoch, 16)
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Get total staked amount
|
|
88
|
+
* @param {string} address
|
|
89
|
+
* @returns {number}
|
|
90
|
+
*/
|
|
91
|
+
getTotalStake = async (address) => {
|
|
92
|
+
const stakeInfo = await this.processRead(this.validatorShareContract, 'getTotalStake', address)
|
|
93
|
+
const [amount] = this._removeHexPrefix(stakeInfo).match(/.{1,64}/g) || []
|
|
94
|
+
return parseInt(amount, 16)
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Get tx data of method to restake the earned rewards
|
|
99
|
+
*/
|
|
100
|
+
restakeReward = () => {
|
|
101
|
+
return this.buildTxData(this.validatorShareContract, 'restake')
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* Get tx data of method to withdraw earned rewards
|
|
106
|
+
*/
|
|
107
|
+
withdrawRewards = () => {
|
|
108
|
+
return this.buildTxData(this.validatorShareContract, 'withdrawRewards')
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Get tx data of method to delegate (stake) amount to validator
|
|
113
|
+
* internally it calls method - **buyVoucher**
|
|
114
|
+
* @param {string} param.amount
|
|
115
|
+
*/
|
|
116
|
+
delegateAmount = ({ amount }) => {
|
|
117
|
+
return this.buildTxData(this.validatorShareContract, 'buyVoucher', amount, 0)
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* Get tx data of method to unstake delegated amount
|
|
122
|
+
* internally it calls - **sellVoucher_new**
|
|
123
|
+
* @param {string} param.amount
|
|
124
|
+
* @param {string} param.maximumSharesToBurn
|
|
125
|
+
* @returns {string}
|
|
126
|
+
*/
|
|
127
|
+
unbond = ({ amount, maximumSharesToBurn }) => {
|
|
128
|
+
const _maximumSharesToBurn = maximumSharesToBurn || amount
|
|
129
|
+
return this.buildTxData(
|
|
130
|
+
this.validatorShareContract,
|
|
131
|
+
'sellVoucher_new',
|
|
132
|
+
amount,
|
|
133
|
+
_maximumSharesToBurn
|
|
134
|
+
)
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* Get tx data of method to claim the delegated amount by supplying nonce
|
|
139
|
+
* internally it calls method - **unstakeClaimTokens_new**
|
|
140
|
+
* @param {number} nonce
|
|
141
|
+
* @returns {string}
|
|
142
|
+
*/
|
|
143
|
+
claimStake = (nonce) => {
|
|
144
|
+
return this.buildTxData(this.validatorShareContract, 'unstakeClaimTokens_new', nonce)
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* Remove 0x prefix of a hex string
|
|
149
|
+
* @param {string} str
|
|
150
|
+
* @returns {string}
|
|
151
|
+
*/
|
|
152
|
+
_removeHexPrefix = (str) => {
|
|
153
|
+
if (typeof str !== 'string' || str === '') {
|
|
154
|
+
return str
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
return str.startsWith('0x') ? str.slice(2) : str
|
|
158
|
+
}
|
|
159
|
+
}
|