Skip to content

Commit

Permalink
Merge pull request #48 from eosnetworkfoundation/yarkin/remove_hardcode
Browse files Browse the repository at this point in the history
pass in linked eos address and evm address instead of hardcode
  • Loading branch information
yarkinwho authored Aug 14, 2024
2 parents 7a440c5 + b0c548f commit d342384
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 15 deletions.
26 changes: 15 additions & 11 deletions antelope_contracts/contracts/erc20/src/erc20.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ void erc20::upgradeto(std::string impl_address) {
--contract_itr;

auto reserved_addr = silkworm::make_reserved_address(receiver_account().value);

auto evm_reserved_addr = silkworm::make_reserved_address(config.evm_account.value);
bytes call_data;
initialize_data(call_data, solidity::proxy::bytecode);

Expand All @@ -143,8 +143,8 @@ void erc20::upgradeto(std::string impl_address) {
call_data.insert(call_data.end(), contract_itr->address.begin(), contract_itr->address.end());

bytes constructor_data;
// sha(function initialize(uint8 _precision,uint256 _egressFee,string memory _name,string memory _symbol,string memory _eos_token_contract)) == 0xd66d4ac3
uint8_t func_[4] = {0xd6,0x6d,0x4a,0xc3};
// initialize(address,address,uint8,uint256,string,string,string) == 0x1fa01e36
uint8_t func_[4] = {0x1f,0xa0,0x1e,0x36};
constructor_data.insert(constructor_data.end(), func_, func_ + sizeof(func_));

auto pack_uint256 = [&](bytes &ds, const intx::uint256 &val) {
Expand All @@ -169,14 +169,18 @@ void erc20::upgradeto(std::string impl_address) {
}
};

pack_uint32(constructor_data, (uint8_t)erc20_precision); // offset 0
pack_uint256(constructor_data, egress_fee_evm); // offset 32
pack_uint32(constructor_data, 160); // offset 64
pack_uint32(constructor_data, 224); // offset 96
pack_uint32(constructor_data, 288); // offset 128
pack_string(constructor_data, evm_token_name); // offset 160
pack_string(constructor_data, evm_token_symbol); // offset 224
pack_string(constructor_data, token_contract.to_string()); // offset 288
constructor_data.insert(constructor_data.end(), 32 - kAddressLength, 0); // padding for address, offset 0
constructor_data.insert(constructor_data.end(), reserved_addr.bytes, reserved_addr.bytes + kAddressLength);
constructor_data.insert(constructor_data.end(), 32 - kAddressLength, 0); // padding for address, offset 32
constructor_data.insert(constructor_data.end(), evm_reserved_addr.bytes, evm_reserved_addr.bytes + kAddressLength);
pack_uint32(constructor_data, (uint8_t)erc20_precision); // offset 64
pack_uint256(constructor_data, egress_fee_evm); // offset 96
pack_uint32(constructor_data, 224); // offset 128
pack_uint32(constructor_data, 288); // offset 160
pack_uint32(constructor_data, 352); // offset 196
pack_string(constructor_data, evm_token_name); // offset 224
pack_string(constructor_data, evm_token_symbol); // offset 288
pack_string(constructor_data, token_contract.to_string()); // offset 352

pack_uint32(call_data, 64); // offset 32
pack_string(call_data, constructor_data); // offset 64
Expand Down
38 changes: 34 additions & 4 deletions solidity_contracts/erc20/contract.sol
Original file line number Diff line number Diff line change
Expand Up @@ -1488,22 +1488,52 @@ contract BridgeERC20 is Initializable, ERC20Upgradeable, UUPSUpgradeable {
address public evmAddress;
uint8 public precision;
uint256 public egressFee;
function initialize(uint8 _precision,
function initialize(address _linkedEOSAddress,
address _evmAddress,
uint8 _precision,
uint256 _egressFee,
string memory _name,
string memory _symbol,
string memory _eos_token_contract
) initializer public {
__ERC20_init(_name, _symbol);
__UUPSUpgradeable_init();
evmAddress = 0xbBBBbBbbbBBBBbbbbbbBBbBB5530EA015b900000;
linkedEOSAddress = 0xbbBbbbBbbBBbBBbBBBbbbBbB5530eA015740a800;
linkedEOSAccountName = "eosio.erc2o";
evmAddress = _evmAddress;
linkedEOSAddress = _linkedEOSAddress;
linkedEOSAccountName = _addressToName(linkedEOSAddress);
precision = _precision;
egressFee = _egressFee;
eos_token_contract = _eos_token_contract;
}

function _addressToName(address input) internal pure returns (string memory) {
require(_isReservedAddress(input));
uint64 a = uint64(uint160(input));
bytes memory bstr = new bytes(12);

uint count = 0;
for (uint i = 0; i < 12 ; i++) {
uint64 c = (a >> (64 - 5*(i+1))) & uint64(31);
if (c == 0) {
bstr[i] = bytes1(uint8(46)); // .
}
else if (c <= 5) {
bstr[i] = bytes1(uint8(c + 48)); // '0' + b
count = i + 1;
}
else {
bstr[i] = bytes1(uint8(c - 6 + 97)); // 'a' + b - 6
count = i + 1;
}
}

bytes memory bstrTrimmed = new bytes(count);
for (uint j = 0; j < count; j++) {
bstrTrimmed[j] = bstr[j];
}
return string(bstrTrimmed);
}

function setFee(uint256 _egressFee) public {
require(msg.sender == linkedEOSAddress, "Bridge: only linked EOS address can set fee");
egressFee = _egressFee;
Expand Down

0 comments on commit d342384

Please sign in to comment.