Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Native Token Bridge #40

Merged
merged 220 commits into from
Dec 8, 2023
Merged
Show file tree
Hide file tree
Changes from 219 commits
Commits
Show all changes
220 commits
Select commit Hold shift + click to select a range
799c289
fix comment and commented code (#30)
Sep 21, 2023
a393c5b
Working copied test
geoff-vball Sep 14, 2023
275c9c9
Fix constructor
geoff-vball Sep 25, 2023
97d4cf1
Add minter to network genesis
geoff-vball Sep 25, 2023
99df7ee
Working test
geoff-vball Sep 25, 2023
31120c7
Remove unnecessary files
geoff-vball Sep 26, 2023
36465ed
Remove Imported contracts
geoff-vball Sep 26, 2023
f7ee05a
wip
geoff-vball Sep 26, 2023
91b83ae
remove unneeded change
geoff-vball Sep 26, 2023
6d271c5
Update Geth
geoff-vball Sep 26, 2023
93805ff
generate abi files
geoff-vball Sep 26, 2023
5ddae27
Slither fixes
geoff-vball Sep 26, 2023
121d835
Merge branch 'main' into gstuart/native-token-bridge-ginkgo
geoff-vball Sep 26, 2023
8ceb73e
typo
geoff-vball Sep 26, 2023
060be4e
Fix
geoff-vball Sep 26, 2023
7b45964
Undo go mod updates
geoff-vball Sep 26, 2023
95248f9
wip
geoff-vball Sep 28, 2023
df2bba8
Merge branch 'main' into gstuart/native-token-bridge-ginkgo
geoff-vball Sep 28, 2023
96f6d90
working tests
geoff-vball Sep 28, 2023
0602743
fix
geoff-vball Sep 28, 2023
1dd2858
Refactor bytecode extraction
geoff-vball Sep 29, 2023
ff56992
Add one-way transfer
geoff-vball Sep 29, 2023
a64ec01
Working transfer
geoff-vball Sep 29, 2023
1c365d6
test wip
geoff-vball Sep 29, 2023
000e4a5
Rename contracts
geoff-vball Sep 29, 2023
4b69e18
Rename contracts
geoff-vball Sep 29, 2023
db52a48
Add partner contract address to constuctor
geoff-vball Sep 29, 2023
9bbf156
Regenerate abi files
geoff-vball Sep 29, 2023
4ec83d3
Format contracts
geoff-vball Sep 29, 2023
ece2e5b
Fixup test
geoff-vball Sep 29, 2023
4d00724
Fix comments in interfaces
geoff-vball Sep 29, 2023
6ca248f
Broken test
geoff-vball Sep 29, 2023
f9e7710
Review fixes
geoff-vball Sep 29, 2023
d010484
Working mint
geoff-vball Oct 3, 2023
52e2d3a
linter
geoff-vball Oct 3, 2023
ba18e5b
Working e2e roundtrip
geoff-vball Oct 4, 2023
c9c2cc9
Change revert to require
geoff-vball Oct 4, 2023
9c2c700
clean up contracts
geoff-vball Oct 4, 2023
03a473f
Add tokenReserve
geoff-vball Oct 4, 2023
3f36fb3
remove check for custom errors
geoff-vball Oct 4, 2023
eeecf1f
Update abi
geoff-vball Oct 4, 2023
3456f8b
Deploy contract through abi instead of forge
geoff-vball Oct 4, 2023
7cda837
cleanup
geoff-vball Oct 4, 2023
0440efd
Merge branch 'main' into gstuart/native-token-bridge-ginkgo
geoff-vball Oct 5, 2023
fb56ad6
Use abigen script for native token contracts
geoff-vball Oct 5, 2023
02b9045
Cleanup e2etest
geoff-vball Oct 5, 2023
ce45fb4
Refactor native token bridge test
geoff-vball Oct 5, 2023
c135025
Format
geoff-vball Oct 5, 2023
7a31baa
cleanup
geoff-vball Oct 5, 2023
fc73351
Rename subnet info fields
geoff-vball Oct 5, 2023
7eacec7
Revert go sum change
geoff-vball Oct 5, 2023
2496979
Test token reserve
geoff-vball Oct 5, 2023
e42884f
Merge branch 'main' into gstuart/native-token-bridge-ginkgo
geoff-vball Oct 6, 2023
d94a736
Move go abis to correct folder
geoff-vball Oct 6, 2023
d60d97b
Fixup tests
geoff-vball Oct 6, 2023
2f5f093
Update comments
geoff-vball Oct 16, 2023
30be639
Rename partner contract field
geoff-vball Oct 16, 2023
c1574ea
Run tests in same describe block
geoff-vball Oct 16, 2023
4527054
Cleanup contracts
geoff-vball Oct 16, 2023
9927e9f
Inline abi.encode
geoff-vball Oct 16, 2023
4516a44
Add allowed relayers to transfer methods
geoff-vball Oct 16, 2023
a686b54
Merge branch 'main' into gstuart/native-token-bridge-ginkgo
geoff-vball Oct 23, 2023
f3cc0ec
Fix import
geoff-vball Oct 23, 2023
b6d628f
Add README
geoff-vball Oct 24, 2023
9863960
Review fixes
geoff-vball Oct 24, 2023
a297918
Move go abis to correct folder
geoff-vball Oct 26, 2023
5afcbef
Review fixes
geoff-vball Oct 26, 2023
27798b7
worksum
geoff-vball Oct 26, 2023
fa42e80
Merge branch 'main' into gstuart/native-token-bridge-ginkgo
geoff-vball Oct 26, 2023
549361a
Fix path for abi imports
geoff-vball Oct 26, 2023
80c4dbd
Add ERC20TokenSource
geoff-vball Oct 27, 2023
14b11b0
Add abi binding for ERCTokenSource
geoff-vball Oct 27, 2023
06aee49
Add ExampleERC20 to abi bindings
geoff-vball Oct 27, 2023
ea0ed4c
Add ERC20 to native bridge test
geoff-vball Oct 27, 2023
3ccf5e3
Review fixes
geoff-vball Oct 27, 2023
28adeb3
Update contracts/src/CrossChainApplications/NativeTokenBridge/INative…
geoff-vball Oct 27, 2023
722d5cf
Merge branch 'main' into gstuart/native-token-bridge-ginkgo
geoff-vball Oct 29, 2023
af1007a
Cleanup merge
geoff-vball Oct 29, 2023
b10f970
Merge branch 'main' into gstuart/native-token-bridge-ginkgo
geoff-vball Oct 29, 2023
72d42e4
Pin foundry to most recent long-term nightly release
geoff-vball Oct 29, 2023
b51443f
Update abi bindings
geoff-vball Oct 29, 2023
228646b
New transactor for new transaction
geoff-vball Oct 29, 2023
ada88f2
Update contracts/src/CrossChainApplications/NativeTokenBridge/INative…
geoff-vball Oct 29, 2023
271a8a7
Update contracts/src/CrossChainApplications/NativeTokenBridge/INative…
geoff-vball Oct 29, 2023
293570c
Update contracts/src/CrossChainApplications/NativeTokenBridge/INative…
geoff-vball Oct 29, 2023
88f7e84
Update contracts/src/CrossChainApplications/NativeTokenBridge/INative…
geoff-vball Oct 29, 2023
9e8ac53
Update contracts/src/CrossChainApplications/NativeTokenBridge/NativeT…
geoff-vball Oct 29, 2023
2e9c6e1
Grab foundryup from same version
geoff-vball Oct 29, 2023
809f344
Exit on error in dockerfile
geoff-vball Oct 27, 2023
072a697
Fix foundry version
geoff-vball Oct 29, 2023
7187ac2
Review fixes
geoff-vball Oct 29, 2023
14d3b69
Try to fix test
geoff-vball Oct 29, 2023
6111e86
Fix test
geoff-vball Oct 30, 2023
9934478
Refactor contract deployment
geoff-vball Oct 30, 2023
12adf80
review fixes
geoff-vball Oct 30, 2023
3bc64c0
Rename tokenReserve to initialTokenImbalance
geoff-vball Oct 30, 2023
4e80995
Add totalSupply and isCollateralized
geoff-vball Oct 30, 2023
0271061
Fix tests
geoff-vball Oct 30, 2023
d784d24
Uncouple tests
geoff-vball Oct 30, 2023
2cb7180
Merge branch 'main' into gstuart/native-token-bridge-ginkgo
geoff-vball Nov 6, 2023
406c9b3
Merge branch 'main' into gstuart/native-token-bridge-ginkgo
geoff-vball Nov 6, 2023
c8ed6ae
Merge branch 'main' into gstuart/native-token-bridge-ginkgo
geoff-vball Nov 7, 2023
672dc88
Appease solhint linter
geoff-vball Nov 7, 2023
b5356d6
Safe requires check
geoff-vball Nov 7, 2023
c3c0931
Merge branch 'main' into gstuart/native-token-bridge-ginkgo
geoff-vball Nov 13, 2023
48490ea
Update go.work.sum and bindings
geoff-vball Nov 13, 2023
4c8d7d3
WIP
geoff-vball Nov 13, 2023
aad4373
Review fixes
geoff-vball Nov 13, 2023
720a2d8
Fix lint
geoff-vball Nov 13, 2023
40ebc58
Fix lint
geoff-vball Nov 13, 2023
849e938
Update README
geoff-vball Nov 13, 2023
472f62e
WIP
geoff-vball Nov 13, 2023
8bc0717
Merge branch 'gstuart/generic-log-getter' into gstuart/native-token-b…
geoff-vball Nov 13, 2023
01860a5
Fix ERC20 test
geoff-vball Nov 15, 2023
0004f59
Add contracts/pkg to gitignore
geoff-vball Nov 15, 2023
b684f53
Merge branch 'main' into gstuart/native-token-bridge-ginkgo
geoff-vball Nov 15, 2023
19c921e
Test cleanup
geoff-vball Nov 15, 2023
5b28388
Remove duplicate lines
geoff-vball Nov 15, 2023
16c5d33
Update contracts/src/CrossChainApplications/NativeTokenBridge/NativeT…
geoff-vball Nov 16, 2023
4422632
Update contracts/src/CrossChainApplications/NativeTokenBridge/INative…
geoff-vball Nov 16, 2023
526231d
Update contracts/src/CrossChainApplications/NativeTokenBridge/INative…
geoff-vball Nov 16, 2023
1caf678
Update contracts/src/CrossChainApplications/NativeTokenBridge/IERC20T…
geoff-vball Nov 16, 2023
0bbb1b7
Update contracts/src/CrossChainApplications/NativeTokenBridge/IERC20T…
geoff-vball Nov 16, 2023
b9fea48
Update contracts/src/CrossChainApplications/NativeTokenBridge/README.md
geoff-vball Nov 16, 2023
6346cca
Update contracts/src/CrossChainApplications/NativeTokenBridge/README.md
geoff-vball Nov 16, 2023
6f207da
Ignore specific solhint rule
geoff-vball Nov 16, 2023
d78ab64
Ignore specific solhint rule
geoff-vball Nov 16, 2023
581f3d7
Fix burned tokens total
geoff-vball Nov 16, 2023
635ff27
Merge branch 'main' into gstuart/native-token-bridge-ginkgo
geoff-vball Nov 16, 2023
f9305a9
Merge branch 'main' into gstuart/native-token-bridge-ginkgo
geoff-vball Nov 16, 2023
1d9fe93
Merge branch 'main' into gstuart/native-token-bridge-ginkgo
geoff-vball Nov 20, 2023
49a2cb8
Review fixes
geoff-vball Nov 20, 2023
bf1b852
Implement burn functionality
geoff-vball Nov 20, 2023
cdfc623
linter
geoff-vball Nov 20, 2023
01436a4
Split burning and updating
geoff-vball Nov 20, 2023
2d8365a
linter
geoff-vball Nov 20, 2023
471b66a
Update README
geoff-vball Nov 20, 2023
5447a4e
Native token bridge now uses TeleporterOwnerUpgradeable instead of te…
geoff-vball Nov 20, 2023
a67ca77
Revert "Native token bridge now uses TeleporterOwnerUpgradeable inste…
geoff-vball Nov 20, 2023
ea41649
Update README
geoff-vball Nov 20, 2023
045bfc2
Tests for report burn
geoff-vball Nov 20, 2023
c19ddf6
Merge branch 'main' into gstuart/native-token-bridge-ginkgo
geoff-vball Nov 20, 2023
1584c90
Revert "Tests for report burn"
geoff-vball Nov 20, 2023
39f570c
Tests for report burn
geoff-vball Nov 20, 2023
c471cef
Regenerate abi files
geoff-vball Nov 20, 2023
b0445f7
Increase gas limit for burn reporting
geoff-vball Nov 21, 2023
25fb790
Merge branch 'main' into gstuart/native-token-bridge-ginkgo
geoff-vball Nov 21, 2023
8cfde6b
Merge branch 'main' into gstuart/native-token-bridge-ginkgo
geoff-vball Nov 22, 2023
ebdb01d
Update teleporter fee info field name
geoff-vball Nov 22, 2023
a1c08bb
Fix renamed fields
geoff-vball Nov 22, 2023
d723ef2
review fixes
geoff-vball Nov 22, 2023
a4c11b0
Rename destinationChainBurnedBalance to destinationBurnedTotal
geoff-vball Nov 27, 2023
ae0e397
Rename newBurnedBalance to newBurnedTotal
geoff-vball Nov 27, 2023
12b9702
Add comment describing updating burn total
geoff-vball Nov 27, 2023
4bed474
Update contracts/src/CrossChainApplications/NativeTokenBridge/IERC20T…
geoff-vball Nov 27, 2023
22e5cfc
Fix comment
geoff-vball Nov 27, 2023
6380773
Merge branch 'gstuart/native-token-bridge-ginkgo' of github.com:ava-l…
geoff-vball Nov 27, 2023
30519d8
Review comments
geoff-vball Nov 27, 2023
69c3fab
Update contracts/src/CrossChainApplications/NativeTokenBridge/README.md
geoff-vball Nov 28, 2023
5e0c541
Update contracts/src/CrossChainApplications/NativeTokenBridge/README.md
geoff-vball Nov 28, 2023
cd9a06f
Update contracts/src/CrossChainApplications/NativeTokenBridge/README.md
geoff-vball Nov 28, 2023
41e90f5
Update contracts/src/CrossChainApplications/NativeTokenBridge/README.md
geoff-vball Nov 28, 2023
f0d5857
Update contracts/src/CrossChainApplications/NativeTokenBridge/README.md
geoff-vball Nov 28, 2023
2269287
typo
geoff-vball Nov 28, 2023
00faa2c
Merge branch 'main' into gstuart/native-token-bridge-ginkgo
geoff-vball Nov 28, 2023
a1f538a
Rename BLACKHOLE_ADDRESS to eliminate ambiguity
geoff-vball Nov 28, 2023
1276b27
Leave link for blackhole address
geoff-vball Nov 28, 2023
fc1f418
Regenerate abi
geoff-vball Nov 28, 2023
0d881e1
Merge branch 'main' into gstuart/native-token-bridge-ginkgo
geoff-vball Nov 28, 2023
737e890
Fix test
geoff-vball Nov 28, 2023
3a46ee8
Merge branch 'main' into gstuart/native-token-bridge-ginkgo
geoff-vball Nov 29, 2023
0742ead
merge
geoff-vball Nov 29, 2023
64571c4
abigen
geoff-vball Nov 29, 2023
68a322d
run goimports
geoff-vball Nov 29, 2023
9ba19e3
format
geoff-vball Nov 29, 2023
dd0674e
format
geoff-vball Nov 29, 2023
3fe7e0f
Removed unused vars
geoff-vball Nov 29, 2023
a4dcbbd
Merge branch 'main' into gstuart/native-token-bridge-ginkgo
geoff-vball Nov 29, 2023
2af7b11
Merge branch 'main' into gstuart/native-token-bridge-ginkgo
geoff-vball Nov 29, 2023
eda62d6
Update README
geoff-vball Nov 29, 2023
f96024d
Update README
geoff-vball Nov 29, 2023
6162309
Merge branch 'main' into gstuart/native-token-bridge-ginkgo
geoff-vball Nov 29, 2023
91a265d
abigen
geoff-vball Nov 29, 2023
cef97d7
Fix param name
geoff-vball Dec 1, 2023
2355865
Fix param name
geoff-vball Dec 1, 2023
4a6059a
Fix param name
geoff-vball Dec 1, 2023
95fba13
Working test
geoff-vball Dec 1, 2023
f8d720d
Most tests
geoff-vball Dec 4, 2023
8a793db
Most tests
geoff-vball Dec 4, 2023
1e66e26
Merge branch 'main' into gstuart/native-token-bridge-ginkgo
geoff-vball Dec 4, 2023
f606605
Linter
geoff-vball Dec 4, 2023
8b3e203
Linter
geoff-vball Dec 4, 2023
354b53a
NativeTokenSource units tests
geoff-vball Dec 5, 2023
0a3e4ff
ERC20TokenSource units tests
geoff-vball Dec 5, 2023
250ef1a
Review fixes
geoff-vball Dec 5, 2023
9daf35a
Fix
geoff-vball Dec 5, 2023
039b3d9
Merge branch 'main' into gstuart/native-token-bridge-ginkgo
geoff-vball Dec 5, 2023
162a701
Update contracts/src/Mocks/UnitTestMockERC20.sol
geoff-vball Dec 5, 2023
84098a2
Review fixes
geoff-vball Dec 5, 2023
c36aa4b
Merge branch 'gstuart/native-token-bridge-ginkgo' of github.com:ava-l…
geoff-vball Dec 5, 2023
d1cf827
Merge branch 'main' into gstuart/native-token-bridge-ginkgo
geoff-vball Dec 5, 2023
93dab58
Add collateral test
geoff-vball Dec 5, 2023
21ed29a
Add collateral test
geoff-vball Dec 5, 2023
cc0cafe
Merge branch 'main' into gstuart/native-token-bridge-ginkgo
geoff-vball Dec 5, 2023
414cb5d
Remove recipient from collateralAdded event
geoff-vball Dec 5, 2023
a341da3
Add comment
geoff-vball Dec 5, 2023
787f0ef
Linter
geoff-vball Dec 5, 2023
7f6e0fe
Merge branch 'main' into gstuart/native-token-bridge-ginkgo
geoff-vball Dec 5, 2023
07ed07d
Merge branch 'main' into gstuart/native-token-bridge-ginkgo
geoff-vball Dec 6, 2023
b73d38f
Merge branch 'main' into gstuart/native-token-bridge-ginkgo
geoff-vball Dec 6, 2023
9d56c4f
Linter
geoff-vball Dec 6, 2023
c0aaa50
Review fixes
geoff-vball Dec 7, 2023
c217139
linter
geoff-vball Dec 7, 2023
712538c
bindings
geoff-vball Dec 7, 2023
ca029ab
Merge branch 'main' into gstuart/native-token-bridge-ginkgo
geoff-vball Dec 7, 2023
c6fbd80
Merge branch 'main' into gstuart/native-token-bridge-ginkgo
geoff-vball Dec 7, 2023
2ab9ec8
Review fixes
geoff-vball Dec 8, 2023
2b30495
Factor out helper functions
geoff-vball Dec 8, 2023
1e09c2e
Fix
geoff-vball Dec 8, 2023
2f10597
Use Address.sendValue for transfers
geoff-vball Dec 8, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ UniversalTeleporterMessengerContractAddress.txt

# miscellaneous
.idea/
contracts/pkg

# Generated template files
docker-compose-test-local.yml
Expand Down

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion contracts/src/.solhint.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"reason-string": [
"warn",
{
"maxLength": 60
"maxLength": 75
}
],
"custom-errors": "off",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ contract ERC20Bridge is

// For non-zero fee amounts, first transfer the fee to this contract, and then
// allow the Teleporter contract to spend it.
uint256 adjustedFeeAmount = 0;
uint256 adjustedFeeAmount;
if (messageFeeAmount > 0) {
adjustedFeeAmount = SafeERC20TransferFrom.safeTransferFrom(
IERC20(messageFeeAsset),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ contract ExampleCrossChainMessenger is
.getLatestTeleporter();
// For non-zero fee amounts, first transfer the fee to this contract, and then
// allow the Teleporter contract to spend it.
uint256 adjustedFeeAmount = 0;
uint256 adjustedFeeAmount;
if (feeAmount > 0) {
adjustedFeeAmount = SafeERC20TransferFrom.safeTransferFrom(
IERC20(feeTokenAddress),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,224 @@
// (c) 2023, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.

// SPDX-License-Identifier: Ecosystem

pragma solidity 0.8.18;

import {ReentrancyGuard} from "@openzeppelin/contracts/security/ReentrancyGuard.sol";
import {IWarpMessenger} from "@subnet-evm-contracts/interfaces/IWarpMessenger.sol";
import {IERC20TokenSource} from "./IERC20TokenSource.sol";
import {ITokenSource} from "./ITokenSource.sol";
import {ITeleporterMessenger, TeleporterMessageInput, TeleporterFeeInfo} from "../../Teleporter/ITeleporterMessenger.sol";
import {ITeleporterReceiver} from "../../Teleporter/ITeleporterReceiver.sol";
import {SafeERC20TransferFrom} from "../../Teleporter/SafeERC20TransferFrom.sol";
import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import {SafeERC20} from "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";

contract ERC20TokenSource is
geoff-vball marked this conversation as resolved.
Show resolved Hide resolved
ITeleporterReceiver,
IERC20TokenSource,
ITokenSource,
ReentrancyGuard
{
// The address where the burned transaction fees are credited.
// Defined as BLACKHOLE_ADDRESS at
// https://github.com/ava-labs/subnet-evm/blob/e23ab058d039ff9c8469c89b139d21d52c4bd283/constants/constants.go
address public constant BURNED_TX_FEES_ADDRESS =
0x0100000000000000000000000000000000000000;
uint256 public constant MINT_NATIVE_TOKENS_REQUIRED_GAS = 100_000;
// Used to keep track of tokens burned through transactions on the destination chain. They can
// be reported to this contract to burn an equivalent number of tokens on this chain.
uint256 public destinationBurnedTotal;
bytes32 public immutable destinationBlockchainID;
address public immutable nativeTokenDestinationAddress;
address public immutable erc20ContractAddress;

// Used for sending and receiving Teleporter messages.
ITeleporterMessenger public immutable teleporterMessenger;

constructor(
address teleporterMessengerAddress,
bytes32 destinationBlockchainID_,
address nativeTokenDestinationAddress_,
address erc20ContractAddress_
cam-schultz marked this conversation as resolved.
Show resolved Hide resolved
) {
require(
teleporterMessengerAddress != address(0),
"ERC20TokenSource: zero TeleporterMessenger address"
);
teleporterMessenger = ITeleporterMessenger(teleporterMessengerAddress);

require(
destinationBlockchainID_ != bytes32(0),
"ERC20TokenSource: zero destination blockchain ID"
);
require(
destinationBlockchainID_ !=
IWarpMessenger(0x0200000000000000000000000000000000000005)
.getBlockchainID(),
"ERC20TokenSource: cannot bridge with same blockchain"
);
destinationBlockchainID = destinationBlockchainID_;

require(
nativeTokenDestinationAddress_ != address(0),
"ERC20TokenSource: zero destination contract address"
);
nativeTokenDestinationAddress = nativeTokenDestinationAddress_;

require(
erc20ContractAddress_ != address(0),
"ERC20TokenSource: zero ERC20 contract address"
);
erc20ContractAddress = erc20ContractAddress_;
}

/**
* @dev See {ITeleporterReceiver-receiveTeleporterMessage}.
*
* Receives a Teleporter message and routes to the appropriate internal function call.
geoff-vball marked this conversation as resolved.
Show resolved Hide resolved
*/
function receiveTeleporterMessage(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Calling out that we should use TeleporterUpgradable here so that we can use the onlyAllowedTeleporter modifier. If we want to do this in a later PR, lets create an issue for it now. 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I implemented it in this commit, but reverted the changes because the e2e setup doesn't currently deploy the registry, and I didn't want to go fooling around in there until the general-e2e PR is merged.

Should we have a general ticket for all the cross-chain applications to use the registry?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seconding this. I'm in favor of creating a followup ticket to add registry and upgrade support to this set of contracts so that we don't further bloat this PR's scope.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

agree with above, lets create a new issue for it and defer

bytes32 senderBlockchainID,
address senderAddress,
bytes calldata message
) external nonReentrant {
// Only allow the Teleporter messenger to deliver messages.
require(
msg.sender == address(teleporterMessenger),
"ERC20TokenSource: unauthorized TeleporterMessenger contract"
);

// Only allow messages from the destination chain.
require(
senderBlockchainID == destinationBlockchainID,
"ERC20TokenSource: invalid destination chain"
);

// Only allow the partner contract to send messages.
require(
senderAddress == nativeTokenDestinationAddress,
"ERC20TokenSource: unauthorized sender"
);

// Decode the payload to recover the action and corresponding function parameters
(SourceAction action, bytes memory actionData) = abi.decode(
message,
(SourceAction, bytes)
);

// Route to the appropriate function.
if (action == SourceAction.Unlock) {
(address recipient, uint256 amount) = abi.decode(
actionData,
(address, uint256)
);
_unlockTokens(recipient, amount);
} else if (action == SourceAction.Burn) {
uint256 newBurnTotal = abi.decode(actionData, (uint256));
_handleBurnTokens(newBurnTotal);
} else {
revert("ERC20TokenSource: invalid action");
}
}

/**
* @dev See {IERC20TokenSource-transferToDestination}.
*/
function transferToDestination(
address recipient,
uint256 totalAmount,
uint256 feeAmount,
address[] calldata allowedRelayerAddresses
) external nonReentrant {
// The recipient cannot be the zero address.
require(
recipient != address(0),
"ERC20TokenSource: zero recipient address"
);

// Lock tokens in this contract. Supports "fee/burn on transfer" ERC20 token
// implementations by only bridging the actual balance increase reflected by the call
// to transferFrom.
uint256 adjustedAmount = SafeERC20TransferFrom.safeTransferFrom(
IERC20(erc20ContractAddress),
totalAmount
);

// Ensure that the adjusted amount is greater than the fee to be paid.
require(
adjustedAmount > feeAmount,
"ERC20TokenSource: insufficient adjusted amount"
);

// Allow the Teleporter messenger to spend the fee amount.
if (feeAmount > 0) {
SafeERC20.safeIncreaseAllowance(
IERC20(erc20ContractAddress),
address(teleporterMessenger),
feeAmount
);
}

uint256 transferAmount = adjustedAmount - feeAmount;

uint256 messageID = teleporterMessenger.sendCrossChainMessage(
TeleporterMessageInput({
destinationBlockchainID: destinationBlockchainID,
destinationAddress: nativeTokenDestinationAddress,
feeInfo: TeleporterFeeInfo({
feeTokenAddress: erc20ContractAddress,
amount: feeAmount
}),
requiredGasLimit: MINT_NATIVE_TOKENS_REQUIRED_GAS,
allowedRelayerAddresses: allowedRelayerAddresses,
message: abi.encode(recipient, transferAmount)
})
);

emit TransferToDestination({
sender: msg.sender,
recipient: recipient,
amount: transferAmount,
teleporterMessageID: messageID
});
}

/**
* @dev Unlocks tokens to recipient.
*/
function _unlockTokens(address recipient, uint256 amount) private {
require(
recipient != address(0),
"ERC20TokenSource: zero recipient address"
);

// Transfer to recipient
emit UnlockTokens(recipient, amount);
geoff-vball marked this conversation as resolved.
Show resolved Hide resolved
SafeERC20.safeTransfer(IERC20(erc20ContractAddress), recipient, amount);
}

/**
* @dev Sends tokens to BURNED_TX_FEES_ADDRESS.
*/
function _burnTokens(uint256 amount) private {
emit BurnTokens(amount);
SafeERC20.safeTransfer(
IERC20(erc20ContractAddress),
BURNED_TX_FEES_ADDRESS,
amount
);
}

/**
* @dev Update destinationBurnedTotal sent from destination chain
*/
function _handleBurnTokens(uint256 newBurnTotal) private {
if (newBurnTotal > destinationBurnedTotal) {
uint256 difference = newBurnTotal - destinationBurnedTotal;
_burnTokens(difference);
destinationBurnedTotal = newBurnTotal;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// (c) 2023, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.

// SPDX-License-Identifier: Ecosystem

pragma solidity 0.8.18;

/**
* @dev Interface that describes functionalities for a contract that will lock ERC20 tokens and send a
* Teleporter message to a {INativeTokenDestination} contract to mint native tokens on the destination chain.
*/
interface IERC20TokenSource {
/**
* @dev Locks ERC20 tokens on the source contract chain, and sends a message to mint the
* corresponding native tokens on the destination chain.
*/
function transferToDestination(
address recipient,
uint256 totalAmount,
uint256 feeAmount,
address[] calldata allowedRelayerAddresses
) external;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// (c) 2023, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.

// SPDX-License-Identifier: Ecosystem

pragma solidity 0.8.18;

import {TeleporterFeeInfo} from "../../Teleporter/ITeleporterMessenger.sol";

/**
* @dev Interface that describes functionalities for a contract that can mint native tokens when
* paired with a {INativeTokenSource} or {IERC20TokenSource} contract that will lock tokens on another chain.
*/
interface INativeTokenDestination {
/**
* @dev Emitted when tokens are burned in the destination contract and to be unlocked on the source contract.
*/
event TransferToSource(
geoff-vball marked this conversation as resolved.
Show resolved Hide resolved
address indexed sender,
address indexed recipient,
uint256 indexed teleporterMessageID,
uint256 amount
);

/**
geoff-vball marked this conversation as resolved.
Show resolved Hide resolved
* @dev Emitted when tokens are not minted in order to collateralize the source contract.
*/
event CollateralAdded(uint256 amount, uint256 remaining);

/**
* @dev Emitted when minting native tokens.
minghinmatthewlam marked this conversation as resolved.
Show resolved Hide resolved
geoff-vball marked this conversation as resolved.
Show resolved Hide resolved
*/
event NativeTokensMinted(address indexed recipient, uint256 amount);

/**
* @dev Emitted when reporting total burned tx fees to source chain.
*/
event ReportTotalBurnedTxFees(
uint256 indexed teleporterMessageID,
uint256 burnAddressBalance
);

/**
* @dev Burns native tokens on the destination contract chain, and sends a message to the source
* contract to unlock corresponding tokens.
*/
function transferToSource(
address recipient,
TeleporterFeeInfo calldata feeInfo,
address[] calldata allowedRelayerAddresses
geoff-vball marked this conversation as resolved.
Show resolved Hide resolved
) external payable;

/**
* @dev Reports the current total burned transaction fees on this chain to the source chain.
*/
function reportTotalBurnedTxFees(
TeleporterFeeInfo calldata feeInfo,
address[] calldata allowedRelayerAddresses
) external;

/**
* @dev Returns true if the reserve imbalance for this contract has been accounted for.
* When this is true, all tokens sent to this chain will be minted, and sending tokens
* to the source chain is allowed.
*/
function isCollateralized() external view returns (bool);

/**
* @dev Returns a best-estimate (upper bound) of tokens in circulation on this chain.
*/
function totalSupply() external view returns (uint256);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// (c) 2023, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.

// SPDX-License-Identifier: Ecosystem

pragma solidity 0.8.18;

import {TeleporterFeeInfo} from "../../Teleporter/ITeleporterMessenger.sol";

/**
* @dev Interface that describes functionalities for a contract that will lock native tokens on the source chain and send a
* Teleporter message to a {INativeTokenDestination} contract to mint native tokens on the destination chain.
*/
interface INativeTokenSource {
/**
* @dev Locks native tokens on the source contract chain, and sends a message to the destination
* contract to mint corresponding tokens.
*/
function transferToDestination(
address recipient,
TeleporterFeeInfo calldata feeInfo,
address[] calldata allowedRelayerAddresses
) external payable;
}
Loading