This repository was archived by the owner on Jun 25, 2024. It is now read-only.
forked from msfeldstein/create-stellar-token
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathindex.js
More file actions
137 lines (124 loc) · 3.92 KB
/
Copy pathindex.js
File metadata and controls
137 lines (124 loc) · 3.92 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
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
#!/usr/bin/env node
const StellarSdk = require("stellar-sdk");
const fetch = require("node-fetch");
const chalk = require("chalk");
const cmd = require("command-line-args");
const readlineSync = require("readline-sync");
const opts = cmd([
{ name: "client-seed", type: String },
{ name: "issuer-seed", type: String },
{ name: "distribution-seed", type: String },
{ name: "asset", type: String, defaultValue: "LMC" },
{ name: "issue-amount", type: Number, defaultValue: 10000 },
{ name: "client-amount", type: Number, defaultValue: 0 }
]);
const server = new StellarSdk.Server("https://horizon-testnet.stellar.org");
const ASSET = opts["asset"] || "LMC";
const ISSUE_AMOUNT = opts["issue-amount"];
const AMOUNT_TO_CLIENT = opts["client-amount"];
function getPubKey(seed) {
if (!seed) return null;
return StellarSdk.Keypair.fromSecret(seed).publicKey();
}
console.log(
chalk.green(
`Creating ${ISSUE_AMOUNT} ${ASSET} from
${getPubKey(opts["issuer-seed"]) || "a random issuer"},
to be distributed by
${getPubKey(opts["distribution-seed"]) || "a random distributer"}
with ${AMOUNT_TO_CLIENT} given to
${getPubKey(opts["client-seed"]) || "a new client account"}
on Testnet
`
)
);
readlineSync.question("Continue? ");
const accounts = {};
async function createAccount(name) {
const pair = opts[`${name}-seed`]
? StellarSdk.Keypair.fromSecret(opts[`${name}-seed`])
: StellarSdk.Keypair.random();
console.log(
chalk.green(`========== Creating/funding ${name} account ==========`)
);
console.log("> Seed: " + pair.secret());
console.log("> Pub : " + pair.publicKey());
await fetch(`https://friendbot.stellar.org?addr=${pair.publicKey()}`);
accounts[name] = pair;
return pair;
}
async function generate() {
const issuerKey = await createAccount("issuer");
const distributionKey = await createAccount("distribution");
const asset = new StellarSdk.Asset(ASSET, issuerKey.publicKey());
const [fee, distributionAccount] = await Promise.all([
server.fetchBaseFee(),
server.loadAccount(distributionKey.publicKey())
]);
console.log(
chalk.green("Creating trust-line and issuing to distribution account")
);
const trustAndIssueTx = new StellarSdk.TransactionBuilder(
distributionAccount,
{
fee,
networkPassphrase: StellarSdk.Networks.TESTNET
}
)
.addOperation(
StellarSdk.Operation.changeTrust({
asset,
limit: String(ISSUE_AMOUNT)
})
)
.addOperation(
StellarSdk.Operation.payment({
destination: distributionKey.publicKey(),
asset,
amount: String(ISSUE_AMOUNT),
source: issuerKey.publicKey()
})
)
.setTimeout(100)
.build();
trustAndIssueTx.sign(issuerKey);
trustAndIssueTx.sign(distributionKey);
const issueResult = await server.submitTransaction(trustAndIssueTx);
if (AMOUNT_TO_CLIENT > 0) {
const clientKey = await createAccount("client");
console.log(chalk.green(`Sending ${AMOUNT_TO_CLIENT} ${ASSET} to client`));
const clientAccount = await server.loadAccount(clientKey.publicKey());
const sendToClientTx = new StellarSdk.TransactionBuilder(clientAccount, {
fee,
networkPassphrase: StellarSdk.Networks.TESTNET
})
.addOperation(
StellarSdk.Operation.changeTrust({
asset
})
)
.addOperation(
StellarSdk.Operation.payment({
destination: clientKey.publicKey(),
asset,
amount: String(AMOUNT_TO_CLIENT),
source: distributionKey.publicKey()
})
)
.setTimeout(100)
.build();
sendToClientTx.sign(clientKey);
sendToClientTx.sign(distributionKey);
await server.submitTransaction(sendToClientTx);
}
Object.keys(accounts).forEach(name => {
console.log(
chalk.green(
`> ${name}: https://stellar.expert/explorer/testnet/account/${accounts[
name
].publicKey()}`
)
);
});
}
generate();