WA-521 Logic for handling txs via provider signatures

This commit is contained in:
apanizo 2018-08-22 13:43:43 +02:00
parent b1c281b6d2
commit c0970f3906
1 changed files with 45 additions and 0 deletions

View File

@ -3,6 +3,9 @@ import { calculateGasOf, checkReceiptStatus, calculateGasPrice } from '~/logic/w
import { type Operation, submitOperation } from '~/logic/safe/safeTxHistory' import { type Operation, submitOperation } from '~/logic/safe/safeTxHistory'
import { getDailyLimitModuleFrom } from '~/logic/contracts/dailyLimitContracts' import { getDailyLimitModuleFrom } from '~/logic/contracts/dailyLimitContracts'
import { getSafeEthereumInstance } from '~/logic/safe/safeFrontendOperations' import { getSafeEthereumInstance } from '~/logic/safe/safeFrontendOperations'
import { generateMetamaskSignature, generateTxGasEstimateFrom, estimateDataGas } from '~/logic/safe/safeTxSigner'
import { storeSignature, getSignaturesFrom } from '~/utils/localStorage/signatures'
import { signaturesViaMetamask } from '~/config'
export const approveTransaction = async ( export const approveTransaction = async (
safeAddress: string, safeAddress: string,
@ -15,6 +18,14 @@ export const approveTransaction = async (
) => { ) => {
const gasPrice = await calculateGasPrice() const gasPrice = await calculateGasPrice()
if (signaturesViaMetamask()) {
const safe = await getSafeEthereumInstance(safeAddress)
const txGasEstimate = await generateTxGasEstimateFrom(safe, safeAddress, data, to, valueInWei, operation)
const signature =
await generateMetamaskSignature(safe, safeAddress, sender, to, valueInWei, nonce, data, operation, txGasEstimate)
storeSignature(safeAddress, nonce, signature)
}
const gnosisSafe = await getSafeEthereumInstance(safeAddress) const gnosisSafe = await getSafeEthereumInstance(safeAddress)
const txData = gnosisSafe.contract.approveTransactionWithParameters.getData(to, valueInWei, data, operation, nonce) const txData = gnosisSafe.contract.approveTransactionWithParameters.getData(to, valueInWei, data, operation, nonce)
const gas = await calculateGasOf(txData, sender, safeAddress) const gas = await calculateGasOf(txData, sender, safeAddress)
@ -39,6 +50,40 @@ export const executeTransaction = async (
) => { ) => {
const gasPrice = await calculateGasPrice() const gasPrice = await calculateGasPrice()
if (signaturesViaMetamask()) {
const safe = await getSafeEthereumInstance(safeAddress)
const txGasEstimate = await generateTxGasEstimateFrom(safe, safeAddress, data, to, valueInWei, operation)
const signature =
await generateMetamaskSignature(safe, safeAddress, sender, to, valueInWei, nonce, data, operation, txGasEstimate)
storeSignature(safeAddress, nonce, signature)
const sigs = getSignaturesFrom(safeAddress, nonce)
const threshold = await safe.getThreshold()
const gas = await estimateDataGas(safe, to, valueInWei, data, operation, txGasEstimate, 0, nonce, Number(threshold))
const numOwners = await safe.getOwners()
const gasIncludingRemovingStoreUpfront = gas + txGasEstimate + (numOwners.length * 15000)
const txReceipt = await safe.execTransactionAndPaySubmitter(
to,
valueInWei,
data,
operation,
txGasEstimate,
0,
0,
0,
sigs,
{ from: sender, gas: gasIncludingRemovingStoreUpfront, gasPrice },
)
const txHash = txReceipt.tx
await checkReceiptStatus(txHash)
// await submitOperation(safeAddress, to, valueInWei, data, operation, nonce, txHash, sender, 'execution')
return txHash
}
const gnosisSafe = await getSafeEthereumInstance(safeAddress) const gnosisSafe = await getSafeEthereumInstance(safeAddress)
const txConfirmationData = const txConfirmationData =
gnosisSafe.contract.execTransactionIfApproved.getData(to, valueInWei, data, operation, nonce) gnosisSafe.contract.execTransactionIfApproved.getData(to, valueInWei, data, operation, nonce)