Created
September 6, 2020 09:12
-
-
Save itzmeanjan/d19c5dca071dc164bb9a273fc7185795 to your computer and use it in GitHub Desktop.
ChainLink's LINK Token upgrade for mapping on Matic Network [ also deployment details ]
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
*.sol linguist-language=Solidity |
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
//SPDX-License-Identifier: None | |
// Deployed at: 0x6a3e42FDA39D199417B87D023459d203e4A74b66 on Goerli | |
pragma solidity ^0.6.11; | |
interface ERC677Receiver { | |
function onTokenTransfer(address _sender, uint _value, bytes memory _data) external; | |
} | |
library linkSafeMath { | |
function mul(uint256 a, uint256 b) internal pure returns (uint256) { | |
uint256 c = a * b; | |
assert(a == 0 || c / a == b); | |
return c; | |
} | |
function div(uint256 a, uint256 b) internal pure returns (uint256) { | |
// assert(b > 0); // Solidity automatically throws when dividing by 0 | |
uint256 c = a / b; | |
// assert(a == b * c + a % b); // There is no case in which this doesn't hold | |
return c; | |
} | |
function sub(uint256 a, uint256 b) internal pure returns (uint256) { | |
assert(b <= a); | |
return a - b; | |
} | |
function add(uint256 a, uint256 b) internal pure returns (uint256) { | |
uint256 c = a + b; | |
assert(c >= a); | |
return c; | |
} | |
} | |
interface LinkERC20Basic { | |
function balanceOf(address who) external view returns (uint256); | |
function transfer(address to, uint256 value) external returns (bool); | |
event Transfer(address indexed from, address indexed to, uint256 value); | |
} | |
interface LinkERC20 is LinkERC20Basic { | |
function allowance(address owner, address spender) external view returns (uint256); | |
function transferFrom(address from, address to, uint256 value) external returns (bool); | |
function approve(address spender, uint256 value) external returns (bool); | |
event Approval(address indexed owner, address indexed spender, uint256 value); | |
} | |
interface ERC677 is LinkERC20 { | |
function transferAndCall(address to, uint value, bytes memory data) external returns (bool success); | |
event Transfer(address indexed from, address indexed to, uint256 value, bytes data); | |
} | |
contract LinkToken is ERC677 { | |
string public constant name = 'ChainLink Token'; | |
uint8 public constant decimals = 18; | |
string public constant symbol = 'LINK'; | |
uint256 public constant totalSupply = 10**27; | |
mapping(address => uint256) public balances; | |
mapping (address => mapping (address => uint256)) public allowed; | |
using linkSafeMath for uint256; | |
modifier validRecipient(address _recipient) { | |
require(_recipient != address(0) && _recipient != address(this), "Invalid Recipient"); | |
_; | |
} | |
constructor() public { | |
balances[msg.sender] = totalSupply; | |
} | |
function transfer(address _to, uint256 _value) public override validRecipient(_to) returns (bool) { | |
balances[msg.sender] = balances[msg.sender].sub(_value); | |
balances[_to] = balances[_to].add(_value); | |
emit Transfer(msg.sender, _to, _value); | |
return true; | |
} | |
function balanceOf(address _owner) public view override returns (uint256 balance) { | |
return balances[_owner]; | |
} | |
function transferFrom(address _from, address _to, uint256 _value) public override validRecipient(_to) returns (bool) { | |
uint256 _allowance = allowed[_from][msg.sender]; | |
balances[_from] = balances[_from].sub(_value); | |
balances[_to] = balances[_to].add(_value); | |
allowed[_from][msg.sender] = _allowance.sub(_value); | |
emit Transfer(_from, _to, _value); | |
return true; | |
} | |
function approve(address _spender, uint256 _value) public override validRecipient(_spender) returns (bool) { | |
allowed[msg.sender][_spender] = _value; | |
emit Approval(msg.sender, _spender, _value); | |
return true; | |
} | |
function allowance(address _owner, address _spender) public view override returns (uint256 remaining) { | |
return allowed[_owner][_spender]; | |
} | |
function increaseApproval (address _spender, uint _addedValue) public returns (bool success) { | |
allowed[msg.sender][_spender] = allowed[msg.sender][_spender].add(_addedValue); | |
emit Approval(msg.sender, _spender, allowed[msg.sender][_spender]); | |
return true; | |
} | |
function decreaseApproval (address _spender, uint _subtractedValue) public returns (bool success) { | |
uint oldValue = allowed[msg.sender][_spender]; | |
if (_subtractedValue > oldValue) { | |
allowed[msg.sender][_spender] = 0; | |
} else { | |
allowed[msg.sender][_spender] = oldValue.sub(_subtractedValue); | |
} | |
emit Approval(msg.sender, _spender, allowed[msg.sender][_spender]); | |
return true; | |
} | |
function contractFallback(address _to, uint _value, bytes memory _data) private { | |
ERC677Receiver receiver = ERC677Receiver(_to); | |
receiver.onTokenTransfer(msg.sender, _value, _data); | |
} | |
function isContract(address _addr) private view returns (bool hasCode) { | |
uint length; | |
assembly { | |
length := extcodesize(_addr) | |
} | |
return length > 0; | |
} | |
function transferAndCall(address _to, uint256 _value, bytes memory _data) | |
public | |
override | |
validRecipient(_to) | |
returns (bool success) | |
{ | |
transfer(_to, _value); | |
emit Transfer(msg.sender, _to, _value, _data); | |
if (isContract(_to)) { | |
contractFallback(_to, _value, _data); | |
} | |
return true; | |
} | |
} |
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
//SPDX-License-Identifier: None | |
// Deployed at: 0xB2b5790d0f4DFC79aBaA6F35011681D9A53731B0 on Matic Mumbai | |
pragma solidity ^0.6.11; | |
interface ERC677Receiver { | |
function onTokenTransfer(address _sender, uint _value, bytes memory _data) external; | |
} | |
library linkSafeMath { | |
function mul(uint256 a, uint256 b) internal pure returns (uint256) { | |
uint256 c = a * b; | |
assert(a == 0 || c / a == b); | |
return c; | |
} | |
function div(uint256 a, uint256 b) internal pure returns (uint256) { | |
// assert(b > 0); // Solidity automatically throws when dividing by 0 | |
uint256 c = a / b; | |
// assert(a == b * c + a % b); // There is no case in which this doesn't hold | |
return c; | |
} | |
function sub(uint256 a, uint256 b) internal pure returns (uint256) { | |
assert(b <= a); | |
return a - b; | |
} | |
function add(uint256 a, uint256 b) internal pure returns (uint256) { | |
uint256 c = a + b; | |
assert(c >= a); | |
return c; | |
} | |
} | |
interface LinkERC20Basic { | |
function balanceOf(address who) external view returns (uint256); | |
function transfer(address to, uint256 value) external returns (bool); | |
event Transfer(address indexed from, address indexed to, uint256 value); | |
} | |
interface LinkERC20 is LinkERC20Basic { | |
function allowance(address owner, address spender) external view returns (uint256); | |
function transferFrom(address from, address to, uint256 value) external returns (bool); | |
function approve(address spender, uint256 value) external returns (bool); | |
event Approval(address indexed owner, address indexed spender, uint256 value); | |
} | |
interface ERC677 is LinkERC20 { | |
function transferAndCall(address to, uint value, bytes memory data) external returns (bool success); | |
event Transfer(address indexed from, address indexed to, uint256 value, bytes data); | |
} | |
contract LinkToken is ERC677 { | |
string public constant name = 'ChainLink Token'; | |
uint8 public constant decimals = 18; | |
string public constant symbol = 'LINK'; | |
uint256 public totalSupply; | |
// child chain manager | |
address public depositor; | |
address public deployer; | |
// main data structures | |
mapping(address => uint256) public balances; | |
mapping (address => mapping (address => uint256)) public allowed; | |
using linkSafeMath for uint256; | |
modifier validRecipient(address _recipient) { | |
require(_recipient != address(0) && _recipient != address(this), "Invalid Recipient"); | |
_; | |
} | |
constructor() public { | |
// on child chain totalSupply is 0 at beginning | |
totalSupply = 0; | |
// child chain manager proxy | |
depositor = 0xb5505a6d998549090530911180f38aC5130101c6; | |
deployer = msg.sender; | |
} | |
function updateDepositor(address newDespositor) external { | |
require(msg.sender == deployer, "You can't update me"); | |
depositor = newDespositor; | |
} | |
function transfer(address _to, uint256 _value) public override validRecipient(_to) returns (bool) { | |
balances[msg.sender] = balances[msg.sender].sub(_value); | |
balances[_to] = balances[_to].add(_value); | |
emit Transfer(msg.sender, _to, _value); | |
return true; | |
} | |
function balanceOf(address _owner) public view override returns (uint256 balance) { | |
return balances[_owner]; | |
} | |
function transferFrom(address _from, address _to, uint256 _value) public override validRecipient(_to) returns (bool) { | |
uint256 _allowance = allowed[_from][msg.sender]; | |
balances[_from] = balances[_from].sub(_value); | |
balances[_to] = balances[_to].add(_value); | |
allowed[_from][msg.sender] = _allowance.sub(_value); | |
emit Transfer(_from, _to, _value); | |
return true; | |
} | |
function approve(address _spender, uint256 _value) public override validRecipient(_spender) returns (bool) { | |
allowed[msg.sender][_spender] = _value; | |
emit Approval(msg.sender, _spender, _value); | |
return true; | |
} | |
function allowance(address _owner, address _spender) public view override returns (uint256 remaining) { | |
return allowed[_owner][_spender]; | |
} | |
function increaseApproval (address _spender, uint _addedValue) public returns (bool success) { | |
allowed[msg.sender][_spender] = allowed[msg.sender][_spender].add(_addedValue); | |
emit Approval(msg.sender, _spender, allowed[msg.sender][_spender]); | |
return true; | |
} | |
function decreaseApproval (address _spender, uint _subtractedValue) public returns (bool success) { | |
uint oldValue = allowed[msg.sender][_spender]; | |
if (_subtractedValue > oldValue) { | |
allowed[msg.sender][_spender] = 0; | |
} else { | |
allowed[msg.sender][_spender] = oldValue.sub(_subtractedValue); | |
} | |
emit Approval(msg.sender, _spender, allowed[msg.sender][_spender]); | |
return true; | |
} | |
function contractFallback(address _to, uint _value, bytes memory _data) private { | |
ERC677Receiver receiver = ERC677Receiver(_to); | |
receiver.onTokenTransfer(msg.sender, _value, _data); | |
} | |
function isContract(address _addr) private view returns (bool hasCode) { | |
uint length; | |
assembly { | |
length := extcodesize(_addr) | |
} | |
return length > 0; | |
} | |
function transferAndCall(address _to, uint256 _value, bytes memory _data) | |
public | |
override | |
validRecipient(_to) | |
returns (bool success) | |
{ | |
transfer(_to, _value); | |
emit Transfer(msg.sender, _to, _value, _data); | |
if (isContract(_to)) { | |
contractFallback(_to, _value, _data); | |
} | |
return true; | |
} | |
// additional methods needed for enabling cross chain asset transfer | |
function deposit(address user, bytes calldata depositData) external { | |
require(msg.sender == depositor, "Only ChildChainManager can deposit"); | |
require(user != address(0), "Not a valid address"); | |
uint256 amount = abi.decode(depositData, (uint256)); | |
totalSupply = totalSupply.add(amount); | |
balances[user] = balances[user].add(amount); | |
emit Transfer(address(0), user, amount); | |
} | |
function withdraw(uint256 amount) external { | |
balances[msg.sender] = balances[msg.sender].sub(amount); | |
totalSupply = totalSupply.sub(amount); | |
emit Transfer(msg.sender, address(0), amount); | |
} | |
} |
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
Mapping was done by: 0xf89154D7A42c5E9f77884511586A9db4618683C5 | |
Transaction: https://goerli.etherscan.io/tx/0x234f511a65d8849c239bbd8f2ff8fd722062d08888bafb32d9e5941f6a3189d7 |
Root Token on Goerli: 0x6a3e42FDA39D199417B87D023459d203e4A74b66
Child Token on Matic Mumbai: 0xB2b5790d0f4DFC79aBaA6F35011681D9A53731B0
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Before mapping LINK token on Matic Mumbai, I had to rewrite LINK with Solidity 0.6.11, due to backward incompatible changes happened in solidity overtime. I decided to put it here, so that this can be used when in future it might be required to map LINK on Matic Main Network.
😉