Skip to content

Commit

Permalink
feat(tests): fix Integration tests (#13772)
Browse files Browse the repository at this point in the history
  • Loading branch information
clemsos committed May 6, 2024
1 parent ce66567 commit 22a0627
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 95 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ jobs:
BUILDKIT_PROGRESS: plain
steps:
- uses: actions/checkout@v4
# - name: Run Integration Tests
# run: scripts/integration-tests.sh
- name: Run Integration Tests
run: scripts/integration-tests.sh
- name: Archive production artifacts
uses: actions/upload-artifact@v4
with:
Expand Down
1 change: 0 additions & 1 deletion packages/hardhat-plugin/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ describe('Unlock Hardhat plugin', function () {
assert.equal(this.hre.unlock.networks['1'].unlockAddress, 'newAddress')
})
it('should store additional networks info', function () {
console.log(this.hre.unlock.networks['12345'])
assert.isTrue(Object.keys(this.hre.unlock.networks).includes('12345'))
assert.isTrue(Object.keys(this.hre.unlock.networks).includes('31337'))
assert.equal(
Expand Down
4 changes: 1 addition & 3 deletions tests/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
"@nomicfoundation/hardhat-network-helpers": "1.0.10",
"@nomicfoundation/hardhat-toolbox": "4.0.0",
"@nomicfoundation/hardhat-verify": "2.0.4",
"@nomiclabs/hardhat-ethers": "2.2.3",
"@nomiclabs/hardhat-etherscan": "3.1.8",
"@typechain/ethers-v5": "10.1.0",
"@typechain/ethers-v6": "0.5.1",
"@typechain/hardhat": "6.1.2",
Expand All @@ -31,7 +29,7 @@
"apollo-link-http": "1.5.17",
"chai": "4.2.0",
"eslint": "8.46.0",
"ethers": "5.7.2",
"ethers": "6.10.0",
"graphql": "16.6.0",
"hardhat": "2.20.1",
"hardhat-gas-reporter": "1.0.8",
Expand Down
3 changes: 1 addition & 2 deletions tests/test/helpers/fixtures.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { ethers } from 'hardhat'
const { constants, utils } = ethers
import { DEFAULT_KEY_PRICE } from './keys'

export const lockParams = {
expirationDuration: 60 * 60 * 24 * 30, // 30 days
currencyContractAddress: constants.AddressZero, // address 0 is ETH but could be any ERC20 token
currencyContractAddress: ethers.ZeroAddress, // address 0 is ETH but could be any ERC20 token
keyPrice: DEFAULT_KEY_PRICE, // in wei
maxNumberOfKeys: 100,
name: 'Unlock-Protocol Sample Lock',
Expand Down
51 changes: 25 additions & 26 deletions tests/test/helpers/keys.ts
Original file line number Diff line number Diff line change
@@ -1,71 +1,70 @@
const { ethers, unlock } = require('hardhat')
const { AddressZero } = ethers.constants
import type { BigNumber } from 'ethers'
const { ZeroAddress } = ethers

export const DEFAULT_KEY_PRICE = ethers.utils.parseEther('0.01')
export const DEFAULT_KEY_PRICE = ethers.parseEther('0.01')

export const genKeyId = (lockAddress: string, tokenId: BigNumber) =>
export const genKeyId = (lockAddress: string, tokenId: bigint) =>
`${lockAddress}-${tokenId.toString()}`

export const purchaseKey = async (
lockAddress: string,
keyOwnerAddress: string,
price = null,
isErc20 = false
price = null
) => {
// make sure we got ethers lock
const lock = await unlock.getLockContract(lockAddress)
const isErc20 = (await lock.tokenAddress()) !== ZeroAddress

const purchasePrice = price || (await lock.keyPrice())

const tx = await lock.purchase(
[purchasePrice],
isErc20 ? [purchasePrice] : [],
[keyOwnerAddress],
[AddressZero],
[AddressZero],
[[]],
[ZeroAddress],
[ZeroAddress],
['0x'],
{
value: isErc20 ? 0 : purchasePrice,
}
)
// get token ids
const { events, blockNumber, transactionHash } = await tx.wait()
const { args } = events.find(
(v: any) =>
v.event === 'Transfer' &&
v.address.toLowerCase() === lockAddress.toLowerCase()
const { logs, blockNumber, hash } = await tx.wait()
const { args } = logs.find(
({ fragment }: any) => fragment && fragment.name === 'Transfer'
)

const { tokenId, from, to } = args

return { tokenId, blockNumber, from, to, transactionHash }
return { tokenId, blockNumber, from, to, transactionHash: hash }
}

export const purchaseKeys = async (
lockAddress: string,
nbOfKeys = 1,
price = DEFAULT_KEY_PRICE,
isErc20 = false
price = DEFAULT_KEY_PRICE
) => {
// make sure we got ethers lock
const lock = await unlock.getLockContract(lockAddress)
const isErc20 = (await lock.tokenAddress()) !== ZeroAddress

// signer 0 is the lockOwner so keyOwners starts at index 1
const signers = await ethers.getSigners()
const keyOwners = signers.slice(1, nbOfKeys + 1)

const tx = await lock.purchase(
isErc20 ? keyOwners.map(() => price) : [],
keyOwners.map((k: any) => k.address),
keyOwners.map(() => AddressZero),
keyOwners.map(() => AddressZero),
keyOwners.map(() => []),
keyOwners.map(({ address }: { address: string }) => address),
keyOwners.map(() => ZeroAddress),
keyOwners.map(() => ZeroAddress),
keyOwners.map(() => '0x'),
{
value: isErc20 ? 0 : price.mul(nbOfKeys),
value: isErc20 ? 0 : price * BigInt(nbOfKeys),
}
)
// get token ids
const { events } = await tx.wait()
const tokenIds = events
.filter((v: any) => v.event === 'Transfer')
const { logs } = await tx.wait()
const tokenIds = logs
.filter(({ fragment }: any) => fragment && fragment.name === 'Transfer')
.map(({ args }: any) => args.tokenId)

return {
Expand Down
18 changes: 9 additions & 9 deletions tests/test/helpers/subgraph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {
} from '@apollo/client'
import { HttpLink } from 'apollo-link-http'
import { genKeyId } from './keys'
import type { BigNumber } from 'ethers'

import { localhost } from '@unlock-protocol/networks'

Expand Down Expand Up @@ -62,7 +61,7 @@ export const getLock = async (lockAddress: string) => {
} = await subgraph.query({
query: getLockQuery,
variables: {
id: lockAddress,
id: lockAddress.toLowerCase(),
},
})
return lock
Expand All @@ -82,8 +81,8 @@ const getKeyQuery = gql`
}
}
`
export const getKey = async (lockAddress: string, tokenId: BigNumber) => {
const keyId = genKeyId(lockAddress, tokenId)
export const getKey = async (lockAddress: string, tokenId: bigint) => {
const keyId = genKeyId(lockAddress.toLowerCase(), tokenId)
const {
data: { key },
} = await subgraph.query({
Expand Down Expand Up @@ -115,21 +114,22 @@ export const getReceipt = async (txHash: string) => {
} = await subgraph.query({
query: getReceiptQuery,
variables: {
id: txHash,
id: txHash.toLowerCase(),
},
})
return receipt
}

const getLocksQuery = gql`
query Locks($first: Int = 1) {
locks(first: $first) {
export const getLocks = async (first = 100, isErc20 = false) => {
const getLocksQuery = gql`query Locks($first: Int = 1) {
locks(first: $first, where: {${
isErc20 ? `tokenAddress_not_contains` : `tokenAddress_contains`
}: "0000000000000000000000000000000000000000" }) {
tokenAddress
address
}
}
`
export const getLocks = async (first = 100) => {
const { data } = await subgraph.query({
query: getLocksQuery,
variables: {
Expand Down

0 comments on commit 22a0627

Please sign in to comment.