forked from webaverse/preview-backend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.js
103 lines (96 loc) · 3.99 KB
/
api.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
const Web3 = require('web3');
const bip32 = require('./bip32.js');
const bip39 = require('./bip39.js');
const {infuraApiKey, network, mnemonic} = require('./config.json');
const webaverseAbi = require('./webaverse-abi.json');
const webaverseAddress = require('./webaverse-address.json');
const _makeContracts = web3 => {
return {
webaverse: new web3.eth.Contract(webaverseAbi, webaverseAddress),
};
};
async function _execute(spec) {
const _waitTransaction = txId => new Promise((accept, reject) => {
const _recurse = () => {
this.eth.getTransactionReceipt(txId, (err, result) => {
if (!err) {
if (result !== null) {
accept(result);
} else {
_recurse();
}
} else {
reject(err);
}
});
};
_recurse();
});
const {method, data, wait} = spec;
switch (method) {
case 'mintToken': {
const {tokenId, addr, name} = data;
const nonce = (await this.eth.getTransactionCount(this.eth.defaultAccount)) + 1;
const gas = await this.contracts.webaverse.methods.mintToken(tokenId, addr, name).estimateGas({from: this.eth.defaultAccount, nonce});
console.log('estimate gas', gas);
// const {transactionHash} = await this.contracts.webaverse.methods.mintToken(tokenId, addr, name).send({from: this.eth.defaultAccount, gas, nonce});
const transactionHash = await new Promise((accept, reject) => {
const p = this.contracts.webaverse.methods.mintToken(tokenId, addr, name).send({from: this.eth.defaultAccount, gas, nonce});
p.once('transactionHash', accept);
p.once('error', reject);
});
console.log('got txid', transactionHash);
if (wait) {
const rx = await _waitTransaction(transactionHash);
console.log('got rx', rx);
return rx;
/* const result = await this.contracts.webaverse.methods.getTokenByName(name).call();
console.log('got result 1', result);
const tokenId = parseInt(result[1], 10);
console.log('got result 2', tokenId);
return tokenId; */
} else {
return null;
}
}
case 'transferTo': {
const {addr, tokenId} = data;
const nonce = (await this.eth.getTransactionCount(this.eth.defaultAccount)) + 1;
const gas = await this.contracts.webaverse.methods.transferTo(addr, tokenId).estimateGas({from: this.eth.defaultAccount, nonce});
console.log('estimate gas', gas);
// const {transactionHash} = await this.contracts.webaverse.methods.mintToken(tokenId, addr, name).send({from: this.eth.defaultAccount, gas, nonce});
const transactionHash = await new Promise((accept, reject) => {
const p = this.contracts.webaverse.methods.transferTo(addr, tokenId).send({from: this.eth.defaultAccount, gas, nonce});
p.once('transactionHash', accept);
p.once('error', reject);
});
console.log('got txid', transactionHash);
if (wait) {
const rx = await _waitTransaction(transactionHash);
console.log('got rx', rx);
return rx;
/* const result = await this.contracts.webaverse.methods.getTokenByName(name).call();
console.log('got result 1', result);
const tokenId = parseInt(result[1], 10);
console.log('got result 2', tokenId);
return tokenId; */
} else {
return null;
}
}
default: throw new Error(`unknown execute method ${method}`);
}
}
const makeWeb3 = () => {
const rpcUrl = `https://${network}.infura.io/v3/${infuraApiKey}`;
const web3 = new Web3(new Web3.providers.HttpProvider(rpcUrl));
const seed = bip39.mnemonicToSeedSync(mnemonic, '');
const privateKey = '0x' + bip32.fromSeed(seed).derivePath("m/44'/60'/0'/0").derive(0).privateKey.toString('hex');
const account = web3.eth.accounts.privateKeyToAccount(privateKey);
web3.eth.accounts.wallet.add(account);
web3.eth.defaultAccount = account.address;
web3.contracts = _makeContracts(web3);
web3.execute = _execute;
return web3;
};
module.exports = makeWeb3();