Skip to content

Commit

Permalink
feat: add deployment fetching by network name (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
Polybius93 authored May 31, 2024
1 parent c6ef297 commit c118167
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 14 deletions.
62 changes: 62 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1 +1,63 @@
# Coming Soon

## Ethereum

### Interacting with the DLC Smart Contracts

TThe DLC-related smart contracts are currently deployed on the Arbitrum and Arbitrum Sepolia chains. To interact with these contracts and mint dlcBTC Tokens, you'll need to use an instance of the EthereumHandler class. This object facilitates interaction with the deployed smart contracts.

The three contracts you'll need to interact with are named: 'TokenManager', 'DLCManager', and 'DLCBTC'.

To create an EthereumHandler object, you need to provide the following parameters:

- Deployment plans for all three contracts
- The Ethereum Private Key of your Wallet or a Provider for an external Ethereum wallet (e.g., MetaMask)
- The Ethereum Node API URL for write interactions
- [OPTIONAL] The Ethereum Node API URL for read interactions (if not provided, the write API will be used for reading as well)

```ts
import { EthereumHandler } from 'dlc-btc-lib';

const ethereumHandler = new EthereumHandler(
deploymentPlans, // Deployment plans for all three contracts
ethereumPrivateKey, // The Ethereum Private Key of your wallet or a provider for an external Ethereum wallet (e.g., MetaMask)
ethereumNodeAPI, // The Ethereum Node API URL for write interactions
ethereumReadOnlyNodeAPI // [OPTIONAL] The Ethereum Node API URL for read interactions
);
```

After the Ethereum Handler is setup, you can interact with the smart contracts using the following methods:

```ts
async getAllVaults(): Promise<RawVault[]> // Returns all the User's Vaults
async getRawVault(vaultUUID: string): Promise<RawVault> // Returns the Vault with the specified UUID
async setupVault(bitcoinDepositAmount: number): Promise<any | undefined> // Sets up a new Vault and returns a Transaction Receipt
async closeVault(vaultUUID: string): Promise<any | undefined> // Closes the Vault with the specified UUID and returns a Transaction Receipt
async getDLCBTCBalance(): Promise<number | undefined> // Returns the User's dlcBTC balance
async getAttestorGroupPublicKey(): Promise<string> // Returns the Group Public Key of the Attestors for Bitcoin transactions
```

### Reading the DLC Smart Contracts

If you would like to only read the data from the smart contracts, you can use the ReadOnlyEthereumHandler class. This object facilitates reading data from the deployed smart contracts.

To create an ReadOnlyEthereumHandler object, you need to provide the following parameters:

- Deployment plans for all three contracts
- The Ethereum Node API URL for read interactions

```ts
const readOnlyEthereumHandler = new ReadOnlyEthereumHandler(
deploymentPlans, // Deployment plans for all three contracts
ethereumNodeAPI // The Ethereum Node API URL for read interactions
);
```

After the Read Only Ethereum Handler is setup, you can interact with the smart contracts using the following methods:

```ts
async getAttestorGroupPublicKey(): Promise<string> // Returns the Group Public Key of the Attestors for Bitcoin transactions
async getContractTransferEvents(): Promise<Event[]> // Returns all the transfer events from the DLCBTC Contract
async getContractTotalSupply(): Promise<number> // Returns the total supply of dlcBTC Tokens
async getContractFundedVaults(amount: number = 50): Promise<RawVault[]> // Returns all funded Vaults
```
10 changes: 9 additions & 1 deletion src/constants/ethereum-constants.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { EthereumNetwork, EthereumNetworkID } from '../models/ethereum-models.js';
import {
DLCEthereumContractName,
EthereumNetwork,
EthereumNetworkID,
} from '../models/ethereum-models.js';

export const ethereumArbitrumSepolia: EthereumNetwork = {
name: 'ArbSepolia',
Expand Down Expand Up @@ -52,3 +56,7 @@ export const addNetworkParams = {
},
],
};

export const GITHUB_SOLIDITY_URL = 'https://raw.githubusercontent.com/DLC-link/dlc-solidity';

export const dlcContractNames: DLCEthereumContractName[] = ['TokenManager', 'DLCManager', 'DLCBTC'];
16 changes: 5 additions & 11 deletions src/example.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ import { LEDGER_APPS_MAP } from './constants/ledger-constants.js';
import { LedgerDLCHandler } from './dlc-handlers/ledger-dlc-handler.js';
import { PrivateKeyDLCHandler } from './dlc-handlers/private-key-dlc-handler.js';
import { broadcastTransaction } from './functions/bitcoin/bitcoin-request-functions.js';
import { fetchEthereumDeploymentPlan } from './functions/ethereum/ethereum-functions.js';
import {
fetchEthereumDeploymentPlan,
fetchEthereumDeploymentPlansByNetwork,
} from './functions/ethereum/ethereum-functions.js';
import { getLedgerApp } from './functions/hardware-wallet/ledger-functions.js';
import { EthereumHandler } from './network-handlers/ethereum-handler.js';
import { ReadOnlyEthereumHandler } from './network-handlers/read-only-ethereum-handler.js';
Expand Down Expand Up @@ -215,16 +218,7 @@ async function runFlowWithLedger() {

async function runProofOfReserveCalculation() {
// Fetch Ethereum Contract Deployment Plans
const deploymentPlans = await Promise.all(
['TokenManager', 'DLCManager', 'DLCBTC'].map(contractName => {
return fetchEthereumDeploymentPlan(
contractName,
ethereumArbitrumSepolia,
EXAMPLE_ETHEREUM_TESTNET_GITHUB_DEPLOYMENT_PLAN_BRANCH,
EXAMPLE_ETHEREUM_GITHUB_DEPLOYMENT_PLAN_ROOT_URL
);
})
);
const deploymentPlans = await fetchEthereumDeploymentPlansByNetwork('arbitrum-sepolia-testnet');

// Setup Read-Only Ethereum Handler
const ethereumHandler = new ReadOnlyEthereumHandler(deploymentPlans, EXAMPLE_ETHEREUM_NODE_API);
Expand Down
45 changes: 45 additions & 0 deletions src/functions/ethereum/ethereum-functions.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
import { Contract, Wallet, providers } from 'ethers';

import {
GITHUB_SOLIDITY_URL,
dlcContractNames,
ethereumArbitrum,
ethereumArbitrumSepolia,
} from '../../constants/ethereum-constants.js';
import { EthereumError } from '../../models/errors.js';
import {
DLCEthereumContracts,
DLCSolidityBranchName,
EthereumDeploymentPlan,
EthereumNetwork,
RawVault,
SupportedNetworks,
VaultState,
} from '../../models/ethereum-models.js';

Expand All @@ -31,6 +39,43 @@ export async function fetchEthereumDeploymentPlan(
}
}

export async function fetchEthereumDeploymentPlansByNetwork(
network: SupportedNetworks
): Promise<EthereumDeploymentPlan[]> {
try {
let ethereumNetwork: EthereumNetwork;
let deploymentBranch: DLCSolidityBranchName;
switch (network) {
case 'arbitrum':
ethereumNetwork = ethereumArbitrum;
deploymentBranch = 'dev';
break;
case 'arbitrum-sepolia-testnet':
ethereumNetwork = ethereumArbitrumSepolia;
deploymentBranch = 'testnet-rolling';
break;
case 'arbitrum-sepolia-devnet':
ethereumNetwork = ethereumArbitrumSepolia;
deploymentBranch = 'dev';
break;
default:
throw new Error('Unsupported Network');
}
return Promise.all(
dlcContractNames.map(async contractName => {
return await fetchEthereumDeploymentPlan(
contractName,
ethereumNetwork,
deploymentBranch,
GITHUB_SOLIDITY_URL
);
})
);
} catch (error) {
throw new EthereumError(`Could not fetch Ethereum Deployment Plans: ${error}`);
}
}

export function getProvider(
rpcEndpoint: string
): providers.JsonRpcProvider | providers.WebSocketProvider {
Expand Down
7 changes: 5 additions & 2 deletions src/functions/ethereum/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import { fetchEthereumDeploymentPlan } from '../ethereum/ethereum-functions.js';
import {
fetchEthereumDeploymentPlan,
fetchEthereumDeploymentPlansByNetwork,
} from '../ethereum/ethereum-functions.js';

export { fetchEthereumDeploymentPlan };
export { fetchEthereumDeploymentPlan, fetchEthereumDeploymentPlansByNetwork };
4 changes: 4 additions & 0 deletions src/models/ethereum-models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,7 @@ export interface DLCReadOnlyEthereumContracts {
dlcManagerContract: Contract;
dlcBTCContract: Contract;
}

export type SupportedNetworks = 'arbitrum' | 'arbitrum-sepolia-testnet' | 'arbitrum-sepolia-devnet';
export type DLCEthereumContractName = 'TokenManager' | 'DLCManager' | 'DLCBTC';
export type DLCSolidityBranchName = 'dev' | 'testnet-rolling';

0 comments on commit c118167

Please sign in to comment.