Skip to content

Commit

Permalink
Merging the develop branch into the main branch, v5.5.0
Browse files Browse the repository at this point in the history
Prefixes map (#172), fixes (#174, #175, #179, #180)
  • Loading branch information
EvgenKor authored Jan 10, 2024
2 parents 5fef4f4 + f0d613e commit 9086fa6
Show file tree
Hide file tree
Showing 12 changed files with 311 additions and 76 deletions.
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "zkbob-client-js",
"version": "5.4.0",
"version": "5.5.0",
"description": "zkBob integration library",
"repository": "[email protected]:zkBob/libzkbob-client-js.git",
"author": "Dmitry Vdovin <[email protected]>",
Expand Down Expand Up @@ -30,8 +30,8 @@
"graphql": "16.7.1",
"hdwallet-babyjub": "^0.0.2",
"idb": "^7.0.0",
"libzkbob-rs-wasm-web": "1.5.0",
"libzkbob-rs-wasm-web-mt": "1.5.0",
"libzkbob-rs-wasm-web": "1.6.0",
"libzkbob-rs-wasm-web-mt": "1.6.0",
"promise-throttle": "^1.1.2",
"regenerator-runtime": "^0.13.9",
"tronweb": "^5.3.0",
Expand Down
66 changes: 66 additions & 0 deletions src/address-prefixes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import { ZkAddressPrefix } from "./config"

export const GENERIC_ADDRESS_PREFIX = 'zkbob';
export const PREFIXED_ADDR_REGEX: RegExp = /^[a-zA-Z][a-zA-Z0-9+_\-\,@&]+:([1-9A-HJ-NP-Za-km-z]{62,63})$/;
export const NAKED_ADDR_REGEX: RegExp = /^([1-9A-HJ-NP-Za-km-z]{62,63})$/;


export const hardcodedPrefixes: ZkAddressPrefix[] = [
// Production address prefixes
{
poolId: 0x000000,
prefix: 'zkbob_polygon',
name: 'USDC on Polygon'
},
{
poolId: 0x000001,
prefix: 'zkbob_optimism',
name: 'USDC on Optimism'
},
{
poolId: 0x000002,
prefix: 'zkbob_optimism_eth',
name: 'WETH on Optimism'
},
{
poolId: 0x000003,
prefix: 'zkbob_tron',
name: 'USDT on Tron'
},
// Staging address prefixes
{
poolId: 0x000000,
prefix: 'zkbob_sepolia',
name: 'BOB on Sepolia testnet'
},
{
poolId: 0xffff02,
prefix: 'zkbob_goerli',
name: 'BOB on Goerli testnet'
},
{
poolId: 0xffff03,
prefix: 'zkbob_goerli_optimism',
name: 'BOB on Goerli Optimism testnet'
},
{
poolId: 0xffff04,
prefix: 'zkbob_goerli_eth',
name: 'WETH on Goerli testnet'
},
{
poolId: 0xffff05,
prefix: 'zkbob_goerli_usdc',
name: 'USDC on Goerli testnet'
},
{
poolId: 0xffff06,
prefix: 'zkbob_shasta',
name: 'USDT on Shasta testnet'
},
{
poolId: 0xffff07,
prefix: 'zkbob_nile',
name: 'USDT on Nile testnet'
},
];
55 changes: 41 additions & 14 deletions src/client-provider.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Chains, ProverMode, Pool, Pools } from "./config";
import { Chains, ProverMode, Pool, Pools, ZkAddressPrefix } from "./config";
import { InternalError } from "./errors";
import { NetworkBackendFactory } from "./networks";
import { NetworkType } from "./network-type";
Expand All @@ -10,14 +10,14 @@ import { ColdStorageConfig } from "./coldstorage";
import { bufToHex, HexStringReader, HexStringWriter, hexToBuf, truncateHexPrefix } from "./utils";
import { RegularTxType } from "./tx";
import { ZkBobSubgraph } from "./subgraph";
import { hardcodedPrefixes } from "./address-prefixes";

const bs58 = require('bs58')

const LIB_VERSION = require('../package.json').version;

const RELAYER_FEE_LIFETIME = 30; // when to refetch the relayer fee (in seconds)
const NATIVE_AMOUNT_LIFETIME = 3600; // when to refetch the max supported swap amount (in seconds)
const DEFAULT_RELAYER_FEE = BigInt(100000000);
const MIN_TX_AMOUNT = BigInt(50000000);
const GIFT_CARD_CODE_VER = 1;

Expand Down Expand Up @@ -97,13 +97,20 @@ export class ZkBobProvider {
private relayerFee: { [name: string]: RelayerFeeFetch } = {};
private maxSwapAmount: { [name: string]: MaxSwapAmountFetch } = {};
private coldStorageCfg: { [name: string]: ColdStorageConfig } = {};
protected addressPrefixes: ZkAddressPrefix[] = [];
protected supportId: string | undefined;

// The current pool alias should always be set
protected curPool: string;

// public constructor
constructor(pools: Pools, chains: Chains, currentPool: string, supportId: string | undefined) {
constructor(
pools: Pools,
chains: Chains,
currentPool: string,
extraPrefixes: ZkAddressPrefix[],
supportId: string | undefined
) {
this.supportId = supportId;

for (const [chainId, chain] of Object.entries(chains)) {
Expand Down Expand Up @@ -156,6 +163,19 @@ export class ZkBobProvider {
throw new InternalError(`Cannot initialize with the unknown current pool (${currentPool})`);
}
this.curPool = currentPool;

this.addressPrefixes.push(...hardcodedPrefixes);
const existingPoolIds = this.addressPrefixes.map((val) => val.poolId);
const existingAddrPrefs = this.addressPrefixes.map((val) => val.prefix.toLowerCase());
for (const aPrefix of extraPrefixes) {
if (existingPoolIds.includes(aPrefix.poolId)) {
console.warn(`Address prefix for the pool id ${aPrefix.poolId} already exist. Ignoring ${aPrefix.poolId} -> ${aPrefix.prefix}`);
} else if (existingAddrPrefs.includes(aPrefix.prefix.toLowerCase())) {
console.warn(`Address prefix ${aPrefix.prefix} already exist. Ignoring ${aPrefix.poolId} -> ${aPrefix.prefix}`);
} else {
this.addressPrefixes.push(aPrefix, )
}
}
}

// --------------=========< Configuration properties >=========----------------
Expand Down Expand Up @@ -338,6 +358,21 @@ export class ZkBobProvider {
return undefined;
}

protected async addressPrefix(): Promise<ZkAddressPrefix> {
const poolId = await this.poolId();
const pref = this.addressPrefixes.filter((val) => val.poolId == poolId);
if (pref.length > 0) {
// Polygon and Sepolia pools share the same pool id (0)
// So we should select proper address prefix here
if (poolId == 0 && pref.length > 1 && this.pool().chainId == 11155111) {
return pref[1];
}
return pref[0];
}

throw new InternalError(`The current pool (id = 0x${poolId.toString(16)}) has no configured address prefix`);
}

// -------------=========< Converting Amount Routines >=========---------------
// | Between wei and pool resolution |
// ----------------------------------------------------------------------------
Expand Down Expand Up @@ -381,17 +416,9 @@ export class ZkBobProvider {
public async getRelayerFee(): Promise<RelayerFee> {
let cachedFee = this.relayerFee[this.curPool];
if (!cachedFee || cachedFee.timestamp + RELAYER_FEE_LIFETIME * 1000 < Date.now()) {
try {
const fee = await this.relayer().fee();
cachedFee = {fee, timestamp: Date.now()};
this.relayerFee[this.curPool] = cachedFee;
} catch (err) {
const res = this.relayerFee[this.curPool]?.fee ??
{fee: DEFAULT_RELAYER_FEE, oneByteFee: 0n};
console.error(`Cannot fetch relayer fee, will using default (${res.fee} per tx, ${res.oneByteFee} per byte): ${err}`);

return res;
}
const fee = await this.relayer().fee();
cachedFee = {fee, timestamp: Date.now()};
this.relayerFee[this.curPool] = cachedFee;
}

return cachedFee.fee;
Expand Down
Loading

0 comments on commit 9086fa6

Please sign in to comment.