Skip to content

Commit b229666

Browse files
authored
Merge pull request #85 from smartcontractkit/small-refactors
address some smaller feedback on styling
2 parents 88032a3 + 394e6d7 commit b229666

File tree

4 files changed

+18
-13
lines changed

4 files changed

+18
-13
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ Up-to-date documentation on Chainlink Functions can be found [here](https://docs
3838

3939
Install Node.js version `18.18.0` or higher _and_ Deno version `1.36.0` or higher.
4040

41-
For version 0.3.2 and above of this functions-toolkit package, Anvil is required to run local tests. A `Makefile` is included to automate the installation and build process. Simply run `make install` to install Anvil and all npm packages.
41+
For version 0.3.2 and above of this functions-toolkit package, [Anvil](https://book.getfoundry.sh/anvil/) is required to run local tests. A `Makefile` is included to automate the installation and build process. Simply run `make install` to install Anvil and all npm packages.
4242
If you already have anvil and/or foundry (the Foundry toolchain) installed, you would not need to run this. But you may want to run `foundryup` to update your version and `npm install` to install packages.
4343

4444
Chainlink Functions requires signing a terms of service agreement before creating a billing subscription. See this [getting started](https://docs.chain.link/chainlink-functions/getting-started) section in the docs.
@@ -638,6 +638,7 @@ export const maxQueryResponseBytes = 2097152 // Maximum size of incoming HTTP re
638638
functionsRouterContract: Contract // Mock FunctionsRouter contract
639639
}
640640
```
641+
where Anvil instance is defined as [such](https://github.com/wevm/anvil.js/blob/main/packages/anvil.js/src/anvil/createAnvil.ts#L10)
641642

642643
Now you can connect to the local Functions testnet RPC node with your preferred blockchain tooling, deploy a FunctionsConsumer contract, instantiate and initialize the`SubscriptionManager`, create, add the consumer contract and fund the subscription, send requests, and use the `ResponseListener` to listen for responses all on your machine.
643644

src/localFunctionsTestnet.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ import type {
3333
RequestEventData,
3434
} from './types'
3535

36+
// this chain id is defined here as a default: https://github.com/wevm/viem/blob/main/src/chains/definitions/localhost.ts#L4
37+
const chainId = 1337
38+
3639
export const startLocalFunctionsTestnet = async (
3740
simulationConfigPath?: string,
3841
port = 8545,
@@ -41,28 +44,26 @@ export const startLocalFunctionsTestnet = async (
4144
try {
4245
anvil = createAnvil({
4346
port,
44-
chainId: 1337,
47+
chainId: chainId,
4548
})
4649
} catch (error) {
4750
console.error('Error creating Anvil instance: ', error)
4851
console.error(
49-
'Please refer to README about how to properly set up the environment, including anvil.',
52+
'Please refer to README about how to properly set up the environment, including anvil.\n',
5053
)
5154
throw error
5255
}
5356

5457
await anvil.start()
55-
console.log(`Anvil started on port ${port} with chain ID 1337`)
58+
console.log(`Anvil started on port ${port} with chain ID 1337\n`)
5659

5760
anvil.on('message', message => {
5861
console.log('Anvil message:', message)
5962
})
6063

61-
let privateKey = process.env.PRIVATE_KEY
62-
if (!privateKey) {
63-
// this is a hardcoded private key provided by anvil
64-
privateKey = 'ac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80'
65-
}
64+
// this is a hardcoded private key provided by anvil, defined here: https://book.getfoundry.sh/anvil/#getting-started
65+
const privateKey =
66+
process.env.PRIVATE_KEY || 'ac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80'
6667

6768
const admin = new Wallet(privateKey, new providers.JsonRpcProvider(`http://127.0.0.1:${port}`))
6869

test/integration/fetchRequestCommitment.test.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import { setupLocalTestnetFixture } from '../utils'
33

44
import { Contract, Wallet, utils, providers } from 'ethers'
55

6+
const localhost = 'http://127.0.0.1:8004/'
7+
68
jest.retryTimes(2, { logErrorsBeforeRetry: true })
79

810
describe('fetchRequestCommitment', () => {
@@ -62,7 +64,7 @@ describe('fetchRequestCommitment', () => {
6264

6365
const commitment = await fetchRequestCommitment({
6466
requestId: reqId,
65-
provider: new providers.JsonRpcProvider('http://127.0.0.1:8004/'),
67+
provider: new providers.JsonRpcProvider(`${localhost}`),
6668
functionsRouterAddress,
6769
donId,
6870
})
@@ -105,7 +107,7 @@ describe('fetchRequestCommitment', () => {
105107

106108
const commitment = await fetchRequestCommitment({
107109
requestId: reqId,
108-
provider: new providers.JsonRpcProvider('http://127.0.0.1:8004/'),
110+
provider: new providers.JsonRpcProvider(`${localhost}`),
109111
functionsRouterAddress,
110112
donId,
111113
toBlock: 1000,
@@ -119,7 +121,7 @@ describe('fetchRequestCommitment', () => {
119121
await expect(async () => {
120122
await fetchRequestCommitment({
121123
requestId: '0xDummyRequestId',
122-
provider: new providers.JsonRpcProvider('http://127.0.0.1:8004/'),
124+
provider: new providers.JsonRpcProvider(`${localhost}`),
123125
functionsRouterAddress,
124126
donId: 'invalid donId',
125127
})
@@ -132,7 +134,7 @@ describe('fetchRequestCommitment', () => {
132134
await expect(async () => {
133135
await fetchRequestCommitment({
134136
requestId: '0xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa',
135-
provider: new providers.JsonRpcProvider('http://127.0.0.1:8004/'),
137+
provider: new providers.JsonRpcProvider(`${localhost}`),
136138
functionsRouterAddress,
137139
donId,
138140
})

test/utils/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ const createTestWallets = (port = 8545): Wallet[] => {
7676

7777
// these are hardcoded private keys provided by anvil. you can see these private keys in the console output if you simply run `anvil`
7878
// using these makes sure that these wallets are properly connected to Anvil local node
79+
// check https://book.getfoundry.sh/anvil/#getting-started for more details
7980
const privateKeys = [
8081
'59c6995e998f97a5a0044966f0945389dc9e86dae88c7a8412f4603b6b78690d',
8182
'5de4111afa1a4b94908f83103eb1f1706367c2e68ca870fc3fb9a804cdab365a',

0 commit comments

Comments
 (0)