-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlocalAPI.ts
More file actions
204 lines (193 loc) · 7.44 KB
/
localAPI.ts
File metadata and controls
204 lines (193 loc) · 7.44 KB
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
import {chains} from '../assets/chains'
import BepHydro from '../assets/abis/bephydro_copy.json'
import {AbiItem} from 'web3-utils'
import bridgeContract from '../assets/abis/bridgeContract.json'
import {addressForWeb3, chainIDs, hydroAddresses, swapContractAddresses} from '../common/common'
import {Contract} from 'web3-eth-contract'
const Web3 = require('web3')
let web3 = new Web3(new Web3.providers.HttpProvider(addressForWeb3))
export const localAPI = {
getAccountAddress: async (): Promise<string> => {
const accounts = await web3.eth.getAccounts()
return accounts[0]
},
getChainID: async (): Promise<number> => {
return await web3.eth.net.getId()
},
connectToMetamask: async function (): Promise<connectToMetamaskReturnType> {
let returnValues = {
status: false,
account: '',
chainID: 0,
}
try {
if (typeof window.ethereum !== 'undefined') {
await window.ethereum.enable()
web3 = new Web3(window.ethereum) // Instance web3 with the provided information
}
if (typeof web3 !== 'undefined') {
web3 = new Web3(web3.currentProvider)
returnValues.account = await this.getAccountAddress()
} else {
console.log('No Web3 Detected')
web3 = new Web3(new Web3.providers.WebsocketProvider('wss://infura.io/ws/v3/72e114745bbf4822b987489c119f858b'))
}
returnValues.status = true
} catch (error) {
console.error('localAPI.connectToMetamask error')
}
returnValues.chainID = await web3.eth.net.getId()
return returnValues
},
changeNetwork: async (chainID: number): Promise<boolean | undefined> => {
try {
await window.ethereum.request({
method: 'wallet_switchEthereumChain',
params: [{chainId: chains[chainID].chainId}],
})
return true
} catch (error) {
try {
if ((error as ErrorType).code === 4902) {
await window.ethereum.request({
method: 'wallet_addEthereumChain',
params: [chains[chainID]],
})
return true
}
} catch (error) {
return false
}
}
},
createHydroContractInstance: (chainID: number): Contract => {
let hydroAddress
switch (chainID) {
case chainIDs.eth: {
hydroAddress = hydroAddresses.forEth
break
}
case chainIDs.bsc: {
hydroAddress = hydroAddresses.forBsc
break
}
case chainIDs.moonriverMainnet: {
hydroAddress = hydroAddresses.forMoonriver
break
}
case chainIDs.coinex: {
hydroAddress = hydroAddresses.forCoinex
break
}
case chainIDs.mumbaiTest:
case chainIDs.rinkebyTest:
case chainIDs.moonbeamAlphaTestnet:
case chainIDs.coinExTest: {
hydroAddress = hydroAddresses.forTestNets
break
}
}
return new web3.eth.Contract(BepHydro as AbiItem[], hydroAddress)
},
getBridgeContractInstance: (way: ConversionWayType): Contract => {
return new web3.eth.Contract(bridgeContract as AbiItem[], swapContractAddresses[way])
},
fromWei: function (weiBalance: string, ether = ''): string {
return web3.utils.fromWei(weiBalance, ether)
},
toWei: function (approvedAmount: string): string {
return web3.utils.toWei(approvedAmount)
},
getHydroBalance: async function (hydroContractInstance: Contract): Promise<string> {
let address
address = await this.getAccountAddress()
try {
const HydroBalance = await hydroContractInstance.methods.balanceOf(address).call()
return this.fromWei(HydroBalance)
} catch (error) {
console.error('getHydroBalance error')
return ''
}
},
approveTokens: async function (hydroContractInstance: Contract,
approvedAmount: string,
leftChainId: ChainIdType,
conversionWay: ConversionWayType,
bridgeContractInstance: Contract): Promise<void> {
let outputHash = ''
const account = await this.getAccountAddress()
const finalAmount = leftChainId === chainIDs.mumbaiTest
? approvedAmount
: web3.utils.toWei(approvedAmount)
await hydroContractInstance.methods
.approve(swapContractAddresses[conversionWay], finalAmount) // there need to check approve or not
.send({from: account,})
.on('transactionHash', (hash: string) => {
if (hash !== null) {
finalAmount !== '0' ? outputHash = hash : outputHash = ''
}
})
},
/*
* example:
* https://rinkeby.etherscan.io/address/0x9477b2d4442fcd35368c029a0016e6800437bae2#readContract
* owner - address of Metamask Account: string
* spender - bridge contract:
* */
contractAllowance: async function (contract: Contract, conversionWay: ConversionWayType): Promise<number> {
/// @dev This function makes it easy to read the `allowed[]` map
/// @param _owner The address of the account that owns the token
/// @param _spender The address of the account able to transfer the tokens
/// @return Amount of remaining tokens of _owner that _spender is allowed
/// to spend
const owner = await this.getAccountAddress()
const spender = swapContractAddresses[conversionWay]
const result = await contract.methods.allowance(owner, spender).call()
return web3.utils.fromWei(result)
},
getBalance: async function (address: string) {
// let accounts = await web3.eth.getAccounts();
const balance = await web3.eth.getBalance(address)
return web3.utils.fromWei(balance)
}
}
declare let window: any // todo: maybe fix any
type ErrorType = {
code: number
}
export type ConversionWayType = 'coinexSmartChainTestnet' | 'mumbaiTestnet' | 'rinkebyTestnet' | 'eth' | 'bsc' | 'polygon' | 'moonbeamAlphaTestnet' | 'moonriverMainnet' | 'coinex'
type connectToMetamaskReturnType = {
status: boolean
account: string
chainID: number
}
export type ChainIdType = chainIDs.eth | chainIDs.bsc | chainIDs.polygon | chainIDs.mumbaiTest | chainIDs.rinkebyTest | chainIDs.coinExTest | chainIDs.moonbeamAlphaTestnet | chainIDs.moonriverMainnet | chainIDs.coinex
export type ReceiptedType = {
transactionHash: string
transactionIndex: number,
blockHash: string
blockNumber: number
contractAddress: string
cumulativeGasUsed: number
gasUsed: number
events: {
MyEvent: {
returnValues: {
myIndexedParam: number
myOtherIndexedParam: string
myNonIndexParam: string
},
raw: {
data: string
topics: Array<string>
},
}
}
}
type ReturnSwapTokensType = {
data: {
transactionStatus: string
explorerLink: string
transactionHash: string
}
}