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 Decimals Handling #93

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from

Conversation

Mameta29
Copy link
Collaborator

@Mameta29 Mameta29 commented Feb 26, 2025

🎨 Overview

Fixed the decimal handling when transferring JPYC tokens to ensure that the intended amount is accurately sent. This fix resolves the discrepancy between the user-specified amount and the actual transfer amount on the blockchain.

🌈 Details

  • Added a DECIMALS constant to support JPYC's 18 decimal places
  • Modified the balanceOf method to convert on-chain values to human-readable format
  • Updated the transfer method to adjust user-specified values by 18 decimal places for on-chain transactions
  • Similar modifications will need to be applied to other token operation methods (transferFrom, mint, approve, etc.) in the future
  • Implemented BigInt for accurate numerical calculations in the SDK

This fix resolves the issue where attempting to send 100 JPYC previously resulted in only 0.0000000000000001 JPYC being transferred, ensuring that the intended 100 JPYC is now correctly sent.

📚 References

When transfer below code

const txHash = await jpyc.transfer({
      to: receiverAddress,
      value: Uint256.from('100'),
    });

@Mameta29 Mameta29 self-assigned this Feb 26, 2025
Comment on lines 33 to 47
/**
* Helper functions for decimal handling
*/

private fromOnChainValue(value: bigint): Uint256 {
const adjustedValue = value / BigInt(10 ** this.DECIMALS);
return Uint256.from(adjustedValue.toString());
}

private toOnChainValue(value: Uint256): Uint256 {
const rawValue = BigInt(value.toString());
const adjustedValue = rawValue * BigInt(10 ** this.DECIMALS);
return Uint256.from(adjustedValue.toString());
}

Copy link
Collaborator

Choose a reason for hiding this comment

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

こちらの処理は今後他のコントラクトの SDK でも使用できるものなので、インスタンスメソッドとしてではなく、独立したヘルパー関数として実装するのが良いと思います。

Copy link
Collaborator

Choose a reason for hiding this comment

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

a1bc406 で対応しました

Comment on lines +10 to +16
export const removeDecimals = (value: number): Uint256 => {
return toUint256(BigInt(value * DECIMALS_QUANTIFIER));
};

export const addDecimals = (value: Uint256): number => {
return Number(value.toString()) / DECIMALS_QUANTIFIER;
};
Copy link
Collaborator

Choose a reason for hiding this comment

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

decimal 変換処理を行う部分を外部に切り出し、to/from-onChainValue よりもより具体的な関数名に修正しています。
また、それぞれの引数のタイプについても適切なものにしました。

@@ -22,22 +22,22 @@ export interface IJPYC {
* @param minter - Minter address
* @returns minter allowance
*/
minterAllowance(params: { minter: Address }): Promise<Uint256>;
minterAllowance(params: { minter: Address }): Promise<number>;
Copy link
Collaborator

Choose a reason for hiding this comment

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

各種 SDK インターフェースの変更が必要になったので、そちらも修正しています。


import { toUint256, removeDecimals, addDecimals } from './math';

describe('Unit tests of toUint256()', () => {
Copy link
Collaborator

Choose a reason for hiding this comment

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

今回追加した関数のユニットテストも追加しました

@SeiyaKobayashi SeiyaKobayashi changed the title fix decimal handling in transfers and balance Dix Decimals Handling Mar 1, 2025
@SeiyaKobayashi SeiyaKobayashi changed the title Dix Decimals Handling Fix Decimals Handling Mar 1, 2025
@SeiyaKobayashi SeiyaKobayashi self-assigned this Mar 1, 2025
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.

2 participants