Skip to content

Commit

Permalink
Support of USDC pool migrated from BOB (#147)
Browse files Browse the repository at this point in the history
  • Loading branch information
EvgenKor authored Jul 10, 2023
1 parent 2505fd3 commit 482a660
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
13 changes: 10 additions & 3 deletions src/client-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,10 @@ export class ZkBobProvider {
try {
const pool = this.pool();
denominator = await this.network().getDenominator(pool.poolAddress);
const negFlag = 1n << 255n;
if (denominator & negFlag) {
denominator = -(denominator ^ negFlag);
}
this.poolDenominators[this.curPool] = denominator;
} catch (err) {
console.error(`Cannot fetch denominator value from the pool contract: ${err}`);
Expand Down Expand Up @@ -327,20 +331,23 @@ export class ZkBobProvider {
// Convert native pool amount to the base units
public async shieldedAmountToWei(amountShielded: bigint): Promise<bigint> {
const denominator = await this.denominator();
return amountShielded * denominator;
return denominator > 0 ? amountShielded * denominator : amountShielded / (-denominator);
}

// Convert base units to the native pool amount
public async weiToShieldedAmount(amountWei: bigint): Promise<bigint> {
const denominator = await this.denominator();
return amountWei / denominator;
return denominator > 0 ? amountWei / denominator : amountWei * (-denominator);
}

// Round up the fee if needed with fixed fee decimal places (after point)
protected async roundFee(fee: bigint): Promise<bigint> {
const feeDecimals = this.pool().feeDecimals;
if (feeDecimals !== undefined) {
const denomLog = (await this.denominator()).toString().length - 1;
const denominator = await this.denominator();
const denomLog = denominator > 0 ?
denominator.toString().length - 1 :
-((-denominator).toString().length - 1);
const poolResDigits = (await this.decimals()) - denomLog;
if (poolResDigits > feeDecimals) {
const rounder = 10n ** BigInt(poolResDigits - feeDecimals);
Expand Down
12 changes: 7 additions & 5 deletions src/ephemeral.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export class EphemeralPool {
private web3: Web3;
private token: Contract;
private rpcUrl: string;
private poolDenominator: bigint; // we represent all amounts in that library as in pool (Gwei currently)
private poolDenominator: bigint; // we represent all amounts in that library as in pool

// save last scanned address to decrease scan time
private startScanIndex = 0;
Expand Down Expand Up @@ -333,18 +333,20 @@ export class EphemeralPool {
return existing;
}

// in pool dimension (Gwei)
// in pool dimension
private async getNativeBalance(address: string): Promise<bigint> {
const result = await this.web3.eth.getBalance(address);

return BigInt(result) / this.poolDenominator;
return BigInt(result);
}

// in pool dimension (Gwei)
// in pool dimension
private async getTokenBalance(address: string): Promise<bigint> {
const result = await this.token.methods.balanceOf(address).call();

return BigInt(result) / this.poolDenominator;
return this.poolDenominator > 0 ?
BigInt(result) / this.poolDenominator :
BigInt(result) * (-this.poolDenominator);
}

// number of outgoing transfers via permit
Expand Down

0 comments on commit 482a660

Please sign in to comment.