btcpayserver / btcpayserver-docker

Docker resources for hosting BTCPayServer easily
MIT License
590 stars 364 forks source link

Addition of custom FIAT Currency value (Chilean Unidad de Fomento) #618

Open Goro2030 opened 2 years ago

Goro2030 commented 2 years ago

I have a server in Chile, and I would like to use a local pseudo-currency that we have called "Chilean Unit of Account" (UF for short, Unidad de Fomento in spanish). We of course have the Chilean Peso ( CLP in BTCPay ), but some transactions are done in UF's.

The UF has an official exchange rate into Chilean Pesos that's dictated by the government and it changes daily.

I can easily get the value of one UF by doing a search in google, or using the GoogleFinance API from within Google Sheets, but I was wondering if I can add a "custom FIAT currency" in BTCPayServer, and tell BTCPay how to find out it's quote?

I currently get it for other purposes with this API call: =IMPORTJSON("https://mindicador.cl/api","/uf/valor","noHeaders")

Goro2030 commented 2 years ago

ANy ideas?

leesalminen commented 2 years ago

Off topic, but I lived in Chile for years and still don't understand why UF exists and what purpose it serves. :P los chilenos wn!

Goro2030 commented 2 years ago

Off topic, but I lived in Chile for years and still don't understand why UF exists and what purpose it serves. :P los chilenos wn!

But it still unfortunatelly exists and we need to deal with it, right? :)

AdiuvoInc commented 2 years ago

Off topic, but I lived in Chile for years and still don't understand why UF exists and what purpose it serves. :P los chilenos wn!

UF helps a lot when you have to make long term contracts and protect the transactional value against inflation, don't be wn! :D

Goro2030 commented 2 years ago

@NicolasDorier , any chance to assign/review this Feature request?

Goro2030 commented 2 years ago

@NicolasDorier , any chance to assign/review this Feature request?

@NicolasDorier ?

NicolasDorier commented 2 years ago

hey @Goro2030 sorry for taking time about this, our currency field is free typing, so you can enter what you want. We can add the currency in the suggestion list though.

The problem is that we need to pull out the rate from some supported exchange. So either:

  1. you know an exchange which has UF/BTC available and that we support and use this.
  2. you don't know any exchange doing this and you need to specify manually a fixed exchange rate between one supported pair and the other currency. In this case, you need to used advanced rating rules in your store's setting and configure something like this:
UF_BTC=UF_USD * USD_BTC;
UF_USD=234.49;
USD_BTC=kraken(USD_BTC);

(Or you can also just use a single line like UF_BTC=234.49 * kraken(USD_BTC);) Then BTCPay will use this formula to calculate the rate of UF_BTC. This is just an example, if there are several conversion along the way, and not directly to USD, you can add those.

UF_BTC=UF_CLP * CLP_USD * USD_BTC;
UF_CLP=234.49;
CLP_USD=CLP_BTC * BTC_USD;
CLP_BTC=someotherexchange(CLP_BTC);
USD_BTC=kraken(USD_BTC);
  1. If your rate isn't fixed but dynamic, you can provide my the source you want to pull it from and I can check if I can code it for next release.
Goro2030 commented 2 years ago

hey @NicolasDorier ! Thanks for following up on this.

The UF is not a real currency, but an "unit of account" made up by the government to serve as an inflation adjusted unit. The citizens use it to create contracts that automatically updates by inflation ( like a subscription service ).

On your 3 options:

  1. Exchanges do not handle UF's, just Chilean Pesos ( CLP )
  2. The rate fluctuates daily, it's not fixed.
  3. This is the option we need to implement.

All the API calls that the government entities provide are "API KEY" based, for which one needs to register for.

The call is like this: https://api.cmfchile.cl/api-sbifv3/recursos_api/uf?apikey=&formato=xml

The response, for the value for TODAY is 32.518,72 ( over 32 thousand Chilean pesos! ). Then if a product costs 1 UF, the system will have to translate that to CLP ( x 32518 ), and then look up that amount at the exchange (for Chile you developed for us the CryptoMarket API call ) under BTC_CLP for 32518 CLPs to get the BTC value for the invoice.

<IndicadoresFinancieros xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://api.sbif.cl" SchemaVersion="1.0" xsi:schemaLocation="http://api.sbif.cl http://api.sbif.cl/IndicadoresFinancieros-v1.0.xsd">
    <UFs>
      <UF>
        <Fecha>2022-05-20</Fecha>
        <Valor>32.518,72</Valor>
      </UF>
    </UFs>
  <UTMs/>
  <TABs/>
  <IPCs/>
  <Dolares/>
  <Euros/>
  <TMCs/>
  <TIPs/>
  <Perfiles/>
</IndicadoresFinancieros>

We can do 2 things on the API KEY. I can give you mine, or you can get your own?