Total Value Locked - AtomicLoans/atomicloans-rfc GitHub Wiki

Intro

Calculating Total Value Locked (TVL) is not as straightforward as some other protocols completely built on Ethereum.

In Atomic Loans, there is two bitcoin collateral addresses for every loan (refundable and seizable collateral).

Contract addresses

Markets:

DAI

  • Funds: 0x7791cF9a85072698e9B805eb8156EC1e9c3fc724
  • Loans: 0xa25Ad02862756680Ee8aE7aa9ccC37D3d3F75A4C
  • Sales: 0x3171781bcfCd9E111225CDB42f33E856BA9F7A5a
  • Collateral: 8be077228f4e8977a2366653a75c0eb3d68d86b3
  • P2WSH: 0x095925A67EDE4FE0D794f7797342528B98C7DA15

USDC

  • Funds: 0x3528C164e3fCA20E2333Cf58Ab4B1c99DeF83347
  • Loans: 0x20233a2095787DAC434F20f8954d3758986EF30E
  • Sales: 0xf30Cb0Ae1879b18dEb48932A8a6F362e5789EE01
  • Collateral: 0xacce090abee68402a2fb8e3acbc31b58a9341466
  • P2WSH: 0x1C6148Cb6EED725d8F2F01b4F24040a855B40191

Calculating total stablecoins locked in a market

To calculate the total amount of stablecoins locked in a market, simply call Funds.tokenMarketLiquidity()

Calculating total Bitcoin locked in a market

To get the refundable collateral address for a specific loan, call P2WSH.getP2WSH(loanId, false), which will return two values, the redeem script and the pubkeyhash.

For example, if you go to: https://etherscan.io/address/0x095925A67EDE4FE0D794f7797342528B98C7DA15#readContract and call

P2WSH.getP2WSH(0x0000000000000000000000000000000000000000000000000000000000000001, false)

the response will be:

script: 0x63820120877ca820d3ea1cbf2b52f1e6c8dce97886f0deb6cc19198a4e698c52699c8a6d81b88b13879352877c820120877ca820c1d47fb7af455869979bb2ff3a54388d41ea97dcc50dc1dcfa4bddf07a8108dd879352879351a26976a9149bb1e018d3f5645d34f3b2b8ee94557d26409dac88ac676352210219f611ac366dcd431e54e6f2f6c2e9b9568963fae6a68652d6eba5a4d2711ee92103bb4bf879fcdb250917c79e8a87949497951f0bf4a27b0a50bc056377af75ad502103624022b8923f31746732c8b78dd653e49ad502cde1be7631a65c0d6718f4841153ae676382012088a820221f57b1a4eb990a679b943b42609f4cd0b16c311d5baefad0a6d0a43ef8a8158804d566035fb17576a9149bb1e018d3f5645d34f3b2b8ee94557d26409dac88ac6704d509065fb17576a9149bb1e018d3f5645d34f3b2b8ee94557d26409dac88ac686868

pubkh: 0x7190e8a2c0519a9e70fc250eeb0bdeecef20dee20496517c04d224d4af568c89

Convert script / pubkeyhash to address

To convert this value, into the refundable bitcoin collateral address, you can use a library like bitcoinjs-lib

For example using the redeem script:

const bitcoin = require('bitcoinjs-lib')

const script = '63820120877ca820d3ea1cbf2b52f1e6c8dce97886f0deb6cc19198a4e698c52699c8a6d81b88b13879352877c820120877ca820c1d47fb7af455869979bb2ff3a54388d41ea97dcc50dc1dcfa4bddf07a8108dd879352879351a26976a9149bb1e018d3f5645d34f3b2b8ee94557d26409dac88ac676352210219f611ac366dcd431e54e6f2f6c2e9b9568963fae6a68652d6eba5a4d2711ee92103bb4bf879fcdb250917c79e8a87949497951f0bf4a27b0a50bc056377af75ad502103624022b8923f31746732c8b78dd653e49ad502cde1be7631a65c0d6718f4841153ae676382012088a820221f57b1a4eb990a679b943b42609f4cd0b16c311d5baefad0a6d0a43ef8a8158804d566035fb17576a9149bb1e018d3f5645d34f3b2b8ee94557d26409dac88ac6704d509065fb17576a9149bb1e018d3f5645d34f3b2b8ee94557d26409dac88ac686868'

const refundableAddress = bitcoin.payments.p2wsh({ redeem: { output: Buffer.from(script, 'hex'), network: bitcoin.networks.bitcoin }, network: bitcoin.networks.bitcoin }).address

Or you can also do it using the pubkeyhash

const bitcoin = require('bitcoinjs-lib')

const pubkh = `7190e8a2c0519a9e70fc250eeb0bdeecef20dee20496517c04d224d4af568c89`

const refundableAddress = bitcoin.payments.p2wsh({ hash: Buffer.from(pubkh, 'hex'), network: bitcoin.networks.bitcoin }).address

And the refundable address is: bc1qwxgw3gkq2xdfuu8uy58wkz77anhjphhzqjt9zlqy6gjdft6k3jys8dp9el

To get the seizable collateral address for a specific loan, call P2WSH.getP2WSH(loanId, true), and use the returned script and pubkeyhash to get calculate the address using the method above.