-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtemp_stellar.mjs
More file actions
79 lines (67 loc) · 2.41 KB
/
Copy pathtemp_stellar.mjs
File metadata and controls
79 lines (67 loc) · 2.41 KB
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
import { Server, Keypair, Asset, Operation, TransactionBuilder, Networks } from '@stellar/stellar-sdk';
import fetch from 'node-fetch';
const secret = process.env.STELLAR_SECRET_TESTNET;
const horizonUrl = process.env.HORIZON_URL_TESTNET || 'https://horizon-testnet.stellar.org';
if (!secret) {
console.error('Missing STELLAR_SECRET_TESTNET');
process.exit(1);
}
const sourceKeypair = Keypair.fromSecret(secret.trim());
const server = new Server(horizonUrl);
async function ensureFunded(publicKey) {
try {
await server.loadAccount(publicKey);
console.log('Account ' + publicKey + ' is already funded.');
} catch (e) {
if (e.response && e.response.status === 404) {
console.log('Funding ' + publicKey + ' via friendbot...');
await fetch('https://friendbot.stellar.org?addr=' + publicKey);
await server.loadAccount(publicKey);
} else {
throw e;
}
}
}
async function run() {
try {
await ensureFunded(sourceKeypair.publicKey());
const destKeypair = Keypair.random();
console.log('Destination Public Key: ' + destKeypair.publicKey());
console.log('Funding destination via friendbot...');
const resultFb = await fetch('https://friendbot.stellar.org?addr=' + destKeypair.publicKey());
if (!resultFb.ok) {
console.error('Friendbot failed: ' + (await resultFb.text()));
}
const sourceAccount = await server.loadAccount(sourceKeypair.publicKey());
const transaction = new TransactionBuilder(sourceAccount, {
fee: '1000',
networkPassphrase: Networks.TESTNET,
})
.addOperation(
Operation.payment({
destination: destKeypair.publicKey(),
asset: Asset.native(),
amount: '1',
})
)
.setTimeout(30)
.build();
transaction.sign(sourceKeypair);
const result = await server.submitTransaction(transaction);
console.log('PASS');
console.log('Tx Hash: ' + result.hash);
console.log('Source: ' + sourceKeypair.publicKey());
console.log('Destination: ' + destKeypair.publicKey());
console.log('Amount: 1 XLM');
console.log('Explorer: https://stellar.expert/explorer/testnet/tx/' + result.hash);
} catch (error) {
console.error('FAIL');
if (error.response && error.response.data) {
console.error(JSON.stringify(error.response.data, null, 2));
} else {
console.error(error);
}
process.exit(1);
}
}
run();