Skip to content

Commit

Permalink
✨ Add AlchemyProvider class (#205)
Browse files Browse the repository at this point in the history
* feat: AlchemyProvider class

* chore: deleting yarn.lock

* chore: fixing package.json

* test: testing alchemy provider

* Simplify alchemy provider

* env cleanup

* Improve testing logging errros

* Remove excess dotenv

---------

Co-authored-by: dawsbot <[email protected]>
  • Loading branch information
jtfirek and dawsbot authored Apr 22, 2023
1 parent f673a1c commit e535cb5
Show file tree
Hide file tree
Showing 9 changed files with 50 additions and 17 deletions.
2 changes: 1 addition & 1 deletion .env → .env.example
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
RPC_ORIGIN=https://free-eth-node.com
# RPC_ORIGIN=http://localhost:3000
ALCHEMY_API_KEY=API_KEY_HERE
1 change: 1 addition & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ jobs:
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v3
with:
ALCHEMY_API_KEY: ${{ secrets.ALCHEMY_API_KEY }}
node-version: ${{ matrix.node-version }}
cache: 'npm'
- run: npm ci
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.env
node_modules
lib
dist
Expand Down
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { BaseContract, Contract } from './classes/Contract';
import { AlchemyProvider } from './providers/AlchemyProvider';
import {
ConstructorOptions,
FallthroughProvider,
Expand Down Expand Up @@ -47,6 +48,7 @@ export {
jsonRpcProvider,
JsonRpcProvider,
FallthroughProvider,
AlchemyProvider,
tinyBig,
toChecksumAddress,
weiToEther,
Expand Down
8 changes: 8 additions & 0 deletions src/providers/AlchemyProvider.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { JsonRpcProvider } from './JsonRpcProvider';

export class AlchemyProvider extends JsonRpcProvider {
constructor(apiKey: string, network = 'mainnet') {
const alchemyUrl = `https://eth-${network}.alchemyapi.io/v2/${apiKey}`;
super(alchemyUrl);
}
}
8 changes: 3 additions & 5 deletions src/providers/test/json-rpc-provider/get-block-number.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,9 @@ const mockPostResponse = JSON.stringify({
describe('provider.getBlockNumber', () => {
it('should get number integer', async () => {
const essentialEthProvider = new JsonRpcProvider();
mockOf(unfetch).mockReturnValueOnce(
Promise.resolve({
text: () => mockPostResponse,
} as unknown as Response),
);
mockOf(unfetch).mockResolvedValueOnce({
text: () => Promise.resolve(mockPostResponse),
} as Response);
const essentialEthBlockNumber = await essentialEthProvider.getBlockNumber();
expect(essentialEthBlockNumber).toBe(10);
});
Expand Down
8 changes: 3 additions & 5 deletions src/providers/test/json-rpc-provider/get-gas-price.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,9 @@ const rpcUrl = rpcUrls.mainnet;
describe('provider.getGasPrice', () => {
it('should get TinyBig integer', async () => {
const provider = new JsonRpcProvider(rpcUrl);
mockOf(unfetch).mockReturnValueOnce(
Promise.resolve({
text: () => mockPostResponse,
} as unknown as Response),
);
mockOf(unfetch).mockResolvedValueOnce({
text: () => Promise.resolve(mockPostResponse),
} as Response);

const gasPrice = await provider.getGasPrice();
expect(z.instanceof(TinyBig).safeParse(gasPrice).success).toBe(true);
Expand Down
18 changes: 12 additions & 6 deletions src/providers/test/rpc-urls.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
import z from 'zod';
export const fakeUrls = {
notRPCButRealHttp: 'https://httpstat.us/200',
} as const;

const RPC_ORIGIN = process.env.RPC_ORIGIN;
if (!z.string().url().safeParse(RPC_ORIGIN).success) {
throw new Error('RPC_ORIGIN is not defined or is invalid URL');
}

export const rpcUrls = {
mainnet: `${process.env.RPC_ORIGIN}/api/eth`,
matic: `${process.env.RPC_ORIGIN}/api/MATIC`,
gno: `${process.env.RPC_ORIGIN}/api/gno`,
bnb: `${process.env.RPC_ORIGIN}/api/bnb`,
arb1: `${process.env.RPC_ORIGIN}/api/arb1`,
gor: `${process.env.RPC_ORIGIN}/api/gor`,
mainnet: `${RPC_ORIGIN}/api/eth`,
matic: `${RPC_ORIGIN}/api/MATIC`,
gno: `${RPC_ORIGIN}/api/gno`,
bnb: `${RPC_ORIGIN}/api/bnb`,
arb1: `${RPC_ORIGIN}/api/arb1`,
gor: `${RPC_ORIGIN}/api/gor`,
};
19 changes: 19 additions & 0 deletions src/providers/test/test-alchemy-provider.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { z } from 'zod';
import { AlchemyProvider, TinyBig } from '../../index';

const ALCHEMY_API_KEY = process.env.ALCHEMY_API_KEY;

if (!ALCHEMY_API_KEY) {
throw new Error(
'ALCHEMY_API_KEY is not defined in the environment variables.',
);
}

const provider = new AlchemyProvider(ALCHEMY_API_KEY);

describe('alchemyProvider.getGasPrice', () => {
it('should return the current gas price', async () => {
const gasPrice = await provider.getGasPrice();
expect(z.instanceof(TinyBig).safeParse(gasPrice).success).toBe(true);
});
});

0 comments on commit e535cb5

Please sign in to comment.