BridgeFacet

Withdrawing from Polygon to Ethereum

The two functions below are called on Polygon and are called on the AavegotchiDiamond. These functions exist in the BridgeFacet in the diamond. You do not need to use the matic.js library to call these functions. Just call them like normal using ethersjs.
Aavegotchi items are withdrawn from Polygon by calling this function:
1
function withdrawItemsBatch(
2
uint256[] calldata _ids,
3
uint256[] calldata _values
4
) external
Copied!
Aavegotchi's are withdrawn from Polygon by calling this function:
1
function withdrawAavegotchiBatch(uint256[] calldata _tokenIds) external
Copied!
Make sure to save the transaction hash of the transaction that withdraws using either of the two functions above.
The next step is to wait for about 3 hours for the next checkpoint to complete. Matic sends its transactions to Ethereum about every 3 hours. This is a checkpoint.
The two functions above are made when connected to Polygon. These next functions are called on Ethereum and mint the assets on Ethereum and transfer them to the user. These functions are part of matic.js. They are not currently in the matic.js documentation. You can find them in the source code for matic.js. These functions are part of the MaticPOSClient class.
1
exitBatchERC721(txHash: string, options?: SendOptions)
Copied!
1
exitBatchERC1155(txHash: string, options?: SendOptions)
Copied!
Use the earlier saved transaction hash for the argument for these functions. That should be it. That should mint and transfer the assets to the user on Ethereum.

Depositing into Polygon From Ethereum

When depositing ERC1155 items they are burned on Ethereum and minted on Polygon.
When depositing ERC721 Aavegotchis they are burned on Ethereum and unlocked on Polygon.
Call the following smart contract function on Ethereum. This function exists in the RootChainManager contract on Ethereum. This is the main, standard Polygon bridging contract. The Ethereum address for it is: 0xA0c68C638235ee32657e8f720a23ceC1bFc77C77 This exact same function is used for moving ERC20 tokens, ERC1155 tokens and ERC721 tokens. One function for all of them.
1
/**
2
* @notice Move tokens from root to child chain
3
* @dev This mechanism supports arbitrary tokens
4
* @param user address of account that should receive this deposit on child chain
5
* @param rootToken address of token that is being deposited
6
* @param depositData bytes data that is sent to child token contracts to handle deposit
7
*/
8
function depositFor(
9
address user,
10
address rootToken,
11
bytes calldata depositData
12
) external
Copied!
The value for user is the address on Polygon that will receive the token(s).
The value for rootToken is the AavegotchiDiamond address on Ethereum.
The data in depositData is what specifies what kind of token is being deposited and which ones and/or how many to send.
The first part of depositData is a uint256 that has the value 721 or 1155. That specifies which token type is being deposited.
The second part of depositData contains what tokens are being sent.
Here is example code using ethersjs to make a function call to deposit a number of Aavegotchis:
1
const abi = ['function depositFor(address user, address rootToken, bytes calldata depositData) external']
2
const diamondAddress = 'the address'
3
const rootChainManagerAddress = '0xA0c68C638235ee32657e8f720a23ceC1bFc77C77'
4
const user = 'the address'
5
6
const rootChainManager = await ethers.getContractAt(abi, rootChainManagerAddress)
7
8
const tokenIds = [1, 2, 3, 4, 5, 6]
9
const tokenIdsBytes = ethers.utils.defaultAbiCoder.encode(['uint256[]'], [tokenIds])
10
const depositData = ethers.utils.defaultAbiCoder.encode(['uint256', 'bytes'], [721, tokenIdsBytes])
11
const tx = await rootChainManager.depositFor(user, diamondAddress, depositData)
12
const receipt = await tx.wait()
13
if (receipt.status) {
14
console.log('Depoist successful')
15
} else {
16
console.log('Deposit Failed')
17
}
Copied!
Here is example code using ethersjs to make a function call to deposit a number of ERC1155 Aavegotchi items:
1
const abi = ['function depositFor(address user, address rootToken, bytes calldata depositData) external']
2
const diamondAddress = 'the address'
3
const rootChainManagerAddress = '0xA0c68C638235ee32657e8f720a23ceC1bFc77C77'
4
const user = 'the address'
5
6
const rootChainManager = await ethers.getContractAt(abi, rootChainManagerAddress)
7
8
const itemTypes = [1, 2, 3, 4, 5, 6]
9
const itemAmounts = [100, 35, 78, 3, 99, 2000]
10
const itemBytes = ethers.utils.defaultAbiCoder.encode(['uint256[]', 'uint256[]'], [itemTypes, itemAmounts])
11
const depositData = ethers.utils.defaultAbiCoder.encode(['uint256', 'bytes'], [1155, itemBytes])
12
const tx = await rootChainManager.depositFor(user, diamondAddress, depositData)
13
const receipt = await tx.wait()
14
if (receipt.status) {
15
console.log('Depoist successful')
16
} else {
17
console.log('Deposit Failed')
18
}
Copied!
That is it. It is just one function call on Ethereum to move a batch of Aavegotchis to Matic, or a batch of items to Matic. In about 10 minutes the Aavegotchis or wearables show up in the user's wallet.
Last modified 9mo ago