-
Notifications
You must be signed in to change notification settings - Fork 107
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adding automated anvil testing client creation. #324
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import { spawn } from "node:child_process"; | ||
import { join } from "path"; | ||
import { test } from "vitest"; | ||
import { | ||
PublicClient, | ||
TestClient, | ||
WalletClient, | ||
createPublicClient, | ||
createTestClient, | ||
createWalletClient, | ||
http, | ||
} from "viem"; | ||
import { foundry } from "viem/chains"; | ||
|
||
export interface AnvilViemClientsTest { | ||
viemClients: { | ||
walletClient: WalletClient; | ||
publicClient: PublicClient; | ||
testClient: TestClient; | ||
}; | ||
} | ||
|
||
async function waitForAnvilInit(anvil: any) { | ||
return new Promise((resolve) => { | ||
anvil.stdout.once("data", () => { | ||
resolve(true); | ||
}); | ||
}); | ||
} | ||
|
||
export const anvilTest = test.extend<AnvilViemClientsTest>({ | ||
viemClients: async ({task}, use) => { | ||
console.log('setting up clients for ', task.name); | ||
const port = Math.floor(Math.random() * 2000) + 4000; | ||
const anvil = spawn( | ||
"anvil", | ||
[ | ||
"--port", | ||
`${port}`, | ||
"--fork-url", | ||
"https://rpc.zora.co/", | ||
"--fork-block-number", | ||
"6133407", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. how about allowing chain name ( |
||
"--chain-id", | ||
"31337", | ||
], | ||
{ | ||
cwd: join(__dirname, ".."), | ||
killSignal: "SIGINT", | ||
}, | ||
); | ||
const anvilHost = `http://0.0.0.0:${port}`; | ||
await waitForAnvilInit(anvil); | ||
|
||
const chain = { | ||
...foundry, | ||
}; | ||
|
||
const walletClient = createWalletClient({ | ||
chain, | ||
transport: http(anvilHost), | ||
}); | ||
|
||
const testClient = createTestClient({ | ||
chain, | ||
mode: "anvil", | ||
transport: http(anvilHost), | ||
}); | ||
|
||
const publicClient = createPublicClient({ | ||
chain, | ||
transport: http(anvilHost), | ||
}); | ||
|
||
await use({ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. great use of |
||
publicClient, | ||
walletClient, | ||
testClient, | ||
}); | ||
|
||
// clean up function, called once after all tests run | ||
anvil.kill("SIGINT"); | ||
}, | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,61 +1,32 @@ | ||
import { | ||
Address, | ||
createPublicClient, | ||
createTestClient, | ||
createWalletClient, | ||
http, | ||
parseEther, | ||
} from "viem"; | ||
import { foundry } from "viem/chains"; | ||
import { describe, it, beforeEach, expect, afterEach } from "vitest"; | ||
import { parseEther } from "viem"; | ||
import { describe, expect } from "vitest"; | ||
import { | ||
createNew1155Token, | ||
getTokenIdFromCreateReceipt, | ||
} from "./1155-create-helper"; | ||
|
||
const chain = foundry; | ||
|
||
const walletClient = createWalletClient({ | ||
chain, | ||
transport: http(), | ||
}); | ||
|
||
const testClient = createTestClient({ | ||
chain, | ||
mode: "anvil", | ||
transport: http(), | ||
}); | ||
|
||
const publicClient = createPublicClient({ | ||
chain, | ||
transport: http(), | ||
}); | ||
|
||
const [creatorAccount] = (await walletClient.getAddresses()) as [Address]; | ||
import { anvilTest } from "src/anvil"; | ||
|
||
const demoTokenMetadataURI = "ipfs://DUMMY/token.json"; | ||
const demoContractMetadataURI = "ipfs://DUMMY/contract.json"; | ||
|
||
describe("create-helper", () => { | ||
beforeEach(async () => { | ||
await testClient.setBalance({ | ||
address: creatorAccount, | ||
value: parseEther("1"), | ||
}); | ||
}); | ||
afterEach(() => testClient.reset()); | ||
|
||
it( | ||
anvilTest( | ||
"creates a new contract given arguments", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. how about renaming to |
||
async () => { | ||
async ({ viemClients: { testClient, publicClient, walletClient } }) => { | ||
const addresses = await walletClient.getAddresses(); | ||
const creatorAddress = addresses[0]!; | ||
await testClient.setBalance({ | ||
address: creatorAddress, | ||
value: parseEther("1"), | ||
}); | ||
const new1155TokenRequest = await createNew1155Token({ | ||
publicClient, | ||
contract: { | ||
name: "testContract", | ||
uri: demoContractMetadataURI, | ||
}, | ||
tokenMetadataURI: demoTokenMetadataURI, | ||
account: creatorAccount, | ||
account: creatorAddress, | ||
mintToCreatorCount: 1, | ||
}); | ||
const hash = await new1155TokenRequest.send(walletClient); | ||
|
@@ -64,22 +35,27 @@ describe("create-helper", () => { | |
expect(receipt.to).to.equal("0x777777c338d93e2c7adf08d102d45ca7cc4ed021"); | ||
expect(getTokenIdFromCreateReceipt(receipt)).to.be.equal(1n); | ||
}, | ||
10 * 1000, | ||
20 * 1000, | ||
); | ||
it( | ||
anvilTest( | ||
"creates a new contract, than creates a new token on this existing contract", | ||
async () => { | ||
async ({ viemClients: { publicClient, walletClient } }) => { | ||
const addresses = await walletClient.getAddresses(); | ||
const creatorAccount = addresses[0]!; | ||
|
||
const new1155TokenRequest = await createNew1155Token({ | ||
publicClient, | ||
contract: { | ||
name: "testContract", | ||
name: "testContract2", | ||
uri: demoContractMetadataURI, | ||
}, | ||
tokenMetadataURI: demoTokenMetadataURI, | ||
account: creatorAccount, | ||
mintToCreatorCount: 1, | ||
}); | ||
expect(new1155TokenRequest.contractAddress).to.be.equal('0xA72724cC3DcEF210141a1B84C61824074151Dc99'); | ||
expect(new1155TokenRequest.contractAddress).to.be.equal( | ||
"0xb1A8928dF830C21eD682949Aa8A83C1C215194d3", | ||
); | ||
expect(new1155TokenRequest.contractExists).to.be.false; | ||
const hash = await new1155TokenRequest.send(walletClient); | ||
const receipt = await publicClient.waitForTransactionReceipt({ hash }); | ||
|
@@ -90,14 +66,16 @@ describe("create-helper", () => { | |
const newTokenOnExistingContract = await createNew1155Token({ | ||
publicClient, | ||
contract: { | ||
name: "testContract", | ||
name: "testContract2", | ||
uri: demoContractMetadataURI, | ||
}, | ||
tokenMetadataURI: demoTokenMetadataURI, | ||
account: creatorAccount, | ||
mintToCreatorCount: 1, | ||
}); | ||
expect(newTokenOnExistingContract.contractAddress).to.be.equal('0xA72724cC3DcEF210141a1B84C61824074151Dc99'); | ||
expect(newTokenOnExistingContract.contractAddress).to.be.equal( | ||
"0xb1A8928dF830C21eD682949Aa8A83C1C215194d3", | ||
); | ||
expect(newTokenOnExistingContract.contractExists).to.be.true; | ||
const newHash = await newTokenOnExistingContract.send(walletClient); | ||
const newReceipt = await publicClient.waitForTransactionReceipt({ | ||
|
@@ -107,6 +85,6 @@ describe("create-helper", () => { | |
expect(tokenId).to.be.equal(2n); | ||
expect(newReceipt).not.toBeNull(); | ||
}, | ||
10 * 1000, | ||
20 * 1000, | ||
); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,9 @@ | ||
export * from "./premint-client"; | ||
export * from "./premint/premint-client"; | ||
|
||
export * from "./preminter"; | ||
export * from "./premint/preminter"; | ||
|
||
export * from "./premint-api-client"; | ||
export * from "./premint/premint-api-client"; | ||
|
||
export * from "./mint-api-client"; | ||
export * from "./mint/mint-api-client"; | ||
|
||
export * from "./1155-create-helper"; | ||
export * from "./create/1155-create-helper"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nice!