Skip to content

Commit

Permalink
Update and simplify browser tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rigwild committed Jul 25, 2024
1 parent 2fea754 commit ee97149
Show file tree
Hide file tree
Showing 7 changed files with 133 additions and 216 deletions.
12 changes: 1 addition & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -236,17 +236,7 @@ pnpm i --global parcel
```

```bash
# Using ethers.js
parcel tests/testEthersJs.html

# Using window.ethereum
parcel tests/testWindowEthereum.html
```

Then open a server, for example with [`serve`](https://github.com/vercel/serve)

```bash
serve dist/ -p 5555
parcel tests/browser/index.html
```

## License
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "nemeos-sdk",
"version": "0.1.3",
"version": "0.1.4",
"type": "module",
"description": "Nemeos SDK to facilitate integration with the Nemeos platform",
"license": "MIT",
Expand Down Expand Up @@ -40,6 +40,6 @@
"ofetch": "^1.3.4"
},
"devDependencies": {
"typescript": "^5.5.3"
"typescript": "^5.5.4"
}
}
18 changes: 9 additions & 9 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 31 additions & 0 deletions tests/browser/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body>
<h1>Nemeos SDK Test</h1>

<div>
Is wallet connected?
<span id="isConnected">---</span>
</div>

<div>
<button id="connectWallet">Connect wallet</button>
</div>
<div>
<button id="startLoan">Start Loan for nftId 224 and 90 days</button>
</div>
<div>
<button id="retrieveLoan">Retrieve Loan for nftId 224</button>
</div>
<div>
<button id="payNextLoanStep">Pay Next Loan Step for nftId 224</button>
</div>

<script type="module" src="./script.ts"></script>
</body>
</html>
90 changes: 90 additions & 0 deletions tests/browser/script.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import * as ethers from 'ethers'
import { getBrowserProvider, NemeosSDK } from '../../src/index.js'

declare global {
interface Window {
ethereum: any
}
}

let signer: ethers.Signer
let nemeosSdk: NemeosSDK
let nemeosPoolBuyOpenSeaClient: ReturnType<NemeosSDK['getNemeosPoolClient']>

const nemeosPoolAddress = '0x812db15b8Bb43dBA89042eA8b919740C23aD48a3'
const cyberKongzAddress = '0x15cd1cfCd48C06cfC44D433D66C7a9fE06b2C2c3'

export async function isMetamaskConnected() {
if (!window.ethereum) return false

try {
const _provider = new ethers.BrowserProvider(window.ethereum)
const accounts = await _provider.send('eth_accounts', [])
const accounts2 = await window.ethereum.request({ method: 'eth_accounts' })

if (accounts.length !== accounts2.length || accounts[0] !== accounts2[0]) {
throw new Error('Metamask accounts are not the same when using ethers.js and window.ethereum.request')
}

console.log('[isMetamaskConnected] Metamask is connected', { accounts })
return accounts && accounts.length > 0
} catch (error) {
console.error('[isMetamaskConnected] There was an error while trying to connect to Metamask isMetamaskConnected()', { error })
return false
}
}

function setWalletConnected(bool: boolean) {
document.getElementById('isConnected')!.innerText = `${bool}`
}

async function connectWallet() {
const provider1 = new ethers.BrowserProvider(window.ethereum)
const signer1 = await provider1.getSigner()
const address1 = await signer1.getAddress()

const provider2 = getBrowserProvider(window.ethereum)
const signer2 = await provider2.getSigner()
const address2 = await signer2.getAddress()

if (address1 !== address2) {
throw new Error('Addresses are not the same when using ethers.js and window.ethereum.request')
}

signer = signer1
nemeosSdk = new NemeosSDK(signer)
nemeosPoolBuyOpenSeaClient = nemeosSdk.getNemeosPoolClient({
nemeosPoolAddress,
nftCollectionAddress: cyberKongzAddress,
nemeosPoolMode: NemeosSDK.NemeosPoolMode.BuyOpenSea,
})

setWalletConnected(true)
}

async function startLoan() {
await nemeosPoolBuyOpenSeaClient.startLoan(224, 90)
}
async function retrieveLoan() {
await nemeosPoolBuyOpenSeaClient.retrieveLoan(224)
}
async function payNextLoanStep() {
await nemeosPoolBuyOpenSeaClient.payNextLoanStep(224)
}

function addEventListeners() {
document.getElementById('connectWallet')!.addEventListener('click', connectWallet)
document.getElementById('startLoan')!.addEventListener('click', startLoan)
document.getElementById('retrieveLoan')!.addEventListener('click', retrieveLoan)
document.getElementById('payNextLoanStep')!.addEventListener('click', payNextLoanStep)
}

document.addEventListener('DOMContentLoaded', async () => {
addEventListeners()
if (await isMetamaskConnected()) {
setWalletConnected(true)
connectWallet()
} else {
setWalletConnected(false)
}
})
98 changes: 0 additions & 98 deletions tests/testEthersJs.html

This file was deleted.

96 changes: 0 additions & 96 deletions tests/testWindowEthereum.html

This file was deleted.

0 comments on commit ee97149

Please sign in to comment.