-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Wrap ETH address handling iniside sdk (#445)
Closes: #426 Closes: #428 This PR wraps the ETH address handling inside the SDK and refactores some modules. Here we remove the `Staking` module and split it into 2 separate modules `Account` and `Protocol`. The `Account` module exposes features related to a given Bitcoin account such as initializing deposits, fetching balance etc. The `Protocol` module provides general information related to the Acre protocol such as fees, minimum deposit/withdrawal amount, tvl etc.
- Loading branch information
Showing
16 changed files
with
442 additions
and
457 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,14 @@ | ||
export * from "./lib/bitcoin" | ||
export * from "./lib/contracts" | ||
export * from "./lib/ethereum" | ||
export * from "./lib/utils" | ||
export { DepositStatus } from "./lib/api/TbtcApi" | ||
|
||
export * from "./modules/staking" | ||
export * from "./modules/account" | ||
export { default as Account } from "./modules/account" | ||
|
||
export { default as StakeInitialization } from "./modules/staking" | ||
|
||
export { default as Protocol } from "./modules/protocol" | ||
export * from "./modules/protocol" | ||
|
||
export * from "./acre" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
import { AcreContracts, ChainIdentifier } from "../lib/contracts" | ||
import StakeInitialization from "./staking" | ||
import { toSatoshi } from "../lib/utils" | ||
import Tbtc from "./tbtc" | ||
import AcreSubgraphApi from "../lib/api/AcreSubgraphApi" | ||
import { DepositStatus } from "../lib/api/TbtcApi" | ||
|
||
export { DepositReceipt } from "./tbtc" | ||
|
||
/** | ||
* Represents the deposit data. | ||
*/ | ||
export type Deposit = { | ||
/** | ||
* Unique deposit identifier represented as | ||
* `keccak256(bitcoinFundingTxHash | fundingOutputIndex)`. | ||
*/ | ||
id: string | ||
/** | ||
* Bitcoin transaction hash (or transaction ID) in the same byte order as | ||
* used by the Bitcoin block explorers. | ||
*/ | ||
txHash: string | ||
/** | ||
* Amount of Bitcoin funding transaction. | ||
*/ | ||
amount: bigint | ||
/** | ||
* Status of the deposit. | ||
*/ | ||
status: DepositStatus | ||
/** | ||
* Timestamp when the deposit was initialized. | ||
*/ | ||
timestamp: number | ||
} | ||
|
||
/** | ||
* Module exposing features related to the account. | ||
*/ | ||
export default class Account { | ||
/** | ||
* Acre contracts. | ||
*/ | ||
readonly #contracts: AcreContracts | ||
|
||
/** | ||
* tBTC Module. | ||
*/ | ||
readonly #tbtc: Tbtc | ||
|
||
/** | ||
* Acre subgraph api. | ||
*/ | ||
readonly #acreSubgraphApi: AcreSubgraphApi | ||
|
||
readonly #bitcoinAddress: string | ||
|
||
readonly #ethereumAddress: ChainIdentifier | ||
|
||
constructor( | ||
contracts: AcreContracts, | ||
tbtc: Tbtc, | ||
acreSubgraphApi: AcreSubgraphApi, | ||
account: { bitcoinAddress: string; ethereumAddress: ChainIdentifier }, | ||
) { | ||
this.#contracts = contracts | ||
this.#tbtc = tbtc | ||
this.#acreSubgraphApi = acreSubgraphApi | ||
this.#bitcoinAddress = account.bitcoinAddress | ||
this.#ethereumAddress = account.ethereumAddress | ||
} | ||
|
||
/** | ||
* Initializes the Acre deposit process. | ||
* @param referral Data used for referral program. | ||
* @param bitcoinRecoveryAddress `P2PKH` or `P2WPKH` Bitcoin address that can | ||
* be used for emergency recovery of the deposited funds. If | ||
* `undefined` the bitcoin address from bitcoin provider is used as | ||
* bitcoin recovery address - note that an address returned by bitcoin | ||
* provider must then be `P2WPKH` or `P2PKH`. This property is | ||
* available to let the consumer use `P2SH-P2WPKH` as the deposit owner | ||
* and another tBTC-supported type (`P2WPKH`, `P2PKH`) address as the | ||
* tBTC Bridge recovery address. | ||
* @returns Object represents the deposit process. | ||
*/ | ||
async initializeStake( | ||
referral: number, | ||
bitcoinRecoveryAddress?: string, | ||
): Promise<StakeInitialization> { | ||
// tBTC-v2 SDK will handle Bitcoin address validation and throw an error if | ||
// address is not supported. | ||
const finalBitcoinRecoveryAddress = | ||
bitcoinRecoveryAddress ?? this.#bitcoinAddress | ||
|
||
const tbtcDeposit = await this.#tbtc.initiateDeposit( | ||
this.#ethereumAddress, | ||
finalBitcoinRecoveryAddress, | ||
referral, | ||
) | ||
|
||
return new StakeInitialization(tbtcDeposit) | ||
} | ||
|
||
/** | ||
* @returns Balance of the account's stBTC shares (in 1e18 precision). | ||
*/ | ||
async sharesBalance() { | ||
return this.#contracts.stBTC.balanceOf(this.#ethereumAddress) | ||
} | ||
|
||
/** | ||
* @returns Balance of Bitcoin position in Acre estimated based on the | ||
* account's stBTC shares (in 1e8 satoshi precision). | ||
*/ | ||
async estimatedBitcoinBalance() { | ||
return toSatoshi( | ||
await this.#contracts.stBTC.assetsBalanceOf(this.#ethereumAddress), | ||
) | ||
} | ||
|
||
/** | ||
* @returns All deposits associated with the account. They include all | ||
* deposits: queued, initialized and finalized. | ||
*/ | ||
async getDeposits(): Promise<Deposit[]> { | ||
const subgraphData = await this.#acreSubgraphApi.getDepositsByOwner( | ||
this.#ethereumAddress, | ||
) | ||
|
||
const initializedOrFinalizedDepositsMap = new Map( | ||
subgraphData.map((data) => [data.depositKey, data]), | ||
) | ||
|
||
const tbtcData = await this.#tbtc.getDepositsByOwner(this.#ethereumAddress) | ||
|
||
return tbtcData.map((deposit) => { | ||
const depositFromSubgraph = initializedOrFinalizedDepositsMap.get( | ||
deposit.depositKey, | ||
) | ||
|
||
const amount = toSatoshi( | ||
depositFromSubgraph?.initialAmount ?? deposit.initialAmount, | ||
) | ||
|
||
return { | ||
id: deposit.depositKey, | ||
txHash: deposit.txHash, | ||
amount, | ||
status: deposit.status, | ||
timestamp: deposit.timestamp, | ||
} | ||
}) | ||
} | ||
} |
Oops, something went wrong.