-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adjust amount when there is a fee error
- Loading branch information
Showing
7 changed files
with
166 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
packages/core-mobile/app/hooks/earn/utils/extractNeededAmount.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { extractNeededAmount } from './extractNeededAmount' | ||
|
||
it('should return the correct BigInt amount for a matching error message', () => { | ||
const errorMessage = | ||
'Insufficient funds: provided UTXOs need 10057 more nAVAX (asset id: U8iRqJoiJm8xZHAacmvYyZVwqQx6uDNtQeP3CQ6fcgQk3JqnK)' | ||
const assetId = 'U8iRqJoiJm8xZHAacmvYyZVwqQx6uDNtQeP3CQ6fcgQk3JqnK' | ||
|
||
const result = extractNeededAmount(errorMessage, assetId) | ||
expect(result).toBe(BigInt(10057)) | ||
}) | ||
|
||
it('should return null for an error message that does not match the regex', () => { | ||
const errorMessage = 'Some random error message without needed amount' | ||
const assetId = 'U8iRqJoiJm8xZHAacmvYyZVwqQx6uDNtQeP3CQ6fcgQk3JqnK' | ||
|
||
const result = extractNeededAmount(errorMessage, assetId) | ||
expect(result).toBeNull() | ||
}) | ||
|
||
it('should return null if the asset ID in the error message does not match the provided asset ID', () => { | ||
const errorMessage = | ||
'Insufficient funds: provided UTXOs need 10057 more nAVAX (asset id: U8iRqJoiJm8xZHAacmvYyZVwqQx6uDNtQeP3CQ6fcgQk3JqnK)' | ||
const assetId = 'DifferentAssetId' | ||
|
||
const result = extractNeededAmount(errorMessage, assetId) | ||
expect(result).toBeNull() | ||
}) | ||
|
||
it('should return null if the needed amount is not present in the error message', () => { | ||
const errorMessage = | ||
'Insufficient funds: provided UTXOs need more nAVAX (asset id: U8iRqJoiJm8xZHAacmvYyZVwqQx6uDNtQeP3CQ6fcgQk3JqnK)' | ||
const assetId = 'U8iRqJoiJm8xZHAacmvYyZVwqQx6uDNtQeP3CQ6fcgQk3JqnK' | ||
|
||
const result = extractNeededAmount(errorMessage, assetId) | ||
expect(result).toBeNull() | ||
}) | ||
|
||
it('should handle edge cases like empty error messages gracefully', () => { | ||
const errorMessage = '' | ||
const assetId = 'U8iRqJoiJm8xZHAacmvYyZVwqQx6uDNtQeP3CQ6fcgQk3JqnK' | ||
|
||
const result = extractNeededAmount(errorMessage, assetId) | ||
expect(result).toBeNull() | ||
}) |
16 changes: 16 additions & 0 deletions
16
packages/core-mobile/app/hooks/earn/utils/extractNeededAmount.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// use a regex to match "Insufficient funds" and capture the missing amount | ||
export const extractNeededAmount = ( | ||
errorMessage: string, | ||
assetId: string | ||
): bigint | null => { | ||
const regex = new RegExp( | ||
`Insufficient funds.*need (\\d+) more nAVAX \\(asset id: ${assetId}\\)` | ||
) | ||
const match = errorMessage.match(regex) | ||
|
||
if (match && match[1]) { | ||
return BigInt(match[1]) // convert to BigInt and return | ||
} | ||
|
||
return null // return null if no match is found | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters