-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add getter functions for tokens and destination chains
- Loading branch information
Showing
3 changed files
with
118 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,12 +13,13 @@ import {INativeTokenTransferrer} from | |
import {SendTokensInput} from "@avalabs/avalanche-ictt/interfaces/ITokenTransferrer.sol"; | ||
import {IWarpMessenger} from | ||
"@avalabs/[email protected]/contracts/interfaces/IWarpMessenger.sol"; | ||
import {SafeERC20TransferFrom} from "@teleporter/SafeERC20TransferFrom.sol"; | ||
|
||
import {Ownable} from "@openzeppelin/[email protected]/access/Ownable.sol"; | ||
import {ReentrancyGuard} from "@openzeppelin/[email protected]/security/ReentrancyGuard.sol"; | ||
import {IERC20} from "@openzeppelin/[email protected]/token/ERC20/IERC20.sol"; | ||
import {SafeERC20} from "@openzeppelin/[email protected]/token/ERC20/utils/SafeERC20.sol"; | ||
import {Address} from "@openzeppelin/[email protected]/utils/Address.sol"; | ||
import {SafeERC20TransferFrom} from "@teleporter/SafeERC20TransferFrom.sol"; | ||
|
||
/** | ||
* @title AvalancheICTTRouter | ||
|
@@ -29,6 +30,9 @@ import {SafeERC20TransferFrom} from "@teleporter/SafeERC20TransferFrom.sol"; | |
contract AvalancheICTTRouter is Ownable, ReentrancyGuard, IAvalancheICTTRouter { | ||
using Address for address; | ||
|
||
/// @notice List of tokens deployed on the source chain | ||
address[] public tokensList; | ||
|
||
/** | ||
* @notice Token address => source bridge address | ||
* @notice Address `0x0` is used for the native token | ||
|
@@ -43,6 +47,13 @@ contract AvalancheICTTRouter is Ownable, ReentrancyGuard, IAvalancheICTTRouter { | |
bytes32 destinationChainID => mapping(address token => DestinationBridge destinationBridge) | ||
) public tokenDestinationChainToDestinationBridge; | ||
|
||
/** | ||
* @notice Token Address => list of destination chains | ||
* @notice Address `0x0` is used for the native token | ||
*/ | ||
mapping(address token => bytes32[] destinationChainsIDList) public | ||
tokenToDestinationChainsIDList; | ||
|
||
/// @notice Router chain ID | ||
bytes32 private immutable routerChainID; | ||
|
||
|
@@ -62,6 +73,7 @@ contract AvalancheICTTRouter is Ownable, ReentrancyGuard, IAvalancheICTTRouter { | |
revert AvalancheICTTRouter__BridgeAddrNotAContract(bridgeAddress); | ||
} | ||
tokenToSourceBridge[tokenAddress] = bridgeAddress; | ||
tokensList.push(tokenAddress); | ||
|
||
emit RegisterSourceTokenBridge(tokenAddress, bridgeAddress); | ||
} | ||
|
@@ -89,13 +101,15 @@ contract AvalancheICTTRouter is Ownable, ReentrancyGuard, IAvalancheICTTRouter { | |
DestinationBridge(bridgeAddress, requiredGasLimit, isMultihop); | ||
tokenDestinationChainToDestinationBridge[destinationChainID][tokenAddress] = | ||
destinationBridge; | ||
tokenToDestinationChainsIDList[tokenAddress].push(destinationChainID); | ||
|
||
emit RegisterDestinationTokenBridge(tokenAddress, destinationChainID, destinationBridge); | ||
} | ||
|
||
/// @inheritdoc IAvalancheICTTRouter | ||
function removeSourceTokenBridge(address tokenAddress) external onlyOwner { | ||
delete tokenToSourceBridge[tokenAddress]; | ||
_burnToken(tokenAddress); | ||
|
||
emit RemoveSourceTokenBridge(tokenAddress); | ||
} | ||
|
@@ -106,6 +120,7 @@ contract AvalancheICTTRouter is Ownable, ReentrancyGuard, IAvalancheICTTRouter { | |
bytes32 destinationChainID | ||
) external onlyOwner { | ||
delete tokenDestinationChainToDestinationBridge[destinationChainID][tokenAddress]; | ||
_burnDestinationChainID(tokenAddress, destinationChainID); | ||
|
||
emit RemoveDestinationTokenBridge(tokenAddress, destinationChainID); | ||
} | ||
|
@@ -207,4 +222,44 @@ contract AvalancheICTTRouter is Ownable, ReentrancyGuard, IAvalancheICTTRouter { | |
) external view returns (DestinationBridge memory) { | ||
return tokenDestinationChainToDestinationBridge[chainID][token]; | ||
} | ||
|
||
/// @inheritdoc IAvalancheICTTRouter | ||
function getTokensList() external view returns (address[] memory) { | ||
return (tokensList); | ||
} | ||
|
||
/// @inheritdoc IAvalancheICTTRouter | ||
function getDestinationChainsForToken(address token) external view returns (bytes32[] memory) { | ||
return (tokenToDestinationChainsIDList[token]); | ||
} | ||
|
||
/** | ||
* @notice Remove a token from the tokensList array (internal function) | ||
* @param _token The address of the token | ||
*/ | ||
function _burnToken(address _token) internal { | ||
for (uint256 i; i < tokensList.length; i++) { | ||
if (tokensList[i] == _token) { | ||
tokensList[i] = tokensList[tokensList.length - 1]; | ||
tokensList.pop(); | ||
break; | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* @notice Remove a destination chain from the list of destination chain associated with a token (internal function) | ||
* @param _token The address of the token | ||
* @param _chainID The ID of the destination chain | ||
*/ | ||
function _burnDestinationChainID(address _token, bytes32 _chainID) internal { | ||
for (uint256 i; i < tokenToDestinationChainsIDList[_token].length; i++) { | ||
if (tokenToDestinationChainsIDList[_token][i] == _chainID) { | ||
tokenToDestinationChainsIDList[_token][i] = | ||
tokenToDestinationChainsIDList[_token][tokensList.length - 1]; | ||
tokenToDestinationChainsIDList[_token].pop(); | ||
break; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters