/
ledger.bash
259 lines (207 loc) · 10.3 KB
/
ledger.bash
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
#!/usr/bin/env bats
load ../utils/_
setup() {
standard_setup
install_asset ledger
install_shared_asset subnet_type/shared_network_settings/system
dfx identity import --storage-mode plaintext alice alice.pem
dfx identity import --storage-mode plaintext bob bob.pem
dfx_start_for_nns_install
dfx extension install nns --version 0.2.1 || true
dfx nns install --ledger-accounts 345f723e9e619934daac6ae0f4be13a7b0ba57d6a608e511a00fd0ded5866752 22ca7edac648b814e81d7946e8bacea99280e07c5f51a04ba7a38009d8ad8e89 5a94fe181e9d411c58726cb87cbf2d016241b6c350bc3330e4869ca76e54ecbc
}
teardown() {
dfx_stop
standard_teardown
}
current_time_nanoseconds() {
echo "$(date +%s)"000000000
}
@test "ledger account-id" {
dfx identity use alice
assert_command dfx ledger account-id
assert_match 345f723e9e619934daac6ae0f4be13a7b0ba57d6a608e511a00fd0ded5866752
assert_command dfx ledger account-id --of-principal fg7gi-vyaaa-aaaal-qadca-cai
assert_match a014842f64a22e59887162a79c7ca7eb02553250704780ec4d954f12d0ea0b18
# --of-canister accepts both canister alias and canister principal
assert_command dfx canister create dummy_canister
assert_command dfx ledger account-id --of-canister "$(dfx canister id dummy_canister)"
assert_eq "$(dfx ledger account-id --of-canister dummy_canister)"
}
@test "ledger balance & transfer" {
dfx identity use alice
assert_command dfx ledger account-id
assert_eq 345f723e9e619934daac6ae0f4be13a7b0ba57d6a608e511a00fd0ded5866752
assert_command dfx ledger balance
assert_eq "1000000000.00000000 ICP"
assert_command dfx ledger transfer --amount 100 --memo 1 22ca7edac648b814e81d7946e8bacea99280e07c5f51a04ba7a38009d8ad8e89 # to bob
assert_contains "Transfer sent at block height"
# The sender(alice) paid transaction fee which is 0.0001 ICP
assert_command dfx ledger balance
assert_eq "999999899.99990000 ICP"
dfx identity use bob
assert_command dfx ledger account-id
assert_eq 22ca7edac648b814e81d7946e8bacea99280e07c5f51a04ba7a38009d8ad8e89
assert_command dfx ledger balance
assert_eq "1000000100.00000000 ICP"
assert_command dfx ledger transfer --icp 100 --e8s 1 --memo 2 345f723e9e619934daac6ae0f4be13a7b0ba57d6a608e511a00fd0ded5866752 # to alice
assert_contains "Transfer sent at block height"
# The sender(bob) paid transaction fee which is 0.0001 ICP
# 10100 - 100 - 0.0001 - 0.00000001 = 9999.99989999
assert_command dfx ledger balance
assert_eq "999999999.99989999 ICP"
# Transaction Deduplication
t=$(current_time_nanoseconds)
assert_command dfx ledger transfer --icp 1 --memo 1 --created-at-time "$t" 345f723e9e619934daac6ae0f4be13a7b0ba57d6a608e511a00fd0ded5866752
# shellcheck disable=SC2154
block_height=$(echo "$stdout" | sed '1q' | sed 's/Transfer sent at block height //')
# shellcheck disable=SC2154
assert_eq "Transfer sent at block height $block_height" "$stdout"
assert_command dfx ledger transfer --icp 1 --memo 1 --created-at-time $((t+1)) 345f723e9e619934daac6ae0f4be13a7b0ba57d6a608e511a00fd0ded5866752
# shellcheck disable=SC2154
assert_contains "Transfer sent at block height" "$stdout"
# shellcheck disable=SC2154
assert_not_contains "Transfer sent at block height $block_height" "$stdout"
assert_command dfx ledger transfer --icp 1 --memo 1 --created-at-time "$t" 345f723e9e619934daac6ae0f4be13a7b0ba57d6a608e511a00fd0ded5866752
# shellcheck disable=SC2154
assert_eq "transaction is a duplicate of another transaction in block $block_height" "$stderr"
assert_eq "Transfer sent at block height $block_height" "$stdout"
assert_command dfx ledger transfer --icp 1 --memo 2 --created-at-time "$t" 345f723e9e619934daac6ae0f4be13a7b0ba57d6a608e511a00fd0ded5866752
# shellcheck disable=SC2154
assert_contains "Transfer sent at block height" "$stdout"
# shellcheck disable=SC2154
assert_not_contains "Transfer sent at block height $block_height" "$stdout"
}
@test "ledger subaccounts" {
subacct=000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f
assert_command dfx ledger account-id --identity bob --subaccount "$subacct"
assert_match 5a94fe181e9d411c58726cb87cbf2d016241b6c350bc3330e4869ca76e54ecbc
dfx identity use alice
assert_command dfx ledger balance
assert_match "1000000000.00000000 ICP"
assert_command dfx ledger transfer --amount 100 --memo 1 5a94fe181e9d411c58726cb87cbf2d016241b6c350bc3330e4869ca76e54ecbc # to bob+subacct
assert_match "Transfer sent at block height"
assert_command dfx ledger balance
assert_match "999999899.99990000 ICP"
dfx identity use bob
assert_command dfx ledger balance
assert_match "1000000000.00000000 ICP"
assert_command dfx ledger balance --subaccount "$subacct"
assert_match "1000000100.00000000 ICP"
assert_command dfx ledger transfer --amount 100 --memo 2 345f723e9e619934daac6ae0f4be13a7b0ba57d6a608e511a00fd0ded5866752 --from-subaccount "$subacct" # to alice
assert_match "Transfer sent at block height"
assert_command dfx ledger balance
assert_match "1000000000.00000000 ICP"
assert_command dfx ledger balance --subaccount "$subacct"
assert_match "999999999.99990000 ICP"
assert_command dfx ledger balance --identity alice
assert_match "999999999.99990000 ICP"
}
tc_to_num() {
if [[ $1 =~ T ]]; then
echo "${1%%[^0-9]*}000000000000"
else
echo "${1%%[^0-9]*}"
fi
}
@test "ledger top-up" {
dfx identity use alice
assert_command dfx ledger balance
assert_match "1000000000.00000000 ICP"
wallet=$(dfx identity get-wallet)
balance=$(tc_to_num "$(dfx wallet balance)")
assert_command dfx ledger top-up "$wallet" --icp 5
assert_match "Canister was topped up with 617283500000000 cycles"
balance_now=$(tc_to_num "$(dfx wallet balance)")
(( balance_now - balance > 600000000000000 ))
# Transaction Deduplication
t=$(current_time_nanoseconds)
assert_command dfx ledger top-up "$wallet" --icp 5 --created-at-time "$t"
# shellcheck disable=SC2154
block_height=$(echo "$stdout" | sed '1q' | sed 's/Transfer sent at block height //')
# shellcheck disable=SC2154
assert_match "Transfer sent at block height $block_height" "$stdout"
# shellcheck disable=SC2154
assert_match "Using transfer at block height $block_height" "$stdout"
# shellcheck disable=SC2154
assert_match "Canister was topped up with" "$stdout"
assert_command dfx ledger top-up "$wallet" --icp 5 --created-at-time $((t+1))
# shellcheck disable=SC2154
assert_match "Transfer sent at block height" "$stdout"
# shellcheck disable=SC2154
assert_match "Using transfer at block height" "$stdout"
# shellcheck disable=SC2154
assert_match "Canister was topped up with" "$stdout"
# shellcheck disable=SC2154
assert_not_match "Transfer sent at block height $block_height" "$stdout"
# shellcheck disable=SC2154
assert_not_match "Using transfer at block height $block_height" "$stdout"
assert_command dfx ledger top-up "$wallet" --icp 5 --created-at-time "$t"
# shellcheck disable=SC2154
assert_contains "transaction is a duplicate of another transaction in block $block_height" "$stderr"
# shellcheck disable=SC2154
assert_contains "Transfer sent at block height $block_height" "$stdout"
# shellcheck disable=SC2154
assert_contains "Using transfer at block height $block_height" "$stdout"
# shellcheck disable=SC2154
assert_contains "Canister was topped up with" "$stdout"
# Top up canister by name instead of principal
dfx_new
assert_command dfx canister create e2e_project_backend
assert_command dfx ledger top-up e2e_project_backend --amount 5
assert_contains "Canister was topped up with 617283500000000 cycles"
}
@test "ledger create-canister" {
dfx identity use alice
assert_command dfx ledger create-canister --amount=100 --subnet-type "type1" "$(dfx identity get-principal)"
assert_match "Transfer sent at block height"
assert_match "Refunded at block height"
assert_match "with message: Provided subnet type type1 does not exist"
SUBNET_ID="5kdm2-62fc6-fwnja-hutkz-ycsnm-4z33i-woh43-4cenu-ev7mi-gii6t-4ae" # a random, valid principal
assert_command dfx ledger create-canister --amount=100 --subnet "$SUBNET_ID" "$(dfx identity get-principal)"
assert_match "Transfer sent at block height"
assert_match "Refunded at block height"
assert_match "with message: Subnet $SUBNET_ID does not exist"
# Transaction Deduplication
t=$(current_time_nanoseconds)
assert_command dfx ledger create-canister --amount=100 --created-at-time "$t" "$(dfx identity get-principal)"
# shellcheck disable=SC2154
block_height=$(echo "$stdout" | sed '1q' | sed 's/Transfer sent at block height //')
# shellcheck disable=SC2154
created_canister_id=$(echo "$stdout" | sed '3q;d' | sed 's/Canister created with id: //')
# shellcheck disable=SC2154
assert_match "Transfer sent at block height $block_height" "$stdout"
# shellcheck disable=SC2154
assert_match "Using transfer at block height $block_height" "$stdout"
# shellcheck disable=SC2154
assert_match "Canister created with id: $created_canister_id" "$stdout"
assert_command dfx ledger create-canister --amount=100 --created-at-time $((t+1)) "$(dfx identity get-principal)"
# shellcheck disable=SC2154
assert_match "Transfer sent at block height" "$stdout"
# shellcheck disable=SC2154
assert_match "Using transfer at block height" "$stdout"
# shellcheck disable=SC2154
assert_match "Canister created with id:" "$stdout"
# shellcheck disable=SC2154
assert_not_match "Transfer sent at block height $block_height" "$stdout"
# shellcheck disable=SC2154
assert_not_match "Using transfer at block height $block_height" "$stdout"
# shellcheck disable=SC2154
assert_not_match "Canister created with id: $created_canister_id" "$stdout"
assert_command dfx ledger create-canister --amount=100 --created-at-time "$t" "$(dfx identity get-principal)"
# shellcheck disable=SC2154
assert_contains "transaction is a duplicate of another transaction in block $block_height" "$stderr"
# shellcheck disable=SC2154
assert_contains "Transfer sent at block height $block_height" "$stdout"
# shellcheck disable=SC2154
assert_contains "Using transfer at block height $block_height" "$stdout"
# shellcheck disable=SC2154
assert_contains "Canister created with id: $created_canister_id" "$stdout"
}
@test "ledger show-subnet-types" {
install_asset cmc
dfx deploy cmc
CANISTER_ID=$(dfx canister id cmc)
assert_command dfx ledger show-subnet-types --cycles-minting-canister-id "$CANISTER_ID"
assert_eq '["type1", "type2"]'
}