-
Notifications
You must be signed in to change notification settings - Fork 1
/
nearConnector.js
210 lines (193 loc) · 6.48 KB
/
nearConnector.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
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
const { ethers } = require('ethers');
const nearAPI = require('near-api-js');
const log = require('./log');
const signerAccountId = process.env.CONTRACT_NAME;
const contractName = process.env.SIGNEAR_ACCOUNT;
const keyStore = new nearAPI.keyStores.UnencryptedFileSystemKeyStore(
process.env.CREDENTIALS_PATH
);
let _contract;
let connected = false;
const init = async () => {
log.info(`[Ⓝ] Near Connector - initialization`);
try {
await connect();
connected = true;
log.info(`[Ⓝ] Near connection successful`);
} catch(e) {
log.error('[Ⓝ] error on connection');
log.error(e);
}
};
const networkConfig = {
mainnet: {
networkId: 'mainnet',
nodeUrl: 'https://rpc.mainnet.near.org',
contractName,
walletUrl: 'https://wallet.near.org',
helperUrl: 'https://helper.mainnet.near.org',
explorerUrl: 'https://explorer.mainnet.near.org',
},
testnet: {
networkId: 'testnet',
nodeUrl: 'https://rpc.testnet.near.org',
contractName,
walletUrl: 'https://wallet.testnet.near.org',
helperUrl: 'https://helper.testnet.near.org',
explorerUrl: 'https://explorer.testnet.near.org',
}
}
const connect = async () => {
const network = networkConfig[process.env.NETWORK || 'testnet'];
const near = await nearAPI.connect({
deps: {
keyStore,
},
...network,
});
const account = await near.account(signerAccountId);
_contract = new nearAPI.Contract(
account,
contractName,
{
// View methods are read only.
viewMethods: [
'getAccessoriesForUser',
'getGameTokens',
'getGlobalAccessories',
'getUserObject',
'hasAccessory',
'turnsToPlay',
'getLine',
'getUserId',
'getGameConfig',
'getAccessory',
],
// Change methods can modify the state.
changeMethods: [
'init',
'buyAccessory', // user - payable
'takeUserOwnership',
'addToLine', // admin
'allocateUser', // admin
'buyAccessoryWithPoints', // user
'peek', // admin
'rewardGameToken', // admin
'rewardPoints', // admin
'setBaseURI', // admin
'setMaxLineCapacity', // admin
'setMaxPointsReward', // admin
'setPriceToUnlockUser', // admin
'setPriceForAccessory', // admin
'setUserOwnership', // admin
'unlockAccessoryForPublic', // admin
'removeAccessoryForPublic', // admin
'unlockAccessoryForUser', // admin
'nft_mint',
],
}
);
};
const getContract = () => {
return _contract;
}
const initContract = async () => {
if (!_contract) throw Error('not connected to near platform');
log.info(`[Ⓝ] >> Initializating metadata for NFTs - (wait)`);
await _contract.init({
args: {
metadata: {
spec: 'toum-0.1.0',
name: 'Unsolved Mysteries - testnet',
symbol: 'ToUM',
icon: 'https://js13kgames.com/games/spaceship-wars-13k/__small.jpg',
base_uri: '',
reference: 'https://bafybeiejdg3z267rzcpnniirmmi5h3n3ku2fs4f5g6rmjeh2wpdvfcotxq.ipfs.dweb.link/',
reference_hash: '',
},
}
});
log.info(`[Ⓝ] >> contract initializated`);
};
const allocateUser = async (sessionID, secretWord) => {
if (!_contract) throw Error('not connected to near platform');
log.info(`[Ⓝ] >> Blockchain allocating user for session: ${ sessionID } - (wait)`);
const encodedKey = ethers.utils.solidityKeccak256(['string'],[secretWord]);
const userID = await _contract.allocateUser({
args: {
uuid: sessionID,
unlockKey: encodedKey
}
});
log.info(`[Ⓝ] user allocated: ${ userID } <<`);
return { userID, encodedKey };
};
const syncUser = async (user) => {
if (!_contract) throw Error('not connected to Near network');
const userId = user.getUserID();
log.info(`[Ⓝ] >> Blockchain syncing user: ${ userId } - (wait)`);
const { turn } = await _contract.getUserObject({ userId });
if (turn > 0) {
log.info(`[Ⓝ] Blockchain user ${ userId } has turn: ${ turn } <<`);
user.assignTurn(turn);
} else {
log.info(`[Ⓝ] Blockchain user ${ userId } doesn't have turn assigned <<`);
}
};
const addToLine = async (userId) => {
if (!_contract) throw Error('not connected to Near network');
log.info(`[Ⓝ] >> Blockchain requesting turn for user: ${ userId } - (wait)`);
try {
const turn = await _contract.addToLine({ args: { userId } });
log.info(`[Ⓝ] Blockchain user ${ userId } got turn: ${ turn } <<`);
return turn;
} catch (error) {
console.error(error);
return -1;
}
};
const peek = async () => {
if (!_contract) throw Error('not connected to near platform');
log.info(`[Ⓝ] >> Blockchain line peek requested - (wait)`);
const removedUserId = await _contract.peek({ args: {} });
log.info(`[Ⓝ] Blockchain first user in line removed <<`);
return removedUserId;
};
const rewardGameToken = async (userId, metadata) => {
if (!_contract) throw Error('not connected to Near network');
log.info(`[Ⓝ] >> Blockchain tokenRewarded event: ${ userId } ${ metadata.reference } - (wait)`);
const tokenRewardId = await _contract.rewardGameToken({ args: { userId, metadata }});
const user = await _contract.getUserObject({ userId });
if (user.nearAccount) {
await _contract.nft_mint({ args: { receiver_id: user.nearAccount, token_id: tokenRewardId.toString(), metadata }});
}
log.info(`[Ⓝ] Blockchain tokenRewarded <<`);
return tokenRewardId;
};
const rewardPoints = async (userId, points) => {
if (!_contract) throw Error('not connected to Near network');
log.info(`[Ⓝ] >> Blockchain reward points event: ${ userId } ${ points } - (wait)`);
const totalPoints = await _contract.rewardPoints({args: { userId, points }});
log.info(`[Ⓝ] Blockchain points rewarded <<`);
return totalPoints;
};
const setUserOwnership = async (userId, account, secret) => {
if (!_contract) throw Error('not connected to Near network');
log.info(`[Ⓝ] >> Blockchain set user ${ userId } ownership to address: ${ account } -(wait)`);
await _contract.setUserOwnership({args: { userId, account, secret }});
log.info(`[Ⓝ] Blockchain user set! <<`);
};
module.exports = {
init,
initContract,
isConnected: () => connected,
getContract,
allocateUser,
syncUser,
addToLine,
peek,
rewardGameToken,
rewardPoints,
setUserOwnership,
encodeKey: (key) => ethers.utils.solidityKeccak256(['string'],[key])
};