From 97d7118984b348cd2c7f1018aac98c3b8721602e Mon Sep 17 00:00:00 2001 From: apanizo Date: Sat, 4 Aug 2018 13:27:18 +0200 Subject: [PATCH] Safe Tx History POST helper function --- src/wallets/safeTxHistory.js | 69 ++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 src/wallets/safeTxHistory.js diff --git a/src/wallets/safeTxHistory.js b/src/wallets/safeTxHistory.js new file mode 100644 index 00000000..4e2dafa6 --- /dev/null +++ b/src/wallets/safeTxHistory.js @@ -0,0 +1,69 @@ +// @flow +import { getWeb3 } from '~/wallets/getWeb3' +import { getGnosisSafeContract } from '~/wallets/safeContracts' + +type Type = 'confirmation' | 'execution' +type Operation = 0 | 1 | 2 + +const calculateBodyFrom = async ( + safeAddress: string, + to: string, + value: number, + data: string, + operation: Operation, + nonce: number, + transactionHash: string, + sender: string, + type: Type, +) => { + const web3 = getWeb3() + const valueInWei = value > 0 ? web3.toWei(value, 'ether') : value + const GnosisSafe = await getGnosisSafeContract(web3) + const gnosisSafe = GnosisSafe.at(safeAddress) + const contractTransactionHash = await gnosisSafe.getTransactionHash(safeAddress, valueInWei, data, operation, nonce) + + return JSON.stringify({ + to, + value, + data, + operation, + nonce, + contractTransactionHash, + transactionHash, + sender, + type, + }) +} + +export const submitOperation = async ( + safeAddress: string, + to: string, + value: number, + data: string, + operation: Operation, + nonce: number, + txHash: string, + sender: string, + type: Type, +) => { + const base = `safes/${safeAddress}/transaction/` + const url = `https://safe-transaction-history.dev.gnosisdev.com/${base}` + const headers = { + Accept: 'application/json', + 'Content-Type': 'application/json', + } + + const body = await calculateBodyFrom(safeAddress, to, value, data, operation, nonce, txHash, sender, type) + + const response = await fetch(url, { + method: 'POST', + headers, + body, + }) + + if (response.status !== 201) { + return Promise.reject(new Error('Error submitting the transaction')) + } + + return Promise.resolve() +}