►
From YouTube: How To Build a Basic Loan Contract
Description
Kate Sills, Software Engineer at Agoric, will teach you the basics of how to build a loan contract.
Get started with Agoric: https://agoric.com/documentation/getting-started/
Access the Loan Contract example: https://agoric.com/documentation/zoe/guide/contracts/loan.html
ABOUT
Build Fast, Earn Fast - Agoric enables developers to rapidly deploy dapps and DeFi markets on-chain using composable JavaScript smart contracts. Safely get your project from vision to global market.
LINKS
https://agoric.com
https://agoric.com/twitter
https://agoric.com/discord
https://agoric.com/telegram
https://agoric.com/newsletter
Click the 'SUBSCRIBE' button to follow our Youtube channel!
B
Hi
all
right:
let's
go
ahead
and
get
started,
then
so
hi
everyone.
Today
we
are
going
to
be
building
a
basic
loan
contract,
so
you
might
be
familiar
with
maker,
dow
and
their
vaults.
This
is
a
bit
similar
to
that.
This
is
just
gonna,
be
the
very
basics
all
right,
so,
as
always
we'll
be
taking
questions
on
youtube,
live
and
discord.
If
you're
watching
this
afterwards
feel
free
to
still
ask
questions
so
just
to
go
over
a
bit
of
the
loan
process.
B
So
what's
going
to
be
happening,
is
that
the
the
lender
is
going
to
be
creating
a
contract
instance.
The
lender
then
escrows
the
maximum
loan
amount
and
the
borrower
adds
collateral
and
takes
out
a
loan.
So
that's
pretty
simple
and
then
the
loan
yeah
okay.
A
Really
quick
you're
not
sharing
your
screen
if
you're
intending
to
so
let
me
oh.
B
All
right,
sorry
about
that:
let's
go
back,
okay,
so
the
feel
free
to
join
us
on
discord
and
explained
a
little
bit
about
the
loan
process.
B
And
then
this
loan
is
ongoing
until
the
borrower
repays
the
loan
entirely
plus
interest
and
gets
collateral
back
or
the
value
of
the
collateral
falls
triggering
the
liquidation
of
the
collateral.
So
it's
sold
for
money
which
is
given
to
the
lender
so
to
protect
against
liquidation.
The
borrower
can
add
more
collateral,
and
this
raises
the
ratio
of
value
of
collateral
to
the
loan
amount.
B
Okay.
So
this
is
the
basic
loan,
so
there's
some
to
do's
that
we
hope
to
do
in
the
future.
That
would
make
this
a
lot
more
complex
and
usable.
So,
for
instance,
if
the
value
of
the
collateral
goes
up,
then
you
might
have
excess
collateral
that
you
might
want
to
put
to
other
uses.
So
you
might
want
to
withdraw
your
excess
collateral.
B
You
also
might
want
to
repay
partially
most
loans
you're
able
to
repay
partially
right.
So
it's
a
bit
silly
to
only
be
able
to
repay
at
the
end
when
you
have
all
this
compounding
interest
and
then
also
on
the
on
the
lender.
In
the
contract
side,
the
contract
might
want
to
liquidate
partially
so
that
if
the
value
of
the
collateral
goes
down,
it's
not
the
end
of
the
contract.
They
just
the
contract
liquidates
enough
to
continue
all
right.
B
So
there's
just
a
few
things
that
I
want
to
explain
before
we
get
actually
get
into
the
code.
So
first
I
want
to
explain
about
zoe
installations
and
instances.
So
this
is
a
little
tricky,
but
it's
actually
a
very
cool
feature
of
zoe.
B
So
an
installation
is
bundled
code
installed
on
zoe
and
it's
given
a
unique
identifier
so,
instead
of
it
just
being
a
contract
address
that
determines
what
code
is
running
where
in
zoe
each
each
running
instance
actually
has
an
installation
has
an
identifier
for
that
particular
code
and
the
instance
is
the
running
version
of
the
code
that
a
user
interacts
with.
B
So
you
can
create
a
many
instances
from
the
same
installation,
and
the
really
cool
feature
of
this
is
that
you
can
compare
installations
to
see
if
a
contract
is
running
code
that
you've
audited
previously.
B
B
So
so,
if
you're,
interacting
with
a
contract,
the
first
thing
that's
going
to
happen
is
you're
going
to
receive
a
zoe
invitation
and
then
you're
going
to
use
this
invitation
to
make
an
offer
with
zoe
and
once
you
make
an
offer,
zoe
returns
a
seat
which
represents
basically
your
ongoing
role
in
the
contract.
B
Your
ongoing
position
in
the
contract
and
when
you
make
that
offer
the
associated
offer
handler
is
called,
and
this
is
a
code
within
the
contract
that
the
contract
sets
up.
That's
associated
with
that
invitation
associated
with
that
offer.
That
gets
called
and
and
we'll
see
this
when
we
get
into
the
contract,
so
the
the
seat
that
you
get
from
zoe.
It
has
methods
that
return
promises
for
results,
and
there
are
two
types
of
results.
B
One
is
a
result
that
you
get
directly
from
the
contract
directly
from
the
results
of
the
offer
handler
and
that's
the
offer
result.
So,
there's
a
method
get
offer
result
and
then
there's
a
a
there's,
two
methods
actually
to
get
payouts
from
zoe
and
these
payouts
are
the
actual
digital
assets.
So
the
offer
result
is
often
used
to
return
a
string
or
return
an
object
or
or
something
like
that.
The
actual
payments,
because
zoe
is
able
to
enforce,
offer
safety
and
conservation
of
rights
and
payout
liveness.
B
Okay,
I
want
to
explain
a
little
bit
about
his
oe
invitation,
and
so
it
itself
is
a
ertp
payment
for
a
non-fungible
digital
asset
and
that
asset
is
the
right
to
join
a
contract
in
the
particular
role
defined
by
the
invitation.
B
So
these
invitations
are
payments
minted
within
zoe
and
they
can
be
validated
or
claimed
using
the
public,
zoe
invitation
issuer
and
to
use
them
use
them
to
make
an
offer
and
they're
consumed
in
that
call.
So
when
you
call
zoe
dot
offer
with
the
invitation
that
invitation
actually
gets
burned,
but
during
the
burning
process,
zoe
validates
that
it's
a
it's
a
correct
invitation
for
a
particular
contract
and
the
really
really
cool
thing
is
that
this
invitation
can
be
traded
on
in
any
generic
zoe
contract.
B
So
if
you
already
have
an
auction
set
up
or
an
exchange,
setup
or
or
anything
that
can
take
ertp
digital
assets,
they
can
take
zoe
invitations.
So
that's
what
we
call
kind
of
the
higher
order.
Composability
of
the
alert
platform,
so
an
invitation
to
a
contract
can
be
bought
and
sold,
which
is
really
cool
all
right.
So
we
talked
a
little
bit
about
seats,
but
I
want
to
go
into
depth
on
that.
A
seat
represents
an
active
honor,
sorry,
an
active
offer
within
zoe,
and
it
has
two
facets.
B
So
there's
the
the
zcf
facet,
the
zcf
seat,
which
is
the
argument
to
an
offer
handler
in
the
contract
code
and
we'll
see
exactly
how
this
happens
later.
And
then
there
is
the
user
seat,
the
seat
that
we
were
talking
about
earlier,
which
is
what
the
user
gets
as
the
as
the
result
of
calling
a
zoe
offer.
B
B
Okay.
So
this
loan
contract
is
going
to
use
a
price
authority,
which
is
something
that
is,
is
new
with
our
our
work
with
chain
link.
So
a
price
authority
is
a
high
level
api
within
the
award
platform
for
an
oracle,
and
it
has
a
particular
issuer
for
making
credible
quotes.
B
So
we're
going
to
use
two
methods
on
the
price
authority,
we're
going
to
use
a
quote
given
which
you
pass
in
the
amount
that
you
want,
the
quote
for
and
the
brand
in
which
you
want
the
quote
so,
for
instance,
I'll
be
passing
in
an
amount
that
represents
all
of
the
collateral
within
the
loan.
And
then
I
want
the
value
of
the
collateral
in
the
brand
that
the
loan
is
so
in
the
money
brand.
B
Essentially,
we're
also
going
to
be
using
a
quote,
win
lesson
or
quote
when
lt
and
that,
as
we'll
get
into
later
returns
a
promise
for
a
quote
that
will
trigger
when
the
value
of
the
collateral
goes
below
a
certain
trigger
amount.
B
So
both
of
these
methods,
eventually
they
return
promises,
but
they
return
a
price
quote,
which
is
a
record
that
includes
a
quote
amount
and
a
quote
payment.
B
So
in
the
future
we
don't
we
don't
have
this
worked
out
entirely
yet
or
coded
entirely
yet,
but
in
the
future
you'll
be
able
to
use
this
quote
payment
as
proof
to
others
that
a
a
quote
was
given,
which
is
very
cool
okay.
So
if
you're,
following
along
we're,
going
to
be
looking
at
the
loan
contract
within
a
gork
sdk,
it's
under
the
zoe
package,
so
you
go
zoe
source
contracts
and
then
loan
all
right.
So,
let's
start
diving
into
the
contract.
B
B
There
is
the
price
authority
which
we
saw
earlier
and
that
is
used
for
setting
the
liquidation
triggers,
among
other
things,
we're
also
going
to
take
in
an
autoswap
instance.
So
autoswap
is
our
constant
product,
amm
so
similar
to
uniswap
and
we're
gonna
actually
be
using
that
for
selling
or
liquidating
the
collateral.
If
it
comes
to
that
we're
also
going
to
take
in
a
period
async
iterable,
so
an
async
iterable.
B
This
is
actually
a
concept
from
javascript
and
we're
going
to
be
using
this
for
knowing
when
a
period
has
passed
and
when
interest
should
be
added,
so
async
iterable
will
be
pushing
values,
basically
telling
us
when
that
period.
Maybe
it's
a
block
height,
maybe
it's
a
year.
It's
really
up
to
the
whoever
is
is,
is
passing
in
the
terms
to
this
contract.
B
B
Okay,
so
let's
go
ahead
and
dive
into
the
contract,
but
let
let
me
give
a
little
bit
of
an
overview,
so
it's
a
very
lengthy
contract,
so
each
of
the
different
parts
of
the
contract
have
been
divided
up
into
different
files,
so
we'll
start
with
the
index
file
and
then
we'll
go
into
the
the
lens
file,
which
is
kind
of
the
lender,
role
we'll
go
into
the
borrower
role
and
then
we'll
go
into
all
of
these
different
parts,
so
scheduling
the
liquidation
actually
liquidating
adding
collateral.
B
Closing
the
loan
things
like
that,
okay,
so
let's
go
over
to
the
contract,
so
we
are
starting
with
with
index
file.
There
are
a
lot
of
comments
here,
I'll,
let
you
read
those
on
your
own,
but
essentially
what
we
are
doing
in
this
file.
We're
checking
that
issuers
for
the
collateral
and
for
the
loan
brand
have
been
determined.
So
this
is.
This
is
telling
the
contract
what
kind
of
assets
are
going
to
be
accepted
as
collateral
and
what
kind
of
assets
are
going
to
be
accepted
as
a
loan.
B
B
So
so
you
can
see
we
set
the
default
for
the
maintenance
margin
requirement
to
150,
and
we
check
that
these
things
have
been
defined
and
then
we
add
them
to
a
config.
So
it's
pretty
simple
and
then,
as
you've
probably
seen
in
some
of
our
other
contracts.
We
return
a
creator
invitation
here.
So
whoever
creates
an
instance
from
this
contract
will
get
this
invitation
and
it's
an
invitation
to
lend
money.
So
it's
defined
by
this
make
lend
invitation
which
takes
zcf,
which
is
the
zoe
contract
facet.
B
So
this
is,
this:
is
the
side
of
zoe,
that's
exposed
to
contract
code,
and
then
it
takes
this
config
that
we
have
defined.
Let's
actually
go
to
that
so
make
land
invitation
is
defined
in
lend
all
right.
So.
B
Let's
go
down
to
the
bottom,
so
what's
happening
here
is
it
is
making
it's
calling
dcf
make
invitation
which,
with
this
function
and
this
function
operates
as
the
offer
handler
that
we
mentioned
earlier.
So
when
someone
uses
this
lind
invitation,
this
is
the
offer
handler
that
will
be
triggered
when
they
make
their
offer.
B
So
all
offer
handlers
are
passed.
The
the
lender
or
they're
passed
a
seat,
a
zcf
seat.
So
this
is,
we
talked
about
seats
and
how
there
are
two
different
facets.
So,
within
the
contract
code
oops,
my
cat
really
wants
to
get
in
the
video
within
the
contract
code,
their
each
offer
handler
is
passed
a
as
ecfc,
so
in
this
case
it's
going
to
be
the
lender
seat,
because
this
is
the
lend
offer
handler
and
there
are
a
few
restrictions
on
what
the
lender
can
do
here.
B
So
because
this
is
a
loan
and
we
may
not
be
able
to
liquidate
in
time.
Perhaps
the
price
of
the
collateral
goes
down
much
faster
than
we
anticipate
and
the
value
of
the
collateral
goes
to
zero.
We
can't
actually,
let
me
see
if
I
can
get
my
cat
out
of
here.
B
We
can't
actually
have
the
lender
want
anything
here,
because
no
return
can
be
guaranteed
okay,
but
the
lender
is
going
to
be
giving
a
a
certain
amount
as
the
maximum
of
the
loan,
so
they're
going
to
be
giving
money
in
the
loan
brand
that
will
eventually
go
to
the
borrower.
B
They
also,
they
must
have
the
exit
rule
on
demand,
and
this
means
that
at
any
time
they
can
take
their
money
out
and
the
reason
for
that
is
that
if,
if
they
weren't
able
to
do
that,
then
their
money
would
be
locked
up
in
the
contract
until
the
borrower
decides
to
borrow
and
if
you're
holding
the
borrower
invitation,
you
might
be
able
to
lock
up
the
lender's
funds
forever.
B
So
we
don't
want
that
to
happen,
so
we're
going
to
make
it
so
that
the
lender
is
able
to
exit
with
their
loan
at
any
time
before
the
money
is
borrowed.
Okay.
So
what
the
lender
gets
back
as
the
result
of
their
offer
is
returned
at
the
end
of
this
offer
handler
so
they're
actually
going
to
get
a
invitation
for
the
borrower.
B
So
let's
go
into
how
that
is
defined.
So
that's
defined
and
borrow
and
feel
free
to
ask
questions
as
I'm
going
along.
A
lot
of
this
stuff
is
pretty
complicated.
It's
a
pretty
complicated
contract,
but
we've
tried
to
divide
it
up
into
small
pieces
to
actually
make
it
make
it
easier
to
understand
all
right.
So
this
is.
This
is
probably
the
most
complex
part
of
this
contract.
Let's
actually,
let's
see,
let's
start
at
the
top
on
this
one.
B
So
we
know
that
this
is
going
to
return
an
invitation
for
the
borrower,
so
you
can
see
that
we
we
get
our
information
from
the
config
that's
passed
in.
We
can
only
lend
what
the
lender
has
already
escrowed,
so
the
amount
that
the
lender
has
escrowed-
and
this
is
how
you
get
the
amount
allocated
within
zoe,
so
zoe
escrows
all
of
these
goods
and
then
represents
them
as
amounts
associated
with
seats.
B
So
here's
the
offer
handler
for
the
borrow
invitation,
so
this
offer
handler
is
just
called
borrow
and
the
zcfc
is
going
to
be
the
borrower
seat.
Of
course,
so
we
just
want
to
check
first
that
the
borrower
seat
is
when,
when
the
borrower
makes
their
offer
they're
going
to
be
giving
collateral
and
they're
going
to
want
the
loan
amount
or
a
loan
amount.
B
So
this
is
just
kind
of
some
some
bookkeeping,
so
we're
going
to
get
the
amount
of
collateral
that
they
gave.
We're
gonna
see
how
much
of
a
loan
they
wanted.
We're
gonna
get
the
loan
brand.
So
if
you've
gone
through
our
ertp
guide,
you
might
know
this
already,
but
the
the
brand
is
kind
of
the
the
identity
of
digital
assets.
B
So
anything
within
the
same
purse
or
of
the
same
issuer
is
going
to
have
the
same
brand
and
then
we're
also
going
to
get
the
loan
math,
which
is
the
the
amount
math
for
the
loan.
So
this
will
tell
us
how
to
add
and
subtract
the
loan
amount
and
we're
going
to
get
the
collateral
math.
So
one
thing
that's
interesting
here
is
that
the
collateral
could
be
non-fungible,
so
you
could
be
putting
your
cryptokitties
in
here
as
collateral
or
your
property
title
or
whatever
it
is.
B
So,
let's
see
okay,
so
so
here's
here's
one
of
the
uses
of
the
price
authority.
So
we
are
going
to
ask
for
a
quote
in
real
time
of.
If
we
were
to
sell
all
of
the
collateral,
that's
given
how
much
of
the
loan
amount
would
we
get
or
how
much
of
the
loan
brand
would
we
get?
And
so
we
get
the
collateral
price
in
the
loan
brand
here
and
we
want
to
ensure
that
it's
greater
than
the
amount
that
was
required
with
the
mmr
parameter.
B
So
just
for
the
error
message
we're
going
to
do
an
approximate
calculation
here,
but
we're
not
going
to
be
using
that
for
actually
checking
whether
it's
safe.
The
actual
assertion
multiplies
the
collateral
price
in
the
loan
brand
by
a
hundred
because
we
were
doing
percent
and
it
also
multiplies
the
the
loan
wanted
value
by
the
mmr,
and
then
it
checks
that
the
collateral
is
greater
than
the
amount
that
is
required.
B
So
this
this
is
certain.
Details
is
specific
to
a
quark.
You
might
have
seen
some
similar
patterns
in
in
node
and
on
other
platforms,
but
this
this
details
allows
you
to
to
put
in
information.
B
That
is
a
private
for
the
consumer
of
this
information,
and
so,
if
it
gets
logged
or
something
like
that,
it
would
likely
not
show
up,
but
you
would
be
able
to
see
this
in
your
console.
B
Let's
see
so
we
have
a
couple
of
more
a
couple
more
asserts
here,
so
we
want
to
check
that
the
collateral
given
has
not
changed
after
the
await.
So
this
is.
This
is
something
that
people
may
not
realize
so,
every
time
that
we
do
an
await.
We
are
effectively
letting
the
execution
go
to
something
else
and
then
come
back.
So
while
the
execution
has
gone
to
something
else,
things
might
have
changed,
so
we
need
to
make
sure
that
things
are
the
same
so
that
we
can
safely
continue.
B
So
we
need
to.
We
need
to
assert
that
the
collateral
given
has
not
changed.
If
it
has
changed,
something
very
strange
has
happened
and
we
should
probably
throw
an
error.
B
We
also
need
to
check
that
the
the
loan
wanted
is
less
than
the
maximum
loan
that
we
can
give
just.
So
that's
just
a
basic
check
all
right.
So,
given
all
of
those
checks,
we
can
actually
start
doing
the
transfers.
B
So,
as
I
said
before,
you
escrow
your
digital
assets
with
zoe,
and
then
the
contract
is
able
to
reallocate
those
assets
in
terms
of
amounts.
So,
within
the
internal
record,
keeping
of
zoe
the
contracts
are
able
to
say.
B
Actually,
this
person
should
be
getting
this
amount,
and
this
person
should
be
getting
this
amount
and
so
forth,
but
it's
all
conditional
on
those
reallocations
passing
off
for
safety,
so
it
can't
violate
what
the
that
each
individual
offer
still
has
to
get
either
what
they
said
they
wanted
or
a
full
refund
of
what
they
put
in,
and
it
can't
violate
rights
conservation.
B
So
here
we're
going
to
be
using
a
helper
called
trade
and
what
trade
allows
you
to
do
is
to
use
kind
of
a
more
declarative
language
saying
I
want
to
describe
what
the
gains
and
losses
are
and
then
under
the
hood.
This
helper
is
going
to
do
all
of
the
math
for
us
to
make
that
happen.
So,
first,
we
are
going
to
transfer
the
loan
amount
from
the
lender
to
this
collateral
seat
that
we
make
so
up
here.
B
We
made
an
empty
seat
it
within
the
contract
without
having
to
make
an
offer
and
we're
going
to
use
this
for
kind
of
a
a
place
to
allocate
things
while
the
contract
is
going
on.
B
So
this
still
fulfills
offer
safety
all
right
and
then
we're
going
to
transfer
all
of
the
collateral
to
the
collateral
seat
and
we
are
going
to
transfer
the
wanted
loan
amount
to
the
borrower.
So
you
can
see
the
collateral
seat
gains
the
collateral
and
it
loses,
and
the
borrower's
seat
gains
the
amount
of
that
was
wanted
of
the
loan.
So
then
we
exit
the
borrower
seat
so
that
the
borrower
gets
their
loan.
B
But
this
isn't
the
end
of
everything
for
the
borrower.
They
will
actually,
let's
scroll,
all
the
way
down
here.
They
will
be
given
as
their
offer
results
of
this
particular
offer
handler
they'll
be
given
this
facet
that
allows
them
to
do
a
few
things.
They
can
close
alone,
they
can
add
collateral
and
they
can
get
a
promise
that
resolves
when
liquidation
occurs
and
they
can
also
get
up-to-date
information
about
what
their
current
debt
is.
So
if
they
choose
to
repay
they'll
know
how
much
needs
to
be
repaid
all
right.
B
So,
let's
scroll
up
a
little
bit
and
find
out
how
we
can
do
this
okay.
So
there
is
another
file
that
allows
us
to
make
a
debt
calculator
and
it
gives
us
back
two
things.
It
gives
us
back
a
function
to
get
the
current
debt
and
it
gives
us
a
the
notifier
that
I
mentioned
that
allows
the
the
borrower
to
know
up-to-date
information
about
what
they
have
to
repay.
How
much
interest
has
been
accrued
that
sort
of
thing?
B
Okay,
so
let's
hop
into
that
notice
that
we
pass
in
the
special
config
for
the
the
debt
calculator,
so
that
is
within
update
debt
all
right.
So
this
is
a
bit
complex
here.
We
just
have
a
basis
point
denominator.
So,
if
you're
trying
to
translate
a
basis
point
out
of
out
of
the
basis
point
notation,
this
is
what
you
would
use
as
the
denominator.
B
B
So
we
also
take
in
this
period
async
iterable,
and
what
we're
going
to
do
is
we're
going
to
have
this
period
async
iterable
every
time
that
it
it
updates.
It's
going
to
call
this
update
state
function
on
our
observer.
So
basically,
what
we're
doing
is
with
this
observed,
iteration,
sorry
observe
iteration.
B
We
are
kind
of
registering
this
observer
to
be
called
every
time
that
this
async
iterable
pushes
a
new
value
through
so
update
state
calls
this
update
debt
function
finish.
This
probably
won't
happen,
but
in
the
case
that
the
async
iterable
says
that
it's
done,
we'll
also
update
the
debt
and
then,
if
something
fails,
well
we're
going
to
throw
an
error
and
we
are
going
to
tell
the
the
user
through
this
notifier
that
hey
it
failed.
B
So
let's
go
up
to
the
update
debt
function.
So
this
each
observer
takes
an
estate.
In
this
case
we
don't
we
don't
care
what
the
state
is,
because
the
period
async
iterable
is
just
every
time
that
it
pushes
any
value
at
all.
That's
when
we're
going
to
be
updating
things,
so
we
don't
care
what
the
value
is,
and
what
we
do
is
that
we
calculate
the
interest
using
a
function
that
is
part
of
this
config.
B
We
add
the
interest
to
the
debt.
We
update
the
notifier
for
the
user
so
that
they
know
that
their
debt
has
been
updated
and
also
we
we
schedule
liquidation
as
appropriate,
so
we'll
go
into
this
part
a
little
bit
later,
but
it's
important
to
know
that
every
time
that
the
debt
is
updated,
this
changes
the
ratio
of
collateral
to
the
debt.
So
we
need
to
reschedule
the
liquidation
to
occur
at
the
right
time,
all
right,
so
we
saw
that
there
was
this
calc
interest
function
here
that
was
defined
here.
B
So
this
is
kind
of
we
have
this
plumbed
out,
most
of
the
way
to
be
able
to
use
your
own
interest
calculator.
B
So
what
this
is
doing
is
just
multiplying
the
interest
rate
by
the
debt
and
dividing
by
the
basis
point
denominator,
but
you
might
have
a
more
complex
interest
function
so
from
there.
Let's
go
back
to
borrow
okay,
so
we
saw
how
gitzet
is
produced
and
how
get
debt
notifier
is
produced.
B
So
let's
go
into
that,
oh
and
throughout
this
contract
we
passed
this
config
around,
and
in
this
case
we
add
the
collateral
seat
and
the
get
debt
function
and
a
promise
kit
that
the
user
here
they're
able
to
get
this
promise
which
will
be
resolved
if
liquidation
does
actually
happen.
So
they're
informed
if
liquidation
happens
and
we
will,
we
will
pass
that
promise
kit,
the
resolver
and
the
promise
through
to
the
liquidation
function
to
resolve
that
promise
when
appropriate,
all
right.
So
so,
let's
see
so
schedule
liquidation.
B
This
is
the
part
where
we're
actually
using
the
price
authority
in
a
very
interesting
way.
B
So
all
of
this
we're
just
getting
information
from
the
config
we're
getting
the
loan
math
again
so
that
we
can
add
and
subtract
loan
values.
We
want
to
get
the
current
debt
and
what
we're
going
to
do
here
is
we're
going
to
calculate
the
liquidation
trigger
value.
So
this
is.
This
is
basically
when
the
value
of
the
collateral
equals
the
maintenance
margin
requirement
percent
of
the
current
debt.
B
So
we
take
the
current
debt
value
we
multiply
by
mmr,
we
divide
by
a
hundred
because
it's
a
percent
and
we
get
this
value,
so
we
use
the
price
authority
and
we
call
quote
when
less
than
or
quote
when
lt
with
all
of
the
collateral
that
is
there.
So
all
the
collateral
is
now
on
the
collateral
seat,
so
we
just
get
the
amount
allocated
and
then
we,
what
we're
saying
is
please
resolve
this
promise.
We're
asking
the
price
authority.
B
Please
resolve
this
promise
when
the
value
of
the
collateral
goes
below
this
particular
value,
and
so
we
get
this.
We
get
this
promise
that
we're
going
to
call
internal
liquidation
promise.
So
this
is
not
what
it's
what
is
exposed
to
the
user,
but
when
this
resolves
we
get
a
price
quote
and
as
as
we
saw
earlier,
a
price
quote,
it
has
a
quote
amount
and
a
quote
payment.
We're
just
going
to
use
the
quote
amount
here,
because
we
don't
need
to
prove
to
anyone
else.
B
That's
that
the
quote
was
made
by
a
credible
source.
We,
we
trust
this
price
authority
and
we're
gonna
get
the
each
each
quote.
Amount
has
an
amount
in
and
amount
out,
so
what
we
want
to
check
is
that
this,
this
quote
is
still
pertinent,
so
the
amount
in
would
have
been
all
of
the
collateral
that
was
sold,
so
it
may
have
been
possible
that
we
were
scheduling
several
liquidation
events,
so
maybe
interest
was
added
to
the
debt.
B
Maybe
collateral
was
added,
so
we
need
to
make
adjustments
the
way
that
we
just
kind
of
throw
away.
The
votes
that
we
don't
need
is
to
check
whether
the
amount
in
of
this
quote
is
equal
to
the
current
collateral.
If
it's
not,
we
do
nothing.
If
it
is,
then
we
actually
go
ahead
and
liquidate.
B
So
this
is
where
that
promise
that
the
user
gets
is
resolved
with
the
price
quote,
so
the
user
is
informed
of
the
liquidation
at
this
point,
and
then
we
actually
liquidate
all
right.
So
if
one
thing
that's
very
important,
if
you're
doing
asynchronous
programming
is
to
handle
your
errors
appropriately.
B
So
here,
if,
for
whatever
reason,
there's
something
wrong
with
this
call,
if
the
if
the
price
authority
isn't
able
to
do
this,
then
we
we
log
in
error
and
we
reallocate
so
at
this
point.
The
borrower
has
exited
with
their
loan.
So
we
can't
really
do
anything
about
the
borrower.
We
can't
take
the
loan
back,
but
all
the
collateral
is
still
there,
so
we
will
give
the
the
lender
blender
their
collateral
and
we
will
shut
down
with
failure
all
right.
So
let's
actually
go
to
liquidate.
B
Let's
go
down
here,
okay,
so
for
testing
purposes.
This
is
divided
into
two
functions,
but
the
liquidate
call
takes
ccf
and
the
config
as
the
pattern
that
we've
kind
of
seen
before
we
use
the
the
auto
swap
instance
object
that
we
got
in
the
terms
to
get
the
public
facet.
B
So
a
facet
as
we
talked
about
with
the
two
facets
for
seats
is
just
a
particular
view
of
some
larger
entity,
so
contracts
have
a
public
facet
and
then
the
creator
invitation
and
the
creator
facets
are
other
facets
of
that
contract
that
are
more
private,
so
the
public
facet
of
autoswap.
Again,
this
is
our
constant
product,
amm
similar
to
uniswap.
This
will
actually
allow
us
to
perform
the
swap
to
perform
the
sale
of
the
liquidation
or
of
the
collateral,
the
liquidation.
B
Okay.
So
let's
go
back
up
here.
We're
gonna
actually
perform
the
liquidation,
so
we
get
the
zoe
service.
B
We
get
the
loan
math,
we
get
all
of
the
collateral
or
the
amount
of
collateral
that
has
been
allocated
to
the
collateral
seat
and
then
what
we
do
is
we
call
this
helper
withdraw
from
seat
to
get
the
collateral
in
the
form
of
a
payment,
and
the
reason
why
we
want
to
do
that
is
because
we're
going
to
act
as
a
user,
we're
going
to
get
an
invitation
from
the
auto
swap
public
facet
here
to
swap
and
then
we're
actually
going
to
make
an
offer
with
the
proposal
and
the
payments
here.
B
So
we're
going
to
say
that
our
proposal
is
that
we're
going
to
give
all
of
this
collateral
the
same
amount
as
in
the
collateral
payment
here
and
because
our
price
authority
is
actually
it
might
be
a
different
source
than
the
autoswap
instance.
We're
going
to
say
that
we
don't
want
anything
because
the
price
may
have
moved
or
the
price
may
have
been
different,
and
if
we
said
that
we
wanted
a
certain
amount
offer,
safety
would
prevent
the
transaction
from
going
through.
B
So
at
this
point,
we're
saying
we're
going
to
sell
it
all.
We
don't
care
what
the
price
is.
We
have
a
best
guess
of
what
that
what
it
is,
but
we're
gonna
sell
it
all
regardless.
B
So
so
we
actually
make
that
offer
with
the
invitation
that
we
make
for
ourselves
from
the
public
facet,
the
proposal
that
we
make
the
payments
that
we
have
withdrawn
from
the
collateral
seat
and
we
get
a
user
seat
in
response.
B
B
So
that
function
is
up
here,
so
the
payouts
will
be
in
the
form
of
in
and
out
in
will
be
the
collateral
payout
which,
hopefully
we
will
have
none
of
and
out
will
be
the
loan
payout.
We
will
also
get
the
the
current
amounts
allocated.
B
That'll
just
save
us
some
time
and
then,
as
you
can
see
any
time
that
we
do
an
await
it's
a
little
bit
dangerous
because
we
are
actually
allowing
other
execution
to
occur
while
we're
waiting.
So
we
like
to
put
this
comment
away,
just
to
make
it
very
clear
because
sometimes
because
everything's
kind
of
in
line
you
can
miss
that
something
like
that
is
happening
all
right.
So,
let's
just
create
two
new
objects
here
that
use
the
keywords
associated
with
with
this
contract,
rather
than
the
auto
swap
contract.
B
So
auto
swap
puts
things
in
terms
of
in
and
out
this
contract.
As
we've
seen,
it
has
only
two
keywords:
collateral
and
loans,
so
we're
going
to
rename
things
so
the
collateral
amount
gets
the
keyword
collateral
loan
amount
gets
the
keyword
loan,
that's
pretty
self-explanatory
and
then
we're
going
to
use
this
helper
called
deposit
to
c.
So
we're
going
to
take
these
payments
and
deposit
them
to
the
lender
seat.
B
Okay,
so
let's
see
so
let's
go
all
the
way
down
to
here.
So
if
this
auto
swap
offer
is
not
successful,
then
get
offer
result
is
going
to
throw
so
we
can.
We
can
handle
this
with
two
functions.
One
will
be
called
if
it
resolves
and
if
this
promise
results
and
the
other
will
be
called
if
this
promise
rejects-
or
if
this
wasn't
asynchronous.
B
This
would
be
throwing
so
close
successfully
we're
going
to
exit
the
lender,
seat,
exit
the
collateral
seat
and
then
shut
down
the
contract.
So
this
call
zcf
shutdown.
It
actually
prevents
any
more
offers
from
being
made
and
it
actually
shuts
down
the
whole
execution
environment
that
this
contract
is
in
so
that
the
objects
themselves
no
longer
exist
and
then,
if
it,
if
this
promise
rejects
if
internally
this
throws,
then
we're
gonna
close
with
failure.
B
So
we're
going
to
kick
out
the
seats,
so
they
will
they'll
get
an
error
message
and
we're
going
to
shut
down
with
failure.
B
Okay,
so
let's
see
so
we've
gone
over
most
of
this
we've
gone
over
the
lender's
position.
The
borrower's
position
we've
gone
over
the
the
more
complex
parts
of
the
borrower,
so
how
the
debt
is
updated,
how
the
liquidation
is
scheduled
with
the
price
authority,
how
the
liquidation
actually
occurs.
So,
let's
go
into
the
two
things
that
the
borrower
can
do,
the
borrower
can
add
collateral
and
the
borrower
can
repay
and
close
all
right.
So
first,
let's
go
into
ad
collateral.
B
This
is
pretty
simple.
So
if
the
borrower
believes
that
a
liquidation
is
imminent,
they
can
add
more
collateral
to
prevent
it,
so
essentially
their
the
borrower
is
able
to
use
the
facet
that
they
get
to
make
an
invitation
to
add
collateral.
That
invitation
has
this
particular
offer
handler
called
add
collateral,
which
has
gets
the
seat
passed
to
it.
So
we're
going
to
check
that
this
offer
is
giving
collateral
that
it
doesn't
want
anything
because,
remember
it's
topping
up
a
loan
but
already
exists,
so
they
can't
want
anything.
B
It
already
has
the
borrower
already
has
loan
and
we're
going
to
perform
a
trade.
So
we
are
going
to
say
that
the
collateral
seat
will
get
all
of
the
collateral
from
this
offer
and
the
the
borrower
who's
adding
the
collateral
gets
nothing.
Then
we
exit
that
particular
seat
for
the
borrower
and,
of
course,
now
that
the
ratio
of
collateral
to
debt
has
changed.
B
B
Okay,
so
the
other
thing
that
the
borrower
can
do-
and
let
me
let
me
actually
go
back
to
the
borrower
to
show
you
where
these
things
happen.
So
the
borrower
gets
this
result,
as
as
the
as
the
offer
result
of
them,
borrowing
the
money
and
so
they're
able
to
to
call
make
add
collateral
invitation
and
make
closed
loan
invitation.
So
let's
go
into
the
code
for
the
closing
of
the
loan.
B
So
in
the
simple
version
all
of
the
debt
mess
must
be
repaid
at
once,
so
we
don't
allow
for
any
kind
of
partial
repayment.
B
Okay,
so
let's
go
all
the
way
down
to
the
bottom
here,
so
we're
going
to
be
making
an
invitation
with
zoe
it's
going
to
have
this
offer
handler
repaint
and
close,
oh
and
we
we
don't
want
to
put
the
amount
of
the
debt
to
be
repaid
in
the
custom
properties
of
this
invitation.
B
Oftentimes
oftentimes,
you
can
put
custom
information
in
a
zoe
invitation
to
kind
of
say
more
about
what
the
invitation
will
allow
you
to
do,
but
we
definitely
don't
want
to
put
the
current
debt
in
that
invitation,
because
that
will
change
over
time,
and
that
is
why
this
is
a
function
here
rather
than
a
particular
value.
We
want.
B
To
get
the
most
up-to-date
debt
so
in
order
to
repay,
let's
check,
let's
check
that
the
offer
is
being
made
correctly
with
this
assert
proposal,
shape,
let's
check
that
they're
actually
giving
the
loan
brands
and
that
they
want
the
collateral
brand.
B
This
is
just
getting
the
loan
math
and
loan
brand
and
the
collateral
brand
again
we're
gonna
get
the
amount
that
was
repaid,
so
this
repay
seat
that
represents
the
borrower's
position
in
this
offer,
will
have
the
the
current
amount
that
they
tried
to
repay
we're
going
to
check
that
what
has
been
repaid
is
greater
than
the
debt,
because
we
want
all
debt
to
be
repaid
and
then
we're
going
we're
going
to
use
the
trade
helper
again
to
actually
do
the
transfers
after
we've
done
these
checks,
so
the
repay
seat,
which
the
represents,
what
the
borrower
is
doing
right
now,
it
will
gain
all
the
collateral
and
the
collateral
seat
will
temporarily
get
all
of
the
loan.
B
So
the
collateral
seat
inside
the
contract
is
kind
of
acting
as
the
place
that
we
we
place
assets
when
we
want
to
transfer
them
later,
then
we
will
take
that
the
loan
amount
that
we
put
on
the
collateral
seat
and
actually
give
it
to
the
lender.
So
now
the
lender
has
been
allocated
their
loan,
and
so
after
these
two
calls
are
done,
we're
in
the
position
where
no
one
has
gotten
their
payouts
yet,
but
within
zoe.
B
The
allocations
that
zoe
is
allocating
to
these
different
seats
are
the
right
ones
for
us
to
exit.
So
at
this
point
the
allocations
have
occurred,
but
no
payouts,
and
then,
when
we
call
exit
that's
when
those
payouts
occur,
so
the
borrower
gets
their
collateral
back
all
of
their
collateral.
The
lender
gets
all
of
the
amount
that
was
loaned
back
and
the
collateral
seat
gets
nothing,
but
it's
just
an
internal
seat
for
the
contract.
So
that's
fine
and
we
have
a
particular
message
that
we're
going
to
return
and
we're
going
to
shut
down
the
contract.
B
So
that
means
again
that
no
further
offers
can
be
made
and
the
actual
execution
environment
the
vat
is
shut
down
such
that
there
are
no,
no
objects
remain
all
right,
so
that
is
effectively
all
of
the
contracts,
so
we've
gone
through
all
of
it.
Let's
see
so.
B
So
there
are
some
resources:
there's
a
hackathon
documentation.
Oh,
come
on,
there's
a
hackathon
documentation.
There's
all
of
this
code
is
within
the
agoric
sdk
repository,
and
then
we
have
a
number
of
contract
examples
on
our
documentation
site.
A
Hey
kate,
so
no
questions
came
in
during
during
the
presentation
but,
as
we've
been
saying,
please
do
make
sure
to
connect
with
us
on
discord.
We
know
a
lot
of
you
are
watching
this
asynchronously
and
we're
we're
here
to
answer
any
questions
about
any
of
the
parts
of
the
contract
that
kay
went
through.