-
Notifications
You must be signed in to change notification settings - Fork 84
/
crowdfundReplay.ts
199 lines (169 loc) · 5.89 KB
/
crowdfundReplay.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
import {
assert,
hash160,
hash256,
HashedMap,
method,
prop,
PubKey,
Sig,
SmartContract,
Utils,
MethodCallOptions,
ContractTransaction,
bsv,
} from 'scrypt-ts'
/*
* Read Medium article about this contract
* https://xiaohuiliu.medium.com/crowdfunding-on-bitcoin-169c1f8b6b63
*/
export type Donators = HashedMap<PubKey, bigint>
export class CrowdfundReplay extends SmartContract {
@prop()
readonly beneficiary: PubKey
@prop(true)
donators: Donators
@prop()
readonly deadline: bigint
@prop()
readonly target: bigint
constructor(
beneficiary: PubKey,
donators: Donators,
deadline: bigint,
target: bigint
) {
super(...arguments)
this.beneficiary = beneficiary
this.donators = donators
this.deadline = deadline
this.target = target
}
@method()
public donate(donator: PubKey, amount: bigint) {
// donation amount is greater than zero
assert(amount > 0n, 'donation amount should be greater than 0')
// can only donate once
assert(!this.donators.has(donator), 'donator already presents')
this.donators.set(donator, amount)
// confine outputs
const outputs =
this.buildStateOutput(this.ctx.utxo.value + amount) +
this.buildChangeOutput()
assert(hash256(outputs) == this.ctx.hashOutputs, 'hashOutputs mismatch')
}
// donators can be refunded at any time
@method()
public refund(donator: PubKey, amount: bigint, sig: Sig) {
// checking the validity of the signature
assert(this.checkSig(sig, donator), 'donator signature check failed')
// whether donated before
assert(this.donators.canGet(donator, amount), 'not donated before')
// remove the donator
assert(this.donators.delete(donator), 'remove donator failed')
// confine outputs
const outputs =
this.buildStateOutput(this.ctx.utxo.value - amount) +
Utils.buildPublicKeyHashOutput(hash160(donator), amount) +
this.buildChangeOutput()
assert(hash256(outputs) == this.ctx.hashOutputs, 'hashOutput mismatch')
}
// collect pledged fund
@method()
public collect(sig: Sig) {
const amount = this.ctx.utxo.value
// ensure the collected amount actually reaches the target.
assert(amount >= this.target, 'cannot collect without target reached')
// Check deadline.
assert(
this.timeLock(this.deadline),
'cannot collet before fundraising expired'
)
// validate signature of beneficiary
assert(
this.checkSig(sig, this.beneficiary),
'beneficiary signature check failed'
)
// confine outputs
const outputs =
Utils.buildPublicKeyHashOutput(hash160(this.beneficiary), amount) +
this.buildChangeOutput()
assert(hash256(outputs) == this.ctx.hashOutputs, 'hashOutputs mismatch')
}
static async buildTxForRefund(
current: CrowdfundReplay,
options: MethodCallOptions<CrowdfundReplay>,
donator: PubKey,
amount: bigint
): Promise<ContractTransaction> {
const defaultChangeAddress = await current.signer.getDefaultAddress()
const balance = current.balance - Number(amount)
const nextInstance = current.next()
nextInstance.applyOffchainUpdatesForRefund(donator)
const unsignedTx = new bsv.Transaction()
.addInput(current.buildContractInput(options.fromUTXO))
.addOutput(
new bsv.Transaction.Output({
script: nextInstance.lockingScript,
satoshis: balance,
})
)
.addOutput(
new bsv.Transaction.Output({
script: bsv.Script.fromHex(
Utils.buildPublicKeyHashScript(hash160(donator))
),
satoshis: Number(amount),
})
)
.change(options.changeAddress || defaultChangeAddress)
return {
tx: unsignedTx,
atInputIndex: 0,
nexts: [
{
instance: nextInstance,
atOutputIndex: 0,
balance: balance,
},
],
}
}
static async buildTxForCollect(
current: CrowdfundReplay,
options: MethodCallOptions<CrowdfundReplay>
): Promise<ContractTransaction> {
const defaultChangeAddress = await current.signer.getDefaultAddress()
const unsignedTx = new bsv.Transaction()
.addInput(current.buildContractInput(options.fromUTXO))
.addOutput(
new bsv.Transaction.Output({
script: bsv.Script.fromHex(
Utils.buildPublicKeyHashScript(
hash160(current.beneficiary)
)
),
satoshis: current.balance,
})
)
.change(options.changeAddress || defaultChangeAddress)
.setInputSequence(0, options.sequence || 0xffffffff - 1)
.setLockTime(options.lockTime || 0)
return {
tx: unsignedTx,
atInputIndex: 0,
nexts: [],
}
}
// methods for updating @prop(true) offchain
//
// when replay contract instance to the latest states
// those method names MUST follow the pattern `applyOffchainUpdatesForXxx`
// where `Xxx` is the name of the corresponding public method with first letter capitalized
applyOffchainUpdatesForDonate(donator: PubKey, amount: bigint) {
this.donators.set(donator, amount)
}
applyOffchainUpdatesForRefund(donator: PubKey) {
this.donators.delete(donator)
}
}