Skip to content
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

938: Implement depositETH and set up the project #4

Merged
merged 14 commits into from
May 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# @pufferfinance/smart-contract-lib
# @pufferfinance/puffer-sdk

Library for interacting with puffer smart contract interface.

Expand All @@ -17,18 +17,18 @@ There are multiple ways to use the package.
Install the dependency through GitHub URL.

```sh
npm install github:pufferfinance/puffer-smart-contract-lib
pnpm install github:pufferfinance/puffer-smart-contract-lib
yarn add github:pufferfinance/puffer-smart-contract-lib
npm install github:pufferfinance/puffer-sdk
pnpm install github:pufferfinance/puffer-sdk
yarn add github:pufferfinance/puffer-sdk
```

Add `#<tag>` (for example, `github:pufferfinance/puffer-smart-contract-lib#1.0.0`) at the end to use a different published tag from GitHub (<https://github.com/PufferFinance/puffer-smart-contract-lib/tags>).
Add `#<tag>` (for example, `github:pufferfinance/puffer-sdk#1.0.0`) at the end to use a different published tag from GitHub (<https://github.com/PufferFinance/puffer-sdk/tags>).

### GitHub Packages

The package is published privately to GitHub Packages. It can be installed by authenticating to GitHub Packages. Please check [GitHub guide for installing a private package](https://docs.github.com/en/packages/working-with-a-github-packages-registry/working-with-the-npm-registry#installing-a-package).

Use `@pufferfinance` as the organization name and `@pufferfinance/smart-contract-lib` as the complete dependency name.
Use `@pufferfinance` as the organization name and `@pufferfinance/puffer-sdk` as the complete dependency name.

### Local

Expand All @@ -41,7 +41,7 @@ pnpm link --global
In the project where you want to use this package as a dependency, run the following command.

```sh
pnpm link --global @pufferfinance/smart-contract-lib
pnpm link --global @pufferfinance/puffer-sdk
```

## Setup
Expand All @@ -60,4 +60,4 @@ pnpm dev

## Release

The release is automated using [release-it](https://github.com/release-it/release-it) and the [`publish.yml`](./.github/workflows/publish.yml) GitHub action. The action can be [dispatched manually](https://github.com/PufferFinance/puffer-smart-contract-lib/actions/workflows/publish.yml) to make the release.
The release is automated using [release-it](https://github.com/release-it/release-it) and the [`publish.yml`](./.github/workflows/publish.yml) GitHub action. The action can be [dispatched manually](https://github.com/PufferFinance/puffer-sdk/actions/workflows/publish.yml) to make the release.
5 changes: 1 addition & 4 deletions dist/main.cjs
Original file line number Diff line number Diff line change
@@ -1,4 +1 @@
'use strict';
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
const d = (e, t) => e + t;
exports.add = d;
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const d=(e,t)=>e+t;exports.add=d;
4 changes: 3 additions & 1 deletion dist/main.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
const t = (d, o) => d + o;
export { t as add };
export {
t as add
};
82 changes: 82 additions & 0 deletions lib/api/puffer-client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import {
Address,
PublicClient,
WalletClient,
createPublicClient,
createWalletClient,
custom,
getContract,
http,
} from 'viem';
import { Chain, VIEM_CHAINS, ViemChain } from '../chains/constants';
import { CHAIN_ADDRESSES } from '../contracts/addresses';
import { ValueOf } from '../utils/types';
import { CHAIN_ABIS } from '../contracts/abis/abis';
import { AccountError } from '../errors/validation-errors';

export class PufferClient {
private chainAddresses: ValueOf<typeof CHAIN_ADDRESSES>;
private chainAbis: ValueOf<typeof CHAIN_ABIS>;

private viemChain: ViemChain;
private walletClient: WalletClient;
private publicClient: PublicClient;

constructor(chain: Chain) {
this.validateEnvironment();

this.chainAddresses = CHAIN_ADDRESSES[chain];
this.chainAbis = CHAIN_ABIS[chain];
this.viemChain = VIEM_CHAINS[chain];

this.walletClient = createWalletClient({
chain: this.viemChain,
transport: custom(window.ethereum!),
});
this.publicClient = createPublicClient({
chain: this.viemChain,
transport: http(),
});
}

public async requestAddresses() {
return await this.walletClient.requestAddresses();
}

public depositETH(walletAddress: Address) {
const contract = getContract({
address: this.chainAddresses.PufferVault as Address,
abi: this.chainAbis.PufferVaultV2,
client: {
wallet: this.walletClient,
// Public client is needed for simulation.
public: this.publicClient,
},
});

const transact = async (value: bigint) =>
await contract.write.depositETH([walletAddress], {
account: walletAddress,
chain: this.viemChain,
value,
});

const estimate = async () =>
await contract.estimateGas.depositETH([walletAddress], {
account: walletAddress,
});

return { transact, estimate };
}

/**
* Validates that the browser environment is correct.
*/
private validateEnvironment() {
if (!window.ethereum) {
throw new AccountError('JSON-RPC account not accessible.', {
fixMessage: 'Make sure a JSON-RPC wallet is set up in the browser.',
});
}
}
}
16 changes: 16 additions & 0 deletions lib/chains/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { Chain as ViemChain } from 'viem';
import { mainnet, anvil, holesky } from 'viem/chains';

export enum Chain {
Mainnet = 'mainnet',
Holesky = 'holesky',
Anvil = 'anvil',
}

export const VIEM_CHAINS: { [key in Chain]: ViemChain } = {
[Chain.Mainnet]: mainnet,
[Chain.Holesky]: holesky,
[Chain.Anvil]: anvil,
};

export { type ViemChain };
Loading
Loading