# maTokens

Information generally about how maTokens work is given in the article here: [Aave's Interest-Bearing aTokens on Matic Network](https://aavegotchi.substack.com/p/aaves-interest-bearing-atokens-on).

### Conversion between aTokens and maTokens

The aToken bridging contract leverages the [Aave protocol](https://docs.aave.com/developers/the-core-protocol/atokens) to convert aToken values to maToken values and back. Exactly how this is done is given in the two functions below.

To convert an aToken value to a maToken value  or vise versa call the following functions on Ethereum on the aToken bridge contract which is at this address: `0x0D29aDA4c818A9f089107201eaCc6300e56E0d5c`

```solidity
 /**
  * @dev Converts aToken value to maToken value
  * @param _aTokenAddress aToken contract address
  * @param _aTokenValue aToken value to convert
  * @return maTokenValue_ The converted maToken value
  **/
 function getMATokenValue(address _aTokenAddress, uint256 _aTokenValue) 
   public 
   view 
   returns 
   (uint256 maTokenValue_) 
 {
   ILendingPool pool = IAToken(_aTokenAddress).POOL();
   uint256 liquidityIndex = pool.getReserveNormalizedIncome(
     IAToken(_aTokenAddress).UNDERLYING_ASSET_ADDRESS()
   );
   maTokenValue_ = p27Div(_aTokenValue, liquidityIndex);
 }
 
 /**
  * @dev Converts maToken value to aToken value
  * @param _aTokenAddress aToken contract address
  * @param _maTokenValue maToken value to convert
  * @return aTokenValue_ The converted aToken value
  **/
 function getATokenValue(address _aTokenAddress, uint256 _maTokenValue) 
   public 
   view 
   returns (uint256 aTokenValue_) 
 {
  ILendingPool pool = IAToken(_aTokenAddress).POOL();
  uint256 liquidityIndex = pool.getReserveNormalizedIncome(
    IAToken(_aTokenAddress).UNDERLYING_ASSET_ADDRESS()
  );
  aTokenValue_ = p27Mul(_maTokenValue, liquidityIndex);
 }
```

The source code for the aToken bridge contract is here: <https://github.com/aavegotchi/pos-portal/blob/master/contracts/root/RootChainManager/ATokenRootChainManager.sol>

Don't use the ABI on Etherscan to call these functions. Instead use the ABI at this link: <https://gist.github.com/mudgen/5da65e965e05ba08354f003aa86e6587>

### Getting aToken and maToken contract addresses

Use the functions below to get aTokens and maToken addresses.

```solidity
 /**
  * @dev Gets the maTokens contract address on Matic that is associate with
  * the aTokens contract on Ethereum.
  * @param _rootToken The aToken contract address on Ethereum
  * @return The maToken contract address on Matic.
  **/
function rootToChildToken(address _rootToken) external view returns (address)

 /**
  * @dev Gets the aTokens contract address on Ethereum that is associate with
  * the maTokens contract on Matic.
  * @param _childToken The maToken contract address on Matic
  * @return The aToken contract address on Ethereum.
  **/
function childToRootToken(address _childToken) external view returns (address)
```

The aToken addresses can be found on Etherscan or in [Aave documentation](https://docs.aave.com/developers/getting-started/deployed-contracts).

The aToken bridge to Matic only works with version 2 aToken contracts.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.aavegotchi.com/developers/aavegotchi-contracts/matokens.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
