-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathbundle.js
164 lines (144 loc) · 5.44 KB
/
bundle.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
(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.module = f()}})(function(){var define,module,exports;return (function(){function r(e,n,t){function o(i,f){if(!n[i]){if(!e[i]){var c="function"==typeof require&&require;if(!f&&c)return c(i,!0);if(u)return u(i,!0);var a=new Error("Cannot find module '"+i+"'");throw a.code="MODULE_NOT_FOUND",a}var p=n[i]={exports:{}};e[i][0].call(p.exports,function(r){var n=e[i][1][r];return o(n||r)},p,p.exports,r,e,n,t)}return n[i].exports}for(var u="function"==typeof require&&require,i=0;i<t.length;i++)o(t[i]);return o}return r})()({1:[function(require,module,exports){
window.addEventListener('load', async () => {
if (window.ethereum) {
window.web3 = new Web3(ethereum);
try {
await ethereum.enable();
} catch (error) {
toastr.error('An error occurred: ' + error);
}
} else if (window.web3) {
window.web3 = new Web3(web3.currentProvider);
} else {
toastr.error('Non-Ethereum browser detected. You should consider trying MetaMask.');
return
}
let defaultAddress = (await web3.eth.getAccounts())[0]
$('#blockscouts').attr('href', 'https://blockscout.com/poa/xdai/address/' + defaultAddress + '/tokens')
$('#etherscan').attr('href', 'https://etherscan.io/address/' + defaultAddress + '#tokentxns')
let network = await web3.eth.net.getNetworkType()
let tokenName
if (network === 'private') {
if (await web3.eth.net.getId() === 100) {
network = 'xdai'
tokenName = 'xGEN'
$('#description').text('Your wallet is connected to the xDai network.')
$('#direction').text('The direction of the bridge is xGEN to GEN.')
}
} else if (network === 'main') {
network = 'mainnet'
tokenName = 'GEN'
$('#description').text('Your wallet is connected to the Ethereum mainnet network.')
$('#direction').text('The direction of the bridge is GEN to xGEN.')
}
if (network !== 'mainnet' && network !== 'xdai') {
toastr.error('Invalid network specified (please use xDai/ mainnet).')
$('#description').text('No web3 provider detected...')
return
}
const GENTokenContract = await new web3.eth.Contract(
[
{
'constant': true,
'inputs': [
{
'name': 'owner',
'type': 'address'
}
],
'name': 'balanceOf',
'outputs': [
{
'name': '',
'type': 'uint256'
}
],
'payable': false,
'stateMutability': 'view',
'type': 'function'
}
],
'0x543Ff227F64Aa17eA132Bf9886cAb5DB55DCAddf',
{}
)
let genBalance = Math.round(web3.utils.fromWei(await GENTokenContract.methods.balanceOf((await web3.eth.getAccounts())[0]).call()) * 10) / 10
$('#balance').text('Your token balance is ' + genBalance + ' ' + tokenName)
$('#amount').attr('max', genBalance)
$('#form').submit(function(event) {
event.preventDefault();
let xgenToMove = parseFloat($('#amount').val());
if (isNaN(xgenToMove)) {
toastr.error('Invalid amount of tokens.');
return;
} else if (xgenToMove > genBalance) {
toastr.error('You lack the sufficient balance to move this amount.');
return;
}
moveGEN(xgenToMove);
})
});
async function moveGEN (xgenToMove) {
let defaultAddress = (await web3.eth.getAccounts())[0];
let opts = {
from: defaultAddress
}
let weiValue = web3.utils.toWei(xgenToMove.toString())
let bridgeAddress
let network = await web3.eth.net.getNetworkType()
if (network === 'private') {
if (await web3.eth.net.getId() === 100) {
network = 'xdai'
}
} else if (network === 'main') {
network = 'mainnet'
}
if (network === 'mainnet') {
bridgeAddress = '0x6eA6C65E14661C0BcaB5bc862fE5E7D3B5630C2F'
txMsg = ' GEN tokens to the xDai chain'
} else if (network === 'xdai') {
bridgeAddress = '0xe47097ceF3B0bcbb0095A21523714bF0022E2DB8'
txMsg = ' xGEN tokens to the Ethereum mainnet chain'
} else {
console.error('Invalid network specified (please use xDai/ mainnet).')
return
}
let callInterface = {
'constant': false,
'inputs': [
{ 'name': '_from', 'type': 'address' },
{ 'name': '_value', 'type': 'uint256' },
{ 'name': '_data', 'type': 'bytes' }
],
'name': 'onTokenTransfer',
'outputs': [{ 'name': '', 'type': 'bool' }],
'payable': false,
'stateMutability': 'nonpayable',
'type': 'function'
}
let callData = web3.eth.abi.encodeFunctionCall(callInterface, [
defaultAddress,
weiValue,
'0x'
])
const GENTokenContract = await new web3.eth.Contract(
[
{ 'constant': false,
'inputs': [
{ 'name': '_to', 'type': 'address' },
{ 'name': '_value', 'type': 'uint256' },
{ 'name': '_data', 'type': 'bytes' }
],
'name': 'transfer',
'outputs': [{ 'name': '', 'type': 'bool' }],
'payable': false,
'stateMutability': 'nonpayable',
'type': 'function' }
],
'0x543Ff227F64Aa17eA132Bf9886cAb5DB55DCAddf',
opts
)
await GENTokenContract.methods.transfer(bridgeAddress, weiValue, callData).send()
toastr.info('Transaction was mined successfully. The tokens will be in your account in a few blocks.')
}
},{}]},{},[1])(1)
});