/
couponBond.ts
179 lines (151 loc) · 4.77 KB
/
couponBond.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
107
108
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
import {
assert,
hash256,
method,
prop,
PubKey,
pubKey2Addr,
Sig,
SmartContract,
toByteString,
Utils,
} from 'scrypt-ts'
export class CouponBond extends SmartContract {
@prop()
issuer: PubKey
@prop(true)
investor: PubKey
@prop(true)
forSale: boolean
// Price of the bond in satoshis.
@prop(true)
price: bigint
@prop()
faceValue: bigint
@prop()
interestRate: bigint // 1n == 1%
@prop()
matureTime: bigint
constructor(
issuer: PubKey,
faceValue: bigint,
price: bigint,
interestRate: bigint,
matureTime: bigint
) {
super(...arguments)
this.issuer = issuer
this.investor = PubKey(
toByteString(
'0000000000000000000000000000000000000000000000000000000000000000'
)
)
this.faceValue = faceValue
this.interestRate = interestRate
this.matureTime = matureTime
this.forSale = true
this.price = price
}
@method()
public buy(newInvestor: PubKey) {
const prevInvestor = this.investor
// Set new investor.
this.investor = newInvestor
// Toggle for sale flag.
this.forSale = false
let outputs = toByteString('')
const alreadyOwned =
this.investor ==
PubKey(
toByteString(
'0000000000000000000000000000000000000000000000000000000000000000'
)
)
if (alreadyOwned) {
// Pay previous investor.
outputs += this.buildStateOutput(this.ctx.utxo.value)
outputs += Utils.buildAddressOutput(
pubKey2Addr(prevInvestor),
this.price
)
} else {
// Deposit to contract.
outputs += this.buildStateOutput(this.ctx.utxo.value + this.price)
}
// Enforce outputs.
outputs += this.buildChangeOutput()
assert(hash256(outputs) == this.ctx.hashOutputs, 'hashOutputs mismatch')
}
@method()
public makePayment(issuerSig: Sig) {
// Check issuer signature.
assert(this.checkSig(issuerSig, this.issuer), 'invalid sig issuer')
let outputs = this.buildStateOutput(this.ctx.utxo.value)
// Ensure investor gets payed interest.
const interest = (this.faceValue * this.interestRate) / 100n
outputs += Utils.buildAddressOutput(
pubKey2Addr(this.investor),
this.faceValue
)
outputs += this.buildChangeOutput()
assert(hash256(outputs) == this.ctx.hashOutputs, 'hashOutputs mismatch')
}
@method()
public mature(issuerSig: Sig) {
// Check issuer signature.
assert(this.checkSig(issuerSig, this.issuer), 'invalid sig issuer')
// Check mature time passed.
assert(this.timeLock(this.matureTime), 'bond not matured')
// Ensure investor gets payed face value of the bond.
let outputs = Utils.buildAddressOutput(
pubKey2Addr(this.investor),
this.faceValue
)
outputs += this.buildChangeOutput()
assert(hash256(outputs) == this.ctx.hashOutputs, 'hashOutputs mismatch')
}
@method()
public listForSale(price: bigint, investorSig: Sig) {
// Check investor signature.
assert(
this.checkSig(investorSig, this.investor),
'invalid sig investor'
)
// Set price and toggle for sale flag.
this.price = price
this.forSale = true
// Propagate contract.
let outputs = this.buildStateOutput(this.ctx.utxo.value)
outputs += this.buildChangeOutput()
assert(hash256(outputs) == this.ctx.hashOutputs, 'hashOutputs mismatch')
}
@method()
public cancelSale(investorSig: Sig) {
// Check investor signature.
assert(
this.checkSig(investorSig, this.investor),
'invalid sig investor'
)
// Toggle for sale flag.
this.forSale = false
// Propagate contract.
let outputs = this.buildStateOutput(this.ctx.utxo.value)
outputs += this.buildChangeOutput()
assert(hash256(outputs) == this.ctx.hashOutputs, 'hashOutputs mismatch')
}
@method()
public default(investorSig: Sig) {
// After default deadline is reached the investor can
// take everything locked within the smart contract...
// Check investor signature.
assert(
this.checkSig(investorSig, this.investor),
'invalid sig investor'
)
// Check mature time + ~14 days.
assert(
this.timeLock(this.matureTime + 20160n),
'deadline for default not reached'
)
}
}