forked from axelarnetwork/axelar-examples
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathERC20CrossChain.sol
79 lines (67 loc) · 2.85 KB
/
ERC20CrossChain.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
// SPDX-License-Identifier: MIT
pragma solidity 0.8.9;
import { IAxelarGateway } from '@axelar-network/axelar-gmp-sdk-solidity/contracts/interfaces/IAxelarGateway.sol';
import { IAxelarGasService } from '@axelar-network/axelar-gmp-sdk-solidity/contracts/interfaces/IAxelarGasService.sol';
import { IERC20CrossChain } from './IERC20CrossChain.sol';
import { ERC20 } from '@axelar-network/axelar-cgp-solidity/contracts/ERC20.sol';
import { AxelarExecutable } from '@axelar-network/axelar-gmp-sdk-solidity/contracts/executable/AxelarExecutable.sol';
import { Upgradable } from '@axelar-network/axelar-gmp-sdk-solidity/contracts/upgradable/Upgradable.sol';
import { StringToAddress, AddressToString } from '@axelar-network/axelar-gmp-sdk-solidity/contracts/utils/AddressString.sol';
contract ERC20CrossChain is AxelarExecutable, ERC20, Upgradable, IERC20CrossChain {
using StringToAddress for string;
using AddressToString for address;
error AlreadyInitialized();
event FalseSender(string sourceChain, string sourceAddress);
IAxelarGasService public immutable gasService;
constructor(
address gateway_,
address gasReceiver_,
uint8 decimals_
) AxelarExecutable(gateway_) ERC20('', '', decimals_) {
gasService = IAxelarGasService(gasReceiver_);
}
function _setup(bytes calldata params) internal override {
(string memory name_, string memory symbol_) = abi.decode(params, (string, string));
if (bytes(name).length != 0) revert AlreadyInitialized();
name = name_;
symbol = symbol_;
}
// This is for testing.
function giveMe(uint256 amount) external {
_mint(msg.sender, amount);
}
function transferRemote(
string calldata destinationChain,
address destinationAddress,
uint256 amount
) public payable override {
_burn(msg.sender, amount);
bytes memory payload = abi.encode(destinationAddress, amount);
string memory stringAddress = address(this).toString();
if (msg.value > 0) {
gasService.payNativeGasForContractCall{ value: msg.value }(
address(this),
destinationChain,
stringAddress,
payload,
msg.sender
);
}
gateway.callContract(destinationChain, stringAddress, payload);
}
function _execute(
string calldata, /*sourceChain*/
string calldata sourceAddress,
bytes calldata payload
) internal override {
if (sourceAddress.toAddress() != address(this)) {
emit FalseSender(sourceAddress, sourceAddress);
return;
}
(address to, uint256 amount) = abi.decode(payload, (address, uint256));
_mint(to, amount);
}
function contractId() external pure returns (bytes32) {
return keccak256('example');
}
}