Skip to content

Instantly share code, notes, and snippets.

@ralph-pichler

ralph-pichler/cashout.sh Secret

Last active Jul 30, 2021
Embed
What would you like to do?
#!/usr/bin/env bash
[ -z ${DEBUG_API+x} ] && DEBUG_API=http://localhost:1635
[ -z ${MIN_AMOUNT+x} ] && MIN_AMOUNT=10000000000000000
# cashout script for bee >= 0.6.0
# note this is a simple bash script which might not work well or at all on some platforms
# for a more robust interface take a look at https://github.com/ethersphere/swarm-cli
function getPeers() {
curl -s "$DEBUG_API/chequebook/cheque" | jq -r '.lastcheques | .[].peer'
}
function getUncashedAmount() {
curl -s "$DEBUG_API/chequebook/cashout/$1" | jq '.uncashedAmount'
}
function cashout() {
local peer=$1
txHash=$(curl -s -XPOST "$DEBUG_API/chequebook/cashout/$peer" | jq -r .transactionHash)
echo cashing out cheque for $peer in transaction $txHash >&2
}
function cashoutAll() {
local minAmount=$1
for peer in $(getPeers)
do
local uncashedAmount=$(getUncashedAmount $peer)
if (( "$uncashedAmount" > $minAmount ))
then
echo "uncashed cheque for $peer ($uncashedAmount uncashed)" >&2
cashout $peer
fi
done
}
function listAllUncashed() {
for peer in $(getPeers)
do
local uncashedAmount=$(getUncashedAmount $peer)
if (( "$uncashedAmount" > 0 ))
then
echo $peer $uncashedAmount
fi
done
}
case $1 in
cashout)
cashout $2
;;
cashout-all)
cashoutAll $MIN_AMOUNT
;;
uncashed-for-peer)
getUncashedAmount $2
;;
list-uncashed|*)
listAllUncashed
;;
esac
@hlbcal

This comment has been minimized.

Copy link

@hlbcal hlbcal commented Feb 18, 2021

cashout API might respond below JSON, this will cause a dead loop in function cashout()

{
"message": "no prior cashout",
"code": 404
}

@pk27734

This comment has been minimized.

Copy link

@pk27734 pk27734 commented Mar 6, 2021

It seems stuck when a null transaction occurs, what to do then?

@faraggi

This comment has been minimized.

Copy link

@faraggi faraggi commented Mar 11, 2021

I can't PR this thing, so a comment will have to do. Top line has a typo.

should be #!/usr/bin/env and not #1/usr/bin/env
Also, at least on my system, (kubuntu, pretty much out of the box), sh won't work, I had to change it to #!/usr/bin/env bash.

@yaebay

This comment has been minimized.

Copy link

@yaebay yaebay commented Mar 20, 2021

Administrator@wind MINGW64 /f/bee
$ sh cashout.sh
cashout.sh: line 12: [: ==: unary operator expected
cashout.sh: line 34: [: ==: unary operator expected
cashout.sh: line 23: [: ==: unary operator expected
cashout.sh: line 41: let: uncashedAmount=-: syntax error: operand expected (error token is "-")
cashout.sh: line 76: ((: > 0 : syntax error: operand expected (error token is "> 0 ")
cashout.sh: line 12: [: ==: unary operator expected
cashout.sh: line 34: [: ==: unary operator expected
cashout.sh: line 23: [: ==: unary operator expected
cashout.sh: line 41: let: uncashedAmount=-: syntax error: operand expected (error token is "-")
cashout.sh: line 76: ((: > 0 : syntax error: operand expected (error token is "> 0 ")
cashout.sh: line 12: [: ==: unary operator expected
cashout.sh: line 34: [: ==: unary operator expected
cashout.sh: line 23: [: ==: unary operator expected
cashout.sh: line 41: let: uncashedAmount=-: syntax error: operand expected (error token is "-")
cashout.sh: line 76: ((: > 0 : syntax error: operand expected (error token is "> 0 ")
cashout.sh: line 12: [: ==: unary operator expected
cashout.sh: line 34: [: ==: unary operator expected
cashout.sh: line 23: [: ==: unary operator expected
cashout.sh: line 41: let: uncashedAmount=-: syntax error: operand expected (error token is "-")
cashout.sh: line 76: ((: > 0 : syntax error: operand expected (error token is "> 0 ")
cashout.sh: line 12: [: ==: unary operator expected
cashout.sh: line 34: [: ==: unary operator expected
cashout.sh: line 23: [: ==: unary operator expected
cashout.sh: line 41: let: uncashedAmount=-: syntax error: operand expected (error token is "-")
cashout.sh: line 76: ((: > 0 : syntax error: operand expected (error token is "> 0 ")
cashout.sh: line 12: [: ==: unary operator expected
cashout.sh: line 34: [: ==: unary operator expected
cashout.sh: line 23: [: ==: unary operator expected
cashout.sh: line 41: let: uncashedAmount=-: syntax error: operand expected (error token is "-")
cashout.sh: line 76: ((: > 0 : syntax error: operand expected (error token is "> 0 ")
cashout.sh: line 12: [: ==: unary operator expected
cashout.sh: line 34: [: ==: unary operator expected
cashout.sh: line 23: [: ==: unary operator expected
cashout.sh: line 41: let: uncashedAmount=-: syntax error: operand expected (error token is "-")
cashout.sh: line 76: ((: > 0 : syntax error: operand expected (error token is "> 0 ")
f5d***************************************************ced2220c0b219 44971000000000

@gumb0

This comment has been minimized.

Copy link

@gumb0 gumb0 commented Apr 1, 2021

[ -z ${MIN_AMOUNT+x} ] && MIN_AMOUNT=10000000000000000

This line doesn't work for me and always sets the default amount ignoring the command line

@oneoneplus

This comment has been minimized.

Copy link

@oneoneplus oneoneplus commented May 11, 2021

What should users do under Windows 64?

@filwu8

This comment has been minimized.

Copy link

@filwu8 filwu8 commented May 16, 2021

jq: error: syntax error, unexpected ':', expecting $end (Windows cmd shell quoting issues?) at , line 1:
E:\SWARM\cashout.sh
jq: 1 compile error

@tomzhang

This comment has been minimized.

Copy link

@tomzhang tomzhang commented May 18, 2021

this shell only work fine under linux env, and which can not list all uncashed bee directly (may need to make improment)

@2019jack

This comment has been minimized.

Copy link

@2019jack 2019jack commented Jun 12, 2021

@ralph-pichler ./cashout.sh
./cashout.sh: line 40: ((: "244000000000" > 0 : syntax error: operand expected (error token is ""244000000000" > 0 ")

this is wrong ?

@tigerich

This comment has been minimized.

Copy link

@tigerich tigerich commented Jun 13, 2021

After running this script, it returned me a txid which I couldn't find it from goerli explorer....looks like the transaction didn't really get broadcast to the network...which could go wrong here?

@liuweisir

This comment has been minimized.

Copy link

@liuweisir liuweisir commented Jun 19, 2021

$ ./cashout.sh
./cashout.sh: line 40: ((: > 0 : syntax error: operand expected (error token is "> 0 ")
./cashout.sh: line 40: ((: > 0 : syntax error: operand expected (error token is "> 0 ")
./cashout.sh: line 40: ((: > 0 : syntax error: operand expected (error token is "> 0 ")
./cashout.sh: line 40: ((: > 0 : syntax error: operand expected (error token is "> 0 ")
./cashout.sh: line 40: ((: > 0 : syntax error: operand expected (error token is "> 0 ")
./cashout.sh: line 40: ((: > 0 : syntax error: operand expected (error token is "> 0 ")
./cashout.sh: line 40: ((: > 0 : syntax error: operand expected (error token is "> 0 ")
./cashout.sh: line 40: ((: > 0 : syntax error: operand expected (error token is "> 0 ")
./cashout.sh: line 40: ((: > 0 : syntax error: operand expected (error token is "> 0 ")
./cashout.sh: line 40: ((: > 0 : syntax error: operand expected (error token is "> 0 ")

@fab-io

This comment has been minimized.

Copy link

@fab-io fab-io commented Jun 25, 2021

$ ./cashout.sh
./cashout.sh: line 40: ((: > 0 : syntax error: operand expected (error token is "> 0 ")
./cashout.sh: line 40: ((: > 0 : syntax error: operand expected (error token is "> 0 ")

I had the same error and fixed it changing line 14 to this

curl -s "$DEBUG_API/chequebook/cashout/$1" | jq 'if .uncashedAmount then (.uncashedAmount|tonumber) else -1 end'

@hugoQ1

This comment has been minimized.

Copy link

@hugoQ1 hugoQ1 commented Jul 30, 2021

can it work?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment