2022-06-09 21:39:38 +03:00
|
|
|
/**
|
|
|
|
* Updates cy.visit() to include an injected window.ethereum provider.
|
|
|
|
*/
|
2020-05-13 15:29:54 +03:00
|
|
|
|
2021-09-22 02:21:28 +03:00
|
|
|
import { Eip1193Bridge } from '@ethersproject/experimental/lib/eip1193-bridge'
|
2022-09-13 19:07:47 +03:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-restricted-imports
|
2022-01-12 21:06:22 +03:00
|
|
|
import { JsonRpcProvider } from '@ethersproject/providers'
|
|
|
|
import { Wallet } from '@ethersproject/wallet'
|
2020-05-13 15:29:54 +03:00
|
|
|
|
2021-06-16 04:04:54 +03:00
|
|
|
// todo: figure out how env vars actually work in CI
|
|
|
|
// const TEST_PRIVATE_KEY = Cypress.env('INTEGRATION_TEST_PRIVATE_KEY')
|
|
|
|
const TEST_PRIVATE_KEY = '0xe580410d7c37d26c6ad1a837bbae46bc27f9066a466fb3a66e770523b4666d19'
|
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
|
|
|
|
2022-10-06 23:16:58 +03:00
|
|
|
const provider = new JsonRpcProvider('https://goerli.infura.io/v3/4bf032f2d38a4ed6bb975b80d6340847', 4)
|
2022-06-14 00:43:58 +03:00
|
|
|
const signer = new Wallet(TEST_PRIVATE_KEY, provider)
|
2022-06-14 22:40:52 +03:00
|
|
|
export const injected = new (class extends Eip1193Bridge {
|
2022-10-06 23:16:58 +03:00
|
|
|
chainId = /* GOERLI= */ 5
|
2021-04-21 19:19:34 +03:00
|
|
|
|
2022-06-09 21:39:38 +03:00
|
|
|
async sendAsync(...args: any[]) {
|
2020-05-13 15:29:54 +03:00
|
|
|
console.debug('sendAsync called', ...args)
|
|
|
|
return this.send(...args)
|
|
|
|
}
|
2022-06-09 21:39:38 +03:00
|
|
|
async send(...args: any[]) {
|
2020-05-13 15:29:54 +03:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-06-14 00:43:58 +03:00
|
|
|
})(signer, provider)
|