Skip to content

Commit

Permalink
Refactor: ChainType and LedgerType
Browse files Browse the repository at this point in the history
Signed-off-by: OjusWiZard <[email protected]>
  • Loading branch information
OjusWiZard committed Nov 13, 2024
1 parent 31a7f22 commit 8c495bb
Show file tree
Hide file tree
Showing 24 changed files with 357 additions and 332 deletions.
18 changes: 9 additions & 9 deletions api.md
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ Returns the list of existing service configurations.
"env_variables": {...},
"hash": "bafybeidicxsruh3r4a2xarawzan6ocwyvpn3ofv42po5kxf7x6ck7kn22u",
"hash_history": {"1731487112": "bafybeidicxsruh3r4a2xarawzan6ocwyvpn3ofv42po5kxf7x6ck7kn22u"},
"home_chain_id": "100",
"home_chain": "100",
"keys": [...],
"name": "valory/trader_omen_gnosis",
"service_config_id": "sc-85a7a12a-8c6b-46b8-919a-b8a3b8e3ad39",
Expand Down Expand Up @@ -324,7 +324,7 @@ Create a service configuration using a template.
"env_variables": {...},
"hash": "bafybeidicxsruh3r4a2xarawzan6ocwyvpn3ofv42po5kxf7x6ck7kn22u",
"image": "https://operate.olas.network/_next/image?url=%2Fimages%2Fprediction-agent.png&w=3840&q=75",
"home_chain_id": "100",
"home_chain": "100",
"name": "valory/trader_omen_gnosis",
"service_version": "v0.18.4"
}
Expand All @@ -342,7 +342,7 @@ Create a service configuration using a template.
"env_variables": {...},
"hash": "bafybeidicxsruh3r4a2xarawzan6ocwyvpn3ofv42po5kxf7x6ck7kn22u",
"hash_history": {"1731487112": "bafybeidicxsruh3r4a2xarawzan6ocwyvpn3ofv42po5kxf7x6ck7kn22u"},
"home_chain_id": "100",
"home_chain": "100",
"keys": [...],
"name": "valory/trader_omen_gnosis",
"service_config_id": "sc-85a7a12a-8c6b-46b8-919a-b8a3b8e3ad39",
Expand All @@ -369,7 +369,7 @@ Update all the service configurations whose Service Public ID match the Service
"env_variables": {...},
"hash": "bafybeibpseosblmaw6sk6zsnic2kfxfsijrnfluuhkwboyqhx7ma7zw2me",
"image": "https://operate.olas.network/_next/image?url=%2Fimages%2Fprediction-agent.png&w=3840&q=75",
"home_chain_id": "100",
"home_chain": "100",
"name": "valory/trader_omen_gnosis",
"service_version": "v0.19.0"
}
Expand All @@ -390,7 +390,7 @@ The response contains an array of the services which have been updated (an empty
"env_variables": {...},
"hash": "bafybeidicxsruh3r4a2xarawzan6ocwyvpn3ofv42po5kxf7x6ck7kn22u",
"hash_history": {"1731487112": "bafybeidicxsruh3r4a2xarawzan6ocwyvpn3ofv42po5kxf7x6ck7kn22u", "1731490000": "bafybeibpseosblmaw6sk6zsnic2kfxfsijrnfluuhkwboyqhx7ma7zw2me"},
"home_chain_id": "100",
"home_chain": "100",
"keys": [...],
"name": "valory/trader_omen_gnosis",
"service_config_id": "sc-85a7a12a-8c6b-46b8-919a-b8a3b8e3ad39",
Expand Down Expand Up @@ -451,7 +451,7 @@ Returns the service configuration `service_config_id`.
"env_variables": {...},
"hash": "bafybeidicxsruh3r4a2xarawzan6ocwyvpn3ofv42po5kxf7x6ck7kn22u",
"hash_history": {"1731487112": "bafybeidicxsruh3r4a2xarawzan6ocwyvpn3ofv42po5kxf7x6ck7kn22u"},
"home_chain_id": "100",
"home_chain": "100",
"keys": [...],
"name": "valory/trader_omen_gnosis",
"service_config_id": "sc-85a7a12a-8c6b-46b8-919a-b8a3b8e3ad39",
Expand Down Expand Up @@ -498,7 +498,7 @@ The response contains the updated service configuration following the on-chain o
"env_variables": {...},
"hash": "bafybeidicxsruh3r4a2xarawzan6ocwyvpn3ofv42po5kxf7x6ck7kn22u",
"hash_history": {"1731487112": "bafybeidicxsruh3r4a2xarawzan6ocwyvpn3ofv42po5kxf7x6ck7kn22u"},
"home_chain_id": "100",
"home_chain": "100",
"keys": [...],
"name": "valory/trader_omen_gnosis",
"service_config_id": "sc-85a7a12a-8c6b-46b8-919a-b8a3b8e3ad39",
Expand All @@ -525,7 +525,7 @@ Update service configuration `service_config_id` with the provided template.
"env_variables": {...},
"hash": "bafybeibpseosblmaw6sk6zsnic2kfxfsijrnfluuhkwboyqhx7ma7zw2me",
"image": "https://operate.olas.network/_next/image?url=%2Fimages%2Fprediction-agent.png&w=3840&q=75",
"home_chain_id": "100",
"home_chain": "100",
"name": "valory/trader_omen_gnosis",
"service_version": "v0.19.0"
}
Expand All @@ -545,7 +545,7 @@ Update service configuration `service_config_id` with the provided template.
"env_variables": {...},
"hash": "bafybeidicxsruh3r4a2xarawzan6ocwyvpn3ofv42po5kxf7x6ck7kn22u",
"hash_history": {"1731487112": "bafybeidicxsruh3r4a2xarawzan6ocwyvpn3ofv42po5kxf7x6ck7kn22u", "1731490000": "bafybeibpseosblmaw6sk6zsnic2kfxfsijrnfluuhkwboyqhx7ma7zw2me"},
"home_chain_id": "100",
"home_chain": "100",
"keys": [...],
"name": "valory/trader_omen_gnosis",
"service_config_id": "sc-85a7a12a-8c6b-46b8-919a-b8a3b8e3ad39",
Expand Down
14 changes: 7 additions & 7 deletions frontend/client/enums.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ export enum Action {
}

export enum MiddlewareChain {
ETHEREUM = 0,
GOERLI = 1,
GNOSIS = 2,
SOLANA = 3,
OPTIMISM = 4,
BASE = 5,
MODE = 6,
ETHEREUM = "ethereum",
GOERLI = "goerli",
GNOSIS = "gnosis",
SOLANA = "solana",
OPTIMISM = "optimism",
BASE = "base",
MODE = "mode",
}

export enum Ledger {
Expand Down
2 changes: 1 addition & 1 deletion frontend/client/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export type ServiceTemplate = {
image: string;
description: string;
service_version: string;
home_chain_id: string;
home_chain: string;
configurations: { [key: string]: ConfigurationTemplate };
env_variables: { [key: string]: EnvVariableAttributes };
deploy?: boolean;
Expand Down
2 changes: 1 addition & 1 deletion frontend/components/MainPage/header/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { SERVICE_TEMPLATES } from '@/constants/serviceTemplates';

export const requiredGas = Number(
formatUnits(
`${SERVICE_TEMPLATES[0].configurations[CHAINS.OPTIMISM.chainId].monthly_gas_estimate}`,
`${SERVICE_TEMPLATES[0].configurations[CHAINS.OPTIMISM.middlewareChain].monthly_gas_estimate}`,
18,
),
);
14 changes: 9 additions & 5 deletions frontend/components/SetupPage/Create/SetupCreateSafe.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,14 @@ import { delayInSeconds } from '@/utils/delay';

const { Text } = Typography;

const capitalizedMiddlewareChainNames = {
[+MiddlewareChain.ETHEREUM]: 'Ethereum',
[+MiddlewareChain.BASE]: 'Base',
[+MiddlewareChain.OPTIMISM]: 'Optimism',
const capitalizedMiddlewareChainNames: { [key in MiddlewareChain]: string } = {
[MiddlewareChain.ETHEREUM]: 'Ethereum',
[MiddlewareChain.BASE]: 'Base',
[MiddlewareChain.OPTIMISM]: 'Optimism',
[MiddlewareChain.GOERLI]: 'Goerli',
[MiddlewareChain.GNOSIS]: 'Gnosis',
[MiddlewareChain.SOLANA]: 'Solana',
[MiddlewareChain.MODE]: 'Mode',
};

export const SetupCreateSafe = () => {
Expand Down Expand Up @@ -129,7 +133,7 @@ export const SetupCreateSafe = () => {

const safeCreationsRequired = Object.entries(chainsToCreateSafesFor).reduce(
(acc, [chain, safeAddressAlreadyExists]) => {
const middlewareChain = +chain as MiddlewareChain;
const middlewareChain = chain as MiddlewareChain;
if (safeAddressAlreadyExists) {
switch (middlewareChain) {
case MiddlewareChain.OPTIMISM:
Expand Down
6 changes: 3 additions & 3 deletions frontend/components/SetupPage/Create/SetupEoaFunding.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -173,11 +173,11 @@ export const SetupEoaFunding = () => {
const { goto } = useSetup();
const { masterEoaAddress } = useWallet();
const [currentChain, setCurrentChain] = useState<MiddlewareChain>(
+Object.keys(eoaFundingMap)[0] as MiddlewareChain,
Object.keys(eoaFundingMap)[0] as MiddlewareChain,
);

const currentFundingMapObject =
eoaFundingMap[+currentChain as keyof typeof eoaFundingMap];
eoaFundingMap[currentChain as keyof typeof eoaFundingMap];

const getIsCurrentChainFunded = useCallback(
async (
Expand Down Expand Up @@ -218,7 +218,7 @@ export const SetupEoaFunding = () => {
if (nextChainExists) {
// goto next chain
setCurrentChain(
+Object.keys(eoaFundingMap)[indexOfCurrentChain + 1] as MiddlewareChain,
Object.keys(eoaFundingMap)[indexOfCurrentChain + 1] as MiddlewareChain,
);
return;
}
Expand Down
2 changes: 1 addition & 1 deletion frontend/components/YourWalletPage/YourAgent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ const AgentTitle = () => {
const ServiceAndNftDetails = () => {
const { serviceId } = useServices();
const serviceAddress =
SERVICE_REGISTRY_L2_CONTRACT_ADDRESS[`${MiddlewareChain.OPTIMISM}`];
SERVICE_REGISTRY_L2_CONTRACT_ADDRESS[MiddlewareChain.OPTIMISM];

return (
<NftCard>
Expand Down
8 changes: 4 additions & 4 deletions frontend/constants/contractAddresses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,21 @@ Object.entries(CHAINS).forEach(([, { chainId }]) => {
setMulticallAddress(chainId, MULTICALL_CONTRACT_ADDRESS);
});

export const SERVICE_REGISTRY_L2_CONTRACT_ADDRESS: Record<number, Address> = {
export const SERVICE_REGISTRY_L2_CONTRACT_ADDRESS: Record<MiddlewareChain.OPTIMISM, Address> = {
// [MiddlewareChain.GNOSIS]: '0x9338b5153AE39BB89f50468E608eD9d764B755fD',
[MiddlewareChain.OPTIMISM]: '0x3d77596beb0f130a4415df3D2D8232B3d3D31e44',
};

export const SERVICE_REGISTRY_TOKEN_UTILITY_CONTRACT_ADDRESS: Record<
number,
MiddlewareChain.OPTIMISM,
Address
> = {
// [MiddlewareChain.GNOSIS]: '0xa45E64d13A30a51b91ae0eb182e88a40e9b18eD8',
[MiddlewareChain.OPTIMISM]: '0xBb7e1D6Cb6F243D6bdE81CE92a9f2aFF7Fbe7eac',
};

export const SERVICE_STAKING_TOKEN_MECH_USAGE_CONTRACT_ADDRESSES: Record<
number,
MiddlewareChain.OPTIMISM,
Record<StakingProgramId, Address>
> = {
// [Chain.GNOSIS]: {
Expand Down Expand Up @@ -81,7 +81,7 @@ export const SERVICE_STAKING_TOKEN_MECH_USAGE_CONTRACT_ADDRESSES: Record<
// };

export const STAKING_ACTIVITY_CHECKER_CONTRACT_ADDRESS: Record<
number,
MiddlewareChain.OPTIMISM,
Address
> = {
[MiddlewareChain.OPTIMISM]: '0x7Fd1F4b764fA41d19fe3f63C85d12bf64d2bbf68',
Expand Down
12 changes: 6 additions & 6 deletions frontend/constants/serviceTemplates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ export const SERVICE_TEMPLATES: ServiceTemplate[] = [
image:
'https://operate.olas.network/_next/image?url=%2Fimages%2Fprediction-agent.png&w=3840&q=75',
service_version: 'v0.18.4',
home_chain_id: '100',
home_chain: 'gnosis',
configurations: {
100: {
[CHAINS.GNOSIS.middlewareChain]: {
staking_program_id: StakingProgramId.OptimusAlpha, // default, may be overwritten
nft: 'bafybeig64atqaladigoc3ds4arltdu63wkdrk3gesjfvnfdmz35amv7faq',
rpc: 'http://localhost:8545',
Expand Down Expand Up @@ -105,9 +105,9 @@ export const SERVICE_TEMPLATES: ServiceTemplate[] = [
// image:
// 'https://operate.olas.network/_next/image?url=%2Fimages%2Fprediction-agent.png&w=3840&q=75',
// service_version: 'v0.2.9',
// home_chain_id: `${CHAINS.OPTIMISM.chainId}`,
// home_chain: `${CHAINS.OPTIMISM}`,
// configurations: {
// [CHAINS.OPTIMISM.chainId]: {
// [CHAINS.OPTIMISM.middlewareChain]: {
// staking_program_id: StakingProgramId.OptimusAlpha, // default, may be overwritten
// nft: 'bafybeig64atqaladigoc3ds4arltdu63wkdrk3gesjfvnfdmz35amv7faq',
// // rpc: 'http://localhost:8545',
Expand All @@ -122,7 +122,7 @@ export const SERVICE_TEMPLATES: ServiceTemplate[] = [
// safe: 1000,
// },
// },
// [CHAINS.ETHEREUM.chainId]: {
// [CHAINS.ETHEREUM.middlewareChain]: {
// staking_program_id: StakingProgramId.OptimusAlpha, // default, may be overwritten
// nft: 'bafybeig64atqaladigoc3ds4arltdu63wkdrk3gesjfvnfdmz35amv7faq',
// // rpc: 'http://localhost:8545',
Expand All @@ -137,7 +137,7 @@ export const SERVICE_TEMPLATES: ServiceTemplate[] = [
// safe: 1000,
// },
// },
// [CHAINS.BASE.chainId]: {
// [CHAINS.BASE.middlewareChain]: {
// staking_program_id: StakingProgramId.OptimusAlpha, // default, may be overwritten
// nft: 'bafybeig64atqaladigoc3ds4arltdu63wkdrk3gesjfvnfdmz35amv7faq',
// // rpc: 'http://localhost:8545',
Expand Down
2 changes: 1 addition & 1 deletion frontend/hooks/useNeedsFunds.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export const useNeedsFunds = () => {

const serviceFundRequirements = useMemo(() => {
const gasEstimate =
serviceTemplate.configurations[CHAINS.OPTIMISM.chainId]
serviceTemplate.configurations[CHAINS.OPTIMISM.middlewareChain]
.monthly_gas_estimate;
const monthlyGasEstimate = Number(formatUnits(`${gasEstimate}`, 18));
const minimumStakedAmountRequired =
Expand Down
8 changes: 4 additions & 4 deletions frontend/hooks/useServices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ const checkServiceIsFunded = async (
Object.assign(acc, {
[address]: instances.includes(address)
? balances[address] >
serviceTemplate.configurations[CHAINS.OPTIMISM.chainId]
.fund_requirements.agent
serviceTemplate.configurations[CHAINS.OPTIMISM.middlewareChain]
.fund_requirements.agent
: balances[address] >
serviceTemplate.configurations[CHAINS.OPTIMISM.chainId]
.fund_requirements.safe,
serviceTemplate.configurations[CHAINS.OPTIMISM.middlewareChain]
.fund_requirements.safe,
}),
{},
);
Expand Down
4 changes: 2 additions & 2 deletions frontend/service/Services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ const createService = async ({
...serviceTemplate,
deploy,
configurations: {
[CHAINS.OPTIMISM.chainId]: {
...serviceTemplate.configurations[CHAINS.OPTIMISM.chainId],
[CHAINS.OPTIMISM.middlewareChain]: {
...serviceTemplate.configurations[CHAINS.OPTIMISM.middlewareChain],
staking_program_id: stakingProgramId,
rpc: `${process.env.OPTIMISM_RPC}`,
use_mech_marketplace: useMechMarketplace,
Expand Down
33 changes: 15 additions & 18 deletions operate/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@
from operate import services
from operate.account.user import UserAccount
from operate.constants import KEY, KEYS, OPERATE, SERVICES
from operate.ledger import get_ledger_type_from_chain_type
from operate.operate_types import ChainType, DeploymentStatus
from operate.operate_types import Chain, DeploymentStatus
from operate.services.health_checker import HealthChecker
from operate.wallet.master import MasterWalletManager

Expand Down Expand Up @@ -408,9 +407,7 @@ async def _get_wallets(request: Request) -> t.List[t.Dict]:
@with_retries
async def _get_wallet_by_chain(request: Request) -> t.List[t.Dict]:
"""Create wallet safe"""
ledger_type = get_ledger_type_from_chain_type(
chain=ChainType.from_string(request.path_params["chain"])
)
ledger_type = Chain.from_string(request.path_params["chain"]).ledger_type
manager = operate.wallet_manager
if not manager.exists(ledger_type=ledger_type):
return JSONResponse(
Expand Down Expand Up @@ -438,8 +435,8 @@ async def _create_wallet(request: Request) -> t.List[t.Dict]:
)

data = await request.json()
chain_type = ChainType(data["chain_type"])
ledger_type = get_ledger_type_from_chain_type(chain=chain_type)
chain_type = Chain(data["chain_type"])
ledger_type = chain_type.ledger_type
manager = operate.wallet_manager
if manager.exists(ledger_type=ledger_type):
return JSONResponse(
Expand Down Expand Up @@ -467,8 +464,8 @@ async def _get_safes(request: Request) -> t.List[t.Dict]:
@with_retries
async def _get_safe(request: Request) -> t.List[t.Dict]:
"""Create wallet safe"""
chain_type = ChainType.from_string(request.path_params["chain"])
ledger_type = get_ledger_type_from_chain_type(chain=chain_type)
chain_type = Chain.from_string(request.path_params["chain"])
ledger_type = chain_type.ledger_type
manager = operate.wallet_manager
if not manager.exists(ledger_type=ledger_type):
return JSONResponse(
Expand Down Expand Up @@ -502,8 +499,8 @@ async def _create_safe(request: Request) -> t.List[t.Dict]:
)

data = await request.json()
chain_type = ChainType(data["chain_type"])
ledger_type = get_ledger_type_from_chain_type(chain=chain_type)
chain_type = Chain(data["chain_type"])
ledger_type = chain_type.ledger_type
manager = operate.wallet_manager
if not manager.exists(ledger_type=ledger_type):
return JSONResponse(content={"error": "Wallet does not exist"})
Expand All @@ -517,7 +514,7 @@ async def _create_safe(request: Request) -> t.List[t.Dict]:
}
)

safes = t.cast(t.Dict[ChainType, str], wallet.safes)
safes = t.cast(t.Dict[Chain, str], wallet.safes)
wallet.create_safe( # pylint: disable=no-member
chain_type=chain_type,
owner=data.get("owner"),
Expand Down Expand Up @@ -549,10 +546,10 @@ async def _create_safes(request: Request) -> t.List[t.Dict]:
)

data = await request.json()
chain_types = [ChainType(chain_type) for chain_type in data["chain_types"]]
chain_types = [Chain(chain_type) for chain_type in data["chain_types"]]
# check that all chains are supported
for chain_type in chain_types:
ledger_type = get_ledger_type_from_chain_type(chain=chain_type)
ledger_type = chain_type.ledger_type
manager = operate.wallet_manager
if not manager.exists(ledger_type=ledger_type):
return JSONResponse(
Expand All @@ -563,15 +560,15 @@ async def _create_safes(request: Request) -> t.List[t.Dict]:

# mint the safes
for chain_type in chain_types:
ledger_type = get_ledger_type_from_chain_type(chain=chain_type)
ledger_type = chain_type.ledger_type
manager = operate.wallet_manager

wallet = manager.load(ledger_type=ledger_type)
if wallet.safes is not None and wallet.safes.get(chain_type) is not None:
logger.info(f"Safe already exists for chain_type {chain_type}")
continue

safes = t.cast(t.Dict[ChainType, str], wallet.safes)
safes = t.cast(t.Dict[Chain, str], wallet.safes)
wallet.create_safe( # pylint: disable=no-member
chain_type=chain_type,
owner=data.get("owner"),
Expand Down Expand Up @@ -603,8 +600,8 @@ async def _update_safe(request: Request) -> t.List[t.Dict]:
)

data = await request.json()
chain_type = ChainType(data["chain_type"])
ledger_type = get_ledger_type_from_chain_type(chain=chain_type)
chain_type = Chain(data["chain_type"])
ledger_type = chain_type.ledger_type
manager = operate.wallet_manager
if not manager.exists(ledger_type=ledger_type):
return JSONResponse(content={"error": "Wallet does not exist"})
Expand Down
Loading

0 comments on commit 8c495bb

Please sign in to comment.