Skip to content

Commit

Permalink
Move Command.RegisterToken to top level
Browse files Browse the repository at this point in the history
  • Loading branch information
yrong committed Apr 4, 2024
1 parent 79c1ba0 commit fdf15aa
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 37 deletions.
21 changes: 15 additions & 6 deletions contracts/src/Gateway.sol
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ import {
SetOperatingModeParams,
TransferNativeFromAgentParams,
SetTokenTransferFeesParams,
SetPricingParametersParams
SetPricingParametersParams,
RegisterForeignTokenParams
} from "./Params.sol";

import {CoreStorage} from "./storage/CoreStorage.sol";
Expand Down Expand Up @@ -214,6 +215,11 @@ contract Gateway is IGateway, IInitializable {
catch {
success = false;
}
} else if (message.command == Command.RegisterForeignToken) {
try Gateway(this).registerForeignToken{gas: maxDispatchGas}(message.params) {}
catch {
success = false;
}
}

// Calculate a gas refund, capped to protect against huge spikes in `tx.gasprice`
Expand Down Expand Up @@ -280,11 +286,7 @@ contract Gateway is IGateway, IInitializable {
(AgentExecuteCommand command, bytes memory commandParams) =
abi.decode(params.payload, (AgentExecuteCommand, bytes));

if (command == AgentExecuteCommand.RegisterToken) {
(bytes32 tokenID, string memory name, string memory symbol, uint8 decimals) =
abi.decode(commandParams, (bytes32, string, string, uint8));
Assets.registerForeignToken(params.agentID, agent, tokenID, name, symbol, decimals);
} else if (command == AgentExecuteCommand.MintToken) {
if (command == AgentExecuteCommand.MintToken) {
(bytes32 tokenID, address recipient, uint256 amount) =
abi.decode(commandParams, (bytes32, address, uint256));
Assets.mintForeignToken(AGENT_EXECUTOR, agent, tokenID, recipient, amount);
Expand Down Expand Up @@ -418,6 +420,13 @@ contract Gateway is IGateway, IInitializable {
/**
* Assets
*/
// @dev Register a new fungible Polkadot token for an agent
function registerForeignToken(bytes calldata data) external onlySelf {
RegisterForeignTokenParams memory params = abi.decode(data, (RegisterForeignTokenParams));
address agent = _ensureAgent(params.agentID);
Assets.registerForeignToken(params.agentID, agent, params.tokenID, params.name, params.symbol, params.decimals);
}

function isTokenRegistered(address token) external view returns (bool) {
return Assets.isTokenRegistered(token);
}
Expand Down
14 changes: 14 additions & 0 deletions contracts/src/Params.sol
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,17 @@ struct SetPricingParametersParams {
/// @dev Fee multiplier
UD60x18 multiplier;
}

// Payload for RegisterForeignToken
struct RegisterForeignTokenParams {
/// @dev The agent ID of the consensus system
bytes32 agentID;
/// @dev The token ID
bytes32 tokenID;
/// @dev The name of the token
string name;
/// @dev The symbol of the token
string symbol;
/// @dev The decimal of the token
uint8 decimals;
}
4 changes: 2 additions & 2 deletions contracts/src/Types.sol
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,12 @@ enum Command {
SetOperatingMode,
TransferNativeFromAgent,
SetTokenTransferFees,
SetPricingParameters
SetPricingParameters,
RegisterForeignToken
}

enum AgentExecuteCommand {
TransferToken,
RegisterToken,
MintToken
}

Expand Down
37 changes: 8 additions & 29 deletions contracts/test/Gateway.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ import {
SetOperatingModeParams,
TransferNativeFromAgentParams,
SetTokenTransferFeesParams,
SetPricingParametersParams
SetPricingParametersParams,
RegisterForeignTokenParams
} from "../src/Params.sol";

import {
Expand Down Expand Up @@ -920,15 +921,18 @@ contract GatewayTest is Test {
}

function testAgentRegisterDot() public {
AgentExecuteParams memory params = AgentExecuteParams({
RegisterForeignTokenParams memory params = RegisterForeignTokenParams({
agentID: assetHubAgentID,
payload: abi.encode(AgentExecuteCommand.RegisterToken, abi.encode(dotTokenID, "DOT", "DOT", 10))
tokenID: dotTokenID,
name: "DOT",
symbol: "DOT",
decimals: 10
});

vm.expectEmit(true, true, false, false);
emit IGateway.ForeignTokenRegistered(bytes32(uint256(1)), assetHubAgentID, address(0));

GatewayMock(address(gateway)).agentExecutePublic(abi.encode(params));
GatewayMock(address(gateway)).registerForeignTokenPublic(abi.encode(params));
}

function testAgentMintDot() public {
Expand Down Expand Up @@ -964,29 +968,4 @@ contract GatewayTest is Test {

IGateway(address(gateway)).sendToken{value: 0.1 ether}(address(dotToken), destPara, recipientAddress32, 1, 1);
}

function testParseAgentExecuteCall() public {
bytes memory data =
hex"000000000000000000000000000000000000000000000000000000000000002081c5ab2571199e3188135178f3c2c8e2d268be1313d029b30f534fa579b69b79000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000001600000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000001008080778c30c20fa2ebc0ed18d2cbca1f30b027625c7d9d97f5d589721c91aeb6000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000c0000000000000000000000000000000000000000000000000000000000000000a0000000000000000000000000000000000000000000000000000000000000003646f7400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003646f740000000000000000000000000000000000000000000000000000000000";

AgentExecuteParams memory params = abi.decode(data, (AgentExecuteParams));

(AgentExecuteCommand command, bytes memory payload) = abi.decode(params.payload, (AgentExecuteCommand, bytes));

//Register foreign token
assertEq(uint256(command), uint256(1));

(bytes32 tokenID, string memory name, string memory symbol, uint8 decimals) =
abi.decode(payload, (bytes32, string, string, uint8));
assertEq(tokenID, 0x8080778c30c20fa2ebc0ed18d2cbca1f30b027625c7d9d97f5d589721c91aeb6);

console.log("name:%s", name);
console.log("symbol:%s", symbol);
console.log("decimals:%s", decimals);

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

GatewayMock(address(gateway)).agentExecutePublic(abi.encode(params));
}
}
4 changes: 4 additions & 0 deletions contracts/test/mocks/GatewayMock.sol
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ contract GatewayMock is Gateway {
function setPricingParametersPublic(bytes calldata params) external {
this.setPricingParameters(params);
}

function registerForeignTokenPublic(bytes calldata params) external {
this.registerForeignToken(params);
}
}

library AdditionalStorage {
Expand Down

0 comments on commit fdf15aa

Please sign in to comment.