citadel

My dotfiles, scripts and nix configs
git clone git://jb55.com/citadel
Log | Files | Refs | README | LICENSE

dca.nix (1164B)


      1 { pkgs, to, bcli, addr }:
      2 
      3 pkgs.writeScript "dca"
      4 ''
      5 #!${pkgs.bash}/bin/bash
      6 
      7 set -e
      8 
      9 keybasesock=/run/user/1000/keybase/keybased.sock
     10 wallet=''${BTCDCA_WALLET:-cc}
     11 user=''${BTCDCA_RPCUSER:-rpcuser}
     12 pass=''${BTCDCA_RPCPASS:-rpcpass}
     13 amount=''${BTCDCA_AMOUNT:-$(cat /home/jb55/var/dca-amount)}
     14 pair=''${BTCDCA_PAIR:-XXBTZCAD}
     15 keybaseto=''${BTCDCA_KEYBASETO:-${to}}
     16 
     17 price=$(${pkgs.curl}/bin/curl -sL "https://api.kraken.com/0/public/Ticker?pair=$pair" | ${pkgs.jq}/bin/jq -r ".result.$pair.a[0]")
     18 invid=$(dd if=/dev/urandom bs=1 count=4 | ${pkgs.xxd}/bin/xxd -p | ${pkgs.libbitcoin-explorer}/bin/bx base58-encode)
     19 
     20 address=${addr}
     21 
     22 satsdec=$(${pkgs.bcalc}/bin/bcalc -n --price $price $amount fiat to sats)
     23 sats=''${satsdec%.*}
     24 btc=$(${pkgs.bcalc}/bin/bcalc -n $sats sats to btc)
     25 
     26 msg=$(printf "Please send %s BTC to %s for \$%s @ \$%s\ne-transfer password is %s\nplease ACK to confirm. e-transfer will be sent when ACKed.\n" \
     27        "$btc" "$address" "$amount" "$price" "$invid")
     28 
     29 if [ ! -e $keybasesock ]; then
     30    ${pkgs.systemd}/bin/systemctl --user restart keybase
     31 fi
     32 
     33 for user in $keybaseto; do
     34     ${pkgs.keybase}/bin/keybase chat send "$user" "$msg"
     35 done
     36 ''