Skip to content

Commit

Permalink
Expose TBTC v2 token total supply getter (#558)
Browse files Browse the repository at this point in the history
Refs: #552

Here we expose a way of getting the total supply of the TBTC v2 token
for an arbitrary block, using the `tbtc-v2.ts` library. This ability
will be leveraged by the monitoring system to raise alerts in case of
significant supply changes.
  • Loading branch information
pdyraga authored Feb 27, 2023
2 parents b7d5aa2 + 183430c commit df7a36a
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 1 deletion.
2 changes: 1 addition & 1 deletion typescript/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"typechain/"
],
"config": {
"contracts": "./node_modules/@keep-network/ecdsa/artifacts/WalletRegistry.json ./node_modules/@keep-network/tbtc-v2/artifacts/{Bridge,TBTCVault}.json"
"contracts": "./node_modules/@keep-network/ecdsa/artifacts/WalletRegistry.json ./node_modules/@keep-network/tbtc-v2/artifacts/{Bridge,TBTCVault,TBTC}.json"
},
"dependencies": {
"@keep-network/ecdsa": "development",
Expand Down
17 changes: 17 additions & 0 deletions typescript/src/chain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -331,3 +331,20 @@ export interface TBTCVault {
*/
getOptimisticMintingFinalizedEvents: GetEvents.Function<OptimisticMintingFinalizedEvent>
}

/**
* Interface for communication with the TBTC v2 token on-chain contract.
*/
export interface TBTCToken {
/**
* Gets the total supply of the TBTC v2 token. The returned value is in
* ERC 1e18 precision, it has to be converted before using as Bitcoin value
* with 1e8 precision in satoshi.
* @param blockNumber Optional parameter determining the block the total
* supply should be fetched for. If this parameter is not set, the
* total supply is taken for the latest block.
*/
// TODO: Consider adding a custom type to handle conversion from ERC with 1e18
// precision to Bitcoin in 1e8 precision (satoshi).
totalSupply(blockNumber?: number): Promise<BigNumber>
}
25 changes: 25 additions & 0 deletions typescript/src/ethereum.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
Bridge as ChainBridge,
TBTCVault as ChainTBTCVault,
TBTCToken as ChainTBTCToken,
Identifier as ChainIdentifier,
GetEvents,
} from "./chain"
Expand All @@ -17,6 +18,7 @@ import {
import BridgeDeployment from "@keep-network/tbtc-v2/artifacts/Bridge.json"
import WalletRegistryDeployment from "@keep-network/ecdsa/artifacts/WalletRegistry.json"
import TBTCVaultDeployment from "@keep-network/tbtc-v2/artifacts/TBTCVault.json"
import TBTCDeployment from "@keep-network/tbtc-v2/artifacts/TBTC.json"
import { backoffRetrier } from "./backoff"
import {
DepositScriptParameters,
Expand Down Expand Up @@ -47,6 +49,7 @@ import type {
} from "../typechain/Bridge"
import type { WalletRegistry as ContractWalletRegistry } from "../typechain/WalletRegistry"
import type { TBTCVault as ContractTBTCVault } from "../typechain/TBTCVault"
import type { TBTC as ContractTBTC } from "../typechain/TBTC"
import { Hex } from "./hex"
import { NewWalletRegisteredEvent } from "./wallet"

Expand Down Expand Up @@ -960,3 +963,25 @@ export class TBTCVault
})
}
}

/**
* Implementation of the Ethereum TBTC v2 token handle.
*/
export class TBTCToken
extends EthereumContract<ContractTBTC>
implements ChainTBTCToken
{
constructor(config: ContractConfig) {
super(config, TBTCDeployment)
}

// eslint-disable-next-line valid-jsdoc
/**
* @see {ChainTBTCToken#totalSupply}
*/
async totalSupply(blockNumber?: number): Promise<BigNumber> {
return this._instance.totalSupply({
blockTag: blockNumber ?? "latest",
})
}
}
1 change: 1 addition & 0 deletions typescript/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ export {
Bridge as EthereumBridge,
Address as EthereumAddress,
TBTCVault as EthereumTBTCVault,
TBTCToken as EthereumTBTCToken,
} from "./ethereum"

export { Hex } from "./hex"
Expand Down

0 comments on commit df7a36a

Please sign in to comment.