Skip to content

Commit

Permalink
feat(protocol): safeguard possible failing calls (#16931)
Browse files Browse the repository at this point in the history
Co-authored-by: Keszey Dániel <[email protected]>
Co-authored-by: Daniel Wang <[email protected]>
Co-authored-by: Brecht Devos <[email protected]>
  • Loading branch information
4 people authored Apr 30, 2024
1 parent 1573735 commit 0f6b6b5
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 6 deletions.
20 changes: 16 additions & 4 deletions packages/protocol/contracts/tokenvault/ERC20Vault.sol
Original file line number Diff line number Diff line change
Expand Up @@ -355,15 +355,27 @@ contract ERC20Vault is BaseVault {
balanceChange_ = _op.amount;
} else {
// If it's a canonical token
IERC20Metadata meta = IERC20Metadata(_op.token);
ctoken_ = CanonicalERC20({
chainId: uint64(block.chainid),
addr: _op.token,
decimals: meta.decimals(),
symbol: meta.symbol(),
name: meta.name()
decimals: 0,
symbol: "",
name: ""
});

// Try fill in the boilerplate values, but use try-catch because functions below are
// ERC20-optional only.
IERC20Metadata meta = IERC20Metadata(_op.token);
try meta.decimals() returns (uint8 _decimals) {
ctoken_.decimals = _decimals;
} catch { }
try meta.name() returns (string memory _name) {
ctoken_.name = _name;
} catch { }
try meta.symbol() returns (string memory _symbol) {
ctoken_.symbol = _symbol;
} catch { }

// Query the balance then query it again to get the actual amount of
// token transferred into this address, this is more accurate than
// simply using `amount` -- some contract may deduct a fee from the
Expand Down
13 changes: 11 additions & 2 deletions packages/protocol/contracts/tokenvault/ERC721Vault.sol
Original file line number Diff line number Diff line change
Expand Up @@ -204,10 +204,19 @@ contract ERC721Vault is BaseNFTVault, IERC721Receiver {
ctoken_ = CanonicalNFT({
chainId: uint64(block.chainid),
addr: _op.token,
symbol: t.symbol(),
name: t.name()
symbol: "",
name: ""
});

// Try fill in the boilerplate values, but use try-catch because functions below are
// ERC20-optional only.
try t.name() returns (string memory _name) {
ctoken_.name = _name;
} catch { }
try t.symbol() returns (string memory _symbol) {
ctoken_.symbol = _symbol;
} catch { }

for (uint256 i; i < _op.tokenIds.length; ++i) {
t.safeTransferFrom(msg.sender, address(this), _op.tokenIds[i]);
}
Expand Down

0 comments on commit 0f6b6b5

Please sign in to comment.