Skip to content

Commit

Permalink
Merge pull request #79 from gnosis/cowordersigner-update
Browse files Browse the repository at this point in the history
Upgrade transaction translation to latest CowOrderSigner
  • Loading branch information
jfschwarz authored Nov 24, 2023
2 parents 305f555 + d67c705 commit 777e93a
Show file tree
Hide file tree
Showing 6 changed files with 135 additions and 84 deletions.
2 changes: 1 addition & 1 deletion extension/.pnp.cjs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

61 changes: 38 additions & 23 deletions extension/.pnp.loader.mjs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 18 additions & 1 deletion extension/src/bridge/iframe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@ interface JsonRpcResponse {
export default class BridgeIframe extends EventEmitter {
private messageId = 0

// GC OmniBridge relies on this property
chainId = '0x1'

// https://app.shinedao.finance/deals relies on this property
selectedAddress: string | undefined = undefined

constructor() {
super()
if (!window.top) throw new Error('Must run inside iframe')
Expand Down Expand Up @@ -44,6 +46,21 @@ export default class BridgeIframe extends EventEmitter {
}

window.addEventListener('message', handleBridgeEvent)

this.request({ method: 'eth_chainId' }).then((chainId) => {
this.chainId = chainId
this.emit('connect', {
chainId,
})
})

// keep window.ethereum.selectedAddress in sync
this.request({ method: 'eth_accounts' }).then((accounts) => {
this.selectedAddress = accounts[0]
})
this.on('accountsChanged', (accounts) => {
this.selectedAddress = accounts[0]
})
}

request(request: JsonRpcRequest): Promise<any> {
Expand Down
50 changes: 47 additions & 3 deletions extension/src/browser/Drawer/SimulatedExecutionCheck.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,28 @@
import { defaultAbiCoder } from 'ethers/lib/utils'
import React, { useEffect, useState } from 'react'
import { RiExternalLinkLine, RiGitBranchLine } from 'react-icons/ri'

import { Flex, Spinner, Tag } from '../../components'
import { useTenderlyProvider } from '../../providers'
import { TenderlyTransactionInfo } from '../../providers/ProvideTenderly'
import { useConnection } from '../../settings'
import { Connection } from '../../types'

import classes from './style.module.css'

enum ExecutionStatus {
PENDING,
SUCCESS,
REVERTED,
MODULE_TRANSACTION_REVERTED,
}

const SimulatedExecutionCheck: React.FC<{
transactionHash: string
mini?: boolean
}> = ({ transactionHash, mini = false }) => {
const tenderlyProvider = useTenderlyProvider()
const { connection } = useConnection()

const [transactionInfo, setTransactionInfo] =
useState<TenderlyTransactionInfo | null>(null)
Expand All @@ -33,6 +44,20 @@ const SimulatedExecutionCheck: React.FC<{
}
}, [tenderlyProvider, transactionHash])

let executionStatus = ExecutionStatus.PENDING
if (transactionInfo?.status === false) {
executionStatus = ExecutionStatus.REVERTED
} else if (transactionInfo?.status === true) {
if (
transactionInfo.receipt.logs.length === 1 &&
isExecutionFromModuleFailure(transactionInfo.receipt.logs[0], connection)
) {
executionStatus = ExecutionStatus.MODULE_TRANSACTION_REVERTED
} else {
executionStatus = ExecutionStatus.SUCCESS
}
}

if (mini) {
return (
<>
Expand Down Expand Up @@ -62,21 +87,27 @@ const SimulatedExecutionCheck: React.FC<{
justifyContent="center"
className={classes.tagContainer}
>
{!transactionInfo && (
{executionStatus === ExecutionStatus.PENDING && (
<Tag head={<Spinner />} color="info">
Pending...
</Tag>
)}
{transactionInfo?.status && (
{executionStatus === ExecutionStatus.SUCCESS && (
<Tag head={<RiGitBranchLine />} color="success">
Success
</Tag>
)}
{transactionInfo && !transactionInfo.status && (
{executionStatus === ExecutionStatus.REVERTED && (
<Tag head={<RiGitBranchLine />} color="danger">
Reverted
</Tag>
)}
{executionStatus ===
ExecutionStatus.MODULE_TRANSACTION_REVERTED && (
<Tag head={<RiGitBranchLine />} color="danger">
Module transaction reverted
</Tag>
)}
</Flex>
</Flex>
{transactionInfo && (
Expand All @@ -96,3 +127,16 @@ const SimulatedExecutionCheck: React.FC<{
}

export default SimulatedExecutionCheck

const isExecutionFromModuleFailure = (
log: TenderlyTransactionInfo['receipt']['logs'][0],
connection: Connection
) => {
return (
log.address.toLowerCase() === connection.avatarAddress.toLowerCase() &&
log.topics[0] ===
'0xacd2c8702804128fdb0db2bb49f6d127dd0181c13fd45dbfe16de0930e2bd375' && // ExecutionFromModuleFailure(address)
log.topics[1] ===
defaultAbiCoder.encode(['address'], [connection.moduleAddress])
)
}
13 changes: 12 additions & 1 deletion extension/src/providers/ProvideTenderly.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ export interface TenderlyTransactionInfo {
queue_origin: string
gas_price: string
value: string
status: true
status: boolean
fork_height: number
block_hash: string
nonce: number
Expand All @@ -124,6 +124,17 @@ export interface TenderlyTransactionInfo {
gasUsed: string
effectiveGasPrice: string
contractAddress: null
logs: {
logIndex: string
address: string
topics: string[]
data: string
blockHash: string
blockNumber: string
removed: boolean
transactionHash: string
transactionIndex: string
}[]
logsBloom: string
status: string
type: string
Expand Down
74 changes: 19 additions & 55 deletions extension/src/transactionTranslations/cowswapSetPreSignature.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,48 +20,10 @@ const GPv2SettlementInterface = new Interface([
])

const COWSWAP_ORDER_SIGNER_ADDRESS =
'0xdEb83d81d4a9758A7bAec5749DA863C409ea6C6B'
'0x23dA9AdE38E4477b23770DeD512fD37b12381FAB'

const CowswapOrderSignerInterface = new Interface([
{
inputs: [
{
internalType: 'contract GPv2Signing',
name: '_signing',
type: 'address',
},
],
stateMutability: 'nonpayable',
type: 'constructor',
},
{
inputs: [
{ internalType: 'contract IERC20', name: 'sellToken', type: 'address' },
{ internalType: 'contract IERC20', name: 'buyToken', type: 'address' },
{ internalType: 'uint256', name: 'sellAmount', type: 'uint256' },
{ internalType: 'uint256', name: 'buyAmount', type: 'uint256' },
{ internalType: 'uint32', name: 'validTo', type: 'uint32' },
{ internalType: 'uint32', name: 'validDuration', type: 'uint32' },
{ internalType: 'uint256', name: 'feeAmount', type: 'uint256' },
{ internalType: 'uint256', name: 'feeAmountBP', type: 'uint256' },
{ internalType: 'bytes32', name: 'kind', type: 'bytes32' },
{ internalType: 'bool', name: 'partiallyFillable', type: 'bool' },
{ internalType: 'bytes32', name: 'sellTokenBalance', type: 'bytes32' },
{ internalType: 'bytes32', name: 'buyTokenBalance', type: 'bytes32' },
],
name: 'signOrder',
outputs: [],
stateMutability: 'nonpayable',
type: 'function',
},
{
inputs: [],
name: 'signing',
outputs: [
{ internalType: 'contract GPv2Signing', name: '', type: 'address' },
],
stateMutability: 'view',
type: 'function',
},
'function signOrder(tuple(address sellToken, address buyToken, address receiver, uint256 sellAmount, uint256 buyAmount, uint32 validTo, bytes32 appData, uint256 feeAmount, bytes32 kind, bool partiallyFillable, bytes32 sellTokenBalance, bytes32 buyTokenBalance) order, uint32 validDuration, uint256 feeAmountBP)',
])

const COWSWAP_SUPPORTED_NETWORK: Record<number, string> = {
Expand Down Expand Up @@ -111,20 +73,22 @@ export default {
(parseInt(order.feeAmount) / parseInt(order.sellAmount)) * 10000
)

const data = CowswapOrderSignerInterface.encodeFunctionData('signOrder', [
order.sellToken,
order.buyToken,
order.sellAmount,
order.buyAmount,
order.validTo,
validDuration,
order.feeAmount,
feeAmountBP,
ethers.utils.id(order.kind),
order.partiallyFillable,
ethers.utils.id(order.sellTokenBalance),
ethers.utils.id(order.buyTokenBalance),
])
let data = ''
try {
data = CowswapOrderSignerInterface.encodeFunctionData('signOrder', [
{
...order,
kind: ethers.utils.id(order.kind),
sellTokenBalance: ethers.utils.id(order.sellTokenBalance),
buyTokenBalance: ethers.utils.id(order.buyTokenBalance),
},
validDuration,
feeAmountBP,
])
} catch (e) {
console.error('CowSwap setPreSignature translation error', e)
return undefined
}

return [
{
Expand Down

0 comments on commit 777e93a

Please sign in to comment.