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

Feat/1209008326914613 add commission rate evm #82

Open
wants to merge 9 commits into
base: feat/1208894234090825_fee_weight_collection
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 8 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
35 changes: 35 additions & 0 deletions ETH/parachain-staking/abi
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,41 @@
"internalType": "uint256",
"name": "amount",
"type": "uint256"
},
{
"internalType": "uint256",
"name": "commission",
"type": "uint256"
}
],
"internalType": "struct ParachainStaking.CollatorInfo[]",
"name": "",
"type": "tuple[]"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "getWaitList",
"outputs": [
{
"components": [
{
"internalType": "bytes32",
"name": "owner",
"type": "bytes32"
},
{
"internalType": "uint256",
"name": "amount",
"type": "uint256"
},
{
"internalType": "uint256",
"name": "commission",
"type": "uint256"
}
],
"internalType": "struct ParachainStaking.CollatorInfo[]",
Expand Down
2 changes: 2 additions & 0 deletions tests/bridge_multiple_collator_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ def get_event(self, block_hash, module, event):
def test_delegator_another_candidate(self):
contract = get_contract(self._w3, PARACHAIN_STAKING_ADDR, PARACHAIN_STAKING_ABI_FILE)
out = contract.functions.getCollatorList().call()
out = sorted(out, key=lambda x: x[1], reverse=True)
collator_num = out[0][1]
collator_eth_addr = out[0][0]

Expand Down Expand Up @@ -181,6 +182,7 @@ def test_delegator_another_candidate(self):
self.assertEqual(receipt.is_success, True, f'force_new_round fails, receipt: {receipt}')

out = contract.functions.getCollatorList().call()
out = sorted(out, key=lambda x: x[1], reverse=True)
collator_eth_addr = out[0][0]
collator_eth_addr = [out[i][0] for i in range(len(out)) if out[i][0] != collator_eth_addr][0]

Expand Down
107 changes: 107 additions & 0 deletions tests/bridge_parachain_staking_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ def setUp(self):
self._kp_mars = get_eth_info()
self._eth_chain_id = get_eth_chain_id(self._substrate)
self._kp_src = Keypair.create_from_uri('//Moon')
self._kp_new_collator = Keypair.create_from_uri('//NewMoon01')

def _fund_users(self, num=100 * 10 ** 18):
if num < 100 * 10 ** 18:
Expand Down Expand Up @@ -70,6 +71,14 @@ def _fund_users(self, num=100 * 10 ** 18):
'new_free': num,
}
)
batch.compose_sudo_call(
'Balances',
'force_set_balance',
{
'who': self._kp_new_collator.ss58_address,
'new_free': num,
}
)
return batch.execute()

def evm_join_delegators(self, contract, eth_kp_src, sub_collator_addr, stake):
Expand Down Expand Up @@ -143,6 +152,7 @@ def test_get_collator_list(self):
contract = get_contract(self._w3, PARACHAIN_STAKING_ADDR, PARACHAIN_STAKING_ABI_FILE)

out = contract.functions.getCollatorList().call()
out = sorted(out, key=lambda x: x[1], reverse=True)
golden_data = self._substrate.query('ParachainStaking', 'TopCandidates')
golden_data = golden_data.value
self.assertEqual(len(out), len(golden_data))
Expand All @@ -165,6 +175,7 @@ def get_event(self, block_hash, module, event):
def test_evm_api_cannot_transfer_over_stake_others(self):
contract = get_contract(self._w3, PARACHAIN_STAKING_ADDR, PARACHAIN_STAKING_ABI_FILE)
out = contract.functions.getCollatorList().call()
out = sorted(out, key=lambda x: x[1], reverse=True)

collator_eth_addr = out[0][0]
collator_num = out[0][1]
Expand Down Expand Up @@ -203,6 +214,7 @@ def test_evm_api_cannot_transfer_over_stake_others(self):
def test_evm_api_cannot_transfer_over_stake_agung(self):
contract = get_contract(self._w3, PARACHAIN_STAKING_ADDR, PARACHAIN_STAKING_ABI_FILE)
out = contract.functions.getCollatorList().call()
out = sorted(out, key=lambda x: x[1], reverse=True)

collator_eth_addr = out[0][0]
collator_num = out[0][1]
Expand Down Expand Up @@ -240,6 +252,7 @@ def test_evm_api_cannot_transfer_over_stake_agung(self):
def test_delegator_join_more_less_leave(self):
contract = get_contract(self._w3, PARACHAIN_STAKING_ADDR, PARACHAIN_STAKING_ABI_FILE)
out = contract.functions.getCollatorList().call()
out = sorted(out, key=lambda x: x[1], reverse=True)

collator_eth_addr = out[0][0]
collator_num = out[0][1]
Expand Down Expand Up @@ -293,9 +306,102 @@ def test_delegator_join_more_less_leave(self):
# Note: The unlock unstaked didn't success because we have to wait about 20+ blocks;
# therefore, we don't test here. Can just test maunally

def set_commission_rate(self, rate, kp=KP_COLLATOR):
batch = ExtrinsicBatch(self._substrate, kp)
batch.compose_call(
'ParachainStaking',
'set_commission',
{
'commission': rate * 10_000,
}
)
return batch.execute()

def test_commission_rate(self):
# Set commission rate as 20
receipt = self.set_commission_rate(20)
self.assertEqual(receipt.is_success, True, f'set_commission fails, receipt: {receipt}')

contract = get_contract(self._w3, PARACHAIN_STAKING_ADDR, PARACHAIN_STAKING_ABI_FILE)
out = contract.functions.getCollatorList().call()
out = sorted(out, key=lambda x: x[1], reverse=True)
all_colators_info = self._substrate.query_map(
module='ParachainStaking',
storage_function='CandidatePool',
params=[],
start_key=None,
page_size=1000,
)

evm_out = {info[0]: {
'amount': info[1],
'commission': info[2],
} for info in out}

for collator_id, collator_info in all_colators_info.records:
pk = bytes.fromhex(self._substrate.ss58_decode(collator_info.value['id']))
self.assertEqual(
evm_out[pk]['commission'],
collator_info.value['commission'],
f'commission rate fails, out: {out}, all_colators_info: {all_colators_info}')
self.assertEqual(
evm_out[pk]['amount'],
collator_info.value['stake'],
f'commission rate fails, out: {out}, all_colators_info: {all_colators_info}')

receipt = self.set_commission_rate(0)
self.assertEqual(receipt.is_success, True, f'set_commission fails, receipt: {receipt}')

def test_wait_list(self):
contract = get_contract(self._w3, PARACHAIN_STAKING_ADDR, PARACHAIN_STAKING_ABI_FILE)
out = contract.functions.getCollatorList().call()
out = sorted(out, key=lambda x: x[1], reverse=True)
collator_num = out[0][1]
receipt = self._fund_users(collator_num * 2)

# Join one collator
batch = ExtrinsicBatch(self._substrate, self._kp_new_collator)
batch.compose_call(
'ParachainStaking',
'join_candidates',
{
'stake': collator_num,
}
)
receipt = batch.execute()
self.assertEqual(receipt.is_success, True, f'join_collator fails, receipt: {receipt}')
receipt = self.set_commission_rate(10, self._kp_new_collator)
self.assertEqual(receipt.is_success, True, f'set_commission fails, receipt: {receipt}')

wait_list = contract.functions.getWaitList().call()
wait_list = sorted(wait_list, key=lambda x: x[1], reverse=True)
self.assertEqual(len(wait_list), 1)
pk = bytes.fromhex(self._substrate.ss58_decode(self._kp_new_collator.ss58_address))
self.assertEqual(wait_list[0][0], pk)
self.assertEqual(wait_list[0][1], collator_num)
self.assertEqual(wait_list[0][2], 10 * 10_000)

# Check the wait list
collator_list = contract.functions.getCollatorList().call()
out = sorted(out, key=lambda x: x[1], reverse=True)
self.assertEqual(len(collator_list), 2)
self.assertTrue(wait_list[0][0] in [collator[0] for collator in collator_list])

batch = ExtrinsicBatch(self._substrate, KP_GLOBAL_SUDO)
batch.compose_sudo_call(
'ParachainStaking',
'force_remove_candidate',
{
'collator': self._kp_new_collator.ss58_address,
}
)
receipt = batch.execute()
self.assertEqual(receipt.is_success, True, f'force_remove_candidate fails, receipt: {receipt}')

def test_delegator_revoke(self):
contract = get_contract(self._w3, PARACHAIN_STAKING_ADDR, PARACHAIN_STAKING_ABI_FILE)
out = contract.functions.getCollatorList().call()
out = sorted(out, key=lambda x: x[1], reverse=True)

collator_eth_addr = out[0][0]
collator_num = out[0][1]
Expand Down Expand Up @@ -348,6 +454,7 @@ def test_delegator_customized_claim(self):

contract = get_contract(self._w3, PARACHAIN_STAKING_ADDR, PARACHAIN_STAKING_ABI_FILE)
out = contract.functions.getCollatorList().call()
out = sorted(out, key=lambda x: x[1], reverse=True)

collator_eth_addr = out[0][0]
collator_num = out[0][1]
Expand Down
2 changes: 0 additions & 2 deletions tests/evm_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,6 @@ def compose_function_name(w3, tx):
input_data = tx['data']
function_selector = input_data[:10]
if function_selector not in func_selector_dict:
import pdb
pdb.set_trace()
return f'Unknown.{function_selector}'

name = func_selector_dict[function_selector]
Expand Down
47 changes: 47 additions & 0 deletions tools/apr_v2_calculation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import sys
sys.path.append('./')


from substrateinterface import SubstrateInterface


URL = 'wss://docker-test.peaq.network'
URL = 'wss://mpfn1.peaq.network'
# URL = 'wss://wss-async.agung.peaq.network'
INFLAITON_PERCENTAGE = 0.035
COLLATR_DELEGATOR_PERCENTAGE = 0.40
TOTAL_ISSUANCE_NUMBER = 4.2 * 10 ** 9 * 10 ** 18


if __name__ == '__main__':
substrate = SubstrateInterface(URL)
now_collators = substrate.query(
module='Session',
storage_function='Validators',
)
now_collators = now_collators.value
candidate_pool = substrate.query_map(
module='ParachainStaking',
storage_function='CandidatePool',
start_key=None,
page_size=1000,
)
candidate_pool = {k.value: v.value for k, v in candidate_pool.records}
in_candidate_pool = {collator: candidate_pool[collator] for collator in now_collators}
print(in_candidate_pool)

total_staking_number = sum([value['total'] for value in in_candidate_pool.values()])
print(total_staking_number)

for k, v in in_candidate_pool.items():
print(f'collator: {k}')
print(f' collator total: {v["total"]}')
# commission_rate = v['commission'] / 1000000
commission_rate = 0.0
print(f' commission rate: {commission_rate}')
for delegator in v['delegators']:
print(f' delegator: {delegator["owner"]}')
print(f' delegator stake: {delegator["amount"]}')
print(f' delegator apr: {(INFLAITON_PERCENTAGE * COLLATR_DELEGATOR_PERCENTAGE * TOTAL_ISSUANCE_NUMBER * (1 - commission_rate) / total_staking_number * 100)}') # noqa
print(f' sum: {total_staking_number}')
print(f' collator apr: {(INFLAITON_PERCENTAGE * COLLATR_DELEGATOR_PERCENTAGE * TOTAL_ISSUANCE_NUMBER * (v["stake"] + commission_rate * sum([delegator["amount"] for delegator in v["delegators"]])) / total_staking_number / v["stake"] * 100)}') # noqa
Loading
Loading