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 3 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
51 changes: 38 additions & 13 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 All @@ -29,6 +30,21 @@ export class JPYC implements IJPYC {
});
}

/**
* 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 で対応しました

/**
* View Functions
*/
Expand All @@ -42,25 +58,25 @@ export class JPYC implements IJPYC {
async minterAllowance(params: { minter: Address }): Promise<Uint256> {
const resp = await this.contract.read.minterAllowance([params.minter]);

return Uint256.from((resp as bigint).toString());
return this.fromOnChainValue(resp as bigint);
}

async totalSupply(): Promise<Uint256> {
const resp = await this.contract.read.totalSupply();

return Uint256.from((resp as bigint).toString());
return this.fromOnChainValue(resp as bigint);
}

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

return Uint256.from((resp as bigint).toString());
return this.fromOnChainValue(resp as bigint);
}

async allowance(params: { owner: Address; spender: Address }): Promise<Uint256> {
const resp = await this.contract.read.allowance([params.owner, params.spender]);

return Uint256.from((resp as bigint).toString());
return this.fromOnChainValue(resp as bigint);
}

async nonces(params: { owner: Address }): Promise<Uint256> {
Expand All @@ -86,7 +102,8 @@ export class JPYC implements IJPYC {
}

async mint(params: { to: Address; amount: Uint256 }): Promise<Hash> {
const args = [params.to, params.amount];
const adjustedAmount = this.toOnChainValue(params.amount);
const args = [params.to, adjustedAmount];

try {
await this.contract.simulate.mint(args);
Expand All @@ -98,7 +115,8 @@ export class JPYC implements IJPYC {
}

async transfer(params: { to: Address; value: Uint256 }): Promise<Hash> {
const args = [params.to, params.value];
const adjustedValue = this.toOnChainValue(params.value);
const args = [params.to, adjustedValue];

try {
await this.contract.simulate.transfer(args);
Expand All @@ -110,7 +128,8 @@ export class JPYC implements IJPYC {
}

async transferFrom(params: { from: Address; to: Address; value: Uint256 }): Promise<Hash> {
const args = [params.from, params.to, params.value];
const adjustedValue = this.toOnChainValue(params.value);
const args = [params.from, params.to, adjustedValue];

try {
await this.contract.simulate.transferFrom(args);
Expand All @@ -132,10 +151,11 @@ export class JPYC implements IJPYC {
r: Bytes32;
s: Bytes32;
}): Promise<Hash> {
const adjustedValue = this.toOnChainValue(params.value);
const args = [
params.from,
params.to,
params.value,
adjustedValue,
params.validAfter,
params.validBefore,
params.nonce,
Expand Down Expand Up @@ -164,10 +184,11 @@ export class JPYC implements IJPYC {
r: Bytes32;
s: Bytes32;
}): Promise<Hash> {
const adjustedValue = this.toOnChainValue(params.value);
const args = [
params.from,
params.to,
params.value,
adjustedValue,
params.validAfter,
params.validBefore,
params.nonce,
Expand Down Expand Up @@ -204,7 +225,8 @@ export class JPYC implements IJPYC {
}

async approve(params: { spender: Address; value: Uint256 }): Promise<Hash> {
const args = [params.spender, params.value];
const adjustedValue = this.toOnChainValue(params.value);
const args = [params.spender, adjustedValue];

try {
await this.contract.simulate.approve(args);
Expand All @@ -216,7 +238,8 @@ export class JPYC implements IJPYC {
}

async increaseAllowance(params: { spender: Address; increment: Uint256 }): Promise<Hash> {
const args = [params.spender, params.increment];
const adjustedIncrement = this.toOnChainValue(params.increment);
const args = [params.spender, adjustedIncrement];

try {
await this.contract.simulate.increaseAllowance(args);
Expand All @@ -228,7 +251,8 @@ export class JPYC implements IJPYC {
}

async decreaseAllowance(params: { spender: Address; decrement: Uint256 }): Promise<Hash> {
const args = [params.spender, params.decrement];
const adjustedDecrement = this.toOnChainValue(params.decrement);
const args = [params.spender, adjustedDecrement];

try {
await this.contract.simulate.decreaseAllowance(args);
Expand All @@ -248,10 +272,11 @@ export class JPYC implements IJPYC {
r: Bytes32;
s: Bytes32;
}): Promise<Hash> {
const adjustedValue = this.toOnChainValue(params.value);
const args = [
params.owner,
params.spender,
params.value,
adjustedValue,
params.deadline,
params.v,
params.r,
Expand Down