Skip to content

Commit ba682c3

Browse files
authored
Merge pull request #9 from enigmampc/transfer-history-total-count
added total amount of txs to transfer history
2 parents 238d76a + 46d0023 commit ba682c3

File tree

5 files changed

+61
-23
lines changed

5 files changed

+61
-23
lines changed

schema/query_answer.json

+8
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,14 @@
130130
"txs"
131131
],
132132
"properties": {
133+
"total": {
134+
"type": [
135+
"integer",
136+
"null"
137+
],
138+
"format": "uint64",
139+
"minimum": 0.0
140+
},
133141
"txs": {
134142
"type": "array",
135143
"items": {

src/contract.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -261,9 +261,9 @@ pub fn query_transactions<S: Storage, A: Api, Q: Querier>(
261261
page_size: u32,
262262
) -> StdResult<Binary> {
263263
let address = deps.api.canonical_address(account).unwrap();
264-
let txs = get_transfers(&deps.api, &deps.storage, &address, page, page_size)?;
264+
let (txs, total) = get_transfers(&deps.api, &deps.storage, &address, page, page_size)?;
265265

266-
let result = QueryAnswer::TransferHistory { txs };
266+
let result = QueryAnswer::TransferHistory { txs, total: Some(total) };
267267
to_binary(&result)
268268
}
269269

@@ -2707,7 +2707,7 @@ mod tests {
27072707
// let a: QueryAnswer = from_binary(&query_result.unwrap()).unwrap();
27082708
// println!("{:?}", a);
27092709
let transfers = match from_binary(&query_result.unwrap()).unwrap() {
2710-
QueryAnswer::TransferHistory { txs } => txs,
2710+
QueryAnswer::TransferHistory { txs, total } => txs,
27112711
_ => panic!("Unexpected"),
27122712
};
27132713
assert!(transfers.is_empty());
@@ -2720,7 +2720,7 @@ mod tests {
27202720
};
27212721
let query_result = query(&deps, query_msg);
27222722
let transfers = match from_binary(&query_result.unwrap()).unwrap() {
2723-
QueryAnswer::TransferHistory { txs } => txs,
2723+
QueryAnswer::TransferHistory { txs, total } => txs,
27242724
_ => panic!("Unexpected"),
27252725
};
27262726
assert_eq!(transfers.len(), 3);
@@ -2733,7 +2733,7 @@ mod tests {
27332733
};
27342734
let query_result = query(&deps, query_msg);
27352735
let transfers = match from_binary(&query_result.unwrap()).unwrap() {
2736-
QueryAnswer::TransferHistory { txs } => txs,
2736+
QueryAnswer::TransferHistory { txs, total } => txs,
27372737
_ => panic!("Unexpected"),
27382738
};
27392739
assert_eq!(transfers.len(), 2);

src/msg.rs

+1
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,7 @@ pub enum QueryAnswer {
290290
},
291291
TransferHistory {
292292
txs: Vec<Tx>,
293+
total: Option<u64>,
293294
},
294295

295296
ViewingKeyError {

src/state.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -137,15 +137,15 @@ pub fn get_transfers<A: Api, S: ReadonlyStorage>(
137137
for_address: &CanonicalAddr,
138138
page: u32,
139139
page_size: u32,
140-
) -> StdResult<Vec<Tx>> {
140+
) -> StdResult<(Vec<Tx>, u64)> {
141141
let store = ReadonlyPrefixedStorage::multilevel(&[PREFIX_TXS, for_address.as_slice()], storage);
142142

143143
// Try to access the storage of txs for the account.
144144
// If it doesn't exist yet, return an empty list of transfers.
145145
let store = if let Some(result) = AppendStore::<StoredTx, _>::attach(&store) {
146146
result?
147147
} else {
148-
return Ok(vec![]);
148+
return Ok((vec![], 0));
149149
};
150150

151151
// Take `page_size` txs starting from the latest tx, potentially skipping `page * page_size`
@@ -159,7 +159,7 @@ pub fn get_transfers<A: Api, S: ReadonlyStorage>(
159159
let txs: StdResult<Vec<Tx>> = tx_iter
160160
.map(|tx| tx.map(|tx| tx.into_humanized(api)).and_then(|x| x))
161161
.collect();
162-
txs
162+
txs.map(|txs| (txs, store.len() as u64))
163163
}
164164

165165
// Config

tests/integration.sh

+44-15
Original file line numberDiff line numberDiff line change
@@ -527,9 +527,9 @@ function test_deposit() {
527527

528528
function unix_time_of_tx() {
529529
set -e
530-
local tx_hash="$1"
530+
local tx="$1"
531531

532-
date -d "$(secretcli q tx "$tx_hash" | jq -r '.timestamp')" '+%s'
532+
date -d "$(jq -r '.timestamp' <<<"$tx")" '+%s'
533533
}
534534

535535
function get_tx_history() {
@@ -544,6 +544,10 @@ function get_tx_history() {
544544
local transfer_history_response
545545
transfer_history_query='{"transfer_history":{"address":"'"$account"'","key":"'"$viewing_key"'","page_size":'"$page_size"',"page":'"$page"'}}'
546546
transfer_history_response="$(compute_query "$contract_addr" "$transfer_history_query")"
547+
log "$transfer_history_response"
548+
# There's no good way of tracking the exact expected value,
549+
# so we just check that the `total` field is a number
550+
jq -e '.transfer_history.total | numbers' <<<"$transfer_history_response" > /dev/null
547551
jq -r '.transfer_history.txs' <<<"$transfer_history_response"
548552
}
549553

@@ -560,6 +564,7 @@ function check_latest_tx_history() {
560564
local receiver="$6"
561565
local amount="$7"
562566
local timestamp="$8"
567+
local block_height="$9"
563568

564569
local txs
565570
local tx
@@ -573,6 +578,7 @@ function check_latest_tx_history() {
573578
assert_eq "$(jq -r '.coins.amount' <<<"$tx")" "$amount"
574579
assert_eq "$(jq -r '.coins.denom' <<<"$tx")" 'SSCRT'
575580
assert_eq "$(jq -r '.timestamp' <<<"$tx")" "$timestamp"
581+
assert_eq "$(jq -r '.block_height' <<<"$tx")" "$block_height"
576582

577583
jq -r '.id' <<<"$tx"
578584
}
@@ -621,14 +627,20 @@ function test_transfer() {
621627
transfer_response="$(data_of wait_for_compute_tx "$tx_hash" 'waiting for transfer from "a" to "b" to process')"
622628
assert_eq "$transfer_response" "$(pad_space '{"transfer":{"status":"success"}}')"
623629

630+
local native_tx
631+
native_tx="$(secretcli q tx "$tx_hash")"
624632
local timestamp
625-
timestamp="$(unix_time_of_tx "$tx_hash")"
633+
timestamp="$(unix_time_of_tx "$native_tx")"
634+
local block_height
635+
block_height="$(jq -r '.height' <<<"$native_tx")"
626636

627637
# Check for both "a" and "b" that they recorded the transfer
628638
local -A tx_ids
629639
for key in a b; do
630640
log "querying the transfer history of \"$key\""
631-
tx_ids[$key]="$(check_latest_tx_history "$contract_addr" "${ADDRESS[$key]}" "${VK[$key]}" "${ADDRESS[a]}" "${ADDRESS[a]}" "${ADDRESS[b]}" 400000 "$timestamp")"
641+
tx_ids[$key]="$(
642+
check_latest_tx_history "$contract_addr" "${ADDRESS[$key]}" "${VK[$key]}" "${ADDRESS[a]}" "${ADDRESS[a]}" "${ADDRESS[b]}" 400000 "$timestamp" "$block_height"
643+
)"
632644
done
633645

634646
assert_eq "${tx_ids[a]}" "${tx_ids[b]}"
@@ -776,8 +788,12 @@ function test_send() {
776788
"$((original_count + 1))"
777789
log 'received send response'
778790

791+
local native_tx
792+
native_tx="$(secretcli q tx "$tx_hash")"
779793
local timestamp
780-
timestamp="$(unix_time_of_tx "$tx_hash")"
794+
timestamp="$(unix_time_of_tx "$native_tx")"
795+
local block_height
796+
block_height="$(jq -r '.height' <<<"$native_tx")"
781797

782798
# Check that the receiver got the message
783799
log 'checking whether state was updated in the receiver'
@@ -790,7 +806,7 @@ function test_send() {
790806

791807
# Check that "a" recorded the transfer
792808
log 'querying the transfer history of "a"'
793-
check_latest_tx_history "$contract_addr" "${ADDRESS[a]}" "${VK[a]}" "${ADDRESS[a]}" "${ADDRESS[a]}" "$receiver_addr" 400000 "$timestamp" >/dev/null
809+
check_latest_tx_history "$contract_addr" "${ADDRESS[a]}" "${VK[a]}" "${ADDRESS[a]}" "${ADDRESS[a]}" "$receiver_addr" 400000 "$timestamp" "$block_height" >/dev/null
794810

795811
# Check that "a" has fewer funds
796812
assert_eq "$(get_balance "$contract_addr" 'a')" 600000
@@ -915,14 +931,21 @@ function test_transfer_from() {
915931
tx_hash="$(compute_execute "$contract_addr" "$transfer_message" ${FROM[b]} --gas 200000)"
916932
transfer_response="$(data_of wait_for_compute_tx "$tx_hash" 'waiting for transfer from "a" to "c" by "b" to process')"
917933
assert_eq "$transfer_response" "$(pad_space '{"transfer_from":{"status":"success"}}')"
934+
935+
local native_tx
936+
native_tx="$(secretcli q tx "$tx_hash")"
918937
local timestamp
919-
timestamp="$(unix_time_of_tx "$tx_hash")"
938+
timestamp="$(unix_time_of_tx "$native_tx")"
939+
local block_height
940+
block_height="$(jq -r '.height' <<<"$native_tx")"
920941

921942
# Check for both "a", "b", and "c" that they recorded the transfer
922943
local -A tx_ids
923944
for key in a b c; do
924945
log "querying the transfer history of \"$key\""
925-
tx_ids[$key]="$(check_latest_tx_history "$contract_addr" "${ADDRESS[$key]}" "${VK[$key]}" "${ADDRESS[b]}" "${ADDRESS[a]}" "${ADDRESS[c]}" 400000 "$timestamp")"
946+
tx_ids[$key]="$(
947+
check_latest_tx_history "$contract_addr" "${ADDRESS[$key]}" "${VK[$key]}" "${ADDRESS[b]}" "${ADDRESS[a]}" "${ADDRESS[c]}" 400000 "$timestamp" "$block_height"
948+
)"
926949
done
927950

928951
assert_eq "${tx_ids[a]}" "${tx_ids[b]}"
@@ -1018,8 +1041,12 @@ function test_send_from() {
10181041
"$((original_count + 1))"
10191042
log 'received send response'
10201043

1044+
local native_tx
1045+
native_tx="$(secretcli q tx "$tx_hash")"
10211046
local timestamp
1022-
timestamp="$(unix_time_of_tx "$tx_hash")"
1047+
timestamp="$(unix_time_of_tx "$native_tx")"
1048+
local block_height
1049+
block_height="$(jq -r '.height' <<<"$native_tx")"
10231050

10241051
# Check that the receiver got the message
10251052
log 'checking whether state was updated in the receiver'
@@ -1034,7 +1061,9 @@ function test_send_from() {
10341061
local -A tx_ids
10351062
for key in a b; do
10361063
log "querying the transfer history of \"$key\""
1037-
tx_ids[$key]="$(check_latest_tx_history "$contract_addr" "${ADDRESS[$key]}" "${VK[$key]}" "${ADDRESS[b]}" "${ADDRESS[a]}" "$receiver_addr" 400000 "$timestamp")"
1064+
tx_ids[$key]="$(
1065+
check_latest_tx_history "$contract_addr" "${ADDRESS[$key]}" "${VK[$key]}" "${ADDRESS[b]}" "${ADDRESS[a]}" "$receiver_addr" 400000 "$timestamp" "$block_height"
1066+
)"
10381067
done
10391068

10401069
assert_eq "${tx_ids[a]}" "${tx_ids[b]}"
@@ -1082,11 +1111,11 @@ function main() {
10821111

10831112
# To make testing faster, check the logs and try to reuse the deployed contract and VKs from previous runs.
10841113
# Remember to comment out the contract deployment and `test_viewing_key` if you do.
1085-
# local contract_addr='secret18vd8fpwxzck93qlwghaj6arh4p7c5n8978vsyg'
1086-
# VK[a]='api_key_Ij3ZwkDOTqMPnmCLGn3F2uX0pMpETw2LTyCkQ0sDMv8='
1087-
# VK[b]='api_key_hV3SlzQMC4YK50GbDrpbjicGOMQpolfPI+O6pMp6oQY='
1088-
# VK[c]='api_key_7Bv00UvQCkZ7SltDn205R0GBugq/l8GnRX6N0JIBQuA='
1089-
# VK[d]='api_key_A3Y3mFe87d2fEq90kNlPSIUSmVgoao448ZpyDAJkB/4='
1114+
local contract_addr='secret18vd8fpwxzck93qlwghaj6arh4p7c5n8978vsyg'
1115+
VK[a]='api_key_U6FcuhP2km6UHtYeFSyaZbggcgMJQAiTMlNWV3X4iXQ='
1116+
VK[b]='api_key_YoQlmqnOkkEoh81XzFkiZ3z7+ZAJh9kyFXvtaMBhiFU='
1117+
VK[c]='api_key_/cdkitEbzaHZA41OB6cGcz1XGnQk6LYTAfSBWTOU5aQ='
1118+
VK[d]='api_key_WQYkuGOco/mSHgtKWG0f7b2UcrSG3s1fIqm1X/wIGDo='
10901119

10911120
log "contract address: $contract_addr"
10921121

0 commit comments

Comments
 (0)