Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add collateralization & examples #1

Open
wants to merge 23 commits into
base: main
Choose a base branch
from
Open

Conversation

Theodus
Copy link
Member

@Theodus Theodus commented Jul 17, 2023

This PR adds the core collateralization contracts and some examples for lending, loan aggregation, and data services.

@Theodus Theodus requested review from pcarranzav and tmigone July 17, 2023 13:41
@Theodus Theodus force-pushed the theodus/collateralization branch 4 times, most recently from b374cab to e5a103a Compare July 24, 2023 00:36
@Theodus Theodus force-pushed the theodus/collateralization branch from e5a103a to 5994d51 Compare July 24, 2023 00:43
Comment on lines 23 to 28
/// Add provider and fund their future payment.
function addProvider(address _provider, uint128 _payment) public onlyOwner {
require(_payment > 0);
require(providers[_provider].payment == 0, "provider exists");
providers[_provider] = ProviderState({deposit: 0, payment: _payment});
collateralization.token().transferFrom(msg.sender, address(this), _payment);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you elaborate what this is/does and why it's onlyOwner?

Copy link
Member

@pcarranzav pcarranzav left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work! Some initial comments (still reviewing and wrapping my head around the whole thing, have only looked at Collateralization for now)

/// @param _unlock Unlock timestamp of the new deposit, in seconds. Set to a nonzero value to lock deposit.
/// @return id Unique ID associated with the new deposit.
function deposit(address _arbiter, uint256 _value, uint64 _unlock) public returns (uint128) {
lastID += 1;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm thinking this way to create IDs is unpredictable, since the resulting ID can be larger than expected if someone creates a deposit in the same block. So it might be a good idea to instead use a per-sender predictable ID e.g. keccak256(msg.sender, nonces[msg.sender]) so that a caller can batch a deposit + lock or getDeposit or some other action knowing what the ID will be.

Copy link
Member

@pcarranzav pcarranzav Jul 27, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And a depositFrom function could be useful for users to create deposits on behalf of other depositors (as long as they have a token approval)

Edit: actually, I think the opposite may be more useful: a depositFor or depositTo where a third party starts a deposit on behalf of someone else, providing the tokens. We have a stakeTo in the Staking contract that serves this purpose. This could be useful, among other things, if a service provider has tokens on several accounts but uses a single address to provide services.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Echoing Pablo's comments on IDs, I would also add block.chainId to the id generation/derivation.

I can't think of any potential problems now, but since we will be building on top of this I think it's better to add it and rule out any silly cross-chain replay attacks.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

aaah good point, making deposit IDs unique cross-chain could prove useful in the future

src/Collateralization.sol Outdated Show resolved Hide resolved
src/Collateralization.sol Outdated Show resolved Hide resolved
src/Collateralization.sol Outdated Show resolved Hide resolved
src/Collateralization.sol Outdated Show resolved Hide resolved
/// This contract manages Deposits as described above.
contract Collateralization {
event Deposit(uint128 indexed id, address indexed arbiter, uint256 value, uint64 unlock);
event Lock(uint128 indexed id, uint64 unlock);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit/suggestion: Event names could be a bit more verbose, e.g. DepositCreated, DepositLocked, etc.

src/Collateralization.sol Outdated Show resolved Hide resolved
src/Collateralization.sol Outdated Show resolved Hide resolved
src/Collateralization.sol Outdated Show resolved Hide resolved
src/Collateralization.sol Outdated Show resolved Hide resolved
@Theodus Theodus force-pushed the theodus/collateralization branch from 7c72228 to 033ab68 Compare August 3, 2023 14:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants