@exodus/ethereum-api 2.13.2 → 2.15.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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@exodus/ethereum-api",
3
- "version": "2.13.2",
3
+ "version": "2.15.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.13.6",
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": "f1d39b200c106794d81bd19b7e41073eeec15cba"
31
+ "gitHead": "5d4cf293088ce0e75848409b3c1af528afb32789"
32
32
  }
package/src/index.js CHANGED
@@ -8,3 +8,4 @@ export * from './exodus-eth-server'
8
8
  export * from './tx-log'
9
9
  export * from './get-balances'
10
10
  export * from './staking'
11
+ export * from './simulate-tx'
@@ -0,0 +1,21 @@
1
+ const ETHEREUM_TX_PREVIEW_API = 'https://simulation.a.exodus.io/simulate'
2
+
3
+ export async function fetchTxPreview(transaction) {
4
+ const response = await fetch(ETHEREUM_TX_PREVIEW_API, {
5
+ method: 'POST',
6
+ headers: {
7
+ 'Content-Type': 'application/json',
8
+ },
9
+ body: JSON.stringify({
10
+ network: 'ethereum',
11
+ chain: 'main',
12
+ transactions: [transaction],
13
+ }),
14
+ })
15
+
16
+ if (!response.ok) {
17
+ throw new Error(await response.text())
18
+ }
19
+
20
+ return response.json()
21
+ }
@@ -0,0 +1,2 @@
1
+ export * from './fetch-tx-preview'
2
+ export * from './simulate-eth-tx'
@@ -0,0 +1,68 @@
1
+ import assets from '@exodus/assets'
2
+
3
+ import { fetchTxPreview } from './fetch-tx-preview'
4
+
5
+ function weiToEth(wei) {
6
+ const asset = assets.ethereum
7
+ const currency = asset.currency
8
+
9
+ const valueInEth = currency.baseUnit(wei).toDefaultString()
10
+
11
+ return valueInEth
12
+ }
13
+
14
+ const ethHexToInt = (hexValue) => parseInt(hexValue, '16')
15
+
16
+ export async function simulateTx(transaction) {
17
+ let accounts = {}
18
+
19
+ if (!transaction.to) throw new Error(`'to' field is missing in the TX object`)
20
+
21
+ const blocknativeTxObject = {
22
+ to: transaction.to,
23
+ input: transaction.data,
24
+ from: transaction.from,
25
+ value: ethHexToInt(transaction.value),
26
+ gas: ethHexToInt(transaction.gas),
27
+ maxFeePerGas: ethHexToInt(transaction.maxFeePerGas),
28
+ maxPriorityFeePerGas: ethHexToInt(transaction.maxPriorityFeePerGas),
29
+ }
30
+
31
+ try {
32
+ const simulatedTx = await fetchTxPreview(blocknativeTxObject)
33
+
34
+ const [simulatedBalanceChanges] = simulatedTx.netBalanceChanges
35
+
36
+ const [sender] =
37
+ simulatedBalanceChanges &&
38
+ simulatedBalanceChanges.filter(({ address }) => address === transaction.from)
39
+
40
+ if (sender) {
41
+ sender.balanceChanges.forEach((balanceChange) => {
42
+ const delta = balanceChange.delta
43
+ if (delta.startsWith('-')) {
44
+ accounts.willSend = {
45
+ symbol: balanceChange.asset.symbol,
46
+ amount: weiToEth(delta.slice(1)),
47
+ }
48
+ } else {
49
+ accounts.willReceive = {
50
+ symbol: balanceChange.asset.symbol,
51
+ amount: weiToEth(delta),
52
+ }
53
+ }
54
+ })
55
+ }
56
+
57
+ if (!accounts.willSend) {
58
+ accounts.willSend = {
59
+ symbol: 'ETH',
60
+ amount: weiToEth(transaction.value),
61
+ }
62
+ }
63
+ } catch (_) {
64
+ throw new Error('Simulation of Ethereum transaction failed.')
65
+ }
66
+
67
+ return accounts
68
+ }
@@ -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
+ }
@@ -1 +1,2 @@
1
1
  export { MaticStaking } from './matic-staking'
2
+ export { FantomStaking } from './fantom-staking'