forked from webaverse/app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
blockchain.js
219 lines (206 loc) · 6.8 KB
/
blockchain.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
211
212
213
214
215
216
217
218
219
import Web3 from './web3.min.js';
import bip39 from './bip39.js';
import hdkeySpec from './hdkey.js';
const hdkey = hdkeySpec.default;
import ethereumJsTx from './ethereumjs-tx.js';
import { makePromise } from './util.js';
import { chainName, web3MainnetSidechainEndpoint, web3TestnetSidechainEndpoint, polygonVigilKey } from './constants.js';
const { Transaction, Common } = ethereumJsTx;
import addresses from 'https://contracts.webaverse.com/config/addresses.js';
import abis from 'https://contracts.webaverse.com/config/abi.js';
export const Networks = {
mainnet: {
displayName: "Mainnet",
transferOptions: ["mainnetsidechain"],
},
mainnetsidechain: {
displayName: "Webaverse",
transferOptions: ["mainnet", "polygon"],
},
polygon: {
displayName: "Polygon",
transferOptions: ["mainnetsidechain"],
},
testnet: {
displayName: "Rinkeby Testnet",
transferOptions: ["testnetsidechain", "testnetpolygon"],
},
testnetsidechain: {
displayName: "Webaverse Testnet",
transferOptions: ["testnet"],
},
testnetpolygon: {
displayName: "Polygon Testnet",
transferOptions: ["testnetsidechain"],
},
};
const injectedWeb3 = new Web3(window.ethereum);
const web3 = {
mainnet: injectedWeb3,
mainnetsidechain: new Web3(new Web3.providers.HttpProvider(web3MainnetSidechainEndpoint)),
testnet: injectedWeb3,
testnetsidechain: new Web3(new Web3.providers.HttpProvider(web3TestnetSidechainEndpoint)),
polygon: new Web3(new Web3.providers.HttpProvider(`https://rpc-mainnet.maticvigil.com/v1/${polygonVigilKey}`)),
testnetpolygon: new Web3(new Web3.providers.HttpProvider(`https://rpc-mumbai.maticvigil.com/v1/${polygonVigilKey}`)),
};
let common = null;
let addressFront;
let addressBack;
let networkName = '';
function _setChain(nn) {
addressFront = addresses[nn];
addressBack = addresses[nn + 'sidechain'];
networkName = nn;
if (nn === 'mainnet') {
common = Common.forCustomChain(
'mainnet',
{
name: 'geth',
networkId: 1,
chainId: 1338,
},
'petersburg',
);
} else if (nn === 'testnet') {
common = Common.forCustomChain(
'mainnet',
{
name: 'geth',
networkId: 1,
chainId: 1337,
},
'petersburg',
);
} else if (nn === 'polygon') {
// throw new Error('cannot set common properties for polygon yet');
} else {
throw new Error('unknown network name', nn);
}
}
const _initSetChain = () => {
_setChain(chainName);
};
_initSetChain();
const contracts = {};
Object.keys(Networks).forEach(network => {
// console.log("*** Network is", network);
contracts[network] = {
Account: new web3[network].eth.Contract(abis.Account, addresses[network].Account),
FT: new web3[network].eth.Contract(abis.FT, addresses[network].FT),
FTProxy: new web3[network].eth.Contract(abis.FTProxy, addresses[network].FTProxy),
NFT: new web3[network].eth.Contract(abis.NFT, addresses[network].NFT),
NFTProxy: new web3[network].eth.Contract(abis.NFTProxy, addresses[network].NFTProxy),
Trade: new web3[network].eth.Contract(abis.Trade, addresses[network].Trade),
LAND: new web3[network].eth.Contract(abis.LAND, addresses[network].LAND),
LANDProxy: new web3[network].eth.Contract(abis.LANDProxy, addresses[network].LANDProxy),
}
});
const getNetworkName = () => chainName;
const getMainnetAddress = async () => {
if (typeof window !== "undefined" && window.ethereum) {
const [address] = await window.ethereum.request({
method: 'eth_requestAccounts',
});
return address || null;
} else {
return null;
}
};
const transactionQueue = {
running: false,
queue: [],
lock() {
if (!this.running) {
this.running = true;
return Promise.resolve();
} else {
const promise = makePromise();
this.queue.push(promise.accept);
return promise;
}
},
unlock() {
this.running = false;
if (this.queue.length > 0) {
this.queue.shift()();
}
},
};
const runSidechainTransaction = mnemonic => async (contractName, method, ...args) => {
const wallet = hdkey.fromMasterSeed(bip39.mnemonicToSeedSync(mnemonic)).derivePath(`m/44'/60'/0'/0/0`).getWallet();
const address = wallet.getAddressString();
const privateKey = wallet.getPrivateKeyString();
const privateKeyBytes = Uint8Array.from(web3['mainnetsidechain'].utils.hexToBytes(privateKey));
const txData = contracts['mainnetsidechain'][contractName].methods[method](...args);
const data = txData.encodeABI();
const gas = await txData.estimateGas({
from: address,
});
let gasPrice = await web3['mainnetsidechain'].eth.getGasPrice();
gasPrice = parseInt(gasPrice, 10);
await transactionQueue.lock();
const nonce = await web3['mainnetsidechain'].eth.getTransactionCount(address);
let tx = Transaction.fromTxData({
to: contracts['mainnetsidechain'][contractName]._address,
nonce: '0x' + new web3['mainnetsidechain'].utils.BN(nonce).toString(16),
gas: '0x' + new web3['mainnetsidechain'].utils.BN(gas).toString(16),
gasPrice: '0x' + new web3['mainnetsidechain'].utils.BN(gasPrice).toString(16),
gasLimit: '0x' + new web3['mainnetsidechain'].utils.BN(8000000).toString(16),
data,
}, {
common,
}).sign(privateKeyBytes);
const rawTx = '0x' + tx.serialize().toString('hex');
// console.log('signed tx', tx, rawTx);
const receipt = await web3['mainnetsidechain'].eth.sendSignedTransaction(rawTx);
transactionQueue.unlock();
return receipt;
};
const getTransactionSignature = async (chainName, contractName, transactionHash) => {
const u = `https://sign.exokit.org/${chainName}/${contractName}/${transactionHash}`;
for (let i = 0; i < 10; i++) {
const signature = await fetch(u).then(res => res.json());
// console.log('got sig', u, signature);
if (signature) {
return signature;
} else {
await new Promise((accept, reject) => {
setTimeout(accept, 1000);
});
}
}
return null;
};
const runMainnetTransaction = async (contractName, method, ...args) => {
const address = await getMainnetAddress();
if (address) {
const m = contracts.front[contractName].methods[method];
m.apply(m, args).send({
from: address,
});
} else {
throw new Error('no addresses passed by web3');
}
};
const _getWalletFromMnemonic = mnemonic => hdkey.fromMasterSeed(bip39.mnemonicToSeedSync(mnemonic))
.derivePath(`m/44'/60'/0'/0/0`)
.getWallet();
const getAddressFromMnemonic = mnemonic => _getWalletFromMnemonic(mnemonic)
.getAddressString();
const networkNameEl = document.getElementById('network-name');
const bindInterface = () => {
if (networkNameEl) {
networkNameEl.innerText = networkName;
}
};
export {
web3,
contracts,
bindInterface,
getNetworkName,
getMainnetAddress,
runSidechainTransaction,
runMainnetTransaction,
getTransactionSignature,
getAddressFromMnemonic,
};