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
Open
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions packages/core/src/jpyc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export class JPYC implements IJPYC {
process.env.SDK_ENV === 'local' ? LOCAL_PROXY_ADDRESS : V2_PROXY_ADDRESS;
private readonly contractAbi: unknown[] = JPYC_V2_ABI;
private readonly contract: GetContractReturnType<typeof this.contractAbi, WalletClient>;
private readonly DECIMALS = 18;

constructor(params: { client: WalletClient }) {
if (!isValidAddress({ address: this.contractAddress })) {
Expand Down Expand Up @@ -53,8 +54,9 @@ export class JPYC implements IJPYC {

async balanceOf(params: { account: Address }): Promise<Uint256> {
const resp = await this.contract.read.balanceOf([params.account]);

return Uint256.from((resp as bigint).toString());
const rawBalance = BigInt((resp as bigint).toString());
const adjustedBalance = rawBalance / BigInt(10 ** this.DECIMALS);
return Uint256.from(adjustedBalance.toString());
}

async allowance(params: { owner: Address; spender: Address }): Promise<Uint256> {
Expand Down Expand Up @@ -98,7 +100,9 @@ export class JPYC implements IJPYC {
}

async transfer(params: { to: Address; value: Uint256 }): Promise<Hash> {
const args = [params.to, params.value];
const rawValue = BigInt(params.value.toString());
const adjustedValue = rawValue * BigInt(10 ** this.DECIMALS);
const args = [params.to, Uint256.from(adjustedValue.toString())];

try {
await this.contract.simulate.transfer(args);
Expand Down