LoanApplication

bizlogic.application.LoanApplicationWriter

Loan Application Writer.

Create a request to ask for funds. Other users will then run a credit check on you and send you loan offers. When the user accepts a loan offer, they can close their loan application to tell others they are no longer interested in additional borrowing.

The filename will be written to IPFS as:

application/borrower_<id>/application_<id>/created_<timestamp>
Source code in bizlogic/application.py
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
class LoanApplicationWriter():
    """Loan Application Writer.

    Create a request to ask for funds. Other users will then
    run a credit check on you and send you loan offers.
    When the user accepts a loan offer, they can close their
    loan application to tell others they are no longer
    interested in additional borrowing.

    The filename will be written to IPFS as:

        application/borrower_<id>/application_<id>/created_<timestamp>
    """

    application_id: str
    borrower: str
    amount_asking: int
    ipfsclient: Ipfs
    data: LoanApplication

    def __init__(
            self: Self,
            ipfsclient: Ipfs,
            borrower: str,
            amount_asking: int) -> None:
        """Create a new loan application.

        Args:
            ipfsclient (Ipfs): The ipfs client
            borrower (str): The borrower id
            amount_asking (int): The amount the borrower is asking for
        """
        self.application_id = str(uuid.uuid4())
        self.borrower = borrower
        self.ipfsclient = ipfsclient
        self.amount_asking = amount_asking
        self.closed = False
        self.data = LoanApplication(
            amount_asking=self.amount_asking,
            closed=self.closed
        )

    def write(self: Self) -> None:
        """Write the loan application to IPFS."""
        self._generate_index()
        store = Store(
            index=self.index,
            ipfs=self.ipfsclient,
            writer=self.data
        )

        store.add()

    @TestingOnly.decorator
    def delete(self: Self) -> None:
        """Delete the loan application from IPFS."""
        # don't need to generate index, just delete the store
        store = Store(
            index=self.index,
            ipfs=self.ipfsclient,
            writer=self.data
        )

        store.delete()

    def _generate_index(self: Self) -> None:
        """Generate the index for the loan application."""
        self.index = Index(
            prefix=PREFIX,
            index={
                "borrower": self.borrower,
                "application": self.application_id
            },
            subindex=Index(
                index={
                    "created": str(time.time_ns())
                }
            )
        )

    def withdraw_loan_application(self: Self) -> None:
        """Withdraw the loan application."""
        # create a new LoanApplication object with closed=True
        self.data = LoanApplication(
            amount_asking=self.amount_asking,
            closed=True
        )

        self.write()

__init__(ipfsclient, borrower, amount_asking)

Create a new loan application.

Parameters:
  • ipfsclient (Ipfs) –

    The ipfs client

  • borrower (str) –

    The borrower id

  • amount_asking (int) –

    The amount the borrower is asking for

bizlogic/application.py
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
def __init__(
        self: Self,
        ipfsclient: Ipfs,
        borrower: str,
        amount_asking: int) -> None:
    """Create a new loan application.

    Args:
        ipfsclient (Ipfs): The ipfs client
        borrower (str): The borrower id
        amount_asking (int): The amount the borrower is asking for
    """
    self.application_id = str(uuid.uuid4())
    self.borrower = borrower
    self.ipfsclient = ipfsclient
    self.amount_asking = amount_asking
    self.closed = False
    self.data = LoanApplication(
        amount_asking=self.amount_asking,
        closed=self.closed
    )

write()

Write the loan application to IPFS.

bizlogic/application.py
60
61
62
63
64
65
66
67
68
69
def write(self: Self) -> None:
    """Write the loan application to IPFS."""
    self._generate_index()
    store = Store(
        index=self.index,
        ipfs=self.ipfsclient,
        writer=self.data
    )

    store.add()

_generate_index()

Generate the index for the loan application.

bizlogic/application.py
83
84
85
86
87
88
89
90
91
92
93
94
95
96
def _generate_index(self: Self) -> None:
    """Generate the index for the loan application."""
    self.index = Index(
        prefix=PREFIX,
        index={
            "borrower": self.borrower,
            "application": self.application_id
        },
        subindex=Index(
            index={
                "created": str(time.time_ns())
            }
        )
    )

withdraw_loan_application()

Withdraw the loan application.

bizlogic/application.py
 98
 99
100
101
102
103
104
105
106
def withdraw_loan_application(self: Self) -> None:
    """Withdraw the loan application."""
    # create a new LoanApplication object with closed=True
    self.data = LoanApplication(
        amount_asking=self.amount_asking,
        closed=True
    )

    self.write()

bizlogic.application.LoanApplicationReader

Loan Application Reader.

Read loan applications from IPFS. This is useful for lenders who want to see who is asking for loans.

Source code in bizlogic/application.py
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
class LoanApplicationReader():
    """Loan Application Reader.

    Read loan applications from IPFS.
    This is useful for lenders who want to see
    who is asking for loans.
    """

    ipfsclient: Ipfs

    def __init__(self: Self, ipfsclient: Ipfs) -> None:
        """Create a new loan application reader.

        Args:
            ipfsclient (Ipfs): The ipfs client
        """
        self.ipfsclient = ipfsclient

    def query_loan_applications(
            self: Self,
            borrower: str = None,
            open_only: bool = True) -> pd.DataFrame:
        """Query loan applications from IPFS.

        Args:
            borrower (str, optional): The borrower to search for.
                Defaults to None.
            open_only (bool, optional): If false, include applications that
                have been closed. Defaults to True.

        Returns:
            pd.DataFrame: A dataframe of loan applications
        """
        # format query parameters
        index = {
            "borrower": borrower
        } if borrower else {}

        # get all applications from ipfs
        applications = Store.query(
            query_index=Index(
                prefix=PREFIX,
                index=index,
                size=2
            ),
            ipfs=self.ipfsclient,
            reader=LoanApplication()
        )

        # parse applications into a dataframe
        df = Store.to_dataframe(
            applications,
            PARSERS[ParserType.LOAN_APPLICATION]
        )
        if df.empty:
            return df

        # filter for most recent applications per loan_id
        df = Utils.get_most_recent(df, GROUP_BY[ParserType.LOAN_APPLICATION])

        # filter for open applications
        return df[~df['closed']] if open_only else df

    def get_loan_application(
            self: Self,
            application_id: str,
            open_only: bool = False) -> pd.DataFrame:
        """Get a loan application by id.

        Args:
            application_id (str): The application id to search for.
            open_only (bool, optional): If false, include applications that
                have been closed. Defaults to True.

        Returns:
            pd.DataFrame: A dataframe of loan applications
        """        # query ipfs
        applications = Store.query(
            query_index=Index(
                prefix=PREFIX,
                index={
                    "application": application_id
                },
                size=2
            ),
            ipfs=self.ipfsclient,
            reader=LoanApplication()
        )

        # parse results into a dataframe
        df = Store.to_dataframe(
            applications,
            PARSERS[ParserType.LOAN_APPLICATION]
        )
        if df.empty:
            return df

        # filter for most recent applications per loan_id
        df = Utils.get_most_recent(df, GROUP_BY[ParserType.LOAN_APPLICATION])

        # filter for open applications
        return df[~df['closed']] if open_only else df

__init__(ipfsclient)

Create a new loan application reader.

Parameters:
  • ipfsclient (Ipfs) –

    The ipfs client

bizlogic/application.py
119
120
121
122
123
124
125
def __init__(self: Self, ipfsclient: Ipfs) -> None:
    """Create a new loan application reader.

    Args:
        ipfsclient (Ipfs): The ipfs client
    """
    self.ipfsclient = ipfsclient