summaryrefslogtreecommitdiff
path: root/bin/xchg.buy
blob: a0a3a8ade8353c396256145adb5a35114e7d9d77 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#!/usr/bin/env bash
nick=$1
amount=$2
target=$3
source=$4
if [ "$nick" = "" ];then
  echo missing argument for nick
  exit 1
fi
if [ ! -d /var/cache/segfault/xchg/$nick/ ];then
  /home/segfault/bin/xchg.init $nick
fi
if [ "$amount" = "" ];then
  echo missing argument for amount
  exit 1
fi
if [ "$target" = "" ];then
  echo missing argument for target currency
  exit 1
fi
if [ "$source" = "" ];then
  echo missing argument for source currency
  exit 1
fi

rate=1

#if [ $target != BTC ];then
#  rate2=$(/home/segfault/bin/xchg.getrate $target)
#  if [ "$rate2" = "null" ];then
#    echo "invalid currency type $target used"
#    exit 1
#  fi
#  rate=$(echo | awk "{print $rate / $rate2}")
#fi

#if [ $source != BTC ];then
#  rate2=$(/home/segfault/bin/xchg.getrate $source)
#  if [ "$rate2" = "null" ];then
#    echo "invalid currency type $source used"
#    exit 1
#  fi
#  rate=$(echo | awk "{print $rate * $rate2}")
#fi

rate=$(/home/segfault/bin/xchg.getdoublerate $source $target)

walletsrc=$(cat /var/cache/segfault/xchg/$nick/$source 2>&-)
walletdst=$(cat /var/cache/segfault/xchg/$nick/$target 2>&-)

if [ "$walletsrc" = "" ];then
  walletsrc=0
fi
if [ "$walletdst" = "" ];then
  walletdst=0
fi

### if other types of ways of specifying how much are added, remember to set both srcamt and amount.
if [ $amount = "all" ];then
  amount="100%"
fi
if grep '%' <<< "$amount" 2>&1 > /dev/null;then
  percentage="$(printf "%s\n" "$amount" | tr -cd '0-9.')"
  srcamt="$(echo | awk "{print ($percentage / 100) * $walletsrc}")"
  amount=$(echo | awk "{print $srcamt / $rate}")
else
  srcamt=$(echo | awk "{print $amount * $rate}")
fi

if [ "$srcamt" = "" ];then
  srcamt=0
fi

echo trying to buy $amount of $target for $srcamt $source...


postwalletsrc=$(echo | awk "{print $walletsrc - $srcamt}")

if printf "%s\n" $postwalletsrc | grep ^- 2>&1 > /dev/null;then
  echo you, $1, would need to have $srcamt $source to buy $amount $target. you only have $walletsrc $source.
  exit 1
fi

### if we got here we have enough money... guess make the purchase
#echo updating your source wallet...
echo $(echo | awk "{print $walletsrc - $srcamt}") > /var/cache/segfault/xchg/$nick/$source
#echo updating your target wallet...
echo $(echo | awk "{print $walletdst + $amount}") > /var/cache/segfault/xchg/$nick/$target
echo done