Skip to content

Commit 212311e

Browse files
committed
Merge branch 'dev' into fix-send-max
2 parents e815f74 + 3b28bf7 commit 212311e

File tree

4 files changed

+44
-42
lines changed

4 files changed

+44
-42
lines changed

src/store/actions/checkIfQuoteExpired.js

-18
This file was deleted.

src/store/actions/index.js

-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { changeActiveWalletId } from './changeActiveWalletId'
22
import { changeActiveNetwork } from './changeActiveNetwork'
33
import { changePassword } from './changePassword'
4-
import { checkIfQuoteExpired } from './checkIfQuoteExpired'
54
import { checkPendingActions } from './checkPendingActions'
65
import { clientExec } from './clientExec'
76
import { getLockForAsset } from './getLockForAsset'
@@ -39,7 +38,6 @@ export {
3938
changeActiveWalletId,
4039
changeActiveNetwork,
4140
changePassword,
42-
checkIfQuoteExpired,
4341
checkPendingActions,
4442
clientExec,
4543
getLockForAsset,

src/store/actions/performNextAction/swap.js

+43-21
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
import { sha256 } from '@liquality/crypto'
22
import { withLock, withInterval, hasChainTimePassed } from './utils'
33
import cryptoassets from '../../../utils/cryptoassets'
4-
import { updateOrder } from '../../utils'
4+
import { updateOrder, timestamp } from '../../utils'
5+
6+
async function hasQuoteExpired (store, { order }) {
7+
return timestamp() >= order.expiresAt
8+
}
59

610
async function canRefund ({ getters }, { network, walletId, order }) {
711
return hasChainTimePassed({ getters }, { network, walletId, asset: order.from, timestamp: order.swapExpiration })
@@ -52,7 +56,9 @@ async function createSecret ({ getters, dispatch }, { order, network, walletId }
5256
}
5357

5458
async function initiateSwap ({ getters, dispatch }, { order, network, walletId }) {
55-
if (await dispatch('checkIfQuoteExpired', { network, walletId, order })) return
59+
if (await hasQuoteExpired({ getters }, { network, walletId, order })) {
60+
return { status: 'QUOTE_EXPIRED' }
61+
}
5662

5763
const fromClient = getters.client(network, walletId, order.from)
5864

@@ -73,31 +79,44 @@ async function initiateSwap ({ getters, dispatch }, { order, network, walletId }
7379
}
7480

7581
async function fundSwap ({ getters, dispatch }, { order, network, walletId }) {
76-
if (await dispatch('checkIfQuoteExpired', { network, walletId, order })) return
82+
if (await hasQuoteExpired({ getters }, { network, walletId, order })) {
83+
return { status: 'QUOTE_EXPIRED' }
84+
}
7785

78-
const toClient = getters.client(network, walletId, order.to)
86+
const fromClient = getters.client(network, walletId, order.from)
7987

80-
const fundTx = await toClient.swap.fundSwap(
81-
order.fromFundHash,
82-
order.fromAmount,
83-
order.fromCounterPartyAddress,
84-
order.fromAddress,
85-
order.secretHash,
86-
order.swapExpiration,
87-
order.fee
88-
)
88+
try {
89+
console.log('funding')
90+
const fundTx = await fromClient.swap.fundSwap(
91+
order.fromFundHash,
92+
order.fromAmount,
93+
order.fromCounterPartyAddress,
94+
order.fromAddress,
95+
order.secretHash,
96+
order.swapExpiration,
97+
order.fee
98+
)
8999

90-
return {
91-
fundTxHash: fundTx?.hash,
92-
status: 'INITIATION_REPORTED'
100+
return {
101+
fundTxHash: fundTx?.hash,
102+
status: 'FUNDED'
103+
}
104+
} catch (e) { // Handle ERC20 contract initiation still to be mined
105+
if (e.name === 'PendingTxError') console.warn(e)
106+
else throw e
93107
}
94108
}
95109

96-
async function reportInitiation (store, { order }) {
110+
async function reportInitiation ({ getters }, { order, network, walletId }) {
111+
if (await hasQuoteExpired({ getters }, { network, walletId, order })) {
112+
console.log('WAITING FOR REFUND')
113+
return { status: 'WAITING_FOR_REFUND' }
114+
}
115+
97116
await updateOrder(order)
98117

99118
return {
100-
status: 'FUNDED'
119+
status: 'INITIATION_REPORTED'
101120
}
102121
}
103122

@@ -297,12 +316,15 @@ export const performNextSwapAction = async (store, { network, walletId, order })
297316
break
298317

299318
case 'INITIATED':
300-
updates = await reportInitiation(store, { order, network, walletId })
319+
updates = await withInterval(
320+
async () => await withLock(store, { item: order, network, walletId, asset: order.from },
321+
async () => fundSwap(store, { order, network, walletId })
322+
)
323+
)
301324
break
302325

303326
case 'FUNDED':
304-
updates = await withLock(store, { item: order, network, walletId, asset: order.from },
305-
async () => fundSwap(store, { order, network, walletId }))
327+
updates = await reportInitiation(store, { order, network, walletId })
306328
break
307329

308330
case 'INITIATION_REPORTED':

src/store/utils.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export { wait }
1515

1616
export const waitForRandom = (min, max) => wait(random(min, max))
1717

18-
export const timestamp = () => Math.ceil(Date.now() / 1000)
18+
export const timestamp = () => Date.now()
1919

2020
export const attemptToLockAsset = (network, walletId, asset) => {
2121
const chain = getChainFromAsset(asset)

0 commit comments

Comments
 (0)