@Untitled Can u dump all ur mermaid sequence diagrams in here and add titles. Make them collapsable ideally but i can do that
claimAndRepay()
- Shows an Arbiter taking a Borrower’s revenue from their Spigot, trading it on 0x protocol to the token they’re currently borrowing, and immediately pays down debt to next lender in queue.SpigotedLine
's claimAndRepay
operates similarly to claimAndTrade
, but includes the additional step of repaying part of, or the entire, loan. It could be thought of as claimAndTradeAndRepay
. Like claimAndTrade
, the function claims (fetches/transfers) earned revenue tokens from the Spigot
to the SpigotedLine
- these need to have been claimed/sent by the revenue contract to the Spigot
in advance. This increases the unused
token balance. This example demonstrates using an ERC20 token, such as $USDC or $DAI, as the credit token, and native $ETH as the revenue token generated by the borrower's revenue contract.
sequenceDiagram
autonumber
actor Borrower
actor Lender
actor Arbiter
actor Anyone
########## Adding Credit ##########
Borrower ->>+ SpigotedLine: [LineOfCredit] addCredit()
SpigotedLine ->>- SpigotedLine: mutualConsent registered
Lender ->> SpigotedLine: [LineOfCredit] addCredit()
activate SpigotedLine
SpigotedLine ->> SpigotedLine: mutualConsent deleted
SpigotedLine --> LineLib: receiveTokenOrEth()
activate LineLib
LineLib ->> ERC20: safeTransferFrom()
activate ERC20
ERC20 --> Lender: []
deactivate ERC20
Lender ->> SpigotedLine: $token
deactivate LineLib
SpigotedLine ->> CreditLib: create()
activate CreditLib
CreditLib ->> Oracle: getLatestAnswer(token)
activate Oracle
Oracle ->>+ FeedRegistry: latestRoundData()
FeedRegistry -->>- Oracle: [price]
Oracle -->> CreditLib: [price]
CreditLib -->> SpigotedLine: [credit]
SpigotedLine ->>+ InterestRateCredit: setRate()
InterestRateCredit -->>- SpigotedLine: [true]
SpigotedLine -->> Lender: [id]
deactivate Oracle
deactivate SpigotedLine
deactivate CreditLib
The borrower then borrows from the position.
sequenceDiagram
autonumber
actor Borrower
actor Lender
actor Arbiter
actor Anyone
########## Borrowing ##########
Borrower ->> SpigotedLine: [LineOfCredit] borrow()
activate SpigotedLine
SpigotedLine ->> CreditLib: accrue()
activate CreditLib
CreditLib -->>+ InterestRateCredit: accrueInterest()
InterestRateCredit -->>- CreditLib: [interestOwed]
CreditLib -->> SpigotedLine: [credit]
deactivate CreditLib
SpigotedLine ->> LineLib: sendOutTokenOrETH()
activate LineLib
LineLib ->>+ ERC20: safeTransfer()
ERC20 -->- SpigotedLine: []
SpigotedLine ->> Borrower: $token
deactivate LineLib
deactivate SpigotedLine
Claiming revenue from the revenue contract and then calling claimAndRepay
.
sequenceDiagram
autonumber
actor Borrower
actor Lender
actor Arbiter
actor Anyone
########## Claiming Revenue ##########
Anyone ->> Spigot: claimRevenue()
activate Spigot
Spigot ->> SpigotLib: claimRevenue()
activate SpigotLib
SpigotLib ->> LineLib: getBalance()
activate LineLib
activate LineLib
LineLib ->> ERC20: balanceOf()
activate ERC20
ERC20 -->> Spigot:
Spigot -->> ERC20: [balance]
ERC20 -->> LineLib: [balance]
deactivate ERC20
LineLib -->> SpigotLib: [existingBalance]
deactivate LineLib
SpigotLib ->> RevenueContract: call("claimFn")
activate RevenueContract
RevenueContract ->> Spigot: $ETH
deactivate RevenueContract
SpigotLib ->> LineLib: getBalance()
activate LineLib
LineLib -->> Spigot: [ETH balance]
deactivate LineLib
Spigot -->> SpigotLib: [ETH balance]
SpigotLib -->> Spigot: [claimed]
deactivate SpigotLib
Spigot -->> Anyone: [claimed]
deactivate Spigot
########## claimAndRepay ##########
Arbiter ->> SpigotedLine: claimAndRepay(address,bytes)
activate SpigotedLine
SpigotedLine ->> CreditLib: accrue()
activate SpigotedLine
CreditLib ->> InterestRateCredit: accrueInterest()
activate InterestRateCredit
InterestRateCredit -->> CreditLib: [interestOwed]
deactivate InterestRateCredit
CreditLib ->> CreditLib: credit.interestAccrued + interestOwed
CreditLib -->> SpigotedLine: [credit]
deactivate SpigotedLine
SpigotedLine ->> SpigotedLineLib: claimAndTrade()
activate SpigotedLineLib
SpigotedLineLib -->> LineLib: getBalance("claimToken")
activate LineLib
LineLib -->> SpigotedLine: balance("ETH")
SpigotedLine -->> LineLib: [balance]
LineLib -->> SpigotedLineLib: [oldClaimTokens]
deactivate LineLib
SpigotedLineLib -->> LineLib: getBalance("targenToken")
activate LineLib
LineLib -->> SpigotedLine: balance("$token")
SpigotedLine -->> LineLib: [balance]
LineLib -->> SpigotedLineLib: [oldTargetTokens]
deactivate LineLib
SpigotedLineLib ->> Spigot: claimOwnerTokens()
activate Spigot
Spigot ->> SpigotLib: claimOwnerTokens()
activate SpigotLib
SpigotLib --> LineLib: sentOutTokenOrETH()
activate LineLib
LineLib ->> Spigot: call{value: $ETH}("")
deactivate LineLib
Spigot ->> SpigotedLine: $ETH
SpigotLib -->> Spigot: [claimed]
deactivate SpigotLib
Spigot -->> SpigotedLineLib: [claimed]
deactivate Spigot
SpigotedLineLib ->> 0x: call{}(tradeData)
activate 0x
SpigotedLine ->> 0x: $ETH
0x ->> SpigotedLine: $token
0x -->> SpigotedLineLib: [true]
deactivate 0x
deactivate SpigotedLineLib
SpigotedLineLib -->> SpigotedLine: [tokensBought, tokensUnused]
SpigotedLine ->> SpigotedLine: unused[claimToken] += tokensUnused
SpigotedLine ->> SpigotedLine: unused[targetToken] += tokensBought
SpigotedLine ->> CreditLib: repay(credit memory)
activate SpigotedLine
CreditLib ->> CreditLib: credit.principal -= principalPayment
CreditLib ->> CreditLib: credit.interestRepaid += interest
CreditLib -->> SpigotedLine: [credit]
deactivate SpigotedLine
SpigotedLine ->> SpigotedLine: credits[id] = credit
SpigotedLine -->> Arbiter: [tokensBought]
deactivate SpigotedLine
claimAndTrade()
- Shows an Arbiter taking a Borrower’s revenue from their Spigot, trading it on 0x protocol to the token they’re currently borrowing, and then adding those tokens to reserves for Borrower or next Lender to repay whenever they want later.SpigotedLine
's claimAndRepaey
operates similarly to claimAndTrade
, but includes the additional step of repaying part of, or the entire, loan. It could be thought of as claimAndTradeAndRepay
. Like claimAndTrade
, the function claims (fetches/transfers) earned revenue tokens from the Spigot
to the SpigotedLine
- these need to have been claimed/sent by the revenue contract to the Spigot
in advance. This increases the unused
token balance. This example demonstrates using an ERC20 token, such as $USDC or $DAI, as the credit token, and native $ETH as the revenue token generated by the borrower's revenue contract.
sequenceDiagram
autonumber
actor Borrower
actor Lender
actor Arbiter
actor Anyone
########## Adding Credit ##########
Borrower ->>+ SpigotedLine: [LineOfCredit] addCredit()
SpigotedLine ->>- SpigotedLine: mutualConsent registered
Lender ->> SpigotedLine: [LineOfCredit] addCredit()
activate SpigotedLine
SpigotedLine ->> SpigotedLine: mutualConsent deleted
SpigotedLine --> LineLib: receiveTokenOrEth()
activate LineLib
LineLib ->> ERC20: safeTransferFrom()
activate ERC20
ERC20 --> Lender: []
deactivate ERC20
Lender ->> SpigotedLine: $token
deactivate LineLib
SpigotedLine ->> CreditLib: create()
activate CreditLib
CreditLib ->> Oracle: getLatestAnswer(token)
activate Oracle
Oracle ->>+ FeedRegistry: latestRoundData()
FeedRegistry -->>- Oracle: [price]
Oracle -->> CreditLib: [price]
CreditLib -->> SpigotedLine: [credit]
SpigotedLine ->>+ InterestRateCredit: setRate()
InterestRateCredit -->>- SpigotedLine: [true]
SpigotedLine -->> Lender: [id]
deactivate Oracle
deactivate SpigotedLine
deactivate CreditLib
```
```mermaid
sequenceDiagram
autonumber
actor Borrower
actor Lender
actor Arbiter
actor Anyone
########## Borrowing ##########
Borrower ->> SpigotedLine: [LineOfCredit] borrow()
activate SpigotedLine
SpigotedLine ->> CreditLib: accrue()
activate CreditLib
CreditLib -->>+ InterestRateCredit: accrueInterest()
InterestRateCredit -->>- CreditLib: [interestOwed]
CreditLib -->> SpigotedLine: [credit]
deactivate CreditLib
SpigotedLine ->> LineLib: sendOutTokenOrETH()
activate LineLib
LineLib ->>+ ERC20: safeTransfer()
ERC20 -->- SpigotedLine: []
SpigotedLine ->> Borrower: $token
deactivate LineLib
deactivate SpigotedLine
```
Claiming revenue from the revenue contract and then calling `claimAndRepay`.
```mermaid
sequenceDiagram
autonumber
actor Borrower
actor Lender
actor Arbiter
actor Anyone
########## Claiming Revenue ##########
Anyone ->> Spigot: claimRevenue()
activate Spigot
Spigot ->> SpigotLib: claimRevenue()
activate SpigotLib
SpigotLib ->> LineLib: getBalance()
activate LineLib
activate LineLib
LineLib ->> ERC20: balanceOf()
activate ERC20
ERC20 -->> Spigot:
Spigot -->> ERC20: [balance]
ERC20 -->> LineLib: [balance]
deactivate ERC20
LineLib -->> SpigotLib: [existingBalance]
deactivate LineLib
SpigotLib ->> RevenueContract: call("claimFn")
activate RevenueContract
RevenueContract ->> Spigot: $ETH
deactivate RevenueContract
SpigotLib ->> LineLib: getBalance()
activate LineLib
LineLib -->> Spigot: [ETH balance]
deactivate LineLib
Spigot -->> SpigotLib: [ETH balance]
SpigotLib -->> Spigot: [claimed]
deactivate SpigotLib
Spigot -->> Anyone: [claimed]
deactivate Spigot
########## claimAndRepay ##########
Arbiter ->> SpigotedLine: claimAndRepay(address,bytes)
activate SpigotedLine
SpigotedLine ->> CreditLib: accrue()
activate SpigotedLine
CreditLib ->> InterestRateCredit: accrueInterest()
activate InterestRateCredit
InterestRateCredit -->> CreditLib: [interestOwed]
deactivate InterestRateCredit
CreditLib ->> CreditLib: credit.interestAccrued + interestOwed
CreditLib -->> SpigotedLine: [credit]
deactivate SpigotedLine
SpigotedLine ->> SpigotedLineLib: claimAndTrade()
activate SpigotedLineLib
SpigotedLineLib -->> LineLib: getBalance("claimToken")
activate LineLib
LineLib -->> SpigotedLine: balance("ETH")
SpigotedLine -->> LineLib: [balance]
LineLib -->> SpigotedLineLib: [oldClaimTokens]
deactivate LineLib
SpigotedLineLib -->> LineLib: getBalance("targenToken")
activate LineLib
LineLib -->> SpigotedLine: balance("$token")
SpigotedLine -->> LineLib: [balance]
LineLib -->> SpigotedLineLib: [oldTargetTokens]
deactivate LineLib
SpigotedLineLib ->> Spigot: claimOwnerTokens()
activate Spigot
Spigot ->> SpigotLib: claimOwnerTokens()
activate SpigotLib
SpigotLib --> LineLib: sentOutTokenOrETH()
activate LineLib
LineLib ->> Spigot: call{value: $ETH}("")
deactivate LineLib
Spigot ->> SpigotedLine: $ETH
SpigotLib -->> Spigot: [claimed]
deactivate SpigotLib
Spigot -->> SpigotedLineLib: [claimed]
deactivate Spigot
SpigotedLineLib ->> 0x: call{}(tradeData)
activate 0x
SpigotedLine ->> 0x: $ETH
0x ->> SpigotedLine: $token
0x -->> SpigotedLineLib: [true]
deactivate 0x
deactivate SpigotedLineLib
SpigotedLineLib -->> SpigotedLine: [tokensBought, tokensUnused]
SpigotedLine ->> SpigotedLine: unused[claimToken] += tokensUnused
SpigotedLine ->> SpigotedLine: unused[targetToken] += tokensBought
SpigotedLine ->> CreditLib: repay(credit memory)
activate SpigotedLine
CreditLib ->> CreditLib: credit.principal -= principalPayment
CreditLib ->> CreditLib: credit.interestRepaid += interest
CreditLib -->> SpigotedLine: [credit]
deactivate SpigotedLine
SpigotedLine ->> SpigotedLine: credits[id] = credit
SpigotedLine -->> Arbiter: [tokensBought]
deactivate SpigotedLine