Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

V3 interaction new updates #35

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
132 changes: 123 additions & 9 deletions contracts/fees_collector_contract.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,31 @@
from contracts.base_contracts import BaseBoostedContract
from copy import copy, deepcopy
from typing import Any, cast
from multiversx_sdk import ApiNetworkProvider, CodeMetadata, ProxyNetworkProvider, AddressComputer, SmartContractQueryResponse
from multiversx_sdk.network_providers.transaction_decoder import TransactionDecoder
from contracts.contract_identities import DEXContractInterface
from contracts.farm_contract import FarmContract
from contracts.metastaking_contract import MetaStakingContract
from contracts.staking_contract import StakingContract
from utils import decoding_structures
from utils.contract_data_fetchers import FeeCollectorContractDataFetcher
from utils.logger import get_logger
from utils.utils_tx import deploy, endpoint_call, upgrade_call
from utils.utils_generic import log_step_pass, log_unexpected_args
from utils.utils_chain import Account, WrapperAddress as Address
from multiversx_sdk import CodeMetadata, ProxyNetworkProvider

from utils.utils_tx import prepare_contract_call_tx, send_contract_call_tx, deploy, endpoint_call, upgrade_call
from utils.utils_generic import log_step_fail, log_step_pass, log_warning, log_unexpected_args
from utils.utils_chain import Account, WrapperAddress as Address, decode_merged_attributes, hex_to_string, log_explorer_transaction, dec_to_padded_hex, string_to_base64
from multiversx_sdk.abi.serializer import Serializer
fgandila marked this conversation as resolved.
Show resolved Hide resolved
from multiversx_sdk.abi.biguint_value import BigUIntValue
from multiversx_sdk.abi.small_int_values import *
from multiversx_sdk.abi.string_value import StringValue
from multiversx_sdk.abi.struct_value import StructValue
from multiversx_sdk.abi.fields import Field
from multiversx_sdk.abi.list_value import ListValue

logger = get_logger(__name__)


class FeesCollectorContract(BaseBoostedContract):
class FeesCollectorContract(DEXContractInterface):
transaction_decoder = TransactionDecoder()

def __init__(self, address: str = ""):
self.address = address

Expand Down Expand Up @@ -59,7 +75,7 @@ def contract_upgrade(self, deployer: Account, proxy: ProxyNetworkProvider, bytec

metadata = CodeMetadata(upgradeable=True, payable_by_contract=True, readable=True)
gas_limit = 200000000

if no_init:
arguments = []
else:
Expand Down Expand Up @@ -192,17 +208,115 @@ def claim_rewards(self, user: Account, proxy: ProxyNetworkProvider):
gas_limit = 80000000
sc_args = []
return endpoint_call(proxy, gas_limit, user, Address(self.address), "claimRewards", sc_args)

def claim_rewards_original_caller(self, user: Account, proxy: ProxyNetworkProvider, opt_original_caller: Account):
function_purpose = f"Claim rewards from fees collector"
logger.info(function_purpose)

gas_limit = 80000000
opt_original_caller = user.address.bech32()
sc_args = [
opt_original_caller
]
return endpoint_call(proxy, gas_limit, user, Address(self.address), "claimBoostedRewards", sc_args)

def claim_boosted_rewards(self, user: Account, proxy: ProxyNetworkProvider):
function_purpose = f"Claim rewards from fees collector"
function_purpose = f"Claim boosted rewards from fees collector"
logger.info(function_purpose)

gas_limit = 80000000
sc_args = []
return endpoint_call(proxy, gas_limit, user, Address(self.address), "claimBoostedRewards", sc_args)


def contract_start(self, deployer: Account, proxy: ProxyNetworkProvider, args: list = None):
pass

def print_contract_info(self):
log_step_pass(f"Deployed fees collector contract: {self.address}")

def allow_external_claim(self, user: Account, proxy: ProxyNetworkProvider, allow_external_claim: bool):
fn = 'setAllowExternalClaimRewards'
sc_args = [
allow_external_claim
]
logger.info(f"{fn}")
# logger.debug(f"Account: {user.address}")

gas_limit = 80000000

return endpoint_call(proxy, gas_limit ,user, Address(self.address), "setAllowExternalClaimRewards", sc_args)

def get_user_energy_for_week(self, user: str, proxy: ProxyNetworkProvider, week: int) -> dict:
data_fetcher = FeeCollectorContractDataFetcher(Address(self.address), proxy.url)
raw_results = data_fetcher.get_data('getUserEnergyForWeek', [Address(user).get_public_key(), week])
print(raw_results)
if not raw_results:
return {}
decoder = TransactionDecoder()
user_energy_for_week = decoder.hex_to_number(raw_results)

return user_energy_for_week

def get_current_week(self, proxy: ProxyNetworkProvider) -> int:
data_fetcher = FeeCollectorContractDataFetcher(Address(self.address), proxy.url)
raw_results = data_fetcher.get_data('getCurrentWeek')
if not raw_results:
return 0
current_week = int(raw_results)

return current_week

def get_total_energy_for_week(self, proxy: ProxyNetworkProvider, week: int) -> int:
data_fetcher = FeeCollectorContractDataFetcher(Address(self.address), proxy.url)
raw_results = data_fetcher.get_data('getTotalEnergyForWeek', [week])
if not raw_results:
return 0
return int(raw_results)

def get_total_rewards_for_week(self, proxy: ProxyNetworkProvider, week: int):
serializer = Serializer("@")

data_fetcher = FeeCollectorContractDataFetcher(Address(self.address), proxy.url)
raw_results = data_fetcher.get_data('getTotalRewardsForWeek', [week])

if not raw_results:
return {}

b = bytes.fromhex(raw_results)

data = b

def create_esdt_token_payment() -> StructValue:
return StructValue([
Field("token_identifier", StringValue()),
Field("token_nonce", U64Value()),
Field("amount", BigUIntValue()),
])

destination = ListValue(
item_creator=create_esdt_token_payment
)

s = serializer.deserialize_parts([data], [destination])

tokens = []

for i, item in enumerate(destination.items):
fields = cast(StructValue, item).fields
for field in fields:
fvalue: Any = field.value
tokens.append([field.name, fvalue.value])

tokens_list = [tokens[i:i+3] for i in range(0, len(tokens), 3)]

return tokens_list

def get_all_stats(self, proxy: ProxyNetworkProvider, user: str, week: int):
fees_collector_stats = {
"current_week": self.get_current_week(proxy),
"total_rewards_for_week": self.get_total_rewards_for_week(proxy, week),
"total_energy_for_week": self.get_total_energy_for_week(proxy, week)
}

return fees_collector_stats
Loading