Skip to content

Commit

Permalink
fix(earn): ensure isNative is false and not undefined (#6024)
Browse files Browse the repository at this point in the history
### Description

isNative is a required field on the triggerShortcut hook

### Test plan

Manually by preparing a swap + deposit tx w/ non native token (cusd ->
usdt deposit)

### Related issues

- N/A

### Backwards compatibility

Y

### Network scalability

If a new NetworkId and/or Network are added in the future, the changes
in this PR will:

- [x] Continue to work without code changes, OR trigger a compilation
error (guaranteeing we find it when a new network is added)
  • Loading branch information
satish-ravi authored Sep 19, 2024
1 parent cc12ac5 commit 1c5dab8
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 66 deletions.
136 changes: 71 additions & 65 deletions src/earn/prepareTransactions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,43 @@ describe('prepareTransactions', () => {
})
})

it('prepares transactions using swap-deposit shortcut', async () => {
jest.mocked(triggerShortcutRequest).mockResolvedValue({
transactions: [
it.each([
{ isNative: true, testSuffix: 'native token', token: mockFeeCurrency },
{ isNative: false, testSuffix: 'non native token', token: mockToken },
])(
'prepares transactions using swap-deposit shortcut ($testSuffix)',
async ({ isNative, token }) => {
jest.mocked(triggerShortcutRequest).mockResolvedValue({
transactions: [
{
from: '0x1234',
to: '0x5678',
data: '0xencodedData',
},
{
from: '0x1234',
to: '0x5678',
data: '0xencodedData',
gas: '50100',
estimatedGasUse: '49800',
},
],
dataProps: {
swapTransaction: 'swapTransaction',
},
})

const result = await prepareDepositTransactions({
amount: '5',
token,
walletAddress: '0x1234',
feeCurrencies: [mockFeeCurrency],
pool: mockEarnPositions[0],
hooksApiUrl: 'https://hooks.api',
shortcutId: 'swap-deposit',
})

const expectedTransactions = [
{
from: '0x1234',
to: '0x5678',
Expand All @@ -140,70 +174,42 @@ describe('prepareTransactions', () => {
from: '0x1234',
to: '0x5678',
data: '0xencodedData',
gas: '50100',
estimatedGasUse: '49800',
gas: BigInt(50100),
_estimatedGasUse: BigInt(49800),
},
]
expect(result).toEqual({
prepareTransactionsResult: {
type: 'possible',
feeCurrency: mockFeeCurrency,
transactions: expectedTransactions,
},
],
dataProps: {
swapTransaction: 'swapTransaction',
},
})

const result = await prepareDepositTransactions({
amount: '5',
token: mockToken,
walletAddress: '0x1234',
feeCurrencies: [mockFeeCurrency],
pool: mockEarnPositions[0],
hooksApiUrl: 'https://hooks.api',
shortcutId: 'swap-deposit',
})

const expectedTransactions = [
{
from: '0x1234',
to: '0x5678',
data: '0xencodedData',
},
{
from: '0x1234',
to: '0x5678',
data: '0xencodedData',
gas: BigInt(50100),
_estimatedGasUse: BigInt(49800),
},
]
expect(result).toEqual({
prepareTransactionsResult: {
type: 'possible',
feeCurrency: mockFeeCurrency,
transactions: expectedTransactions,
},
swapTransaction: 'swapTransaction',
})
expect(prepareTransactions).toHaveBeenCalledWith({
baseTransactions: expectedTransactions,
feeCurrencies: [mockFeeCurrency],
spendToken: mockToken,
spendTokenAmount: new BigNumber(5000000),
isGasSubsidized: false,
origin: 'earn-swap-deposit',
})
expect(isGasSubsidizedForNetwork).toHaveBeenCalledWith(mockToken.networkId)
expect(triggerShortcutRequest).toHaveBeenCalledWith('https://hooks.api', {
address: '0x1234',
appId: mockEarnPositions[0].appId,
networkId: mockEarnPositions[0].networkId,
shortcutId: 'swap-deposit',
swapFromToken: {
tokenId: mockToken.tokenId,
amount: '5',
decimals: 6,
address: mockToken.address,
isNative: mockToken.isNative,
},
})
})
})
expect(prepareTransactions).toHaveBeenCalledWith({
baseTransactions: expectedTransactions,
feeCurrencies: [mockFeeCurrency],
spendToken: token,
spendTokenAmount: new BigNumber(5).times(10 ** token.decimals),
isGasSubsidized: false,
origin: 'earn-swap-deposit',
})
expect(isGasSubsidizedForNetwork).toHaveBeenCalledWith(mockToken.networkId)
expect(triggerShortcutRequest).toHaveBeenCalledWith('https://hooks.api', {
address: '0x1234',
appId: mockEarnPositions[0].appId,
networkId: mockEarnPositions[0].networkId,
shortcutId: 'swap-deposit',
swapFromToken: {
tokenId: token.tokenId,
amount: '5',
decimals: token.decimals,
address: token.address,
isNative,
},
})
}
)

it.each([undefined, {}])(
'throws if swap transaction is not found in swap-deposit shortcut response',
Expand Down
2 changes: 1 addition & 1 deletion src/earn/prepareTransactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export async function prepareDepositTransactions({
amount,
decimals: token.decimals,
address: token.address,
isNative: token.isNative,
isNative: token.isNative ?? false,
},
}

Expand Down

0 comments on commit 1c5dab8

Please sign in to comment.