-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
93 lines (83 loc) · 3.04 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
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
// PRIVATE_KEYを設定しています
require('dotenv').config()
const nem = require('nem-sdk').default
const nameSpace = process.env.NAME_SPACE
const mosaicName = process.env.MOSAIC_NAME
let network, networkId;
if (process.env.TEST_NET) {
network = nem.model.nodes.defaultTestnet
networkId = nem.model.network.data.testnet.id
}else{
network = nem.model.nodes.defaultMainnet
networkId = nem.model.network.data.mainnet.id
}
const endpointSocket = nem.model.objects.create('endpoint')(network, nem.model.nodes.websocketPort)
const connector = nem.com.websockets.connector.create(endpointSocket, '')
const endpoint = nem.model.objects.create('endpoint')(network, nem.model.nodes.defaultPort)
const common = nem.model.objects.create('common')('', process.env.PRIVATE_KEY)
const trackerMosaic = nem.model.objects.create('mosaicAttachment')(nameSpace, mosaicName, 1)
let trackerMosaicDefinition
connector.connect().then(() => {
nem.com.requests.namespace.mosaicDefinitions(endpoint, nameSpace)
.then(res => {
// 作成したモザイクの定義情報を取得します
for (var mosaic in res.data) {
if(mosaic.mosaic.id.name === mosaicName) {
trackerMosaicDefinition = mosaic
break
}
}
})
.catch(err => {
console.log(err)
})
nem.com.websockets.subscribe.chain.blocks(connector, res => {
console.log('new block added! \nsignature: ' + res.signature, '\n\n')
res.transactions.forEach(transaction => {
const sender = nem.model.address.toAddress(transaction.signer, networkId)
let recipient;
if (transaction.type === 257) {
// 通常送金
recipient = transaction.recipient
}else if(transaction.type === 4100){
// マルチシグ
recipient = transaction.otherTrans.recipient
}else {
// 送金以外のときは終了する
return
}
if (transaction.amount === 0) {
// モザイクの送金は無視する
return
}
nem.com.requests.account.mosaics.owned(endpoint, sender)
.then(data => {
const mosaics = data.data
mosaics.forEach(mosaic => {
if(mosaic.mosaicId.namespaceId === nameSpace && mosaic.mosaicId.name === mosaicName) {
console.log('発見!!!!!')
sendTrackerBadge(recipient)
}
})
})
.catch(err => {
console.error(err)
})
})
})
},err => {
console.log(err)
})
const sendTrackerBadge = address => {
const transferTransaction = nem.model.objects.create('transferTransaction')(address, 0, 'You are being chased!!')
transferTransaction.mosaics = [trackerMosaic]
const transactionEntity = nem.model.transactions.prepare('mosaicTransferTransaction')(common, transferTransaction, trackerMosaicDefinition, networkId)
transactionEntity.fee = 100000
nem.model.transactions.send(common, transactionEntity, endpoint)
.then(res => {
console.log('モザイクを送信しました')
})
.catch(err => {
console.error(err)
})
}