-
Notifications
You must be signed in to change notification settings - Fork 0
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
base: develop
Are you sure you want to change the base?
Conversation
packages/core/src/jpyc.ts
Outdated
/** | ||
* 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()); | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
こちらの処理は今後他のコントラクトの SDK でも使用できるものなので、インスタンスメソッドとしてではなく、独立したヘルパー関数として実装するのが良いと思います。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
a1bc406 で対応しました
export const removeDecimals = (value: number): Uint256 => { | ||
return toUint256(BigInt(value * DECIMALS_QUANTIFIER)); | ||
}; | ||
|
||
export const addDecimals = (value: Uint256): number => { | ||
return Number(value.toString()) / DECIMALS_QUANTIFIER; | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
decimal 変換処理を行う部分を外部に切り出し、to/from-onChainValue
よりもより具体的な関数名に修正しています。
また、それぞれの引数のタイプについても適切なものにしました。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ありがとうございます。挙動の意味をユーザーにわかるようにコメント(意味と例)をつけると良いと思いました。
例:
/**
* Converts a user-specified "integer JPYC value" into a Uint256 format that can be used on-chain.
* Example: 100 JPYC → 100000000000000000000 (multiplied by 10^18)
* @param value The numeric value provided by the user (number)
* @returns A Uint256 value with decimal places removed
*/
export const removeDecimals = (value: number): Uint256 => {
return toUint256(BigInt(value * DECIMALS_QUANTIFIER));
};
/**
* Converts the smallest on-chain unit of JPYC (wei-level integer value) into a user-friendly numeric value.
* Example: 1000000000000000000 (wei) → 1 JPYC
* @param value The Uint256 value used on-chain
* @returns A number with decimal places restored
*/
export const addDecimals = (value: Uint256): number => {
return Number(value.toString()) / DECIMALS_QUANTIFIER;
};
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
この関数は既存の JPYC コントラクトだけでなく、今後 JPYC で開発するコントラクトに対しても適用したいので、コメント内容は検討しますがコメント自体は追加しますね
@@ -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>; |
There was a problem hiding this comment.
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()', () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
今回追加した関数のユニットテストも追加しました
🎨 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
DECIMALS
constant to support JPYC's 18 decimal placesbalanceOf
method to convert on-chain values to human-readable formattransfer
method to adjust user-specified values by 18 decimal places for on-chain transactionstransferFrom
,mint
,approve
, etc.) in the futureBigInt
for accurate numerical calculations in the SDKThis 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
https://sepolia.etherscan.io/tx/0x8ce14ea33b9f2f43a89c9bf2e05ea160fa1bc335e07f972b1bda076ee6bcb7bb
https://sepolia.etherscan.io/tx/0x009c5970ff0db05a120fcb9cd2d4b004fe460d84fe7080808a067eb4497d22e7