Skip to content

Commit

Permalink
feat: eliminate read only ethereum handler, export ethereum functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Polybius93 committed Jul 19, 2024
1 parent 5e7b1d5 commit 7e70b4f
Show file tree
Hide file tree
Showing 8 changed files with 263 additions and 198 deletions.
22 changes: 22 additions & 0 deletions src/constants/ethereum-constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,23 @@ export const ethereumArbitrum: EthereumNetwork = {
defaultNodeURL: 'https://arb1.arbitrum.io/rpc',
};

const ethereumHardhat: EthereumNetwork = {
name: 'Hardhat',
displayName: 'Hardhat',
id: EthereumNetworkID.Hardhat,
defaultNodeURL: 'http://localhost:8545',
};

export const supportedEthereumNetworks: EthereumNetwork[] = [
ethereumArbitrumSepolia,
ethereumArbitrum,
ethereumHardhat,
];

export const hexChainIDs: { [key in EthereumNetworkID]: string } = {
[EthereumNetworkID.ArbitrumSepolia]: '0x66eee',
[EthereumNetworkID.Arbitrum]: '0xa4b1',
[EthereumNetworkID.Hardhat]: '0x7a69',
};

export const addNetworkParams = {
Expand Down Expand Up @@ -55,6 +64,19 @@ export const addNetworkParams = {
blockExplorerUrls: ['https://arbiscan.io/'],
},
],
[EthereumNetworkID.Hardhat]: [
{
chainId: '31337',
rpcUrls: ['http://localhost:8545'],
chainName: 'Hardhat',
nativeCurrency: {
name: 'ETH',
symbol: 'ETH',
decimals: 18,
},
blockExplorerUrls: [],
},
],
};

export const GITHUB_SOLIDITY_URL = 'https://raw.githubusercontent.com/DLC-link/dlc-solidity';
Expand Down
167 changes: 160 additions & 7 deletions src/functions/ethereum/ethereum-functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
} from '../../constants/ethereum-constants.js';
import { EthereumError } from '../../models/errors.js';
import {
DLCEthereumContractName,
DLCEthereumContracts,
DLCSolidityBranchName,
EthereumDeploymentPlan,
Expand Down Expand Up @@ -88,10 +89,14 @@ export function getProvider(
}
}

export function getEthereumontract(
export function getEthereumContract(
ethereumDeploymentPlans: EthereumDeploymentPlan[],
contractName: string,
signerOrProvider: Wallet | providers.JsonRpcSigner | providers.JsonRpcProvider
signerOrProvider:
| Wallet
| providers.JsonRpcSigner
| providers.JsonRpcProvider
| providers.WebSocketProvider
): Contract {
try {
const contractDeploymentPlan = ethereumDeploymentPlans.find(
Expand All @@ -118,8 +123,8 @@ export function getEthereumContracts(
ethereumDeploymentPlans: EthereumDeploymentPlan[],
signer: Wallet | providers.JsonRpcSigner
): DLCEthereumContracts {
const dlcManagerContract = getEthereumontract(ethereumDeploymentPlans, 'DLCManager', signer);
const dlcBTCContract = getEthereumontract(ethereumDeploymentPlans, 'DLCBTC', signer);
const dlcManagerContract = getEthereumContract(ethereumDeploymentPlans, 'DLCManager', signer);
const dlcBTCContract = getEthereumContract(ethereumDeploymentPlans, 'DLCBTC', signer);

return { dlcManagerContract, dlcBTCContract };
}
Expand All @@ -128,17 +133,17 @@ export function getReadOnlyEthereumContracts(
ethereumDeploymentPlans: EthereumDeploymentPlan[],
readOnlyProvider: providers.JsonRpcProvider
): { protocolContract: Contract; dlcManagerContract: Contract; dlcBTCContract: Contract } {
const protocolContract = getEthereumontract(
const protocolContract = getEthereumContract(
ethereumDeploymentPlans,
'TokenManager',
readOnlyProvider
);
const dlcManagerContract = getEthereumontract(
const dlcManagerContract = getEthereumContract(
ethereumDeploymentPlans,
'DLCManager',
readOnlyProvider
);
const dlcBTCContract = getEthereumontract(ethereumDeploymentPlans, 'DLCBTC', readOnlyProvider);
const dlcBTCContract = getEthereumContract(ethereumDeploymentPlans, 'DLCBTC', readOnlyProvider);

return { protocolContract, dlcManagerContract, dlcBTCContract };
}
Expand All @@ -155,3 +160,151 @@ export async function getLockedBTCBalance(userVaults: RawVault[]): Promise<numbe
throw new EthereumError(`Could not fetch locked BTC balance: ${error}`);
}
}

export async function getReadOnlyContract(
contractName: DLCEthereumContractName,
ethereumNetwork: EthereumNetwork,
ethereumContractBranch: DLCSolidityBranchName,
rpcEndpoint?: string
): Promise<Contract> {
try {
const dlcManagerContractDeploymentPlan = await fetchEthereumDeploymentPlan(
contractName,
ethereumNetwork,
ethereumContractBranch,
GITHUB_SOLIDITY_URL
);

const provider = getProvider(rpcEndpoint ?? ethereumNetwork.defaultNodeURL);

return new Contract(
dlcManagerContractDeploymentPlan.contract.address,
dlcManagerContractDeploymentPlan.contract.abi,
provider
);
} catch (error) {
throw new EthereumError(`Could not fetch DLCManager Contract: ${error}`);
}
}

export async function isUserWhitelisted(
dlcManagerContract: Contract,
userAddress: string
): Promise<boolean> {
try {
return await dlcManagerContract.isWhitelisted(userAddress);
} catch (error) {
throw new EthereumError(`Could not check if User is whitelisted: ${error}`);
}
}

export async function isWhitelistingEnabled(dlcManagerContract: Contract): Promise<boolean> {
return await dlcManagerContract.whitelistingEnabled();
}

export async function getAttestorGroupPublicKey(dlcManagerContract: Contract): Promise<string> {
try {
const attestorGroupPubKey = await dlcManagerContract.attestorGroupPubKey();
if (!attestorGroupPubKey)
throw new Error('Attestor Group Public key is not set on DLCManager Contract');
return attestorGroupPubKey;
} catch (error) {
throw new EthereumError(`Could not fetch Attestor Public Key: ${error}`);
}
}

export async function getContractVaults(
dlcManagerContract: Contract,
amount: number = 50
): Promise<RawVault[]> {
try {
let totalFetched = 0;
const allVaults: RawVault[] = [];

let shouldContinue = true;
while (shouldContinue) {
const fetchedVaults: RawVault[] = await dlcManagerContract.getAllDLCs(
totalFetched,
totalFetched + amount
);

allVaults.push(...fetchedVaults);

totalFetched += amount;
shouldContinue = fetchedVaults.length === amount;
}

return allVaults;
} catch (error) {
throw new EthereumError(`Could not fetch All Vaults: ${error}`);
}
}

export async function getAllAddressVaults(
dlcManagerContract: Contract,
ethereumAddress: string
): Promise<RawVault[]> {
try {
return await dlcManagerContract.getAllVaultsForAddress(ethereumAddress);
} catch (error) {
throw new EthereumError(`Could not fetch User Vaults: ${error}`);
}
}

export async function getRawVault(
dlcManagerContract: Contract,
vaultUUID: string
): Promise<RawVault> {
try {
const vault: RawVault = await dlcManagerContract.getVault(vaultUUID);
if (!vault) throw new EthereumError('Vault not found');
return vault;
} catch (error) {
throw new EthereumError(`Could not fetch Vault: ${error}`);
}
}

export async function setupVault(dlcManagerContract: Contract): Promise<any | undefined> {
try {
await dlcManagerContract.callStatic.setupVault();
const transaction = await dlcManagerContract.setupVault();
return await transaction.wait();
} catch (error) {
throw new EthereumError(`Could not Setup Vault: ${error}`);
}
}

export async function withdraw(
dlcManagerContract: Contract,
vaultUUID: string,
withdrawAmount: bigint
) {
try {
await dlcManagerContract.callStatic.withdraw(vaultUUID, withdrawAmount);
const transaction = await dlcManagerContract.withdraw(vaultUUID, withdrawAmount);
return await transaction.wait();
} catch (error) {
throw new EthereumError(`Could not Withdraw: ${error}`);
}
}

export async function getAddressDLCBTCBalance(
dlcBTCContract: Contract,
ethereumAddress: string
): Promise<number | undefined> {
try {
const ethereumAddressBalance = await dlcBTCContract.balanceOf(ethereumAddress);
return ethereumAddressBalance.toNumber();
} catch (error) {
throw new EthereumError(`Could not fetch dlcBTC balance: ${error}`);
}
}

export async function getDLCBTCTotalSupply(dlcBTCContract: Contract): Promise<number> {
try {
const totalSupply = await dlcBTCContract.totalSupply();
return totalSupply.toNumber();
} catch (error: any) {
throw new EthereumError(`Could not fetch Total Supply: ${error}`);
}
}
7 changes: 1 addition & 6 deletions src/functions/ethereum/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1 @@
import {
fetchEthereumDeploymentPlan,
fetchEthereumDeploymentPlansByNetwork,
} from '../ethereum/ethereum-functions.js';

export { fetchEthereumDeploymentPlan, fetchEthereumDeploymentPlansByNetwork };
export * from './ethereum-functions.js';
2 changes: 0 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@ import { LedgerDLCHandler } from './dlc-handlers/ledger-dlc-handler.js';
import { PrivateKeyDLCHandler } from './dlc-handlers/private-key-dlc-handler.js';
import { SoftwareWalletDLCHandler } from './dlc-handlers/software-wallet-dlc-handler.js';
import { EthereumHandler } from './network-handlers/ethereum-handler.js';
import { ReadOnlyEthereumHandler } from './network-handlers/read-only-ethereum-handler.js';
import { ProofOfReserveHandler } from './proof-of-reserve-handlers/proof-of-reserve-handler.js';

export {
PrivateKeyDLCHandler,
LedgerDLCHandler,
SoftwareWalletDLCHandler,
EthereumHandler,
ReadOnlyEthereumHandler,
ProofOfReserveHandler,
};
7 changes: 7 additions & 0 deletions src/models/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ export class EthereumError extends Error {
}
}

export class EthereumHandlerError extends Error {
constructor(message: string) {
super(message);
this.name = 'EthereumHandlerError';
}
}

export class BitcoinError extends Error {
constructor(message: string) {
super(message);
Expand Down
9 changes: 9 additions & 0 deletions src/models/ethereum-models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export interface EthereumNetwork {
export enum EthereumNetworkID {
ArbitrumSepolia = '421614',
Arbitrum = '42161',
Hardhat = '31337',
}

export enum VaultState {
Expand Down Expand Up @@ -51,6 +52,14 @@ export interface EthereumDeploymentPlan {
contract: EthereumContract;
}

export interface DetailedEvent {
from: string;
to: string;
value: number;
timestamp: number;
txHash: string;
}

export interface DLCEthereumContracts {
dlcManagerContract: Contract;
dlcBTCContract: Contract;
Expand Down
Loading

0 comments on commit 7e70b4f

Please sign in to comment.