-
Notifications
You must be signed in to change notification settings - Fork 0
/
transferDelegation.sh
executable file
·131 lines (114 loc) · 4.31 KB
/
transferDelegation.sh
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#!/usr/bin/env bash
# -----------------------------------------------------------------------------
# This script can be used to transfer stake from one pool to another pool.
#
# Usage: ./transferDelegation.sh node_address delegator_private_key from_pool_identity_address to_pool_identity_address amount fee network_name
# -----------------------------------------------------------------------------
POOL_REGISTRY_AMITY_ADDRESS="0xa01b68fa4f947ea4829bebdac148d1f7f8a0be9a8fd5ce33e1696932bef05356"
POOL_REGISTRY_MAINNET_ADDRESS="0xa008e42a76e2e779175c589efdb2a0e742b40d8d421df2b93a8a0b13090c7cc8"
TOOLS_JAR=Tools.jar
return=0
function require_success()
{
if [ $1 -ne 0 ]
then
echo "Failed"
exit 1
fi
}
function echo_state()
{
address="$1"
data="$2"
expected="$3"
payload={\"jsonrpc\":\"2.0\",\"method\":\"eth_call\",\"params\":[{\"to\":\"$address\",\"data\":\"$data\"}],\"id\":1}
response=`curl -s -X POST -H "Content-Type: application/json" --data "$payload" "$node_address"`
encoded_stake="$(echo "$response" | egrep -oh 'result":"0x'"[[:xdigit:]]+" | egrep -oh "0x[[:xdigit:]]+")"
stake="$(java -cp $TOOLS_JAR cli.DecodeReturnResult "BigInteger" "$encoded_stake")"
echo "Current stake = $stake"
}
function capture_event()
{
payload={\"jsonrpc\":\"2.0\",\"method\":\"eth_getTransactionReceipt\",\"params\":[\"$1\"],\"id\":1}
response=`curl -s -X POST -H "Content-Type: application/json" --data "$payload" "$node_address"`
# ADSDelegationTransferred topic
if [[ "$response" =~ (\"0x41445344656c65676174696f6e5472616e736665727265640000000000000000\".+\"id) ]];
then
result=${BASH_REMATCH[0]:70}
echo "Transfer Id = $(( 16#${result:2:64}))"
else
echo "Error! Could not find event log for ADSDelegationTransferred."
exit 1
fi
}
function wait_for_receipt()
{
receipt="$1"
result="1"
while [ "1" == "$result" ]
do
echo " waiting..."
sleep 1
`./rpc.sh --check-receipt-status "$receipt" "$node_address"`
result=$?
if [ "2" == "$result" ]
then
echo "Error! Transaction failed."
exit 1
fi
done
}
function get_nonce(){
address="$1"
payload={\"jsonrpc\":\"2.0\",\"method\":\"eth_getTransactionCount\",\"params\":[\"$address\",\"latest\"],\"id\":1}
response=`curl -s -X POST -H "Content-Type: application/json" --data "$payload" "$node_address"`
nonce_hex="$(echo "$response" | egrep -oh 'result":"0x'"[[:xdigit:]]+" | egrep -oh "0x[[:xdigit:]]+")"
return=$(( 16#${nonce_hex:2} ))
}
if [ $# -ne 7 ]
then
echo "Invalid number of parameters."
echo "Usage: ./transferDelegation.sh node_address(ip:port) delegator_private_key from_pool_identity_address to_pool_identity_address amount fee network_name(amity/mainnet)"
exit 1
fi
node_address="$1"
private_key="$2"
from_pool_address="$3"
to_pool_address="$4"
amount="$5"
fee="$6"
network=$( echo "$7" | tr '[A-Z]' '[a-z]' )
pool_registry_address=
if [[ "$network" = "amity" ]]
then
pool_registry_address=${POOL_REGISTRY_AMITY_ADDRESS}
elif [[ "$network" = "mainnet" ]]
then
pool_registry_address=${POOL_REGISTRY_MAINNET_ADDRESS}
else
echo "Invalid network name. Only amity and mainnet networks are supported."
exit 1
fi
if [ ${#private_key} == 130 ]
then
private_key=${private_key::-64}
fi
delegator_address="$(java -cp $TOOLS_JAR cli.KeyExtractor "$private_key")"
get_nonce "$delegator_address"
nonce="$return"
echo "Using nonce $nonce"
echo "Transferring $amount nAmps from $from_pool_address to $to_pool_address..."
# transferDelegation(Address fromPool, Address toPool, BigInteger amount, BigInteger fee)
callPayload="$(java -cp $TOOLS_JAR cli.ComposeCallPayload "transferDelegation" "$from_pool_address" "$to_pool_address" "$amount" "$fee")"
receipt=`./rpc.sh --call "$private_key" "$nonce" "$pool_registry_address" "$callPayload" "0" "$node_address"`
require_success $?
echo "Transaction hash: \"$receipt\". Waiting for transaction to complete..."
wait_for_receipt "$receipt"
echo "Transaction completed"
capture_event "$receipt"
echo "Retrieving the total stake for $delegator_address in from pool..."
# getStake(Address pool, Address staker)
callPayload="$(java -cp $TOOLS_JAR cli.ComposeCallPayload "getStake" "$from_pool_address" "$delegator_address")"
# This result in a BigInteger: 0x23 (byte), length (byte), value (big-endian length bytes)
echo_state "$pool_registry_address" "$callPayload"
echo "Transfer complete."