ethers/lib/utils#hexZeroPad TypeScript Examples

The following examples show how to use ethers/lib/utils#hexZeroPad. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: transfer.ts    From hubble-contracts with MIT License 6 votes vote down vote up
serialize(): string {
        const concated = concat([
            hexZeroPad(hexlify(this.fromIndex), StateIDLen),
            hexZeroPad(hexlify(this.toIndex), StateIDLen),
            float16.compress(this.amount),
            float16.compress(this.fee)
        ]);
        return hexlify(concated);
    }
Example #2
Source File: transfer.ts    From hubble-contracts with MIT License 6 votes vote down vote up
serialize(): string {
        if (!this.signature) throw new Error("Signature must be assigned");
        const concated = concat([
            hexZeroPad(hexlify(this.fromIndex), StateIDLen),
            hexZeroPad(hexlify(this.toIndex), StateIDLen),
            float16.compress(this.amount),
            float16.compress(this.fee),
            hexZeroPad(hexlify(this.nonce), StateIDLen),
            dumpG1(this.signature?.sol)
        ]);
        return hexlify(concated);
    }
Example #3
Source File: decimal.ts    From hubble-contracts with MIT License 6 votes vote down vote up
public compress(input: BigNumberish): string {
        let mantissa = BigNumber.from(input.toString());
        let exponent = 0;
        for (; exponent < this.exponentMax; exponent++) {
            if (mantissa.isZero() || !mantissa.mod(10).isZero()) break;
            mantissa = mantissa.div(10);
        }
        if (mantissa.gt(this.mantissaMax))
            throw new EncodingError(
                `Cannot compress ${input}, expect mantissa ${mantissa} <= ${this.mantissaMax}`
            );

        const hex = BigNumber.from(exponent)
            .shl(this.mantissaBits)
            .add(mantissa)
            .toHexString();
        return hexZeroPad(hex, this.bytesLength);
    }
Example #4
Source File: tx.ts    From hubble-contracts with MIT License 6 votes vote down vote up
public encode(): string {
        const concated = concat([
            hexZeroPad(hexlify(this.fromIndex), stateIDLen),
            hexZeroPad(hexlify(this.toIndex), stateIDLen),
            float16.compress(this.amount),
            float16.compress(this.fee)
        ]);
        return hexlify(concated);
    }
Example #5
Source File: tx.ts    From hubble-contracts with MIT License 6 votes vote down vote up
public encode(): string {
        const concated = concat([
            hexZeroPad(hexlify(this.fromIndex), stateIDLen),
            hexZeroPad(hexlify(this.toIndex), stateIDLen),
            hexZeroPad(hexlify(this.toPubkeyID), stateIDLen),
            float16.compress(this.amount),
            float16.compress(this.fee)
        ]);
        return hexlify(concated);
    }
Example #6
Source File: utils.ts    From hubble-contracts with MIT License 6 votes vote down vote up
export function to32Hex(n: BigNumber): string {
    return hexZeroPad(n.toHexString(), 32);
}
Example #7
Source File: uniswapV3.ts    From index-rebalance-utils with Apache License 2.0 5 votes vote down vote up
export async function getUniswapV3Quote(deployHelper: DeployHelper, token: Address, targetPriceImpact: BigNumber, feeAmount: number = FeeAmount.MEDIUM): Promise<ExchangeQuote> {
  const factoryInstance = await deployHelper.external.getUniswapV3FactoryInstance(UNI_V3_FACTORY);
  const poolAddress = await factoryInstance.getPool(token, ETH_ADDRESS, feeAmount);
  if (poolAddress == ADDRESS_ZERO) {
    return {
      exchange: exchanges.UNISWAP_V3,
      size: ZERO.toString(),
      data: "0x",
    } as ExchangeQuote;
  }

  const poolInstance = await deployHelper.external.getUniswapV3PoolInstance(poolAddress);
  const globalStorage = await poolInstance.slot0();
  const currentSqrtPrice = globalStorage.sqrtPriceX96;

  const currentPrice = preciseDiv(BigNumber.from(2).pow(192), currentSqrtPrice.pow(2));

  if (currentPrice.eq(0)) {
    return {
      exchange: exchanges.UNISWAP_V3,
      size: ZERO.toString(),
      data: "0x",
    } as ExchangeQuote;
  }

  // This is not actually target price where targetPrice = price*(1+targetPriceImpact). It instead sets a maximum price change after trade
  // of 2 * targetPriceImpact. If you assume that the liquidity is flat accross the 2 * targetPriceImpact, then it will equal to targetPriceImpact
  // slippage. The worst-case scenario outcome for this approximation is when you move across ticks and the liquidity falls off significantly,
  // and you execute the trade a price impact of 2% instead of 1%.

  // Divide by 50: convert basis point in percent to basis points in decimal (/100) multiply by two to meet target price impact
  const targetPrice = token > ETH_ADDRESS ? preciseMul(currentPrice, ether(1).add(targetPriceImpact.div(50))) :
    preciseMul(currentPrice, ether(1).sub(targetPriceImpact.div(50)));
  const sqrtPriceLimit = sqrt(preciseDiv(BigNumber.from(2).pow(192), targetPrice));

  const quoterInstance = await deployHelper.external.getUniswapV3QuoterInstance(UNI_V3_QUOTER);

  return {
    exchange: exchanges.UNISWAP_V3,
    size:   (await quoterInstance.callStatic.quoteExactInputSingle(
      ETH_ADDRESS,
      token,
      feeAmount,
      ether(10000),
      sqrtPriceLimit
    )).toString(),
    data: hexZeroPad(hexlify(feeAmount), 3),
  } as ExchangeQuote;
}
Example #8
Source File: tx.ts    From hubble-contracts with MIT License 5 votes vote down vote up
public encode(): string {
        const concated = concat([
            hexZeroPad(hexlify(this.fromIndex), stateIDLen),
            float16.compress(this.amount),
            float16.compress(this.fee)
        ]);
        return hexlify(concated);
    }