Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: sip10 token send form fees bug #5414

Merged
merged 1 commit into from
May 22, 2024
Merged

fix: sip10 token send form fees bug #5414

merged 1 commit into from
May 22, 2024

Conversation

alter-eggo
Copy link
Contributor

@alter-eggo alter-eggo commented May 22, 2024

Try out Leather build 2ed7544Extension build, Test report, Storybook, Chromatic

This pr fixes sip10 send form fees bug, discussed:
https://trustmachines.slack.com/archives/C0520G8J33M/p1716339282877119
https://trustmachines.slack.com/archives/C05LAP952E7/p1716363736626209

Summary by CodeRabbit

  • New Features

    • Introduced a new function to retrieve asset contract addresses from contract IDs or fully qualified asset names.
  • Improvements

    • Updated navigation logic for sending SIP10 tokens to enhance performance and maintainability.
    • Standardized address retrieval in asset string parts for consistency.
  • Refactor

    • Replaced Navigate component with useNavigate hook for more efficient navigation handling.

Copy link

coderabbitai bot commented May 22, 2024

Warning

Rate Limit Exceeded

@alter-eggo has exceeded the limit for the number of commits or files that can be reviewed per hour. Please wait 21 minutes and 55 seconds before requesting another review.

How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.
Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.
Please see our FAQ for further information.

Commits Files that changed from the base of the PR and between 4900e5d and 2ed7544.

Walkthrough

The changes encompass optimizing navigation for sending SIP10 tokens, refining fee estimation, strengthening error handling in transaction generation, introducing a standardized address extraction function, and ensuring uniformity in address retrieval methods across functions.

Changes

Files Change Summary
src/app/pages/send/send-crypto-asset-form/form/sip10/sip10-token-send-form.tsx Updated navigation logic using useNavigate for smoother transitions.
src/app/query/stacks/fees/fees.query.ts Added priority: 2 option to fetchTransactionFeeEstimation for enhanced fee estimation.
src/app/store/transactions/token-transfer.hooks.ts Improved error handling with logger integration in transaction generation logic.
src/app/ui/utils/get-asset-contract-address.ts Introduced getAssetContractAddress function for consistent asset contract address retrieval.
src/app/common/validation/forms/amount-validators.ts Enhanced validation by using ftDecimals for more accurate comparisons.
src/app/query/stacks/sip10/sip10-tokens.utils.ts Enriched createSip10CryptoAssetInfo function with contract address inclusion for SIP10 tokens.
package.json Updated @leather-wallet/models version from "0.6.1" to "0.6.3" for package enhancement.

In fields of code where changes bloom,
A rabbit hops from room to room.
With useNavigate, paths are clear,
Contract addresses now appear.
In TypeScript lines, we find our way,
Brightening functions day by day. 🌟🐇


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Collaborator

@kyranjamie kyranjamie left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure this helper belongs in ui, but will refactor out when working on @leather-wallet/stacks

Comment on lines 8 to 14
export const getAssetContractAddress = (value: string): string => {
if (value.includes('.')) {
return value.split('.')[0];
}

return value;
};
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can use fn declaration as we do elsewhere

Copy link
Collaborator

@kyranjamie kyranjamie May 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also should have a unit test

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

Review Details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits Files that changed from the base of the PR and between 0c6c4d1 and 1416233.
Files selected for processing (3)
  • src/app/pages/send/send-crypto-asset-form/form/sip10/sip10-token-send-form.tsx (2 hunks)
  • src/app/ui/utils/get-asset-contract-address.ts (1 hunks)
  • src/app/ui/utils/get-asset-string-parts.ts (2 hunks)
Additional comments not posted (2)
src/app/ui/utils/get-asset-contract-address.ts (1)

8-14: The implementation of getAssetContractAddress appears correct and efficient for extracting the contract address from a string.

src/app/pages/send/send-crypto-asset-form/form/sip10/sip10-token-send-form.tsx (1)

25-32: Switching to useNavigate for programmatic navigation aligns with modern React Router practices and is implemented correctly here.

@@ -1,3 +1,4 @@
import { getAssetContractAddress } from './get-asset-contract-address';
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tip

Codebase Verification

The fallback logic in getAssetStringParts assigns the same string to both contractName and assetName when the input string does not contain expected delimiters. This might not be the intended behavior.

  • src/app/ui/utils/get-asset-string-parts.ts: Lines 8-16

Please review and ensure that the fallback logic correctly assigns contractName and assetName.

Analysis chain

Refactoring to use getAssetContractAddress enhances consistency in address extraction. However, ensure that the fallback logic (when delimiters are missing) correctly assigns contractName and assetName.

Also applies to: 27-33

Scripts Executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify the fallback logic for `getAssetStringParts` when delimiters are missing.

# Test: Search for usages of `getAssetStringParts` and review the expected input patterns.
rg --type typescript $'getAssetStringParts'

Length of output: 77



Script:

#!/bin/bash
# Description: Verify the fallback logic for `getAssetStringParts` when delimiters are missing.

# Test: Search for usages of `getAssetStringParts` and review the expected input patterns.
rg --type ts 'getAssetStringParts'

Length of output: 813



Script:

#!/bin/bash
# Extract the relevant lines from `token-transfer.hooks.ts` and `sip10-tokens.utils.ts` to review the usage of `getAssetStringParts`.

# Extract lines around the usage in `token-transfer.hooks.ts`
rg --type ts -A 5 -B 5 'getAssetStringParts' src/app/store/transactions/token-transfer.hooks.ts

# Extract lines around the usage in `sip10-tokens.utils.ts`
rg --type ts -A 5 -B 5 'getAssetStringParts' src/app/query/stacks/sip10/sip10-tokens.utils.ts

Length of output: 1927



Script:

#!/bin/bash
# Extract the implementation of `getAssetStringParts` to review the fallback logic.

ast-grep --lang typescript --pattern $'export const getAssetStringParts = ($_): $_ {
  $$$
}' src/app/ui/utils/get-asset-string-parts.ts

Length of output: 137



Script:

#!/bin/bash
# Extract the implementation of `getAssetStringParts` to review the fallback logic.

ast-grep --lang typescript --pattern 'export const getAssetStringParts = ($$) => {
  $$$
}' src/app/ui/utils/get-asset-string-parts.ts

Length of output: 136



Script:

#!/bin/bash
# Extract the implementation of `getAssetStringParts` to review the fallback logic.

rg 'export const getAssetStringParts' -A 20 src/app/ui/utils/get-asset-string-parts.ts

Length of output: 683

@alter-eggo alter-eggo force-pushed the fix/sip10-send-form branch 2 times, most recently from a6246b5 to a341107 Compare May 22, 2024 08:38
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

Review Details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits Files that changed from the base of the PR and between 1416233 and a6246b5.
Files selected for processing (6)
  • src/app/pages/send/send-crypto-asset-form/form/sip10/sip10-token-send-form.tsx (2 hunks)
  • src/app/query/stacks/fees/fees.query.ts (1 hunks)
  • src/app/store/transactions/token-transfer.hooks.ts (2 hunks)
  • src/app/ui/utils/get-asset-contract-address.ts (1 hunks)
  • src/app/ui/utils/get-asset-string-parts.ts (2 hunks)
  • src/app/ui/utils/stacks-ft-utils.spec.ts (1 hunks)
Files skipped from review as they are similar to previous changes (3)
  • src/app/pages/send/send-crypto-asset-form/form/sip10/sip10-token-send-form.tsx
  • src/app/ui/utils/get-asset-contract-address.ts
  • src/app/ui/utils/get-asset-string-parts.ts
Additional Context Used
GitHub Check Runs (1)
test-unit failure (4)

src/app/ui/utils/stacks-ft-utils.spec.ts: [failure] 12-12: src/app/ui/utils/stacks-ft-utils.spec.ts > getAssetContractAddress > should return the contract address from a fully qualified asset name
AssertionError: expected 'SP2KAF9RF86PVX3NEE27DFV1CQX0T4WGR41X3…' to be 'SP2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKQW2…' // Object.is equality

  • Expected
  • Received
  • SP2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKQW2S7D0
  • SP2KAF9RF86PVX3NEE27DFV1CQX0T4WGR41X3S45C

❯ src/app/ui/utils/stacks-ft-utils.spec.ts:12:34


src/app/ui/utils/stacks-ft-utils.spec.ts: [failure] 17-17: src/app/ui/utils/stacks-ft-utils.spec.ts > getAssetContractAddress > should return the contract address from a contract id
AssertionError: expected 'SP2KAF9RF86PVX3NEE27DFV1CQX0T4WGR41X3…' to be 'SP2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKQW2…' // Object.is equality

  • Expected
  • Received
  • SP2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKQW2S7D0
  • SP2KAF9RF86PVX3NEE27DFV1CQX0T4WGR41X3S45C

❯ src/app/ui/utils/stacks-ft-utils.spec.ts:17:41

Additional comments not posted (3)
src/app/ui/utils/stacks-ft-utils.spec.ts (1)

22-22: This test case correctly verifies the contract address when called with a contract address.

src/app/query/stacks/fees/fees.query.ts (1)

26-26: The addition of the priority: 2 option in the axios request configuration is a good enhancement for handling transaction fee estimations.

src/app/store/transactions/token-transfer.hooks.ts (1)

90-153: The addition of error handling and logging in useGenerateFtTokenTransferUnsignedTx enhances the robustness of the transaction generation process.

@314159265359879
Copy link
Contributor

With this latest build
image

I can select a token. It doesn't seem to load fees but defaults to fixed low fee for low, medium and high.
image

When I add an amount that is not available I get no warning about insufficient funds.

Then I can continue to review
When clicking confirm and send transaction I see
image

@314159265359879 314159265359879 linked an issue May 22, 2024 that may be closed by this pull request
@alter-eggo alter-eggo force-pushed the fix/sip10-send-form branch from a341107 to 40371b5 Compare May 22, 2024 10:39
Comment on lines -104 to -107
address: contractAddress,
contractName,
assetName,
} = getAssetStringParts(info.contractId);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed this

@alter-eggo alter-eggo requested a review from kyranjamie May 22, 2024 10:41
Copy link
Collaborator

@kyranjamie kyranjamie left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

Review Details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits Files that changed from the base of the PR and between 1416233 and 40371b5.
Files ignored due to path filters (1)
  • pnpm-lock.yaml is excluded by !pnpm-lock.yaml
Files selected for processing (5)
  • package.json (1 hunks)
  • src/app/pages/send/send-crypto-asset-form/form/sip10/sip10-token-send-form.tsx (2 hunks)
  • src/app/query/stacks/fees/fees.query.ts (1 hunks)
  • src/app/query/stacks/sip10/sip10-tokens.utils.ts (1 hunks)
  • src/app/store/transactions/token-transfer.hooks.ts (3 hunks)
Files skipped from review due to trivial changes (2)
  • package.json
  • src/app/query/stacks/fees/fees.query.ts
Files skipped from review as they are similar to previous changes (1)
  • src/app/pages/send/send-crypto-asset-form/form/sip10/sip10-token-send-form.tsx
Additional comments not posted (2)
src/app/query/stacks/sip10/sip10-tokens.utils.ts (2)

24-24: Proper use of nullish coalescing and logical reuse of isTransferableSip10Token. Well done on maintaining clean and robust code.


19-24: Refactored to use getAssetStringParts for parsing asset details. Ensure integration with other parts of the application.

Verification successful

Integration of getAssetStringParts verified. The function is defined in src/app/ui/utils/get-asset-string-parts.ts and used consistently in src/app/query/stacks/sip10/sip10-tokens.utils.ts.

Scripts Executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify integration of `getAssetStringParts` across the application.

# Test: Search for usage of `getAssetStringParts`. Expect: Consistent usage across the application.
rg --type typescript $'getAssetStringParts'

Length of output: 77



Script:

#!/bin/bash
# Description: Verify integration of `getAssetStringParts` across the application.

# Test: Search for usage of `getAssetStringParts`. Expect: Consistent usage across the application.
rg --type ts 'getAssetStringParts'

Length of output: 596

Comment on lines 86 to 158
stxAddress: account.address,
};

const postConditions = [makePostCondition(postConditionOptions)];

// (transfer (uint principal principal) (response bool uint))
const functionArgs: ClarityValue[] = [
uintCV(amountAsFractionalUnit),
standardPrincipalCVFromAddress(createAddress(account.address)),
standardPrincipalCVFromAddress(recipient),
];

if (info.hasMemo) {
functionArgs.push(memo);
}

const options = {
txData: {
txType: TransactionTypes.ContractCall,
try {
if (!account) return;

const functionName = 'transfer';
const recipient =
values && 'recipient' in values
? createAddress(values.recipient || '')
: createEmptyAddress();
const amount = values && 'amount' in values ? values.amount : 0;
const memo =
values && 'memo' in values && values.memo !== ''
? someCV(bufferCVFromString(values.memo || ''))
: noneCV();

const amountAsFractionalUnit = ftUnshiftDecimals(amount, info.decimals || 0);
const postConditionOptions = {
amount: amountAsFractionalUnit,
contractAddress,
contractAssetName,
contractName,
functionName,
functionArgs: functionArgs.map(serializeCV).map(arg => bytesToHex(arg)),
postConditions,
postConditionMode: PostConditionMode.Deny,
network,
stxAddress: account.address,
};

const postConditions = [makePostCondition(postConditionOptions)];

// (transfer (uint principal principal) (response bool uint))
const functionArgs: ClarityValue[] = [
uintCV(amountAsFractionalUnit),
standardPrincipalCVFromAddress(createAddress(account.address)),
standardPrincipalCVFromAddress(recipient),
];

if (info.hasMemo) {
functionArgs.push(memo);
}

const options = {
txData: {
txType: TransactionTypes.ContractCall,
contractAddress,
contractName,
functionName,
functionArgs: functionArgs.map(serializeCV).map(arg => bytesToHex(arg)),
postConditions,
postConditionMode: PostConditionMode.Deny,
network,
publicKey: account.stxPublicKey,
},
fee: stxToMicroStx(values?.fee || 0).toNumber(),
publicKey: account.stxPublicKey,
},
fee: stxToMicroStx(values?.fee || 0).toNumber(),
publicKey: account.stxPublicKey,
nonce: Number(values?.nonce) ?? nextNonce?.nonce,
} as const;
nonce: Number(values?.nonce) ?? nextNonce?.nonce,
} as const;

return generateUnsignedTransaction(options);
return generateUnsignedTransaction(options);
} catch (error) {
logger.error('Failed to generate unsigned transaction', error);
return;
}
},
[account, info.contractId, info.decimals, info.hasMemo, network, nextNonce?.nonce]
[
account,
info.decimals,
info.hasMemo,
network,
nextNonce?.nonce,
contractName,
contractAssetName,
contractAddress,
]
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comprehensive refactor of useGenerateFtTokenTransferUnsignedTx to include detailed error handling and dynamic construction of transaction details. Consider adding unit tests to cover these new functionalities.

Would you like assistance in setting up unit tests for this function?

@markmhendrickson
Copy link
Collaborator

Let's ship? 🚢

@314159265359879
Copy link
Contributor

Retested this version fix/sip10-send-form#40371b5, doesn't work.

  1. When I confirm and send transaction it fails with this error:
    image

  2. There doesn't seem to be a check for the amount of token
    image

@314159265359879
Copy link
Contributor

fee setting by the wallet does look correct now.

@alter-eggo alter-eggo force-pushed the fix/sip10-send-form branch from 40371b5 to 4900e5d Compare May 22, 2024 12:02
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

Review Details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits Files that changed from the base of the PR and between 40371b5 and 4900e5d.
Files ignored due to path filters (1)
  • pnpm-lock.yaml is excluded by !pnpm-lock.yaml
Files selected for processing (10)
  • package.json (1 hunks)
  • src/app/common/validation/forms/amount-validators.ts (3 hunks)
  • src/app/features/asset-list/stacks/sip10-token-asset-list/sip10-token-asset-item.tsx (1 hunks)
  • src/app/features/asset-list/stacks/sip10-token-asset-list/sip10-token-asset-list-unsupported.tsx (1 hunks)
  • src/app/features/asset-list/stacks/sip10-token-asset-list/sip10-token-asset-list.tsx (1 hunks)
  • src/app/pages/send/send-crypto-asset-form/form/sip10/sip10-token-send-form.tsx (2 hunks)
  • src/app/pages/send/send-crypto-asset-form/form/sip10/use-sip10-send-form.tsx (2 hunks)
  • src/app/query/stacks/fees/fees.query.ts (1 hunks)
  • src/app/query/stacks/sip10/sip10-tokens.utils.ts (1 hunks)
  • src/app/store/transactions/token-transfer.hooks.ts (3 hunks)
Files skipped from review due to trivial changes (3)
  • src/app/features/asset-list/stacks/sip10-token-asset-list/sip10-token-asset-list-unsupported.tsx
  • src/app/features/asset-list/stacks/sip10-token-asset-list/sip10-token-asset-list.tsx
  • src/app/pages/send/send-crypto-asset-form/form/sip10/use-sip10-send-form.tsx
Files skipped from review as they are similar to previous changes (5)
  • package.json
  • src/app/pages/send/send-crypto-asset-form/form/sip10/sip10-token-send-form.tsx
  • src/app/query/stacks/fees/fees.query.ts
  • src/app/query/stacks/sip10/sip10-tokens.utils.ts
  • src/app/store/transactions/token-transfer.hooks.ts
Additional comments not posted (3)
src/app/features/asset-list/stacks/sip10-token-asset-list/sip10-token-asset-item.tsx (1)

23-23: LGTM! The usage of spamFilter on info.tokenName is a good practice to ensure clean data display.

src/app/common/validation/forms/amount-validators.ts (2)

15-15: LGTM! The import of ftDecimals is correctly placed and used in the context of fungible token amount validation.


Line range hint 113-130: LGTM! The stacksFungibleTokenAmountValidator correctly handles decimal precision and balance checks using ftDecimals.

@alter-eggo alter-eggo force-pushed the fix/sip10-send-form branch from 4900e5d to 2ed7544 Compare May 22, 2024 12:21
Copy link
Contributor

@314159265359879 314159265359879 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@alter-eggo LGTM!

tested sending with several SIP010 tokens no issues with the latest build: fix/sip10-send-form#2ed7544

@alter-eggo alter-eggo added this pull request to the merge queue May 22, 2024
Merged via the queue into dev with commit 5903a7b May 22, 2024
28 checks passed
@alter-eggo alter-eggo deleted the fix/sip10-send-form branch May 22, 2024 14:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Sending SIP010 tokens blocked because fee doesn't load
4 participants