Skip to content
This repository has been archived by the owner on Nov 10, 2023. It is now read-only.

Commit

Permalink
(feature) Adding origin for Apps Transactions (#576)
Browse files Browse the repository at this point in the history
* Adding origin field when creates a TX

* refactor: replace list of arg by object in getApprovalTransaction and getExecutionTransaction function

* minor changes

* Allow execute if threshold is 1 for the first tx

- Related to issue #563
- `lastTx` is required due to #489

* - Normalizing logic between createTransaction and processTransaction
- Moving shared function to a new file

* Refactor `doesTxNeedApproval` back to the `isExecution`-related meaning

* Rename function and variable names

* Add tests for `getNewTxNonce` and `shouldExecuteTransaction` functions

* Pass `safeInstance` instead of `safeAddress` to `getNewTxNonce`

* Update Tests

- remove mocked `getGnosisSafeInstanceAt`
- pass `safeInstance` instead of `safeAddress` to `getNewTxNonce`

Co-authored-by: Fernando <[email protected]>
  • Loading branch information
nicosampler and fernandomg committed Mar 9, 2020
1 parent afccc34 commit c047f74
Show file tree
Hide file tree
Showing 8 changed files with 272 additions and 156 deletions.
49 changes: 30 additions & 19 deletions src/logic/safe/transactions/send.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,33 @@ export const DELEGATE_CALL = 1
export const TX_TYPE_EXECUTION = 'execution'
export const TX_TYPE_CONFIRMATION = 'confirmation'

export const getApprovalTransaction = async (
type Transaction = {
safeInstance: any,
to: string,
valueInWei: number | string,
data: string,
operation: Operation,
nonce: number,
safeTxGas: number,
baseGas: number,
gasPrice: number,
gasToken: string,
refundReceiver: string,
sender: string,
) => {
}

export const getApprovalTransaction = async ({
safeInstance,
to,
valueInWei,
data,
operation,
nonce,
safeTxGas,
baseGas,
gasPrice,
gasToken,
refundReceiver,
sender,
}: Transaction & { nonce: number | string, sender: string }) => {
const txHash = await safeInstance.getTransactionHash(
to,
valueInWei,
Expand Down Expand Up @@ -49,21 +62,19 @@ export const getApprovalTransaction = async (
}
}

export const getExecutionTransaction = async (
safeInstance: any,
to: string,
valueInWei: number | string,
data: string,
operation: Operation,
nonce: string | number,
safeTxGas: string | number,
baseGas: string | number,
gasPrice: string | number,
gasToken: string,
refundReceiver: string,
sender: string,
sigs: string,
) => {
export const getExecutionTransaction = async ({
safeInstance,
to,
valueInWei,
data,
operation,
safeTxGas,
baseGas,
gasPrice,
gasToken,
refundReceiver,
sigs,
}: Transaction & { sigs: string }) => {
try {
const web3 = getWeb3()
const contract = new web3.eth.Contract(GnosisSafeSol.abi, safeInstance.address)
Expand Down
24 changes: 22 additions & 2 deletions src/logic/safe/transactions/txHistory.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const calculateBodyFrom = async (
transactionHash: string,
sender: string,
confirmationType: TxServiceType,
origin: string | null,
) => {
const contractTransactionHash = await safeInstance.getTransactionHash(
to,
Expand Down Expand Up @@ -50,6 +51,7 @@ const calculateBodyFrom = async (
transactionHash,
sender: getWeb3().utils.toChecksumAddress(sender),
confirmationType,
origin,
}
}

Expand All @@ -60,7 +62,23 @@ export const buildTxServiceUrl = (safeAddress: string) => {
return `${host}${base}`
}

export const saveTxToHistory = async (
export const saveTxToHistory = async ({
safeInstance,
to,
valueInWei,
data,
operation,
nonce,
safeTxGas,
baseGas,
gasPrice,
gasToken,
refundReceiver,
txHash,
sender,
type,
origin,
}: {
safeInstance: any,
to: string,
valueInWei: number | string,
Expand All @@ -75,7 +93,8 @@ export const saveTxToHistory = async (
txHash: string,
sender: string,
type: TxServiceType,
) => {
origin: string | null,
}) => {
const url = buildTxServiceUrl(safeInstance.address)
const body = await calculateBodyFrom(
safeInstance,
Expand All @@ -92,6 +111,7 @@ export const saveTxToHistory = async (
txHash,
sender,
type,
origin || null,
)
const response = await axios.post(url, body)

Expand Down
2 changes: 1 addition & 1 deletion src/routes/safe/components/Apps/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ function Apps({ web3, safeAddress, safeName, ethBalance, network, createTransact
const onConfirm = async () => {
closeModal()

const txHash = await sendTransactions(web3, createTransaction, safeAddress, data.data)
const txHash = await sendTransactions(web3, createTransaction, safeAddress, data.data, getSelectedApp().name)

if (txHash) {
sendMessageToIframe(operations.ON_TX_UPDATE, {
Expand Down
3 changes: 2 additions & 1 deletion src/routes/safe/components/Apps/sendTransactions.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const multiSendAbi = [
},
]

const sendTransactions = (web3: any, createTransaction: any, safeAddress: String, txs: Array<any>) => {
const sendTransactions = (web3: any, createTransaction: any, safeAddress: String, txs: Array<any>, origin: string) => {
const multiSend = new web3.eth.Contract(multiSendAbi, multiSendAddress)

const encodeMultiSendCalldata = multiSend.methods
Expand Down Expand Up @@ -43,6 +43,7 @@ const sendTransactions = (web3: any, createTransaction: any, safeAddress: String
closeSnackbar: () => {},
operation: DELEGATE_CALL,
navigateToTransactionsTab: false,
origin,
})
}
export default sendTransactions
117 changes: 117 additions & 0 deletions src/routes/safe/store/actions/__tests__/utils.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
import { getNewTxNonce, shouldExecuteTransaction } from '~/routes/safe/store/actions/utils'

describe('Store actions utils > getNewTxNonce', () => {
it(`should return txNonce if it's a valid value`, async () => {
// Given
const txNonce = '45'
const lastTx = {
nonce: 44
}
const safeInstance = {
nonce: () => Promise.resolve({
toString: () => Promise.resolve('45')
})
}

// When
const nonce = await getNewTxNonce(txNonce, lastTx, safeInstance)

// Then
expect(nonce).toBe('45')
})

it(`should return lastTx.nonce + 1 if txNonce is not valid`, async () => {
// Given
const txNonce = ''
const lastTx = {
nonce: 44
}
const safeInstance = {
nonce: () => Promise.resolve({
toString: () => Promise.resolve('45')
})
}

// When
const nonce = await getNewTxNonce(txNonce, lastTx, safeInstance)

// Then
expect(nonce).toBe('45')
})

it(`should retrieve contract's instance nonce value, if txNonce and lastTx are not valid`, async () => {
// Given
const txNonce = ''
const lastTx = null
const safeInstance = {
nonce: () => Promise.resolve({
toString: () => Promise.resolve('45')
})
}

// When
const nonce = await getNewTxNonce(txNonce, lastTx, safeInstance)

// Then
expect(nonce).toBe('45')
})
})

describe('Store actions utils > shouldExecuteTransaction', () => {
it(`should return false if there's a previous tx pending to be executed`, async () => {
// Given
const safeInstance = {
getThreshold: () => Promise.resolve({
toNumber: () => 1
})
}
const nonce = '1'
const lastTx = {
isExecuted: false
}

// When
const isExecution = await shouldExecuteTransaction(safeInstance, nonce, lastTx)

// Then
expect(isExecution).toBeFalsy()
})

it(`should return false if threshold is greater than 1`, async () => {
// Given
const safeInstance = {
getThreshold: () => Promise.resolve({
toNumber: () => 2
})
}
const nonce = '1'
const lastTx = {
isExecuted: true
}

// When
const isExecution = await shouldExecuteTransaction(safeInstance, nonce, lastTx)

// Then
expect(isExecution).toBeFalsy()
})

it(`should return true is threshold is 1 and previous tx is executed`, async () => {
// Given
const safeInstance = {
getThreshold: () => Promise.resolve({
toNumber: () => 1
})
}
const nonce = '1'
const lastTx = {
isExecuted: true
}

// When
const isExecution = await shouldExecuteTransaction(safeInstance, nonce, lastTx)

// Then
expect(isExecution).toBeTruthy()
})
})
Loading

0 comments on commit c047f74

Please sign in to comment.