fix: wrap/unwrap activity parsing (#7384)

* fix: add unit test

* fix: re-use swap parse logic
This commit is contained in:
eddie 2023-09-29 10:26:09 -07:00 committed by GitHub
parent af80079957
commit 3a4dc91e49
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 137 additions and 0 deletions

@ -101,6 +101,42 @@ Object {
}
`;
exports[`parseRemote parseRemoteActivities should parse eth wrap 1`] = `
Object {
"chainId": 1,
"currencies": Array [
ExtendedEther {
"chainId": 1,
"decimals": 18,
"isNative": true,
"isToken": false,
"name": "Ether",
"symbol": "ETH",
},
Token {
"address": "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2",
"chainId": 1,
"decimals": 18,
"isNative": false,
"isToken": true,
"name": "Wrapped Ether",
"symbol": "WETH",
},
],
"descriptor": "100 ETH for 100 WETH",
"from": "0x50EC05ADe8280758E2077fcBC08D878D4aef79C3",
"hash": "someHash",
"logos": Array [
"https://token-icons.s3.amazonaws.com/eth.png",
"https://raw.githubusercontent.com/Uniswap/assets/master/blockchains/ethereum/assets/0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2/logo.png",
],
"nonce": 12345,
"status": "CONFIRMED",
"timestamp": 10000,
"title": "Wrapped",
}
`;
exports[`parseRemote parseRemoteActivities should parse moonpay purchase 1`] = `
Object {
"chainId": 1,

@ -222,6 +222,78 @@ const mockTokenTransferOutPartsFragment = {
},
}
const mockNativeTokenTransferOutPartsFragment = {
__typename: 'TokenTransfer',
id: 'tokenTransferId',
asset: {
__typename: 'Token',
id: 'ETH',
name: 'Ether',
symbol: 'ETH',
address: null,
decimals: 18,
chain: 'ETHEREUM',
standard: null,
project: {
__typename: 'TokenProject',
id: 'Ethereum',
isSpam: false,
logo: {
__typename: 'Image',
id: 'ETH_logo',
url: 'https://token-icons.s3.amazonaws.com/eth.png',
},
},
},
tokenStandard: 'NATIVE',
quantity: '0.25',
sender: MockSenderAddress,
recipient: MockRecipientAddress,
direction: 'OUT',
transactedValue: {
__typename: 'Amount',
id: 'ETH_amount',
currency: 'USD',
value: 399.0225,
},
}
const mockWrappedEthTransferInPartsFragment = {
__typename: 'TokenTransfer',
id: 'tokenTransferId',
asset: {
__typename: 'Token',
id: 'WETH',
name: 'Wrapped Ether',
symbol: 'WETH',
address: '0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2',
decimals: 18,
chain: 'ETHEREUM',
standard: 'ERC20',
project: {
__typename: 'TokenProject',
id: 'weth_project_id',
isSpam: false,
logo: {
__typename: 'Image',
id: 'weth_image',
url: 'https://raw.githubusercontent.com/Uniswap/assets/master/blockchains/ethereum/assets/0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2/logo.png',
},
},
},
tokenStandard: 'ERC20',
quantity: '0.25',
sender: MockSenderAddress,
recipient: MockRecipientAddress,
direction: 'IN',
transactedValue: {
__typename: 'Amount',
id: 'mockWethAmountId',
currency: 'USD',
value: 399.1334007875,
},
}
const mockTokenTransferInPartsFragment = {
__typename: 'TokenTransfer',
id: 'tokenTransferId',
@ -439,3 +511,12 @@ export const MockNFTPurchase = {
],
},
} as AssetActivityPartsFragment
export const MockWrap = {
...mockAssetActivityPartsFragment,
details: {
...commonTransactionDetailsFields,
type: TransactionType.Lend,
assetChanges: [mockNativeTokenTransferOutPartsFragment, mockWrappedEthTransferInPartsFragment],
},
} as AssetActivityPartsFragment

@ -16,6 +16,7 @@ import {
MockTokenReceive,
MockTokenSend,
MockTokenTransfer,
MockWrap,
} from './fixtures/activity'
import { parseRemoteActivities, useTimeSince } from './parseRemote'
@ -80,6 +81,10 @@ describe('parseRemote', () => {
const result = parseRemoteActivities(jest.fn().mockReturnValue('100'), [MockSwapOrder])
expect(result?.['someHash']).toMatchSnapshot()
})
it('should parse eth wrap', () => {
const result = parseRemoteActivities(jest.fn().mockReturnValue('100'), [MockWrap])
expect(result?.['someHash']).toMatchSnapshot()
})
})
describe('useTimeSince', () => {

@ -182,6 +182,19 @@ function parseSwap(changes: TransactionChanges, formatNumberOrString: FormatNumb
return { title: t`Unknown Swap` }
}
/**
* Wrap/unwrap transactions are labelled as lend transactions on the backend.
* This function parses the transaction changes to determine if the transaction is a wrap/unwrap transaction.
*/
function parseLend(changes: TransactionChanges, formatNumberOrString: FormatNumberOrStringFunctionType) {
const native = changes.TokenTransfer.find((t) => t.tokenStandard === 'NATIVE')?.asset
const erc20 = changes.TokenTransfer.find((t) => t.tokenStandard === 'ERC20')?.asset
if (native && erc20 && gqlToCurrency(native)?.wrapped.address === gqlToCurrency(erc20)?.wrapped.address) {
return parseSwap(changes, formatNumberOrString)
}
return { title: t`Unknown Lend` }
}
function parseSwapOrder(changes: TransactionChanges, formatNumberOrString: FormatNumberOrStringFunctionType) {
return { ...parseSwap(changes, formatNumberOrString), prefixIconSrc: UniswapXBolt }
}
@ -305,6 +318,7 @@ type ActivityTypeParser = (
) => Partial<Activity>
const ActivityParserByType: { [key: string]: ActivityTypeParser | undefined } = {
[ActivityType.Swap]: parseSwap,
[ActivityType.Lend]: parseLend,
[ActivityType.SwapOrder]: parseSwapOrder,
[ActivityType.Approve]: parseApprove,
[ActivityType.Send]: parseSendReceive,
@ -395,6 +409,7 @@ function parseRemoteActivity(
})
return undefined
}
const defaultFields = {
hash: assetActivity.details.hash,
chainId: supportedChain,