-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
223 additions
and
124 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { performNextSwapAction } from './swap' | ||
import { performNextTransactionAction } from './send' | ||
import { createHistoryNotification } from '../../../broker/notification' | ||
|
||
export const performNextAction = async (store, { network, walletId, id }) => { | ||
const { dispatch, commit, getters } = store | ||
const item = getters.historyItemById(network, walletId, id) | ||
if (!item) return | ||
if (!item.status) return | ||
|
||
let updates | ||
if (item.type === 'SWAP') { | ||
updates = await performNextSwapAction(store, { network, walletId, order: item }) | ||
} | ||
if (item.type === 'SEND') { | ||
updates = await performNextTransactionAction(store, { network, walletId, transaction: item }) | ||
} | ||
|
||
if (updates) { | ||
commit('UPDATE_HISTORY', { | ||
network, | ||
walletId, | ||
id, | ||
updates | ||
}) | ||
|
||
createHistoryNotification({ | ||
...item, | ||
...updates | ||
}) | ||
|
||
if (!updates.error) { | ||
dispatch('performNextAction', { network, walletId, id }) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
|
||
import { withInterval } from './utils' | ||
|
||
async function waitForConfirmations ({ getters, dispatch }, { transaction, network, walletId }) { | ||
const client = getters.client(network, walletId, transaction.from) | ||
|
||
const tx = await client.chain.getTransactionByHash(transaction.txHash) | ||
|
||
if (tx && tx.confirmations > 0) { | ||
dispatch('updateBalances', { network, walletId, assets: [transaction.from] }) | ||
|
||
return { | ||
endTime: Date.now(), | ||
status: 'SUCCESS' | ||
} | ||
} | ||
} | ||
|
||
export const performNextTransactionAction = async (store, { network, walletId, transaction }) => { | ||
let updates | ||
|
||
if (transaction.status === 'WAITING_FOR_CONFIRMATIONS') { | ||
updates = await withInterval(async () => waitForConfirmations(store, { transaction, network, walletId })) | ||
} | ||
|
||
return updates | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
|
||
import { random } from 'lodash-es' | ||
import { unlockAsset, wait } from '../../utils' | ||
|
||
export async function withLock ({ dispatch }, { order, network, walletId, asset }, func) { | ||
const lock = await dispatch('getLockForAsset', { order, network, walletId, asset }) | ||
try { | ||
return await func() | ||
} catch (e) { | ||
return { error: e.toString() } | ||
} finally { | ||
unlockAsset(lock) | ||
} | ||
} | ||
|
||
export async function withInterval (func) { | ||
const updates = await func() | ||
if (updates) { return updates } | ||
return new Promise((resolve, reject) => { | ||
const interval = setInterval(async () => { | ||
const updates = await func() | ||
if (updates) { | ||
clearInterval(interval) | ||
resolve(updates) | ||
} | ||
}, random(15000, 30000)) | ||
}) | ||
} | ||
|
||
export async function hasChainTimePassed ({ getters }, { network, walletId, asset, timestamp }) { | ||
const client = getters.client(network, walletId, asset) | ||
const maxTries = 3 | ||
let tries = 0 | ||
while (tries < maxTries) { | ||
try { | ||
const blockNumber = await client.chain.getBlockHeight() | ||
const latestBlock = await client.chain.getBlockByNumber(blockNumber) | ||
return latestBlock.timestamp > timestamp | ||
} catch (e) { | ||
tries++ | ||
if (tries >= maxTries) throw e | ||
else { | ||
console.warn(e) | ||
await wait(2000) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,31 @@ | ||
import { createNotification } from '../../broker/notification' | ||
import { prettyBalance } from '../../utils/coinFormatter' | ||
import { getAssetIcon } from '../../utils/asset' | ||
import { v4 as uuidv4 } from 'uuid' | ||
import { createHistoryNotification } from '../../broker/notification' | ||
|
||
export const sendTransaction = async ({ commit, getters }, { network, walletId, asset, to, amount, data, fee }) => { | ||
export const sendTransaction = async ({ dispatch, commit, getters }, { network, walletId, asset, to, amount, data, fee }) => { | ||
const client = getters.client(network, walletId, asset) | ||
|
||
const tx = await client.chain.sendTransaction(to, amount, data, fee) | ||
|
||
const transaction = { | ||
id: uuidv4(), | ||
type: 'SEND', | ||
network, | ||
walletId, | ||
|
||
to: asset, | ||
from: asset, | ||
toAddress: to, | ||
|
||
amount, | ||
fee, | ||
tx, | ||
txHash: tx.hash, | ||
|
||
startTime: Date.now(), | ||
status: 'SUCCESS' | ||
status: 'WAITING_FOR_CONFIRMATIONS' | ||
} | ||
|
||
commit('NEW_TRASACTION', { | ||
network, | ||
walletId, | ||
transaction | ||
}) | ||
commit('NEW_TRASACTION', { network, walletId, transaction }) | ||
|
||
dispatch('performNextAction', { network, walletId, id: transaction.id }) | ||
|
||
createNotification({ | ||
title: `New ${asset} Transaction`, | ||
message: `Sent ${prettyBalance(amount, asset)} ${asset} to ${to}`, | ||
iconUrl: getAssetIcon(asset) | ||
}) | ||
createHistoryNotification(transaction) | ||
|
||
return tx | ||
} |
Oops, something went wrong.