2020-05-13 15:29:54 +03:00
|
|
|
// ***********************************************
|
|
|
|
// For more comprehensive examples of custom
|
|
|
|
// commands please read more here:
|
|
|
|
// https://on.cypress.io/custom-commands
|
|
|
|
// ***********************************************
|
|
|
|
|
|
|
|
import { JsonRpcProvider } from '@ethersproject/providers'
|
|
|
|
import { Wallet } from '@ethersproject/wallet'
|
2021-04-21 19:19:34 +03:00
|
|
|
import { Eip1193Bridge } from '@ethersproject/experimental/lib/eip1193-bridge'
|
2020-05-13 15:29:54 +03:00
|
|
|
|
2021-04-21 20:35:49 +03:00
|
|
|
const TEST_PRIVATE_KEY = Cypress.env('INTEGRATION_TEST_PRIVATE_KEY')
|
2020-05-13 15:29:54 +03:00
|
|
|
|
|
|
|
// address of the above key
|
2021-04-21 20:35:49 +03:00
|
|
|
export const TEST_ADDRESS_NEVER_USE = new Wallet(TEST_PRIVATE_KEY).address
|
2020-05-13 15:29:54 +03:00
|
|
|
|
2021-04-21 20:35:49 +03:00
|
|
|
export const TEST_ADDRESS_NEVER_USE_SHORTENED = `${TEST_ADDRESS_NEVER_USE.substr(
|
|
|
|
0,
|
|
|
|
6
|
|
|
|
)}...${TEST_ADDRESS_NEVER_USE.substr(-4, 4)}`
|
2020-06-05 20:20:47 +03:00
|
|
|
|
2021-04-21 19:19:34 +03:00
|
|
|
class CustomizedBridge extends Eip1193Bridge {
|
|
|
|
chainId = 4
|
|
|
|
|
2020-05-13 15:29:54 +03:00
|
|
|
async sendAsync(...args) {
|
|
|
|
console.debug('sendAsync called', ...args)
|
|
|
|
return this.send(...args)
|
|
|
|
}
|
|
|
|
async send(...args) {
|
|
|
|
console.debug('send called', ...args)
|
|
|
|
const isCallbackForm = typeof args[0] === 'object' && typeof args[1] === 'function'
|
|
|
|
let callback
|
|
|
|
let method
|
|
|
|
let params
|
|
|
|
if (isCallbackForm) {
|
|
|
|
callback = args[1]
|
|
|
|
method = args[0].method
|
|
|
|
params = args[0].params
|
|
|
|
} else {
|
|
|
|
method = args[0]
|
|
|
|
params = args[1]
|
|
|
|
}
|
|
|
|
if (method === 'eth_requestAccounts' || method === 'eth_accounts') {
|
|
|
|
if (isCallbackForm) {
|
|
|
|
callback({ result: [TEST_ADDRESS_NEVER_USE] })
|
|
|
|
} else {
|
|
|
|
return Promise.resolve([TEST_ADDRESS_NEVER_USE])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (method === 'eth_chainId') {
|
|
|
|
if (isCallbackForm) {
|
|
|
|
callback(null, { result: '0x4' })
|
|
|
|
} else {
|
|
|
|
return Promise.resolve('0x4')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
const result = await super.send(method, params)
|
|
|
|
console.debug('result received', method, params, result)
|
|
|
|
if (isCallbackForm) {
|
|
|
|
callback(null, { result })
|
|
|
|
} else {
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
if (isCallbackForm) {
|
|
|
|
callback(error, null)
|
|
|
|
} else {
|
|
|
|
throw error
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// sets up the injected provider to be a mock ethereum provider with the given mnemonic/index
|
|
|
|
Cypress.Commands.overwrite('visit', (original, url, options) => {
|
2020-06-30 21:02:09 +03:00
|
|
|
return original(url.startsWith('/') && url.length > 2 && !url.startsWith('/#') ? `/#${url}` : url, {
|
2020-05-13 15:29:54 +03:00
|
|
|
...options,
|
|
|
|
onBeforeLoad(win) {
|
|
|
|
options && options.onBeforeLoad && options.onBeforeLoad(win)
|
2020-08-27 18:21:51 +03:00
|
|
|
win.localStorage.clear()
|
2020-07-21 19:07:10 +03:00
|
|
|
const provider = new JsonRpcProvider('https://rinkeby.infura.io/v3/4bf032f2d38a4ed6bb975b80d6340847', 4)
|
2021-04-21 20:35:49 +03:00
|
|
|
const signer = new Wallet(TEST_PRIVATE_KEY, provider)
|
2020-05-26 20:35:05 +03:00
|
|
|
win.ethereum = new CustomizedBridge(signer, provider)
|
2021-02-16 11:46:17 +03:00
|
|
|
},
|
2020-05-13 15:29:54 +03:00
|
|
|
})
|
|
|
|
})
|