Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
ConstantKeys.VALUE: hex(value)[2:],
ConstantKeys.FEE: hex(fixed_fee),
ConstantKeys.TIMESTAMP: hex(timestamp),
ConstantKeys.NONCE: hex(nonce),
ConstantKeys.SIGNATURE: signature,
},
},
],
ConstantKeys.PREV_BLOCK_GENERATOR: str(prev_block_generator),
ConstantKeys.PREV_BLOCK_VALIDATORS: [
str(addr) for addr in prev_block_validators
],
ConstantKeys.PREV_BLOCK_VOTES: [[str(addr), hex(v)] for addr, v in prev_votes],
}
ret_params = TypeConverter.convert(request, ParamType.INVOKE)
block_params = ret_params[ConstantKeys.BLOCK]
assert block_height == block_params[ConstantKeys.BLOCK_HEIGHT]
assert block_hash == block_params[ConstantKeys.BLOCK_HASH]
assert timestamp == block_params[ConstantKeys.TIMESTAMP]
assert prev_block_hash == block_params[ConstantKeys.PREV_BLOCK_HASH]
transaction_params = ret_params[ConstantKeys.TRANSACTIONS][0]
assert method == transaction_params[ConstantKeys.METHOD]
transaction_params_params = transaction_params[ConstantKeys.PARAMS]
assert tx_hash == transaction_params_params[ConstantKeys.TX_HASH]
assert version == transaction_params_params[ConstantKeys.VERSION]
assert from_addr == transaction_params_params[ConstantKeys.FROM]
assert to_addr == transaction_params_params[ConstantKeys.TO]
assert value == transaction_params_params[ConstantKeys.VALUE]
"timestamp": hex(timestamp),
"nonce": hex(nonce),
"signature": signature,
"dataType": data_type,
"data": {
"method": data_method,
"params": {
"from": str(data_from),
"to": str(data_to),
"value": hex(data_value)
}
}
}
}
ret_params = TypeConverter.convert(request_params, ParamType.VALIDATE_TRANSACTION)
self.assertEqual(method, ret_params['method'])
self.assertEqual(version, ret_params['params']['version'])
self.assertEqual(from_addr, ret_params['params']['from'])
self.assertEqual(to_addr, ret_params['params']['to'])
self.assertEqual(value, ret_params['params']['value'])
self.assertEqual(step_limit, ret_params['params']['stepLimit'])
self.assertEqual(timestamp, ret_params['params']['timestamp'])
self.assertEqual(nonce, ret_params['params']['nonce'])
self.assertEqual(signature, ret_params['params']['signature'])
self.assertEqual(data_type, ret_params['params']['dataType'])
self.assertEqual(data_method, ret_params['params']['data']['method'])
self.assertNotEqual(data_from, ret_params['params']['data']['params']['from'])
self.assertNotEqual(data_to, ret_params['params']['data']['params']['to'])
self.assertNotEqual(data_value, ret_params['params']['data']['params']['value'])
def test_icx_total_supply_convert():
version = 3
request = {ConstantKeys.VERSION: hex(version)}
ret_params = TypeConverter.convert(request, ParamType.ICX_GET_TOTAL_SUPPLY)
assert version == ret_params[ConstantKeys.VERSION]
request_params = {
"method": method,
"params": {
"tx_hash": bytes.hex(tx_hash),
"from": str(from_addr),
"to": str(to_addr),
"value": hex(value),
"fee": hex(fee),
"timestamp": hex(timestamp),
"nonce": hex(nonce),
"signature": signature
}
}
ret_params = TypeConverter.convert(request_params, ParamType.VALIDATE_TRANSACTION)
self.assertEqual(method, ret_params['method'])
self.assertEqual(tx_hash, ret_params['params']['txHash'])
self.assertEqual(from_addr, ret_params['params']['from'])
self.assertEqual(to_addr, ret_params['params']['to'])
self.assertEqual(value, ret_params['params']['value'])
self.assertEqual(fee, ret_params['params']['fee'])
self.assertEqual(timestamp, ret_params['params']['timestamp'])
self.assertEqual(nonce, ret_params['params']['nonce'])
self.assertEqual(signature, ret_params['params']['signature'])
request = {
ConstantKeys.METHOD: method,
ConstantKeys.PARAMS: {
ConstantKeys.VERSION: hex(version),
ConstantKeys.FROM: str(from_addr),
ConstantKeys.TO: str(to_addr),
ConstantKeys.DATA_TYPE: data_type,
ConstantKeys.DATA: {
ConstantKeys.METHOD: data_method,
ConstantKeys.PARAMS: {ConstantKeys.ADDRESS: str(data_addr),},
},
},
}
ret_params = TypeConverter.convert(request, ParamType.QUERY)
assert method == ret_params[ConstantKeys.METHOD]
params_params = ret_params[ConstantKeys.PARAMS]
assert version == params_params[ConstantKeys.VERSION]
assert from_addr == params_params[ConstantKeys.FROM]
assert to_addr == params_params[ConstantKeys.TO]
assert data_type == params_params[ConstantKeys.DATA_TYPE]
data_params = params_params[ConstantKeys.DATA]
assert data_method == data_params[ConstantKeys.METHOD]
data_params_params = data_params[ConstantKeys.PARAMS]
assert data_addr != data_params_params[ConstantKeys.ADDRESS]
def test_set_delegation(self):
address1 = create_address()
value1 = 1 * 10 ** 18
address2 = create_address()
value2 = 2 * 10 ** 18
request = [
{ConstantKeys.ADDRESS: str(address1), ConstantKeys.VALUE: hex(value1)},
{ConstantKeys.ADDRESS: str(address2), ConstantKeys.VALUE: hex(value2)},
]
ret_delegations = TypeConverter.convert(request, ParamType.IISS_SET_DELEGATION)
self.assertEqual(
address1,
ret_delegations[0][ConstantKeys.ADDRESS],
f"{type(ret_delegations[0][ConstantKeys.ADDRESS])}",
)
self.assertEqual(value1, ret_delegations[0][ConstantKeys.VALUE])
self.assertEqual(address2, ret_delegations[1][ConstantKeys.ADDRESS])
self.assertEqual(value2, ret_delegations[1][ConstantKeys.VALUE])
def test_query_convert_icx_get_score_api():
method = "icx_getScoreApi"
version = 3
addr1 = create_address()
request = {
ConstantKeys.METHOD: method,
ConstantKeys.PARAMS: {
ConstantKeys.VERSION: hex(version),
ConstantKeys.ADDRESS: str(addr1),
},
}
ret_params = TypeConverter.convert(request, ParamType.QUERY)
assert method == ret_params[ConstantKeys.METHOD]
params_params = ret_params[ConstantKeys.PARAMS]
assert version == params_params[ConstantKeys.VERSION]
assert addr1 == params_params[ConstantKeys.ADDRESS]
def handle_set_stake(self, context: 'IconScoreContext', params: dict):
address: 'Address' = context.tx.origin
ret_params: dict = TypeConverter.convert(params, ParamType.IISS_SET_STAKE)
stake: int = ret_params[ConstantKeys.VALUE]
total_stake: int = context.storage.iiss.get_total_stake(context)
if not isinstance(stake, int) or stake < 0:
raise InvalidParamsException('Failed to stake: value is not int type or value < 0')
account: 'Account' = context.storage.icx.get_account(context, address, Intent.ALL)
self._check_from_can_stake(context, stake, account)
unstake_lock_period: int = self._calculate_unstake_lock_period(context.storage.iiss.lock_min,
context.storage.iiss.lock_max,
context.storage.iiss.reward_point,
total_stake,
context.total_supply)
# subtract account's staked amount from the total stake
total_stake -= account.stake