Skip to content

Commit

Permalink
Merge pull request #3 from lidofinance/feature/reward-program-name
Browse files Browse the repository at this point in the history
Add name to reward programs
  • Loading branch information
Psirex authored Aug 6, 2021
2 parents ec694ad + fb23f73 commit bc0904c
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 28 deletions.
9 changes: 5 additions & 4 deletions contracts/EVMScriptFactories/AddRewardProgram.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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
);

Expand All @@ -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);
}
Expand All @@ -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));
}
}
6 changes: 3 additions & 3 deletions contracts/RewardProgramsRegistry.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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);

// -------------
Expand Down Expand Up @@ -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
Expand Down
20 changes: 16 additions & 4 deletions specification.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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.
Expand Down
10 changes: 6 additions & 4 deletions tests/evm_script_factories/test_add_reward_program.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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)

Expand All @@ -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
),
)
]
Expand All @@ -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)
)
2 changes: 1 addition & 1 deletion tests/evm_script_factories/test_remove_reward_program.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
12 changes: 6 additions & 6 deletions tests/evm_script_factories/test_top_up_reward_programs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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
Expand Down Expand Up @@ -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(
Expand Down
3 changes: 2 additions & 1 deletion tests/integration_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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},
)

Expand Down
11 changes: 6 additions & 5 deletions tests/test_reward_programs_registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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}
)


Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit bc0904c

Please sign in to comment.