Skip to content

Commit

Permalink
Fix npm packages to be used externally (zeta-chain#47)
Browse files Browse the repository at this point in the history
* Fix npm packages to be used in external

* add babel and type files

* update readme file

* Update packages/addresses/README.md

Co-authored-by: Lucas <[email protected]>

* Update packages/addresses/README.md

Co-authored-by: Lucas <[email protected]>

* update readme

* move to vite

* update version

* Update packages/addresses/.npmignore

Co-authored-by: Lucas <[email protected]>

* Update packages/addresses/.npmignore

Co-authored-by: Lucas <[email protected]>

* Update packages/addresses/tsconfig.json

Co-authored-by: Lucas <[email protected]>

* update errors

* Update packages/addresses/package.json

Co-authored-by: Lucas <[email protected]>

Co-authored-by: Lucas <[email protected]>
  • Loading branch information
andresaiello and lucas-janon authored Sep 7, 2022
1 parent cc3bbc1 commit fbc563d
Show file tree
Hide file tree
Showing 45 changed files with 1,007 additions and 216 deletions.
24 changes: 24 additions & 0 deletions packages/addresses/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# source code
src
index.html

# compiler
tsconfig.json

# files
*.log
cypress.json
*.code-workspace

# tests
test

# lint
.eslintrc.js
.prettierrc

# IDE - VSCode
.vscode

# git
.git
23 changes: 23 additions & 0 deletions packages/addresses/README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,26 @@
# ZetaChain addresses

This package includes the addresses and networks to use Zetachain.

## Usage

```js
import { getAddress } from "@zetachain/addresses";

const address = getAddress({ address: "zetaToken", networkName: "goerli", zetaNetwork:"athens" });
```

## API


| Method | Description |
| :---- | ------ |
| isTestnetNetworkName = (networkName: string): networkName is TestnetNetworkName | Returns true if it's a valid Testnet name |
| isZetaTestnet = (networkName: string): networkName is ZetaTestnetNetworkName | Returns true if it's a valid ZetaTestnet name |
| isMainnetNetworkName = (networkName: string): networkName is MainnetNetworkName | Returns true if it's a valid Mainnet name |
| isNetworkName = (networkName: string): networkName is NetworkName | Returns true if it's a valid network name |
| isZetaNetworkName = (networkName: string): networkName is ZetaNetworkName | Returns true if it's a valid Zeta network name |
| type ZetaAddress | Valid values for ZetaAddress |
| getAddress = ({ address: ZetaAddress; networkName: string; zetaNetwork: string; }): string | Returns the address of a valid ZetaAddress |

```
5 changes: 0 additions & 5 deletions packages/addresses/hardhat.config.ts

This file was deleted.

22 changes: 19 additions & 3 deletions packages/addresses/package.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,30 @@
{
"name": "@zetachain/addresses",
"version": "0.0.4",
"version": "0.0.8",
"license": "MIT",
"author": "zetachain",
"publishConfig": {
"access": "public",
"registry": "https://registry.npmjs.org/"
},
"main": "src/index.ts",
"main": "./dist/zetachain-addresses.umd.js",
"module": "./dist/zetachain-addresses.mjs",
"types": "./dist/index.d.ts",
"files": [
"dist"
],
"exports": {
".": {
"import": "./dist/zetachain-addresses.mjs",
"require": "./dist/zetachain-addresses.umd.js"
}
},
"scripts": {
"tsc:watch": "npx tsc --watch"
"tsc:watch": "npx tsc --watch",
"prepublishOnly": "vite build"
},
"devDependencies": {
"vite": "^3.1.0",
"vite-plugin-dts": "^1.4.1"
}
}
54 changes: 24 additions & 30 deletions packages/addresses/src/addresses.helpers.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { network } from "hardhat";

import athens from "./addresses.athens.json";
import mainnet from "./addresses.mainnet.json";
import troy from "./addresses.troy.json";
Expand Down Expand Up @@ -134,38 +132,34 @@ export const isNetworkName = (str: string): str is NetworkName =>
export const isZetaNetworkName = (str: string): str is ZetaNetworkName =>
isZetaLocalnet(str) || isZetaTestnet(str) || isZetaMainnet(str);

const MissingZetaNetworkError = new Error(
"ZETA_NETWORK is not defined, please set the environment variable (e.g.: ZETA_NETWORK=athens <command>)"
);

export const getAddress = (
address: ZetaAddress,
{
customNetworkName,
customZetaNetwork
}: { customNetworkName?: NetworkName; customZetaNetwork?: ZetaNetworkName } = {}
): string => {
const { name: _networkName } = network;
const networkName = customNetworkName || _networkName;

const { ZETA_NETWORK: _ZETA_NETWORK } = process.env;
const ZETA_NETWORK = customZetaNetwork || _ZETA_NETWORK;

if (!ZETA_NETWORK) throw MissingZetaNetworkError;

console.log(`Getting ${address} address from ${ZETA_NETWORK}: ${networkName}.`);

if (isZetaLocalnet(ZETA_NETWORK) && isLocalNetworkName(networkName)) {
return getLocalnetList()[ZETA_NETWORK][networkName][address];
const getInvalidNetworkError = (network: string, isZeta: boolean) =>
new Error(`Network: ${network} is invalid${isZeta ? " ZetaNetwork" : ""}, please provide a valid value`);

export const getAddress = ({
address,
networkName,
zetaNetwork
}: {
address: ZetaAddress;
networkName: string;
zetaNetwork: string;
}): string => {
if (!isNetworkName(networkName)) throw getInvalidNetworkError(networkName, false);
if (!isZetaNetworkName(zetaNetwork)) throw getInvalidNetworkError(networkName, true);

console.log(`Getting ${address} address from ${zetaNetwork}: ${networkName}.`);

if (isZetaLocalnet(zetaNetwork) && isLocalNetworkName(networkName)) {
return getLocalnetList()[zetaNetwork][networkName][address];
}

if (isZetaTestnet(ZETA_NETWORK) && isTestnetNetworkName(networkName)) {
return getTestnetList()[ZETA_NETWORK][networkName][address];
if (isZetaTestnet(zetaNetwork) && isTestnetNetworkName(networkName)) {
return getTestnetList()[zetaNetwork][networkName][address];
}

if (isZetaMainnet(ZETA_NETWORK) && isMainnetNetworkName(networkName)) {
return getMainnetList()[ZETA_NETWORK][networkName][address];
if (isZetaMainnet(zetaNetwork) && isMainnetNetworkName(networkName)) {
return getMainnetList()[zetaNetwork][networkName][address];
}

throw new Error(`Invalid ZETA_NETWORK + network combination ${ZETA_NETWORK} ${networkName}.`);
throw new Error(`Invalid ZETA_NETWORK + network combination ${zetaNetwork} ${networkName}.`);
};
15 changes: 15 additions & 0 deletions packages/addresses/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"compilerOptions": {
"typeRoots": ["@types", "./node_modules/@types"],
"target": "es6",
"noImplicitAny": true,
"moduleResolution": "node",
"sourceMap": false,
"outDir": "dist",
"esModuleInterop": true,
"baseUrl": "./",
"declaration": true,
"module": "commonjs",
"resolveJsonModule": true,
}
}
20 changes: 20 additions & 0 deletions packages/addresses/vite.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// vite.config.js
import { resolve } from "path";
import { defineConfig } from "vite";
import dts from "vite-plugin-dts";

export default defineConfig({
build: {
lib: {
entry: resolve(__dirname, "./src/index.ts"),
// the proper extensions will be added
fileName: "zetachain-addresses",
name: "ZetachainAddresses"
}
},
plugins: [
dts({
insertTypesEntry: true
})
]
});
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { getAddress } from "@zetachain/addresses";
import assert from "assert";
import { ContractFactory } from "ethers";
import { ethers, network } from "hardhat";
Expand All @@ -9,8 +8,9 @@ import {
CrossChainWarriorsMock,
CrossChainWarriorsMock__factory as CrossChainWarriorsMockFactory,
CrossChainWarriorsZetaConnectorMock,
CrossChainWarriorsZetaConnectorMock__factory as CrossChainWarriorsZetaConnectorMockFactory,
CrossChainWarriorsZetaConnectorMock__factory as CrossChainWarriorsZetaConnectorMockFactory
} from "../../typechain-types";
import { getAddress } from "../shared/address.helpers";
import { isNetworkName } from "../shared/network.constants";

export type GetContractParams<Factory extends ContractFactory> =
Expand All @@ -30,7 +30,7 @@ export const deployCrossChainWarriorsMock = async ({
customUseEven,
zetaConnectorMockAddress,
zetaTokenMockAddress,
zetaTokenConsumerAddress,
zetaTokenConsumerAddress
}: {
customUseEven: boolean;
zetaConnectorMockAddress: string;
Expand Down Expand Up @@ -61,7 +61,7 @@ export const getCrossChainWarriorsArgs = (): [string, string, string, boolean] =
getAddress("connector"),
getAddress("zetaToken"),
getAddress("zetaTokenConsumerUniV2"),
network.name === "goerli",
network.name === "goerli"
];

export const getCrossChainWarriors = async (existingContractAddress?: string) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { getAddress } from "@zetachain/addresses";
import { ZetaEth, ZetaEth__factory as ZetaEthFactory } from "@zetachain/interfaces/typechain-types";
import assert from "assert";
import { ethers, network } from "hardhat";
Expand All @@ -10,15 +9,16 @@ import {
MultiChainValueMock,
MultiChainValueMock__factory as MultiChainValueMockFactory,
ZetaConnectorMockValue,
ZetaConnectorMockValue__factory as ZetaConnectorMockValueFactory,
ZetaConnectorMockValue__factory as ZetaConnectorMockValueFactory
} from "../../typechain-types";
import { getAddress } from "../shared/address.helpers";

/**
* @description only for testing or local environment
*/
export const deployMultiChainValueMock = async ({
zetaConnectorMockAddress,
zetaTokenMockAddress,
zetaTokenMockAddress
}: {
zetaConnectorMockAddress: string;
zetaTokenMockAddress: string;
Expand All @@ -44,7 +44,7 @@ export const getMultiChainValue = (existingContractAddress?: string) =>
contractName: "MultiChainValue",
...(existingContractAddress
? { existingContractAddress }
: { deployParams: [getAddress("connector"), getAddress("zetaToken")] }),
: { deployParams: [getAddress("connector"), getAddress("zetaToken")] })
});

export const deployZetaConnectorMock = async () => {
Expand Down
23 changes: 23 additions & 0 deletions packages/example-contracts/lib/shared/address.helpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { getAddress as getAddressLib, NetworkName, ZetaAddress, ZetaNetworkName } from "@zetachain/addresses";
import { network } from "hardhat";

const MissingZetaNetworkError = new Error(
"ZETA_NETWORK is not defined, please set the environment variable (e.g.: ZETA_NETWORK=athens <command>)"
);

export const getAddress = (
address: ZetaAddress,
{
customNetworkName,
customZetaNetwork
}: { customNetworkName?: NetworkName; customZetaNetwork?: ZetaNetworkName } = {}
): string => {
const { name: _networkName } = network;
const networkName = customNetworkName || _networkName;

const { ZETA_NETWORK: _ZETA_NETWORK } = process.env;
const zetaNetwork = customZetaNetwork || _ZETA_NETWORK;

if (!zetaNetwork) throw MissingZetaNetworkError;
return getAddressLib({ address, networkName, zetaNetwork });
};
19 changes: 10 additions & 9 deletions packages/example-contracts/lib/shared/deploy.helpers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getAddress, NetworkName, ZetaAddress, ZetaNetworkName } from "@zetachain/addresses";
import { NetworkName, ZetaAddress, ZetaNetworkName } from "@zetachain/addresses";
import { getScanVariable } from "@zetachain/addresses-tools";
import { execSync } from "child_process";
import { BaseContract, ContractFactory } from "ethers";
Expand All @@ -12,8 +12,9 @@ import {
ZetaEthMock,
ZetaEthMock__factory as ZetaEthMockFactory,
ZetaTokenConsumerUniV2,
ZetaTokenConsumerUniV2__factory,
ZetaTokenConsumerUniV2__factory
} from "../../typechain-types";
import { getAddress } from "../shared/address.helpers";

export type GetContractParams<Factory extends ContractFactory> =
| {
Expand All @@ -28,7 +29,7 @@ export type GetContractParams<Factory extends ContractFactory> =
export const getContract = async <Factory extends ContractFactory, Contract extends BaseContract>({
contractName,
deployParams,
existingContractAddress,
existingContractAddress
}: GetContractParams<Factory> & { contractName: string }): Promise<Contract> => {
const ContractFactory = (await ethers.getContractFactory(contractName)) as Factory;

Expand All @@ -47,13 +48,13 @@ export const getContract = async <Factory extends ContractFactory, Contract exte
export const getErc20 = async (existingContractAddress?: string) =>
getContract<ERC20Factory, ERC20>({
contractName: "ERC20",
...(existingContractAddress ? { existingContractAddress } : { deployParams: ["ERC20Mock", "ERC20Mock"] }),
...(existingContractAddress ? { existingContractAddress } : { deployParams: ["ERC20Mock", "ERC20Mock"] })
});

export const getZetaMock = async () =>
getContract<ZetaEthMockFactory, ZetaEthMock>({
contractName: "ZetaEthMock",
deployParams: ["10000000"],
deployParams: ["10000000"]
});

export const getNow = async () => {
Expand All @@ -66,15 +67,15 @@ export const getUniswapV2Router02 = async () =>
contractName: "UniswapV2Router02",
existingContractAddress: getAddress("uniswapV2Router02", {
customNetworkName: "eth-mainnet",
customZetaNetwork: "mainnet",
}),
customZetaNetwork: "mainnet"
})
});

export const verifyContract = (
addressName: ZetaAddress,
{
customNetworkName,
customZetaNetwork,
customZetaNetwork
}: { customNetworkName?: NetworkName; customZetaNetwork?: ZetaNetworkName } = {}
) => {
const ZETA_NETWORK = process.env.ZETA_NETWORK || customZetaNetwork;
Expand All @@ -91,5 +92,5 @@ export const verifyContract = (
export const deployZetaTokenConsumerUniV2 = async (zetaToken_: string, uniswapV2Router_: string) =>
getContract<ZetaTokenConsumerUniV2__factory, ZetaTokenConsumerUniV2>({
contractName: "ZetaTokenConsumerUniV2",
...{ deployParams: [zetaToken_, uniswapV2Router_] },
...{ deployParams: [zetaToken_, uniswapV2Router_] }
});
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import { MaxUint256 } from "@ethersproject/constants";
import { parseUnits } from "@ethersproject/units";
import { SignerWithAddress } from "@nomiclabs/hardhat-ethers/signers";
import { getAddress } from "@zetachain/addresses";
import { BigNumber } from "ethers";
import { ethers } from "hardhat";

import { getAddress } from "../lib/shared/address.helpers";
import { getContract } from "../lib/shared/deploy.helpers";
import {
ERC20__factory,
IUniswapV2Factory__factory,
IUniswapV2Pair__factory,
IUniswapV2Router02,
UniswapV2Router02,
UniswapV2Router02__factory,
UniswapV2Router02__factory
} from "../typechain-types";

const UNISWAP_FACTORY_ADDRESS = "0xb7926c0430afb07aa7defde6da862ae0bde767bc";
Expand Down Expand Up @@ -88,7 +88,7 @@ const estimateEthForZeta = async (
export const getUniswapV2Router02 = async () =>
getContract<UniswapV2Router02__factory, UniswapV2Router02>({
contractName: "UniswapV2Router02",
existingContractAddress: getAddress("uniswapV2Router02"),
existingContractAddress: getAddress("uniswapV2Router02")
});

async function main() {
Expand All @@ -108,7 +108,7 @@ async function main() {

main()
.then(() => process.exit(0))
.catch((error) => {
.catch(error => {
console.error(error);
process.exit(1);
});
Loading

0 comments on commit fbc563d

Please sign in to comment.