# Mutual-credit implementation
## Entries
### Private entries:
```rust
struct Transaction {
debtor_address: Address,
creditor_address: Address,
amount: f64,
timestamp: u64
}
#[derive(Serialize, Deserialize, Debug, DefaultJson, Clone)]
pub enum OfferState {
Pending,
Canceled,
Approved {
approved_header_address: Option<Address>
},
Completed { attestation_address: Address },
}
struct Offer {
transaction: Transaction,
state: OfferState
}
```
### Public entry
```rust
struct Attestation {
header_addresses: Vec<Address>
}
Link: "agent->attestation"
```
Create: only valid if
- It's signed by all the header's agents
- I can get all the headers from the DHT
- The headers' entry addresses are equal
## Create offer
```mermaid
sequenceDiagram
DebtorUI->>DebtorCell: offer_credits(amount, receiver, timestamp)
DebtorCell->>CreditorCell: send_offer(offer)
CreditorCell-->>CreditorCell: create_offer(offer)
CreditorCell-->>CreditorUI: signal_offer(offer)
CreditorCell-->>DebtorCell: created
DebtorCell-->>DebtorCell: create_offer(offer)
```
## Get counterparty balance
```mermaid
sequenceDiagram
participant DebtorCell
participant CreditorCell
participant DHT
participant CreditorUI
ReceiverUI->>CreditorCell: get_balance(offer_address)
CreditorCell->>DebtorCell: get_chain_snapshot(offer_address)
DebtorCell-->>DebtorCell: check_offer(offer_address)
DebtorCell-->>CreditorCell: chain_snapshot
CreditorCell->>DHT: get_last_attestation(sender_address)
DHT-->>CreditorCell: last_attestation
CreditorCell-->>CreditorCell: check attestation & compute balance
CreditorCell-->>CreditorUI: (last_header_address, balance)
```
## Accept offer
```mermaid
sequenceDiagram
participant DebtorCell
participant CreditorCell
participant CreditorUI
CreditorUI->>CreditorCell: accept_offer(transaction_address, last_header_address)
CreditorCell->>DebtorCell: accept_offer(transaction_address, last_header_address)
DebtorCell-->>DebtorCell: check_offer(transaction_address)
DebtorCell-->>DebtorCell: check_last_header(last_header_address)
DebtorCell-->>DebtorCell: create_transaction(transaction_address)
DebtorCell->>CreditorCell: complete_tx(transaction_header)
CreditorCell-->>CreditorCell: check_last_header
CreditorCell-->>CreditorCell: check_offer(transaction_address)
CreditorCell-->>CreditorCell: create_transaction(transaction_address)
CreditorCell-->>CreditorCell: build_atestation(headers)
CreditorCell->>DebtorCell: sign_attestation(headers)
DebtorCell-->>DebtorCell: check_offer(transaction_address)
DebtorCell-->>DebtorCell: check_last_header(last_header_address)
DebtorCell-->>DebtorCell: sign_attestation(attestation)
DebtorCell-->>CreditorCell: singature
CreditorCell-->>CreditorCell: create_attestation
CreditorCell-->>CreditorCell: complete_offer
CreditorCell-->>DebtorCell: (headers, attestation_signature)
DebtorCell-->>DebtorCell: create_attestation
DebtorCell-->>DebtorCell: complete_offer
```