Skip to content

Commit

Permalink
Cherry-pick 65902e1 @ itamarcps/orbitersdk-cpp-evm (Fix ERC20/721 DB …
Browse files Browse the repository at this point in the history
…Dumping)
  • Loading branch information
Jean-Lessa committed Feb 28, 2024
1 parent 3b39e34 commit cbf04ab
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 13 deletions.
16 changes: 10 additions & 6 deletions src/contract/templates/erc20.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@ ERC20::ERC20(ContractManagerInterface &interface, const Address& address, DB& db
}
auto allowances = db_.getBatch(this->getNewPrefix("allowed_"));
for (const auto& dbEntry : allowances) {
BytesArrView valueView(dbEntry.value);
this->allowed_[Address(dbEntry.key)][Address(valueView.subspan(0, 20))] = Utils::fromBigEndian<uint256_t>(valueView.subspan(20));
BytesArrView key(dbEntry.key);
Address owner(key.subspan(0,20));
Address spender(key.subspan(20));
this->allowed_[owner][spender] = Utils::bytesToUint256(dbEntry.value);
}

this->name_.commit();
Expand Down Expand Up @@ -117,12 +119,14 @@ ERC20::~ERC20() {
batchOperations.push_back(key, value, this->getNewPrefix("balances_"));
}

// SafeUnorderedMap<Address, std::unordered_map<Address, uint256_t, SafeHash>>
for (auto it = allowed_.cbegin(); it != allowed_.cend(); ++it) {
for (auto it2 = it->second.cbegin(); it2 != it->second.cend(); ++it2) {
const auto& key = it->first.get();
Bytes value = it2->first.asBytes();
Utils::appendBytes(value, Utils::uintToBytes(it2->second));
batchOperations.push_back(key, value, this->getNewPrefix("allowed_"));
// Key = Address + Address
// value = uint256_t
auto key = it->first.asBytes();
Utils::appendBytes(key, it2->first.asBytes());
batchOperations.push_back(key, Utils::uint256ToBytes(it2->second), this->getNewPrefix("allowed_"));
}
}
this->db_.putBatch(batchOperations);
Expand Down
15 changes: 8 additions & 7 deletions src/contract/templates/erc721.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,10 @@ ERC721::ERC721(
}
auto operatorAddressApprovals = db_.getBatch(this->getNewPrefix("operatorAddressApprovals_"));
for (const auto& dbEntry : operatorAddressApprovals) {
BytesArrView valueView(dbEntry.value);
this->operatorAddressApprovals_[Address(dbEntry.key)][Address(valueView.subspan(0, 20))] = valueView[20];
BytesArrView keyView(dbEntry.key);
Address owner(keyView.subspan(0, 20));
Address operatorAddress(keyView.subspan(20));
this->operatorAddressApprovals_[owner][operatorAddress] = dbEntry.value[0];
}

this->name_.commit();
Expand Down Expand Up @@ -124,11 +126,10 @@ ERC721::~ERC721() {

for (auto it = operatorAddressApprovals_.cbegin(); it != operatorAddressApprovals_.cend(); ++it) {
for (auto it2 = it->second.cbegin(); it2 != it->second.cend(); ++it2) {
// key: address -> value: address + bool (1 byte)
const auto& key = it->first.get();
Bytes value = it2->first.asBytes();
value.insert(value.end(), char(it2->second));
batchedOperations.push_back(key, value, this->getNewPrefix("operatorAddressApprovals_"));
// key: address + address -> bool
Bytes key = it->first.asBytes();
Utils::appendBytes(key, it2->first.asBytes());
Bytes value = {uint8_t(it2->second)};
}
}

Expand Down

0 comments on commit cbf04ab

Please sign in to comment.