-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
51 lines (43 loc) · 1.75 KB
/
index.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
const { Server, Keypair, TransactionBuilder, Networks, Operation, Asset } = require('stellar-sdk');
class CharityDonation {
constructor(serverUrl, contractId, networkPassphrase) {
this.server = new Server(serverUrl);
this.contractId = contractId;
this.networkPassphrase = networkPassphrase;
}
async initializeCharity(charityAddress, signerKeypair) {
const account = await this.server.loadAccount(signerKeypair.publicKey());
const transaction = new TransactionBuilder(account, {
fee: await this.server.fetchBaseFee(),
networkPassphrase: this.networkPassphrase,
})
.addOperation(Operation.manageData({
name: `initialize_charity_${charityAddress}`,
value: charityAddress,
}))
.setTimeout(30)
.build();
transaction.sign(signerKeypair);
return this.server.submitTransaction(transaction);
}
async removeCharity(charityAddress, signerKeypair) {
const account = await this.server.loadAccount(signerKeypair.publicKey());
const transaction = new TransactionBuilder(account, {
fee: await this.server.fetchBaseFee(),
networkPassphrase: this.networkPassphrase,
})
.addOperation(Operation.manageData({
name: `remove_charity_${charityAddress}`,
value: null,
}))
.setTimeout(30)
.build();
transaction.sign(signerKeypair);
return this.server.submitTransaction(transaction);
}
async donate(userKeypair, charityAddress, amount, roundUp) {
const userAccount = await this.server.loadAccount(userKeypair.publicKey());
const donationAmount = roundUp ? Math.ceil(amount) : amount;
const transaction = new TransactionBuilder(userAccount, {
fee: await this.server.fetchBaseFee(),
networkPassphrase: this.network