-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.ts
158 lines (146 loc) · 4.64 KB
/
index.ts
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
import {
AccountHttp,
Address,
MosaicSupplyChangeTransaction,
TimeWindow,
MosaicId,
Account,
NetworkTypes,
MosaicSupplyType,
SimpleWallet,
Password,
TransactionHttp,
NEMLibrary,
TransferTransaction,
MosaicHttp,
EmptyMessage,
NemAnnounceResult
} from 'nem-library';
const fs = require('fs');
import { Observable } from 'rxjs/Observable';
const nemConfig = require('./nem-config.json');
NEMLibrary.bootstrap(NetworkTypes.TEST_NET);
const transactionHttp = new TransactionHttp();
const mosaicHttp = new MosaicHttp();
export interface CertificationGroup {
namespace: string;
mosaicName: string;
reward: number;
}
export interface CertificationTransfer {
toAddress: Address;
signer: Account;
rewardAmount: number;
certMosaicId: MosaicId;
rewardMosaicId: MosaicId;
}
/**
* 1. Check if the address already owns this certification
* 2. If not, generate 1 new certification mosaic
* 3. Then transfer certification mosaic and reward to address
* @param {string} address Address of the incoming request
* @param {string} certName Name of the certification earned
*/
export const processCertification = async (address: string, certName: string) => {
try {
// Grab data from config json file
const cert: CertificationGroup = nemConfig.certifications[certName];
if (!cert) throw Error('Invalid certification name');
const mosaicId = new MosaicId(cert.namespace, cert.mosaicName);
// Only proceed if cert not already owned
const isOwned = await certificationOwned(address, cert.mosaicName);
if (isOwned) throw Error('This certification is already owned by address');
// Create 1 new certification asset
const certCreated = await createCertifcationMosaic(mosaicId);
if (!certCreated) return;
// Transfer certification and reward to address
const transfer: CertificationTransfer = {
toAddress: new Address(address),
signer: loadAccount(),
rewardAmount: cert.reward,
certMosaicId: mosaicId,
rewardMosaicId: new MosaicId(nemConfig.mosaicNamespace, nemConfig.mosaicForReward)
};
await transferMosaics(transfer);
} catch (err) {
console.log(err);
}
};
/**
* Fetch mosaic balances for given address
* Iterate through mosaics to see if certification is owned
* @param {string} address Address to search balances on
* @param {string} certName Certification to check existence of
* @returns {Promise<boolean>}
*/
export const certificationOwned = (address: string, certName: string): Promise<boolean> => {
return new Promise<boolean>((resolve, reject) => {
const accountHttp = new AccountHttp();
try {
const add = new Address(address);
accountHttp.getMosaicOwnedByAddress(add)
.subscribe(mosaics => {
const cert = mosaics.find((mosaic) => {
return mosaic.mosaicId.name === certName
});
if (!cert) resolve(false);
else { resolve (true); }
}, err => {
reject(err);
});
} catch (err) {
reject(err);
}
});
};
/**
* Load the Account through the wallet file and password
* @returns {Account}
*/
const loadAccount = (): Account => {
const contents = fs.readFileSync(nemConfig.walletPath);
const wallet = SimpleWallet.readFromNanoWalletWLF(contents);
const pass = new Password(nemConfig.walletPassword);
return wallet.open(pass);
};
/**
* Create 1 new certification to be immediately transferred
* @param {MosaicId} mosaic
* @returns {Promise<boolean>}
*/
export const createCertifcationMosaic = (mosaic: MosaicId) => {
return new Promise<boolean>((resolve, reject) => {
const supplyChangeTransaction = MosaicSupplyChangeTransaction
.create(TimeWindow.createWithDeadline(), mosaic, MosaicSupplyType.Increase, 1);
const account = loadAccount();
const signed = account.signTransaction(supplyChangeTransaction);
transactionHttp.announceTransaction(signed)
.subscribe(_ => {
resolve(true);
}, err => {
reject(err);
});
});
};
export const transferMosaics = (certTransfer: CertificationTransfer): Promise<NemAnnounceResult> => {
return new Promise<NemAnnounceResult>((resolve, reject) => {
const transactionHttp = new TransactionHttp();
Observable.from([
mosaicHttp.getMosaicTransferableWithAmount(certTransfer.certMosaicId, 1),
mosaicHttp.getMosaicTransferableWithAmount(certTransfer.rewardMosaicId, certTransfer.rewardAmount / 1e6)])
.flatMap(transfer => transfer)
.toArray()
.map(mosaics => TransferTransaction.createWithMosaics(
TimeWindow.createWithDeadline(),
certTransfer.toAddress,
mosaics,
EmptyMessage))
.map(transaction => certTransfer.signer.signTransaction(transaction))
.flatMap(signed => transactionHttp.announceTransaction(signed))
.subscribe(result => {
resolve(result);
}, error => {
reject(error);
});
});
};