From 315fd9af8a423d8ec7286c9f21a1b81dfa2ba368 Mon Sep 17 00:00:00 2001 From: Bogdan Kovtun Date: Mon, 2 Aug 2021 23:12:35 +0300 Subject: [PATCH 1/2] Add title to reward programs registry --- contracts/EVMScriptFactories/AddRewardProgram.sol | 9 +++++---- contracts/RewardProgramsRegistry.sol | 6 +++--- .../evm_script_factories/test_add_reward_program.py | 10 ++++++---- .../test_remove_reward_program.py | 2 +- .../test_top_up_reward_programs.py | 12 ++++++------ tests/integration_test.py | 3 ++- tests/test_reward_programs_registry.py | 11 ++++++----- 7 files changed, 29 insertions(+), 24 deletions(-) diff --git a/contracts/EVMScriptFactories/AddRewardProgram.sol b/contracts/EVMScriptFactories/AddRewardProgram.sol index 678c24e0..1d54f2ef 100644 --- a/contracts/EVMScriptFactories/AddRewardProgram.sol +++ b/contracts/EVMScriptFactories/AddRewardProgram.sol @@ -47,8 +47,9 @@ contract AddRewardProgram is TrustedCaller, IEVMScriptFactory { onlyTrustedCaller(_creator) returns (bytes memory) { + (address rewardProgramAddress,) = _decodeEVMScriptCallData(_evmScriptCallData); require( - !rewardProgramsRegistry.isRewardProgram(_decodeEVMScriptCallData(_evmScriptCallData)), + !rewardProgramsRegistry.isRewardProgram(rewardProgramAddress), ERROR_REWARD_PROGRAM_ALREADY_ADDED ); @@ -66,7 +67,7 @@ contract AddRewardProgram is TrustedCaller, IEVMScriptFactory { function decodeEVMScriptCallData(bytes memory _evmScriptCallData) external pure - returns (address) + returns (address, string memory) { return _decodeEVMScriptCallData(_evmScriptCallData); } @@ -78,8 +79,8 @@ contract AddRewardProgram is TrustedCaller, IEVMScriptFactory { function _decodeEVMScriptCallData(bytes memory _evmScriptCallData) private pure - returns (address) + returns (address, string memory) { - return abi.decode(_evmScriptCallData, (address)); + return abi.decode(_evmScriptCallData, (address, string)); } } diff --git a/contracts/RewardProgramsRegistry.sol b/contracts/RewardProgramsRegistry.sol index ad0af851..23835b77 100644 --- a/contracts/RewardProgramsRegistry.sol +++ b/contracts/RewardProgramsRegistry.sol @@ -12,7 +12,7 @@ contract RewardProgramsRegistry is TrustedCaller { // ------------- // EVENTS // ------------- - event RewardProgramAdded(address indexed _rewardProgram); + event RewardProgramAdded(address indexed _rewardProgram, string _title); event RewardProgramRemoved(address indexed _rewardProgram); // ------------- @@ -43,12 +43,12 @@ contract RewardProgramsRegistry is TrustedCaller { // ------------- /// @notice Adds address to list of allowed reward programs - function addRewardProgram(address _rewardProgram) external onlyTrustedCaller(msg.sender) { + function addRewardProgram(address _rewardProgram, string memory _title) external onlyTrustedCaller(msg.sender) { require(rewardProgramIndices[_rewardProgram] == 0, ERROR_REWARD_PROGRAM_ALREADY_ADDED); rewardPrograms.push(_rewardProgram); rewardProgramIndices[_rewardProgram] = rewardPrograms.length; - emit RewardProgramAdded(_rewardProgram); + emit RewardProgramAdded(_rewardProgram, _title); } /// @notice Removes address from list of allowed reward programs diff --git a/tests/evm_script_factories/test_add_reward_program.py b/tests/evm_script_factories/test_add_reward_program.py index 9179b225..466333fb 100644 --- a/tests/evm_script_factories/test_add_reward_program.py +++ b/tests/evm_script_factories/test_add_reward_program.py @@ -3,7 +3,8 @@ from utils.evm_script import encode_call_script REWARD_PROGRAM_ADDRESS = "0xFFfFfFffFFfffFFfFFfFFFFFffFFFffffFfFFFfF" -EVM_SCRIPT_CALL_DATA = "0x" + encode_single("(address)", [REWARD_PROGRAM_ADDRESS]).hex() +REWARD_PROGRAM_TITLE = "New Reward Program" +EVM_SCRIPT_CALL_DATA = "0x" + encode_single("(address,string)", [REWARD_PROGRAM_ADDRESS, REWARD_PROGRAM_TITLE]).hex() def test_deploy(owner, reward_programs_registry): @@ -25,7 +26,7 @@ def test_create_evm_script_reward_program_already_added( "Must revert with message 'REWARD_PROGRAM_ALREADY_ADDED'" "if reward program already listed in RewardProgramsRegistry" reward_programs_registry.addRewardProgram( - REWARD_PROGRAM_ADDRESS, {"from": evm_script_executor_stub} + REWARD_PROGRAM_ADDRESS, REWARD_PROGRAM_TITLE, {"from": evm_script_executor_stub} ) assert reward_programs_registry.isRewardProgram(REWARD_PROGRAM_ADDRESS) @@ -44,7 +45,8 @@ def test_create_evm_script(owner, add_reward_program, reward_programs_registry): ( reward_programs_registry.address, reward_programs_registry.addRewardProgram.encode_input( - REWARD_PROGRAM_ADDRESS + REWARD_PROGRAM_ADDRESS, + REWARD_PROGRAM_TITLE ), ) ] @@ -57,5 +59,5 @@ def test_decode_evm_script_call_data(add_reward_program): "Must decode EVMScript call data correctly" assert ( add_reward_program.decodeEVMScriptCallData(EVM_SCRIPT_CALL_DATA) - == REWARD_PROGRAM_ADDRESS + == (REWARD_PROGRAM_ADDRESS, REWARD_PROGRAM_TITLE) ) diff --git a/tests/evm_script_factories/test_remove_reward_program.py b/tests/evm_script_factories/test_remove_reward_program.py index 4b5c3403..ea4ccc7c 100644 --- a/tests/evm_script_factories/test_remove_reward_program.py +++ b/tests/evm_script_factories/test_remove_reward_program.py @@ -38,7 +38,7 @@ def test_create_evm_script( ): "Must create correct EVMScript if all requirements are met" reward_programs_registry.addRewardProgram( - REWARD_PROGRAM_ADDRESS, {"from": evm_script_executor_stub} + REWARD_PROGRAM_ADDRESS, "", {"from": evm_script_executor_stub} ) evm_script = remove_reward_program.createEVMScript(owner, EVM_SCRIPT_CALL_DATA) expected_evm_script = encode_call_script( diff --git a/tests/evm_script_factories/test_top_up_reward_programs.py b/tests/evm_script_factories/test_top_up_reward_programs.py index 439e22b4..4c372876 100644 --- a/tests/evm_script_factories/test_top_up_reward_programs.py +++ b/tests/evm_script_factories/test_top_up_reward_programs.py @@ -52,10 +52,10 @@ def test_create_evm_script_zero_amount( "Must revert with message 'ZERO_AMOUNT' if some value in amounts has zero value" amounts = [1 ** 18, 0] reward_programs_registry.addRewardProgram( - REWARD_PROGRAM_ADDRESSES[0], {"from": evm_script_executor_stub} + REWARD_PROGRAM_ADDRESSES[0], "", {"from": evm_script_executor_stub} ) reward_programs_registry.addRewardProgram( - REWARD_PROGRAM_ADDRESSES[1], {"from": evm_script_executor_stub} + REWARD_PROGRAM_ADDRESSES[1], "", {"from": evm_script_executor_stub} ) with reverts("ZERO_AMOUNT"): top_up_reward_programs.createEVMScript( @@ -78,10 +78,10 @@ def test_create_evm_script_reward_program_not_allowed( ) reward_programs_registry.addRewardProgram( - REWARD_PROGRAM_ADDRESSES[0], {"from": evm_script_executor_stub} + REWARD_PROGRAM_ADDRESSES[0], "", {"from": evm_script_executor_stub} ) reward_programs_registry.addRewardProgram( - REWARD_PROGRAM_ADDRESSES[1], {"from": evm_script_executor_stub} + REWARD_PROGRAM_ADDRESSES[1], "", {"from": evm_script_executor_stub} ) # case with added reward programs @@ -110,10 +110,10 @@ def test_create_evm_script( "Must create correct EVMScript if all requirements are met" # add reward programs reward_programs_registry.addRewardProgram( - REWARD_PROGRAM_ADDRESSES[0], {"from": evm_script_executor_stub} + REWARD_PROGRAM_ADDRESSES[0], "", {"from": evm_script_executor_stub} ) reward_programs_registry.addRewardProgram( - REWARD_PROGRAM_ADDRESSES[1], {"from": evm_script_executor_stub} + REWARD_PROGRAM_ADDRESSES[1], "", {"from": evm_script_executor_stub} ) evm_script = top_up_reward_programs.createEVMScript( diff --git a/tests/integration_test.py b/tests/integration_test.py index 36ee37e7..bdc82b3c 100644 --- a/tests/integration_test.py +++ b/tests/integration_test.py @@ -185,6 +185,7 @@ def test_reward_programs_easy_track( ): deployer = accounts[0] reward_program = accounts[5] + reward_program_title = "New Reward Program" trusted_address = accounts[7] # deploy easy track @@ -287,7 +288,7 @@ def test_reward_programs_easy_track( # create new motion to add reward program tx = easy_track.createMotion( add_reward_program, - encode_single("(address)", [reward_program.address]), + encode_single("(address,string)", [reward_program.address, reward_program_title]), {"from": trusted_address}, ) diff --git a/tests/test_reward_programs_registry.py b/tests/test_reward_programs_registry.py index 53e4151c..92625859 100644 --- a/tests/test_reward_programs_registry.py +++ b/tests/test_reward_programs_registry.py @@ -13,21 +13,22 @@ def test_add_reward_program_called_by_stranger( ): "Must revert with message 'CALLER_IS_FORBIDDEN' error if called not by trustedCaller" with reverts("CALLER_IS_FORBIDDEN"): - reward_programs_registry.addRewardProgram(stranger, {"from": stranger}) + reward_programs_registry.addRewardProgram(stranger, "", {"from": stranger}) def test_add_reward_program( evm_script_executor_stub, stranger, reward_programs_registry ): - "Must add new reward program to rewardPrograms array and emit RewardProgramAdded(_rewardProgram) event." + "Must add new reward program to rewardPrograms array and emit RewardProgramAdded(_rewardProgram, _title) event." "When called with already added reward program fails with error 'REWARD_PROGRAM_ALREADY_ADDED' error" new_reward_program = stranger tx = reward_programs_registry.addRewardProgram( - new_reward_program, {"from": evm_script_executor_stub} + new_reward_program, "Reward Program", {"from": evm_script_executor_stub} ) # validate events assert len(tx.events) == 1 assert tx.events["RewardProgramAdded"]["_rewardProgram"] == new_reward_program + assert tx.events["RewardProgramAdded"]["_title"] == "Reward Program" # validate that reward program was added reward_programs = reward_programs_registry.getRewardPrograms() @@ -37,7 +38,7 @@ def test_add_reward_program( # fails when reward program adds second time with reverts("REWARD_PROGRAM_ALREADY_ADDED"): reward_programs_registry.addRewardProgram( - new_reward_program, {"from": evm_script_executor_stub} + new_reward_program, "Reward Program", {"from": evm_script_executor_stub} ) @@ -65,7 +66,7 @@ def test_remove_reward_program(reward_programs_registry, evm_script_executor_stu for reward_program in reward_programs: reward_programs_registry.addRewardProgram( - reward_program, {"from": evm_script_executor_stub} + reward_program, "", {"from": evm_script_executor_stub} ) while len(reward_programs) > 0: From fb23f734ffa00ed3cd64182c91f7ff96f9e6d111 Mon Sep 17 00:00:00 2001 From: Bogdan Kovtun Date: Mon, 2 Aug 2021 23:16:48 +0300 Subject: [PATCH 2/2] Update specification --- specification.md | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/specification.md b/specification.md index 16537818..52ed1b44 100644 --- a/specification.md +++ b/specification.md @@ -401,14 +401,14 @@ Creates EVMScript to add new reward program address to `RewardProgramsRegistry`. #### function createEVMScript(address \_creator, bytes \_evmScriptCallData) external view returns (bytes) -Creates EVMScript to add new reward address to `RewardProgramsRegistry`. `_evmScriptCallData` contains encoded tuple: `(address _rewardProgram)`, where `_rewardProgram` - new reward program address to add. To successfully create EVMScript next requirements must be met: +Creates EVMScript to add new reward address to `RewardProgramsRegistry`. `_evmScriptCallData` contains encoded tuple: `(address _rewardProgram, string _title)`, where `_rewardProgram` - new reward program address to add, `_title` - title of new reward program. To successfully create EVMScript next requirements must be met: - `_creator` must be equal to `trustedCaller` address - `_rewardProgram` address hasn't been added in `RewardProgramsRegistry` earlier. -#### function decodeEVMScriptCallData(bytes \_evmScriptCallData) external returns (address \_rewardProgram) +#### function decodeEVMScriptCallData(bytes \_evmScriptCallData) external returns (address \_rewardProgram, string memory _title) -Decodes `_evmScriptCallData` into tuple `(address _rewardProgram)`. +Decodes `_evmScriptCallData` into tuple `(address _rewardProgram, string _title)`. ## RemoveRewardProgram @@ -435,14 +435,26 @@ Stores list of addresses with reward programs. TopUpRewardsProgram EVMScript fac ### Methods -#### addRewardProgram(address \_rewardProgram) external +#### addRewardProgram(address \_rewardProgram, string memory _title) external Adds reward program address to RewardProgramsRegistry, if it hasn't been added yet, throws `"REWARD_PROGRAM_ALREADY_ADDED"` in other cases. Might be called only by EVMScriptExecutor contract. +Events: + +```solidity= +event RewardProgramAdded(address indexed _rewardProgram, string _title) +``` + #### removeRewardProgram(address \_rewardProgram) external Removes reward program address from RewardProgramsRegistry. Throws `"REWARD_PROGRAM_NOT_FOUND"` if program address misses from the array. Might be called only by EVMScriptExecutor contract. +Events: + +```solidity= +event RewardProgramRemoved(address indexed _rewardProgram) +``` + #### isRewardProgram(address \_rewardProgram) external view (returns bool) Shows if address is whitelisted in RewardProgramsRegistry.