Skip to content

Commit

Permalink
Added token address to the flow
Browse files Browse the repository at this point in the history
  • Loading branch information
NindoK committed Jan 27, 2025
1 parent 26fce6b commit ba08e65
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 18 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@
build/
.vscode/
.env

.history/*
16 changes: 11 additions & 5 deletions overridden_contracts/src/Gateway.sol
Original file line number Diff line number Diff line change
Expand Up @@ -510,15 +510,21 @@ contract Gateway is IOGateway, IInitializable, IUpgradable {
}

// Probably need to send me the token to be minted?
(uint256 epoch, uint256 eraIndex, uint256 totalPointsToken, uint256 totalTokensInflated, bytes32 rewardsRoot) =
abi.decode(data, (uint256, uint256, uint256, uint256, bytes32));
(
uint256 epoch,
uint256 eraIndex,
uint256 totalPointsToken,
uint256 totalTokensInflated,
bytes32 rewardsRoot,
bytes32 foreignTokenId
) = abi.decode(data, (uint256, uint256, uint256, uint256, bytes32, bytes32));

// ! We need a foreignTokenID to mint the token. I don't want to save it in the Assets storage otherwise it's another change to be made. Can we send directly the address of the token everytime?
// bytes32 foreignTokenID = bytes32(0);
// Assets.mintForeignToken(foreignTokenID, middlewareAddress, totalTokensInflated);
Assets.mintForeignToken(foreignTokenId, middlewareAddress, totalTokensInflated);

address tokenAddress = Assets.tokenAddressOf(foreignTokenId);
try IMiddlewareBasic(middlewareAddress).distributeRewards(
epoch, eraIndex, totalPointsToken, totalTokensInflated, rewardsRoot
epoch, eraIndex, totalPointsToken, totalTokensInflated, rewardsRoot, tokenAddress
) {} catch Error(string memory err) {
emit UnableToProcessRewardsS(epoch, eraIndex, totalPointsToken, totalTokensInflated, rewardsRoot, err);
} catch (bytes memory err) {
Expand Down
14 changes: 11 additions & 3 deletions overridden_contracts/src/interfaces/IMiddlewareBasic.sol
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,24 @@ pragma solidity ^0.8.0;

interface IMiddlewareBasic {
/**
* @notice Distribute rewards for a specific era contained in an epoch by providing a Merkle root, total points, and total amount of tokens.
* @notice Distribute rewards for a specific era contained in an epoch by providing a Merkle root, total points, total amount of tokens and the token address of the rewards.
* @param epoch network epoch of the middleware
* @param eraIndex era index of Starlight's rewards distribution
* @param totalPointsToken total amount of points for the reward distribution
* @param amount amount of tokens to distribute
* @param root Merkle root of the reward distribution
* @param tokenAddress The token address of the rewards
* @dev This function is called by the gateway only
* @dev Emit DistributeRewards event.
*/
function distributeRewards(uint256 epoch, uint256 eraIndex, uint256 totalPointsToken, uint256 amount, bytes32 root)
external;
function distributeRewards(
uint256 epoch,
uint256 eraIndex,
uint256 totalPointsToken,
uint256 amount,
bytes32 root,
address tokenAddress
) external;

/**
* @notice Slashes an operator's stake
Expand Down
40 changes: 30 additions & 10 deletions overridden_contracts/test/override_test/Gateway.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,24 @@ import {
import {IGateway} from "../../src/interfaces/IGateway.sol";
import {IMiddlewareBasic} from "../../src/interfaces/IMiddlewareBasic.sol";
import {MockGateway} from "../mocks/MockGateway.sol";
import {CreateAgentParams, CreateChannelParams} from "../../src/Params.sol";
import {
CreateAgentParams,
CreateChannelParams,
SetOperatingModeParams,
RegisterForeignTokenParams
} from "../../src/Params.sol";
import {OperatingMode, ParaID, Command} from "../../src/Types.sol";
import {GatewayProxy} from "../../src/GatewayProxy.sol";
import {MultiAddress} from "../../src/MultiAddress.sol";
import {AgentExecutor} from "../../src/AgentExecutor.sol";
import {SetOperatingModeParams} from "../../src/Params.sol";
import {Verification} from "../../src/Verification.sol";

import {Strings} from "openzeppelin/utils/Strings.sol";

import {Gateway} from "../../src/Gateway.sol";
import {IOGateway} from "../../src/interfaces/IOGateway.sol";
import {Operators} from "../../src/Operators.sol";
import {Assets} from "../../src/Assets.sol";
import {MockOGateway} from "../mocks/MockOGateway.sol";

//NEW
Expand Down Expand Up @@ -170,14 +175,29 @@ contract GatewayTest is Test {
return (Command.ReportSlashes, abi.encode(IOGateway.SlashParams({eraIndex: eraIndex, slashes: slashes})));
}

function _makeReportRewardsCommand() public pure returns (Command, bytes memory) {
function _makeReportRewardsCommand() public returns (Command, bytes memory, address) {
uint256 epoch = 0;
uint256 eraIndex = 1;
uint256 totalPointsToken = 1 ether;
uint256 tokensInflatedToken = 1 ether;
bytes32 rewardsRoot = bytes32(uint256(1));
bytes32 foreignTokenId = bytes32(uint256(1));

RegisterForeignTokenParams memory params =
RegisterForeignTokenParams({foreignTokenID: dotTokenID, name: "Test", symbol: "TST", decimals: 10});

vm.expectEmit(true, true, false, false);
emit IGateway.ForeignTokenRegistered(foreignTokenId, address(0));

return (Command.ReportRewards, abi.encode(epoch, eraIndex, totalPointsToken, tokensInflatedToken, rewardsRoot));
MockGateway(address(gateway)).registerForeignTokenPublic(abi.encode(params));

address tokenAddress = Assets.tokenAddressOf(foreignTokenId);

return (
Command.ReportRewards,
abi.encode(epoch, eraIndex, totalPointsToken, tokensInflatedToken, rewardsRoot, foreignTokenId),
tokenAddress
);
}

function makeMockProof() public pure returns (Verification.Proof memory) {
Expand Down Expand Up @@ -443,10 +463,10 @@ contract GatewayTest is Test {
}
}

function testSubmitRewards() public {
function testSubmitRewardsx() public {
deal(assetHubAgent, 50 ether);

(Command command, bytes memory params) = _makeReportRewardsCommand();
(Command command, bytes memory params, address tokenAddress) = _makeReportRewardsCommand();

// We mock the call so that it does not revert
vm.mockCall(address(1), abi.encodeWithSelector(IMiddlewareBasic.distributeRewards.selector), abi.encode(true));
Expand All @@ -468,7 +488,7 @@ contract GatewayTest is Test {
function testSubmitRewardsWithoutMiddleware() public {
deal(assetHubAgent, 50 ether);

(Command command, bytes memory params) = _makeReportRewardsCommand();
(Command command, bytes memory params, address tokenAddress) = _makeReportRewardsCommand();

vm.expectEmit(true, true, true, true);
emit IOGateway.UnableToProcessRewardsMessageB(abi.encodeWithSelector(Gateway.MiddlewareNotSet.selector));
Expand All @@ -488,7 +508,7 @@ contract GatewayTest is Test {
function testSubmitRewardsWithMiddlewareNotComplyingInterface() public {
deal(assetHubAgent, 50 ether);

(Command command, bytes memory params) = _makeReportRewardsCommand();
(Command command, bytes memory params, address tokenAddress) = _makeReportRewardsCommand();

IOGateway(address(gateway)).setMiddleware(0x0123456789012345678901234567890123456789);

Expand All @@ -513,7 +533,7 @@ contract GatewayTest is Test {
function testSubmitRewardsWithMiddlewareComplyingInterfaceAndRewardsRevert() public {
deal(assetHubAgent, 50 ether);

(Command command, bytes memory params) = _makeReportRewardsCommand();
(Command command, bytes memory params, address tokenAddress) = _makeReportRewardsCommand();

bytes memory expectedError = bytes("can't process rewards"); //This should actually come from IODefaultOperatorRewards

Expand Down Expand Up @@ -554,7 +574,7 @@ contract GatewayTest is Test {
function testSubmitRewardsWithMiddlewareComplyingInterfaceAndRewardsProcessed() public {
deal(assetHubAgent, 50 ether);

(Command command, bytes memory params) = _makeReportRewardsCommand();
(Command command, bytes memory params, address tokenAddress) = _makeReportRewardsCommand();

// We mock the call so that it does not revert
vm.mockCall(address(1), abi.encodeWithSelector(IMiddlewareBasic.distributeRewards.selector), abi.encode(true));
Expand Down

0 comments on commit ba08e65

Please sign in to comment.