Skip to content

Commit

Permalink
Feature: Add support for osx-configs (#324)
Browse files Browse the repository at this point in the history
* add support for osx-configs

* update for new configs

* fix comments and update yarn lock

* fix comments

* fix tests

* fix sonar

* fix sonar

* refactor getNetwork function

* fix comments

* fix: throw error when repo contract is not available

* fix: build
  • Loading branch information
josemarinas authored and banasa44 committed Feb 29, 2024
1 parent 5ba93d3 commit e64f8da
Show file tree
Hide file tree
Showing 59 changed files with 815 additions and 1,056 deletions.
21 changes: 21 additions & 0 deletions modules/client-common/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,27 @@ TEMPLATE:

## [UPCOMING]

### Added

- `getDefaultIpfsNodes` function to get the default IPFS nodes for a network
- `getDefaultGraphNodes` function to get the default Graph nodes for a network

### Changed

- Removed `LIVE_CONTRACTS` and used `contracts` from `@aragon/osx-commons-configs` instead
- Removed address getters and changed them for a generic `getAddress` function
- `ContextParams` keys for contract addresses now are the contract names:
- `daoFactoryAddress` -> `DaoFactory`
- `multisigRepoAddress` -> `MultisigRepoProxy`
- `tokenVotingRepoAddress` -> `TokenVotingRepoProxy`
- etc...
- Removed `IPFS_NODES` and `GRAPH_NODES` and used `getDefaultIpfsNodes` and `getDefaultGraphNodes` instead
- Removed `ADDITIONAL_NETWORKS`
- `SupportedNetworks` and `SupportedVersions` now are exported from `@aragon/osx-commons-configs`
-

## [1.14.1]

### Fixed
- Typo in `LIVE_CONTRACTS` for mainnet

Expand Down
3 changes: 2 additions & 1 deletion modules/client-common/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@aragon/sdk-client-common",
"author": "Aragon Association",
"version": "1.14.1",
"version": "1.15.0",
"license": "MIT",
"main": "dist/index.js",
"module": "dist/sdk-client-common.esm.js",
Expand Down Expand Up @@ -58,6 +58,7 @@
"typescript": "^5.2.2"
},
"dependencies": {
"@aragon/osx-commons-configs": "^0.2.0",
"@aragon/osx-ethers": "^1.3.1",
"@aragon/osx-ethers-v1.0.0": "npm:@aragon/[email protected]",
"@aragon/sdk-ipfs": "^1.1.0",
Expand Down
425 changes: 2 additions & 423 deletions modules/client-common/src/constants.ts

Large diffs are not rendered by default.

229 changes: 51 additions & 178 deletions modules/client-common/src/context-core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,48 +3,39 @@ import { Client as IpfsClient } from "@aragon/sdk-ipfs";
import { GraphQLClient } from "graphql-request";
import { isAddress } from "@ethersproject/address";
import { Signer } from "@ethersproject/abstract-signer";
import { ContextParams, ContextState, OverriddenState } from "./types";
import {
ContextParams,
ContextState,
OverriddenState,
SupportedNetwork,
SupportedNetworksArray,
SupportedVersion,
} from "./types";
import { GRAPHQL_NODES, IPFS_NODES, LIVE_CONTRACTS } from "./constants";
import { getNetwork } from "./utils";
import { DeployedAddressesArray } from "./internal";
getDefaultGraphqlNodes,
getDefaultIpfsNodes,
getNetwork,
} from "./utils";
import {
InvalidAddressError,
InvalidGasEstimationFactorError,
UnsupportedNetworkError,
UnsupportedProtocolError,
} from "./errors";
import {
ContractNames,
getNetworkDeploymentForVersion,
getNetworkNameByAlias,
SupportedVersions,
} from "@aragon/osx-commons-configs";

const DEFAULT_GAS_FEE_ESTIMATION_FACTOR = 0.625;
const supportedProtocols = ["https:"];
const contractNames = Object.values(ContractNames);
if (typeof process !== "undefined" && process?.env?.TESTING) {
supportedProtocols.push("http:");
}

export abstract class ContextCore {
protected state: ContextState = {} as ContextState;
protected overriden: OverriddenState = {
daoFactoryAddress: false,
pluginSetupProcessorAddress: false,
multisigRepoAddress: false,
adminRepoAddress: false,
addresslistVotingRepoAddress: false,
tokenVotingRepoAddress: false,
multisigSetupAddress: false,
adminSetupAddress: false,
addresslistVotingSetupAddress: false,
tokenVotingSetupAddress: false,
ensRegistryAddress: false,
gasFeeEstimationFactor: false,
ipfsNodes: false,
graphqlNodes: false,
};

protected overriden: OverriddenState = contractNames.reduce(
(acc, key) => ({ ...acc, [key]: false }),
{ ENSRegistry: false } as OverriddenState,
);
// INTERNAL CONTEXT STATE
/**
* @param {Object} params
Expand All @@ -61,7 +52,7 @@ export abstract class ContextCore {
if (contextParams.network) {
this.state.network = ContextCore.resolveNetwork(
contextParams.network,
contextParams.ensRegistryAddress,
contextParams.ENSRegistry,
);
// once the network is resolved set default values
this.setNetworkDefaults();
Expand Down Expand Up @@ -90,7 +81,7 @@ export abstract class ContextCore {
this.overriden.ipfsNodes = true;
}
// Set all the available addresses
for (const address of DeployedAddressesArray) {
for (const address of contractNames) {
if (contextParams[address]) {
this.state[address] = contextParams[address]!;
this.overriden[address] = true;
Expand All @@ -107,35 +98,43 @@ export abstract class ContextCore {
}

private setNetworkDefaults() {
const networkName = this.network.name as SupportedNetwork;
if (
!GRAPHQL_NODES[networkName]?.length ||
!IPFS_NODES[networkName]?.length ||
!LIVE_CONTRACTS[SupportedVersion.LATEST][networkName]
) {
throw new UnsupportedNetworkError(networkName);
// check network
const networkName = getNetworkNameByAlias(this.network.name);
if (!networkName) {
throw new UnsupportedNetworkError(this.network.name);
}

// set graphql nodes
if (!this.overriden.graphqlNodes) {
this.state.graphql = ContextCore.resolveGraphql(
GRAPHQL_NODES[networkName],
getDefaultGraphqlNodes(networkName),
);
}

// set ipfs nodes
if (!this.overriden.ipfsNodes) {
this.state.ipfs = ContextCore.resolveIpfs(IPFS_NODES[networkName]);
this.state.ipfs = ContextCore.resolveIpfs(
getDefaultIpfsNodes(networkName),
);
}

for (const address of DeployedAddressesArray) {
if (!this.overriden[address]) {
let defaultAddress =
LIVE_CONTRACTS[SupportedVersion.LATEST][networkName][address];
// set contract addresses
for (const contractName of contractNames) {
if (!this.overriden[contractName]) {
let contractAddress: string | undefined;
// get deployment
let deployment = getNetworkDeploymentForVersion(
networkName,
SupportedVersions.V1_3_0,
);
// get address from deployment
if (deployment) {
contractAddress = deployment[contractName]?.address;
}
// custom check for ensRegistryAddress
if (address === "ensRegistryAddress" && !defaultAddress) {
defaultAddress = this.network.ensAddress;
// set the ensRegistryAddress to the network.ensAddress
if (contractName === ContractNames.ENS_REGISTRY && !contractAddress) {
contractAddress = this.network.ensAddress;
}
if (defaultAddress) {
this.state[address] = defaultAddress;
if (contractAddress) {
this.state[contractName] = contractAddress;
}
}
}
Expand All @@ -159,19 +158,6 @@ export abstract class ContextCore {
get network() {
return this.state.network;
}
/**
* Getter for the Signer
*
* @var signer
*
* @returns {Signer}
*
* @public
*/
get ensRegistryAddress(): string | undefined {
return this.state.ensRegistryAddress;
}

/**
* Getter for the Signer
*
Expand All @@ -198,109 +184,6 @@ export abstract class ContextCore {
return this.state.web3Providers || [];
}

/**
* Getter for daoFactoryAddress property
*
* @var daoFactoryAddress
*
* @returns {string}
*
* @public
*/
get daoFactoryAddress(): string {
return this.state.daoFactoryAddress;
}

/**
* Getter for pluginSetupProcessorAddress property
* @var pluginSetupProcessorAddress
* @returns {string}
* @public
*/
get pluginSetupProcessorAddress(): string {
return this.state.pluginSetupProcessorAddress;
}
/**
* Getter for multisigRepoAddress property
*
* @readonly
* @type {string}
* @memberof ContextCore
*/
get multisigRepoAddress(): string {
return this.state.multisigRepoAddress;
}
/**
* Getter for adminRepoAddress property
*
* @readonly
* @type {string}
* @memberof ContextCore
*/
get adminRepoAddress(): string {
return this.state.adminRepoAddress;
}
/**
* Getter for addresslistVotingRepoAddress property
*
* @readonly
* @type {string}
* @memberof ContextCore
*/
get addresslistVotingRepoAddress(): string {
return this.state.addresslistVotingRepoAddress;
}
/**
* Getter for tokenVotingRepoAddress property
*
* @readonly
* @type {string}
* @memberof ContextCore
*/
get tokenVotingRepoAddress(): string {
return this.state.tokenVotingRepoAddress;
}
/**
* Getter for multisigSetupAddress property
*
* @readonly
* @type {string}
* @memberof ContextCore
*/
get multisigSetupAddress(): string {
return this.state.multisigSetupAddress;
}
/**
* Getter for adminSetupAddress property
*
* @readonly
* @type {string}
* @memberof ContextCore
*/
get adminSetupAddress(): string {
return this.state.adminSetupAddress;
}
/**
* Getter for addresslistVotingSetupAddress property
*
* @readonly
* @type {string}
* @memberof ContextCore
*/
get addresslistVotingSetupAddress(): string {
return this.state.addresslistVotingSetupAddress;
}
/**
* Getter for tokenVotingSetupAddress property
*
* @readonly
* @type {string}
* @memberof ContextCore
*/
get tokenVotingSetupAddress(): string {
return this.state.tokenVotingSetupAddress;
}

/**
* Getter for the gas fee reducer used in estimations
*
Expand Down Expand Up @@ -342,33 +225,23 @@ export abstract class ContextCore {
return this.state.graphql;
}

public getAddress(contractName: ContractNames): string {
return this.state[contractName];
}

// INTERNAL HELPERS
private static resolveNetwork(
networkish: Networkish,
ensRegistryAddress?: string,
): Network {
const network = getNetwork(networkish);
const networkName = network.name as SupportedNetwork;
if (!SupportedNetworksArray.includes(networkName)) {
throw new UnsupportedNetworkError(networkName);
}

if (ensRegistryAddress) {
if (!isAddress(ensRegistryAddress)) {
throw new InvalidAddressError();
} else {
network.ensAddress = ensRegistryAddress;
}
}

if (!network.ensAddress) {
const ensAddress =
LIVE_CONTRACTS[SupportedVersion.LATEST][networkName].ensRegistryAddress;
if (!ensAddress) {
throw new UnsupportedNetworkError(networkName);
}
network.ensAddress = ensAddress;
}
return network;
}

Expand Down
14 changes: 14 additions & 0 deletions modules/client-common/src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -381,3 +381,17 @@ export class InvalidPermissionOperationType extends SdkError {
super("Invalid permission operation", cause);
}
}

export class ContractNotDeployedError extends SdkError {
constructor(
contractName: string,
version: string,
network: string,
cause?: any,
) {
super(
`Contract "${contractName}" version "${version}" is not deployed on network "${network}"`,
cause,
);
}
}
Loading

0 comments on commit e64f8da

Please sign in to comment.