-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconditionalPayments.html
273 lines (246 loc) · 11.4 KB
/
conditionalPayments.html
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="tutorial.css">
<title>Stellar Smart Contracts (Conditional Payments)</title>
</head>
<body>
<header id="main-header">
<nav id="navbar">
<h1><a href="index.html"> ← Back to Home</a></h1>
<ul>
<li class="nav-link"><a href="#introduction">Introduction</a></li>
<li class="nav-link"><a href="#prerequisites">Prerequisites</a></li>
<li class="nav-link"><a href="#step1">Step 1: Set Up the Project</a></li>
<li class="nav-link"><a href="#step2">Step 2: Load the Stellar SDK and Configure Accounts</a></li>
<li class="nav-link"><a href="#step3">Step 3: Create the Conditional Account</a></li>
<li class="nav-link"><a href="#step4">Step 4: Build and Submit the Conditional Payment</a></li>
<li class="nav-link"><a href="#step5">Step 5: Run the Script</a></li>
<li class="nav-link"><a href="#Conclusion">Conclusion</a></li>
</ul>
</nav>
</header>
<main id="main-doc">
<section class="logo">
<h1>Stellar Dev Guide</h1>
</section>
<section class="tutname">
<h1> Tutorial:- Stellar Smart Contracts (Conditional Payments)</h1>
</section>
<section class="main-section" id="introduction">
<header>Introduction</header>
<article class="main-article">
<p>Stellar supports simple smart contracts through multi-signature and conditional payments. This tutorial will guide you through creating a conditional payment using the Stellar SDK for JavaScript.</p>
</article>
</section>
<section class="main-section" id="prerequisites">
<header>Prerequisites</header>
<article class="main-article">
<p>Ensure you have:</p>
<ul>
<li>Node.js installed.</li>
<li>A text editor or IDE (like VSCode).</li>
<li>Basic knowledge of JavaScript.</li>
<li>A funded Stellar account (refer to the first tutorial)</li>
</ul>
</article>
</section>
<section class="main-section" id="step1">
<header>Step 1: Set Up the Project</header>
<article class="main-article">
<p>First, create a new project directory and install the Stellar SDK:</p>
<code>
mkdir stellar-custom-asset <br>
cd stellar-custom-asset<br>
npm init -y<br>
npm install stellar-sdk<br>
</code>
</article>
</section>
<section class="main-section" id="step2">
<header>Step 2: Load the Stellar SDK and Configure Accounts</header>
<article class="main-article">
<p>Create a file named conditionalPayment.js and add the following code:</p>
<code>
const StellarSdk = require('stellar-sdk'); <br>
const server = new StellarSdk.Server('https://horizon-testnet.stellar.org');<br>
<br>
// Source account keys<br>
const sourceKeys = StellarSdk.Keypair.fromSecret('SOURCE_SECRET_KEY');<br>
<br>
// Conditional account keys<br>
const conditionalKeys = StellarSdk.Keypair.random();<br>
<br>
// Destination account public key<br>
const destinationPublicKey = 'DESTINATION_PUBLIC_KEY';<br>
<br>
// Transaction details<br>
const amount = '10'; // Amount of XLM to send<br>
const preimage = 'your_preimage';<br>
const hash = StellarSdk.StrKey.encodePreAuthTx(StellarSdk.hash(Buffer.from(preimage)));<br>
</code>
<p>Replace SOURCE_SECRET_KEY with the secret key of your source account and DESTINATION_PUBLIC_KEY with the public key of your destination account. Replace your_preimage with a string that will be used to create a hash for the pre-auth transaction.</p>
</article>
</section>
<section class="main-section" id="step3">
<header>Step 3: Create the Conditional Account</header>
<article class="main-article">
<p>Add the following code to create the conditional account with the pre-auth transaction signer:</p>
<code>
async function createConditionalAccount() { <br>
try { <br>
const account = await server.loadAccount(sourceKeys.publicKey()); <br>
<br>
const transaction = new StellarSdk.TransactionBuilder(account, { <br>
fee: StellarSdk.BASE_FEE, <br>
networkPassphrase: StellarSdk.Networks.TESTNET, <br>
}) <br>
.addOperation(StellarSdk.Operation.createAccount({ <br>
destination: conditionalKeys.publicKey(), <br>
startingBalance: '2', // Minimum balance for a new account <br>
})) <br>
.addOperation(StellarSdk.Operation.setOptions({ <br>
signer: { <br>
ed25519PublicKey: hash, <br>
weight: 1, <br>
}, <br>
})) <br>
.setTimeout(30) <br>
.build(); <br>
<br>
transaction.sign(sourceKeys); <br>
<br>
const transactionResult = await server.submitTransaction(transaction); <br>
console.log('Conditional account created successfully!', transactionResult); <br>
} <br>
catch (error) { <br>
console.error('Error!', error); <br>
} <br>
} <br>
<br>
createConditionalAccount();
</code>
<p>This script creates a new account with a pre-auth transaction signer, requiring the preimage to authorize transactions.</p>
</article>
</section>
<section class="main-section" id="step4">
<header>Step 4: Build and Submit the Conditional Payment</header>
<article class="main-article">
<p>Add the following code to create and submit the conditional payment:</p>
<code>
async function conditionalPayment() { <br>
try {<br>
const account = await server.loadAccount(conditionalKeys.publicKey());<br>
<br>
const transaction = new StellarSdk.TransactionBuilder(account, {<br>
fee: StellarSdk.BASE_FEE,<br>
networkPassphrase: StellarSdk.Networks.TESTNET,<br>
})<br>
.addOperation(StellarSdk.Operation.payment({<br>
destination: destinationPublicKey,<br>
asset: StellarSdk.Asset.native(),<br>
amount: amount,<br>
}))<br>
.setTimeout(30)<br>
.build();<br>
<br>
transaction.sign(conditionalKeys);<br>
<br>
const transactionResult = await server.submitTransaction(transaction);<br>
console.log('Conditional payment successful!', transactionResult);<br>
} <br>
catch (error) {<br>
console.error('Error!', error);<br>
}<br>
}<br>
<br>
conditionalPayment();
</code>
<p>This script builds and submits a payment that will be conditionally approved using the preimage.</p>
</article>
</section>
<section class="main-section" id="step5">
<header>Step 5: Run the Script</header>
<article class="main-article">
<p>Run the script using Node.js:</p>
<code>
node createConditionalAccount.js
</code>
<p>Run the script to perform the conditional payment:</p>
<code>
node conditionalPayment.js
</code>
<p>If successful, you'll see messages indicating the creation of the conditional account and the conditional payment, along with the transaction details.</p>
</article>
</section>
<section class="main-section" id="Conclusion">
<header>Conclusion</header>
<article class="main-article">
<p>You've successfully created a conditional payment on the Stellar network using the Stellar SDK for JavaScript. You can use this technique to create more complex smart contracts and multi-signature transactions. For more details on conditional payments, check out the Stellar Pre-authorized Transactions documentation.</p>
</article>
</section>
<footer class="footer">
<div class="footer__addr">
<h1 class="footer__logo">Steller Dev Guide</h1>
<h2>Contact</h2>
<address>
</address>
<a class="footer__btn" href="https://github.com/ravixalgorithm/Stellar-Dev-Guide" target=”_blank” >Source Code</a>
</div>
<ul class="footer__nav">
<li class="nav__item">
<h2 class="nav__title">Challenge</h2>
<ul class="nav__ul">
<li>
<a href="https://dev.to/devteam/join-us-for-the-first-community-smart-contract-challenge-with-50000-in-prizes-41gl" target=”_blank”>Dev Challenge</a>
</li>
<li>
<a href="https://github.com/ravixalgorithm" target=”_blank”>GitHub</a>
</li>
<li>
<a href="https://dev.to/ravixalgorithm" target=”_blank”>DEV.to</a>
</li>
</ul>
</li>
<li class="nav__item nav__item--extra">
<h2 class="nav__title">Tech Socials</h2>
<ul class="nav__ul nav__ul--extra">
<li>
<a href="https://github.com/ravixalgorithm" target=”_blank”>GitHub</a>
</li>
<li>
<a href="https://www.linkedin.com/in/ravixalgorithm/" target=”_blank”>LinkedIn</a>
</li>
<li>
<a href="https://linktr.ee/ravixalgorithm" target=”_blank”>linkTree</a>
</li>
<li>
<a href="https://app.daily.dev/ravixalgorithm" target=”_blank”>Daily.Dev</a>
</li>
</ul>
</li>
<li class="nav__item">
<h2 class="nav__title">Social Media</h2>
<ul class="nav__ul">
<li>
<a href="https://www.instagram.com/ravixalgorithm" target=”_blank”>Instagram</a>
</li>
<li>
<a href="https://x.com/ravixalgorithm" target=”_blank”>X</a>
</li>
<li>
<a href="https://www.youtube.com/@ravixalgorithm" target=”_blank”>YouTube</a>
</li>
</ul>
</li>
</ul>
<div class="legal">
<p>© 2024 created for Dev Challenge</p>
</div>
</footer>
</main>
</body>
</html>