This repository has been archived by the owner on Oct 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path4_ico_contract.js
161 lines (135 loc) · 5.25 KB
/
4_ico_contract.js
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
const StellarSdk = require('stellar-sdk')
const YAML = require('node-yaml')
const configFile = 'config.yml'
const config = YAML.readSync(configFile)
const server = new StellarSdk.Server('https://horizon-testnet.stellar.org')
StellarSdk.Network.useTestNetwork()
const contractorKey = StellarSdk.Keypair.fromSecret(config.accounts.contractor.secret)
const escrowKey = StellarSdk.Keypair.fromSecret(config.accounts.joint_account.secret)
const govKey = StellarSdk.Keypair.fromSecret(config.accounts.gov.secret)
const issuerKey = StellarSdk.Keypair.fromSecret(config.accounts.issuer.secret)
const ngoKey = StellarSdk.Keypair.fromSecret(config.accounts.ngo.secret)
const newAsset = new StellarSdk.Asset(config.token.name, issuerKey.publicKey())
createICOContract()
async function createICOContract () {
try {
const escrowAccount = await getEscrowAccount()
// Create ICO Offer
await createOffer(escrowAccount)
const offers = await getOfferId(escrowAccount)
const offerId = offers.records[0].id
// Create success and fail transaction on N+2 sequence but not submit it
console.log('Creating claim tx')
const successTx = await getSuccessTransaction()
const failTx = await getFailTransaction(offerId)
/// / Pre-autorization success and fail tx on N+1 sequence
await preAuthTransactions(escrowKey, [successTx.hash(), failTx.hash()])
console.log('Everything is Success!! Congratulations!!!')
} catch (error) {
console.log('error = ', error.message, JSON.stringify(error.stack, null, 4))
}
}
async function getEscrowAccount () {
return server.loadAccount(escrowKey.publicKey())
}
async function createOffer (escrowAccount) {
const transaction = new StellarSdk.TransactionBuilder(escrowAccount)
.addOperation(StellarSdk.Operation.manageOffer({
selling: newAsset,
buying: StellarSdk.Asset.native(),
amount: '100',
price: 1
}))
.build()
transaction.sign(govKey)
transaction.sign(ngoKey)
return server.submitTransaction(transaction)
}
async function getOfferId (escrowAccount) {
return server.offers('accounts', escrowAccount.accountId()).call()
}
async function getSuccessTransaction () {
const escrowAccount = await getEscrowAccount()
// Set sequence number to N+1 for pre-auth transaction
escrowAccount.incrementSequenceNumber()
const transaction = new StellarSdk.TransactionBuilder(escrowAccount, {
timebounds: {
minTime: parseInt(new Date(config.token.end).getTime() / 1000),
maxTime: parseInt(new Date(config.token.recovery).getTime() / 1000)
}
}).addOperation(StellarSdk.Operation.payment({
destination: contractorKey.publicKey(),
asset: StellarSdk.Asset.native(),
amount: '100'
})).build()
console.log('success transaction hash = ', transaction.hash().toString('base64'))
console.log('XDR = ', decodeURIComponent(transaction.toEnvelope().toXDR().toString('base64')))
return transaction
}
async function getFailTransaction (offerId) {
const escrowAccount = await getEscrowAccount()
// Set sequence number to N+1 for pre-auth transaction
escrowAccount.incrementSequenceNumber()
const transaction = new StellarSdk.TransactionBuilder(escrowAccount, {
timebounds: {
minTime: parseInt(new Date(config.token.recovery).getTime() / 1000),
maxTime: 0
}
}).addOperation(StellarSdk.Operation.manageOffer({
// Cancle ICO Offer
selling: newAsset,
buying: StellarSdk.Asset.native(),
amount: '0',
offerId,
price: 1
}))
// Create offer to claim back donor's fund
.addOperation(StellarSdk.Operation.manageOffer({
selling: StellarSdk.Asset.native(),
buying: newAsset,
amount: '100',
price: 1
}))
// Send money back to Gov
.addOperation(StellarSdk.Operation.payment({
destination: govKey.publicKey(),
asset: StellarSdk.Asset.native(),
amount: '100'
}))
// Send money back to NGO
.addOperation(StellarSdk.Operation.payment({
destination: ngoKey.publicKey(),
asset: StellarSdk.Asset.native(),
amount: '100'
})).build()
console.log('fail transaction hash = ', transaction.hash().toString('base64'))
console.log('XDR = ', decodeURIComponent(transaction.toEnvelope().toXDR().toString('base64')))
return transaction
}
async function preAuthTransactions (escrowKey, preAuthTxHashes) {
console.log('Pre-authorization transactions')
// Reload escrow account to get clean sequence number
const escrowAccount = await server.loadAccount(escrowKey.publicKey())
const transactionBuilder = new StellarSdk.TransactionBuilder(escrowAccount)
// Add success and fail transaction to pre-authorization transaction
for (let i = 0; i < preAuthTxHashes.length; i++) {
console.log('Adding : ', preAuthTxHashes[i])
transactionBuilder.addOperation(StellarSdk.Operation.setOptions({
signer: {
preAuthTx: preAuthTxHashes[i],
// Weight should hit the threshould
weight: '255'
}
}))
}
// Lock account so no further transaction can be submit
transactionBuilder.addOperation(StellarSdk.Operation.setOptions({
lowThreshold: 255,
medThreshold: 255,
highThreshold: 255
}))
const transaction = transactionBuilder.build()
transaction.sign(govKey)
transaction.sign(ngoKey)
return server.submitTransaction(transaction)
}