Skip to content

Commit

Permalink
add load contract by address class method for contracts
Browse files Browse the repository at this point in the history
Signed-off-by: Claudiu Lataretu <claudiu.lataretu@gmail.com>
  • Loading branch information
claudiulataretu committed Jun 18, 2024
1 parent c5c70a9 commit 395e113
Show file tree
Hide file tree
Showing 23 changed files with 182 additions and 18 deletions.
5 changes: 5 additions & 0 deletions contracts/composable_tasks_contract.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ def get_config_dict(self) -> dict:
def load_config_dict(cls, config_dict: dict):
return ComposableTasksContract(address=config_dict['address'])

@classmethod
def load_contract_by_address(cls, address: str):
raise NotImplementedError

def contract_deploy(self, deployer: Account, proxy: ProxyNetworkProvider, bytecode_path, args: list = None):
"""Expecting as args:
"""
Expand Down Expand Up @@ -84,3 +88,4 @@ def set_pair_address_for_tokens(self, deployer: Account, proxy: ProxyNetworkProv
logger.info(function_purpose)

return endpoint_call(proxy, 10000000, deployer, Address(self.address), "setPairAddrForTokens", args)

5 changes: 5 additions & 0 deletions contracts/contract_identities.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ def get_config_dict(self) -> dict:
def load_config_dict(cls, config_dict: dict):
pass

@classmethod
@abstractmethod
def load_contract_by_address(cls, address: str):
pass

@abstractmethod
def contract_deploy(self, deployer: Account, proxy: ProxyNetworkProvider, bytecode_path, args: list):
pass
Expand Down
14 changes: 13 additions & 1 deletion contracts/dex_proxy_contract.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
from contracts.farm_contract import FarmContract
from contracts.pair_contract import PairContract
from multiversx_sdk import ApiNetworkProvider, ProxyNetworkProvider, CodeMetadata
from utils.contract_data_fetchers import ProxyContractDataFetcher
from utils.logger import get_logger
from utils.utils_tx import deploy, upgrade_call, \
endpoint_call, multi_esdt_endpoint_call, ESDTToken
from utils.utils_generic import log_step_fail, log_step_pass, log_substep, \
log_unexpected_args
from utils.utils_chain import Account, WrapperAddress as Address, base64_to_hex, dec_to_padded_hex, decode_merged_attributes
from utils.utils_chain import Account, WrapperAddress as Address, base64_to_hex, dec_to_padded_hex, decode_merged_attributes, hex_to_string

from utils.decoding_structures import LKMEX_ATTRIBUTES, XMEX_ATTRIBUTES, XMEXFARM_ATTRIBUTES, XMEXLP_ATTRIBUTES

Expand Down Expand Up @@ -106,6 +107,17 @@ def load_config_dict(cls, config_dict: dict):
address=config_dict['address'],
version=ProxyContractVersion(config_dict['version']))

@classmethod
def load_contract_by_address(cls, address: str):
data_fetcher = ProxyContractDataFetcher(Address(address), config.DEFAULT_PROXY)
locked_tokens = [hex_to_string(res) for res in data_fetcher.get_data("getLockedTokenIds")]
token = hex_to_string(data_fetcher.get_data("getAssetTokenId"))
proxy_lp_token = data_fetcher.get_data("getWrappedLpTokenId")
proxy_farm_token = data_fetcher.get_data("getWrappedFarmTokenId")
version = ProxyContractVersion.V2

return DexProxyContract(locked_tokens, token, version, address, proxy_lp_token, proxy_farm_token)

def add_liquidity_proxy(self, user: Account, proxy: ProxyNetworkProvider, event: DexProxyAddLiquidityEvent):
function_purpose = "add liquidity via proxy"
logger.debug(f"Executing {function_purpose} for user {user.address} with event {event.__dict__}")
Expand Down
4 changes: 4 additions & 0 deletions contracts/egld_wrap_contract.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ def load_config_dict(cls, config_dict: dict):
return EgldWrapContract(address=config_dict['address'],
wrapped_token=config_dict['wrapped_token'])

@classmethod
def load_contract_by_address(cls, address: str):
raise NotImplementedError

def contract_deploy(self, deployer: Account, proxy: ProxyNetworkProvider, bytecode_path, args: list = None):
""" Expected as args:
"""
Expand Down
4 changes: 4 additions & 0 deletions contracts/energy_update_contract.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ def get_config_dict(self) -> dict:
def load_config_dict(cls, config_dict: dict):
return EnergyUpdateContract(address=config_dict['address'])

@classmethod
def load_contract_by_address(cls, address: str):
raise NotImplementedError

def contract_deploy(
self,
deployer: Account,
Expand Down
4 changes: 4 additions & 0 deletions contracts/escrow_contract.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ def get_config_dict(self) -> dict:
def load_config_dict(cls, config_dict: dict):
return EscrowContract(address=config_dict['address'])

@classmethod
def load_contract_by_address(cls, address: str):
raise NotImplementedError

def contract_deploy(self, deployer: Account, proxy: ProxyNetworkProvider, bytecode_path, args: list):
"""Expecting as args:
type[str]: energy factory address
Expand Down
15 changes: 14 additions & 1 deletion contracts/farm_contract.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from utils.logger import get_logger
from utils.utils_tx import NetworkProviders, ESDTToken, \
multi_esdt_endpoint_call, deploy, upgrade_call, endpoint_call
from utils.utils_chain import Account, WrapperAddress as Address, decode_merged_attributes
from utils.utils_chain import Account, WrapperAddress as Address, decode_merged_attributes, hex_to_string
from multiversx_sdk import CodeMetadata, ProxyNetworkProvider
from utils.utils_generic import log_step_pass, log_substep, log_unexpected_args
from events.farm_events import (EnterFarmEvent, ExitFarmEvent, ClaimRewardsFarmEvent,
Expand Down Expand Up @@ -45,6 +45,19 @@ def load_config_dict(cls, config_dict: dict):
address=config_dict['address'],
version=FarmContractVersion(config_dict['version']))

@classmethod
def load_contract_by_address(cls, address: str):
data_fetcher = FarmContractDataFetcher(Address(address), config.DEFAULT_PROXY)
farming_token = hex_to_string(data_fetcher.get_data("getFarmingTokenId"))
farm_token = hex_to_string(data_fetcher.get_data("getFarmTokenId"))
farmed_token = hex_to_string(data_fetcher.get_data("getRewardTokenId"))
version = FarmContractVersion.V2Boosted # TODO: find a way to determine this automatically

if not farming_token or not farmed_token:
return None

return FarmContract(farming_token, farm_token, farmed_token, address, version)

def has_proxy(self) -> bool:
if self.proxyContract is not None:
return True
Expand Down
4 changes: 4 additions & 0 deletions contracts/fees_collector_contract.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ def get_config_dict(self) -> dict:
def load_config_dict(cls, config_dict: dict):
return FeesCollectorContract(address=config_dict['address'])

@classmethod
def load_contract_by_address(cls, address: str):
return FeesCollectorContract(address=address)

def contract_deploy(self, deployer: Account, proxy: ProxyNetworkProvider, bytecode_path, args: list = None):
""" Expected as args:
type[str]: locked token
Expand Down
4 changes: 4 additions & 0 deletions contracts/governance_contract.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ def load_config_dict(cls, config_dict: dict):
return GovernanceContract(address=config_dict['address'],
fee_token=config_dict['fee_token'])

@classmethod
def load_contract_by_address(cls, address: str):
raise NotImplementedError

def contract_deploy(self, deployer: Account, proxy: ProxyNetworkProvider, bytecode_path, args: list = []):
""" Expected as args:
type[int]: min_energy_for_propose
Expand Down
4 changes: 4 additions & 0 deletions contracts/liquid_locking_contract.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ def load_config_dict(cls, config_dict: dict):
return LiquidLockingContract(address=config_dict['address'],
whitelisted_tokens=config_dict['whitelisted_tokens'])

@classmethod
def load_contract_by_address(cls, address: str):
raise NotImplementedError

def contract_deploy(self, deployer: Account, proxy: ProxyNetworkProvider, bytecode_path, args: list = []):
""" Expected as args:
type[int]: unbond period
Expand Down
4 changes: 4 additions & 0 deletions contracts/lk_wrap_contract.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ def load_config_dict(cls, config_dict: dict):
return LkWrapContract(address=config_dict['address'],
wrapped_token=config_dict['wrapped_token'])

@classmethod
def load_contract_by_address(cls, address: str):
raise NotImplementedError

def contract_deploy(self, deployer: Account, proxy: ProxyNetworkProvider, bytecode_path, args: list):
"""Expecting as args:
type[str]: energy factory address
Expand Down
11 changes: 10 additions & 1 deletion contracts/locked_asset_contract.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
import traceback

from contracts.contract_identities import DEXContractInterface
from utils.contract_data_fetchers import LockedAssetContractDataFetcher
from utils.utils_tx import multi_esdt_endpoint_call, prepare_contract_call_tx, send_contract_call_tx, deploy, upgrade_call, endpoint_call
from utils.utils_generic import log_step_fail, log_step_pass, log_substep, log_unexpected_args
from utils.utils_chain import Account, WrapperAddress as Address, log_explorer_transaction
from utils.utils_chain import Account, WrapperAddress as Address, hex_to_string, log_explorer_transaction
from multiversx_sdk import CodeMetadata, ProxyNetworkProvider
from utils.logger import get_logger

Expand All @@ -31,6 +32,14 @@ def load_config_dict(cls, config_dict: dict):
unlocked_asset=config_dict['unlocked_asset'],
locked_asset=config_dict['locked_asset'])

@classmethod
def load_contract_by_address(cls, address: str):
data_fetcher = LockedAssetContractDataFetcher(Address(address), config.DEFAULT_PROXY)
base_token = hex_to_string(data_fetcher.get_data("getAssetTokenId"))
locked_token = hex_to_string(data_fetcher.get_data("getLockedAssetTokenId"))

return LockedAssetContract(locked_token, base_token, address)

def contract_deploy(self, deployer: Account, proxy: ProxyNetworkProvider, bytecode_path, args: list = []):
function_purpose = f"deploy {type(self).__name__} contract"
logger.info(function_purpose)
Expand Down
4 changes: 4 additions & 0 deletions contracts/locked_token_position_creator_contract.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ def get_config_dict(self) -> dict:
def load_config_dict(cls, config_dict: dict):
return LockedTokenPositionCreatorContract(address=config_dict['address'])

@classmethod
def load_contract_by_address(cls, address: str):
raise NotImplementedError

def contract_deploy(
self,
deployer: Account,
Expand Down
45 changes: 33 additions & 12 deletions contracts/metastaking_contract.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,14 @@
import sys
import traceback
from operator import ne
from typing import Any, Dict, List, Tuple

from contracts.contract_identities import DEXContractInterface, MetaStakingContractIdentity, MetaStakingContractVersion
from events.metastake_events import (EnterMetastakeEvent, ExitMetastakeEvent, ClaimRewardsMetastakeEvent,
MergeMetastakeWithStakeEvent)
from multiversx_sdk import ApiNetworkProvider
from typing import Any, Dict, List, Tuple, override
from contracts.contract_identities import DEXContractInterface, MetaStakingContractVersion
from utils.contract_data_fetchers import MetaStakingContractDataFetcher
from utils.logger import get_logger
from utils.utils_tx import NetworkProviders, deploy, upgrade_call, \
endpoint_call, ESDTToken, multi_esdt_endpoint_call
from utils.utils_chain import Account, WrapperAddress as Address, base64_to_hex, decode_merged_attributes
from utils.utils_tx import deploy, upgrade_call, \
endpoint_call, multi_esdt_endpoint_call
from utils.utils_chain import Account, WrapperAddress as Address, base64_to_hex, decode_merged_attributes, hex_to_string
from multiversx_sdk import CodeMetadata, ProxyNetworkProvider
from utils.utils_generic import log_step_pass, log_substep, log_unexpected_args
from utils.decoding_structures import FARM_TOKEN_ATTRIBUTES, METASTAKE_TOKEN_ATTRIBUTES, STAKE_V2_TOKEN_ATTRIBUTES, STAKE_V1_TOKEN_ATTRIBUTES
import config

logger = get_logger(__name__)

Expand Down Expand Up @@ -61,6 +56,32 @@ def load_config_dict(cls, config_dict: dict):
stake_address=config_dict['stake_address'],
version=MetaStakingContractVersion(config_dict['version']))

@classmethod
def load_contract_by_address(cls, address: str, version=MetaStakingContractVersion.V3Boosted):
data_fetcher = MetaStakingContractDataFetcher(Address(address), config.DEFAULT_PROXY)

staking_token = hex_to_string(data_fetcher.get_data("getStakingTokenId"))
lp_token = hex_to_string(data_fetcher.get_data("getLpTokenId"))
farm_token = hex_to_string(data_fetcher.get_data("getLpFarmTokenId"))
stake_token = hex_to_string(data_fetcher.get_data("getFarmTokenId"))
lp_address = Address.from_hex(data_fetcher.get_data("getPairAddress")).bech32()
farm_address = Address.from_hex(data_fetcher.get_data("getLpFarmAddress")).bech32()
stake_address = Address.from_hex(data_fetcher.get_data("getStakingFarmAddress")).bech32()
metastake_token = hex_to_string(data_fetcher.get_data("getDualYieldTokenId"))

return MetaStakingContract(
staking_token,
lp_token,
farm_token,
stake_token,
lp_address,
farm_address,
stake_address,
version,
metastake_token,
address
)

def contract_deploy(self, deployer: Account, proxy: ProxyNetworkProvider, bytecode_path, args: list = []):
function_purpose = f"Deploy metastaking contract"
logger.info(function_purpose)
Expand Down
16 changes: 15 additions & 1 deletion contracts/pair_contract.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import sys
import traceback
import config

from contracts.contract_identities import (DEXContractInterface, PairContractVersion)
from utils.contract_data_fetchers import PairContractDataFetcher
from utils.logger import get_logger
from utils.utils_tx import NetworkProviders, endpoint_call, upgrade_call, deploy, ESDTToken, multi_esdt_endpoint_call
from utils.utils_generic import log_step_fail, log_step_pass, log_substep, log_unexpected_args
from utils.utils_chain import Account, WrapperAddress as Address
from utils.utils_chain import Account, WrapperAddress as Address, hex_to_string
from multiversx_sdk import CodeMetadata, ProxyNetworkProvider


Expand Down Expand Up @@ -79,6 +81,18 @@ def load_config_dict(cls, config_dict: dict):
address=config_dict['address'],
version=PairContractVersion(config_dict['version']))

@classmethod
def load_contract_by_address(cls, address: str, version=PairContractVersion.V2, proxy_contract=None):
data_fetcher = PairContractDataFetcher(Address(address), config.DEFAULT_PROXY)
first_token = hex_to_string(data_fetcher.get_data("getFirstTokenId"))
second_token = hex_to_string(data_fetcher.get_data("getSecondTokenId"))
lp_token = hex_to_string(data_fetcher.get_data("getLpTokenIdentifier"))

if not first_token or not second_token:
return None

return PairContract(first_token, second_token, version, lp_token, address, proxy_contract)

def hasProxy(self) -> bool:
if self.proxy_contract is not None:
return True
Expand Down
4 changes: 4 additions & 0 deletions contracts/position_creator_contract.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ def get_config_dict(self) -> dict:
def load_config_dict(cls, config_dict: dict):
return PositionCreatorContract(address=config_dict['address'])

@classmethod
def load_contract_by_address(cls, address: str):
return PositionCreatorContract(address)

def contract_deploy(
self,
deployer: Account,
Expand Down
4 changes: 4 additions & 0 deletions contracts/price_discovery_contract.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@ def load_config_dict(cls, config_dict: dict):
max_penalty_percentage=config_dict['max_penalty_percentage'],
fixed_penalty_percentage=config_dict['fixed_penalty_percentage'])

@classmethod
def load_contract_by_address(cls, address: str):
raise NotImplementedError

def deposit_liquidity(self, network_provider: NetworkProviders, user: Account, event: DepositPDLiquidityEvent) -> str:
function_purpose = f"Deposit Price Discovery liquidity"
logger.info(function_purpose)
Expand Down
4 changes: 4 additions & 0 deletions contracts/proxy_deployer_contract.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ def load_config_dict(cls, config_dict: dict):
return ProxyDeployerContract(address=config_dict['address'],
template_name=config_dict['template'])

@classmethod
def load_contract_by_address(cls, address: str):
raise NotImplementedError

def contract_deploy(self, deployer: Account, proxy: ProxyNetworkProvider, bytecode_path, args: list):
"""Expecting as args:
type[str]: template sc address
Expand Down
4 changes: 4 additions & 0 deletions contracts/router_contract.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ def load_config_dict(cls, config_dict: dict):
return RouterContract(address=config_dict['address'],
version=RouterContractVersion(config_dict['version']))

@classmethod
def load_contract_by_address(cls, address: str, version=RouterContractVersion.V2):
return RouterContract(version, address)

def contract_deploy(self, deployer: Account, proxy: ProxyNetworkProvider, bytecode_path, args: list):
"""Expecting as args:
type[str]: pair template address
Expand Down
4 changes: 4 additions & 0 deletions contracts/simple_lock_contract.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ def load_config_dict(cls, config_dict: dict):
lp_proxy_token=config_dict['lp_proxy_token'],
farm_proxy_token=config_dict.get('farm_proxy_token'))

@classmethod
def load_contract_by_address(cls, address: str):
raise NotImplementedError

def contract_deploy(self, deployer: Account, proxy: ProxyNetworkProvider, bytecode_path, args: list = []):
function_purpose = f"Deploy simple lock contract"
logger.info(function_purpose)
Expand Down
12 changes: 11 additions & 1 deletion contracts/simple_lock_energy_contract.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
from utils.logger import get_logger
from utils.utils_tx import multi_esdt_endpoint_call, endpoint_call, deploy, upgrade_call
from utils.utils_generic import log_step_pass, log_substep, log_unexpected_args
from utils.utils_chain import Account, WrapperAddress as Address, decode_merged_attributes
from utils.utils_chain import Account, WrapperAddress as Address, decode_merged_attributes, hex_to_string
from multiversx_sdk import CodeMetadata, ProxyNetworkProvider

import config


logger = get_logger(__name__)

Expand Down Expand Up @@ -40,6 +42,14 @@ def load_config_dict(cls, config_dict: dict):
lp_proxy_token=config_dict['lp_proxy_token'],
farm_proxy_token=config_dict['farm_proxy_token'])

@classmethod
def load_contract_by_address(cls, address: str):
data_fetcher = SimpleLockEnergyContractDataFetcher(Address(address), config.DEFAULT_PROXY)
base_token = hex_to_string(data_fetcher.get_data("getBaseAssetTokenId"))
locked_token = hex_to_string(data_fetcher.get_data("getLockedTokenId"))

return SimpleLockEnergyContract(base_token, locked_token, address)

def contract_deploy(self, deployer: Account, proxy: ProxyNetworkProvider, bytecode_path, args: list):
"""Expecting as args:
type[str]: legacy token id
Expand Down
Loading

0 comments on commit 395e113

Please sign in to comment.