Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Flatten the getEvents response structure #1015

Merged
merged 7 commits into from
Oct 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 4 additions & 12 deletions cmd/crates/soroban-test/tests/fixtures/test-jsons/get-events.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@
"AAAABQAAAAdDT1VOVEVSAA==",
"AAAABQAAAAlpbmNyZW1lbnQAAAA="
],
"value": {
"xdr": "AAAAAQAAAAE="
}
"value": "AAAAAQAAAAE="
},
{
"type": "system",
Expand All @@ -27,9 +25,7 @@
"AAAABQAAAAh0cmFuc2Zlcg==",
"AAAAAQB6Mcc="
],
"value": {
"xdr": "AAAABQAAAApHaWJNb255UGxzAAA="
}
"value": "AAAABQAAAApHaWJNb255UGxzAAA="
},
{
"type": "contract",
Expand All @@ -42,9 +38,7 @@
"AAAABQAAAAh0cmFuc2Zlcg==",
"AAAAAQB6Mcc="
],
"value": {
"xdr": "AAAABQAAAApHaWJNb255UGxzAAA="
}
"value": "AAAABQAAAApHaWJNb255UGxzAAA="
},
{
"type": "system",
Expand All @@ -57,9 +51,7 @@
"AAAABQAAAAh0cmFuc2Zlcg==",
"AAAAAQB6Mcc="
],
"value": {
"xdr": "AAAABQAAAApHaWJNb255UGxzAAA="
}
"value": "AAAABQAAAApHaWJNb255UGxzAAA="
}
]
}
19 changes: 10 additions & 9 deletions cmd/soroban-cli/src/commands/config/events_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use std::{
fs,
path::{Path, PathBuf},
};
use stellar_strkey::{Contract, Strkey};

#[derive(Debug, clap::Args, Clone, Default)]
#[group(skip)]
Expand Down Expand Up @@ -136,19 +137,19 @@ impl Args {
id,
ledger: ledger_info.sequence_number.to_string(),
ledger_closed_at: dt.format("%Y-%m-%dT%H:%M:%SZ").to_string(),
contract_id: hex::encode(
contract_id: Strkey::Contract(Contract(
contract_event
.contract_id
.as_ref()
.unwrap_or(&xdr::Hash([0; 32])),
),
.unwrap_or(&xdr::Hash([0; 32]))
.0,
))
.to_string(),
topic,
value: rpc::EventValue {
xdr: match &contract_event.body {
xdr::ContractEventBody::V0(e) => &e.data,
}
.to_xdr_base64()?,
},
value: match &contract_event.body {
xdr::ContractEventBody::V0(e) => &e.data,
}
.to_xdr_base64()?,
};

events.push(cereal_event);
Expand Down
14 changes: 10 additions & 4 deletions cmd/soroban-cli/src/commands/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ pub struct Cmd {
count: usize,

/// A set of (up to 5) contract IDs to filter events on. This parameter can
/// be passed multiple times, e.g. `--id abc --id def`, or passed with
/// multiple parameters, e.g. `--id abd def`.
/// be passed multiple times, e.g. `--id C123.. --id C456..`, or passed with
/// multiple parameters, e.g. `--id C123 C456`.
///
/// Though the specification supports multiple filter objects (i.e.
/// combinations of type, IDs, and topics), only one set can be specified on
Expand Down Expand Up @@ -352,8 +352,14 @@ mod tests {
assert_eq!(file.events.len(), 2);
assert_eq!(file.events[0].ledger, "2");
assert_eq!(file.events[1].ledger, "2");
assert_eq!(file.events[0].contract_id, "0".repeat(64));
assert_eq!(file.events[1].contract_id, "01".repeat(32));
assert_eq!(
file.events[0].contract_id,
"CAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABSC4"
);
assert_eq!(
file.events[1].contract_id,
"CAAQCAIBAEAQCAIBAEAQCAIBAEAQCAIBAEAQCAIBAEAQCAIBAEAQC526"
);
assert_eq!(file.latest_ledger, 2);
}

Expand Down
21 changes: 6 additions & 15 deletions cmd/soroban-cli/src/rpc/fixtures/event_response.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,39 +10,30 @@
"AAAABQAAAAh0cmFuc2Zlcg==",
"AAAAAQB6Mcc="
],
"value": {
"xdr": "AAAABQAAAApHaWJNb255UGxzAAA="
}
"value": "AAAABQAAAApHaWJNb255UGxzAAA="
}, {
"eventType": "contract",
"ledger": "43601284",
"ledgerClosedAt": "2022-11-16T16:10:41Z",
"contractId": "CDR6QKTWZQYW6YUJ7UP7XXZRLWQPFRV6SWBLQS4ZQOSAF4BOUD77OO5Z"
,
"contractId": "CDR6QKTWZQYW6YUJ7UP7XXZRLWQPFRV6SWBLQS4ZQOSAF4BOUD77OO5Z",
"id": "0164090849041387521-0000000003",
"pagingToken": "164090849041387521-3",
"topic": [
"AAAABQAAAAh0cmFuc2Zlcg==",
"AAAAAQB6Mcc="
],
"value": {
"xdr": "AAAABQAAAApHaWJNb255UGxzAAA="
}
"value": "AAAABQAAAApHaWJNb255UGxzAAA="
}, {
"eventType": "system",
"ledger": "43601285",
"ledgerClosedAt": "2022-11-16T16:10:41Z",
"contractId": "CCR6QKTWZQYW6YUJ7UP7XXZRLWQPFRV6SWBLQS4ZQOSAF4BOUD77OTE2"
,
"contractId": "CCR6QKTWZQYW6YUJ7UP7XXZRLWQPFRV6SWBLQS4ZQOSAF4BOUD77OTE2",
"id": "0164090849041387521-0000000003",
"pagingToken": "164090849041387521-3",
"topic": [
"AAAABQAAAAh0cmFuc2Zlcg==",
"AAAAAQB6Mcc="
],
"value": {
"xdr": "AAAABQAAAApHaWJNb255UGxzAAA="
}
}
]
"value": "AAAABQAAAApHaWJNb255UGxzAAA="
}]
}
13 changes: 4 additions & 9 deletions cmd/soroban-cli/src/rpc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ pub struct Event {
#[serde(rename = "contractId")]
pub contract_id: String,
pub topic: Vec<String>,
pub value: EventValue,
pub value: String,
}

impl Display for Event {
Expand All @@ -338,7 +338,7 @@ impl Display for Event {
let scval = xdr::ScVal::from_xdr_base64(topic).map_err(|_| std::fmt::Error)?;
writeln!(f, " {scval:?}")?;
}
let scval = xdr::ScVal::from_xdr_base64(&self.value.xdr).map_err(|_| std::fmt::Error)?;
let scval = xdr::ScVal::from_xdr_base64(&self.value).map_err(|_| std::fmt::Error)?;
writeln!(f, " Value: {scval:?}")
}
}
Expand Down Expand Up @@ -386,7 +386,7 @@ impl Event {

colored!(
stdout,
" Contract: {}0x{}{}\n",
" Contract: {}{}{}\n",
fg!(Some(Color::Green)),
self.contract_id,
reset!(),
Expand All @@ -404,7 +404,7 @@ impl Event {
)?;
}

let scval = xdr::ScVal::from_xdr_base64(&self.value.xdr)?;
let scval = xdr::ScVal::from_xdr_base64(&self.value)?;
colored!(
stdout,
" Value: {}{:?}{}\n",
Expand All @@ -417,11 +417,6 @@ impl Event {
}
}

#[derive(serde::Deserialize, serde::Serialize, Debug, Clone)]
pub struct EventValue {
pub xdr: String,
}

#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq, clap::ValueEnum)]
pub enum EventType {
All,
Expand Down
24 changes: 10 additions & 14 deletions cmd/soroban-rpc/internal/methods/get_events.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,19 +67,15 @@ func (e eventTypeSet) matches(event xdr.ContractEvent) bool {
}

type EventInfo struct {
EventType string `json:"type"`
Ledger int32 `json:"ledger,string"`
LedgerClosedAt string `json:"ledgerClosedAt"`
ContractID string `json:"contractId"`
ID string `json:"id"`
PagingToken string `json:"pagingToken"`
Topic []string `json:"topic"`
Value EventInfoValue `json:"value"`
InSuccessfulContractCall bool `json:"inSuccessfulContractCall"`
}

type EventInfoValue struct {
XDR string `json:"xdr"`
EventType string `json:"type"`
Ledger int32 `json:"ledger,string"`
LedgerClosedAt string `json:"ledgerClosedAt"`
ContractID string `json:"contractId"`
ID string `json:"id"`
PagingToken string `json:"pagingToken"`
Topic []string `json:"topic"`
Value string `json:"value"`
InSuccessfulContractCall bool `json:"inSuccessfulContractCall"`
}

type GetEventsRequest struct {
Expand Down Expand Up @@ -413,7 +409,7 @@ func eventInfoForEvent(event xdr.DiagnosticEvent, cursor events.Cursor, ledgerCl
ID: cursor.String(),
PagingToken: cursor.String(),
Topic: topic,
Value: EventInfoValue{XDR: data},
Value: data,
InSuccessfulContractCall: event.InSuccessfulContractCall,
}
if event.Event.ContractId != nil {
Expand Down
44 changes: 20 additions & 24 deletions cmd/soroban-rpc/internal/methods/get_events_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -623,16 +623,14 @@ func TestGetEvents(t *testing.T) {
})
assert.NoError(t, err)
expected = append(expected, EventInfo{
EventType: EventTypeContract,
Ledger: 1,
LedgerClosedAt: now.Format(time.RFC3339),
ContractID: "CAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABSC4",
ID: id,
PagingToken: id,
Topic: []string{value},
Value: EventInfoValue{
XDR: value,
},
EventType: EventTypeContract,
Ledger: 1,
LedgerClosedAt: now.Format(time.RFC3339),
ContractID: "CAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABSC4",
ID: id,
PagingToken: id,
Topic: []string{value},
Value: value,
InSuccessfulContractCall: true,
})
}
Expand Down Expand Up @@ -744,7 +742,7 @@ func TestGetEvents(t *testing.T) {
ID: id,
PagingToken: id,
Topic: []string{counterXdr, value},
Value: EventInfoValue{XDR: value},
Value: value,
InSuccessfulContractCall: true,
},
}
Expand Down Expand Up @@ -838,7 +836,7 @@ func TestGetEvents(t *testing.T) {
ID: id,
PagingToken: id,
Topic: []string{counterXdr, value},
Value: EventInfoValue{XDR: value},
Value: value,
InSuccessfulContractCall: true,
},
}
Expand Down Expand Up @@ -898,7 +896,7 @@ func TestGetEvents(t *testing.T) {
ID: id,
PagingToken: id,
Topic: []string{counterXdr},
Value: EventInfoValue{XDR: counterXdr},
Value: counterXdr,
InSuccessfulContractCall: true,
},
}
Expand Down Expand Up @@ -946,16 +944,14 @@ func TestGetEvents(t *testing.T) {
value, err := xdr.MarshalBase64(txMeta[i].MustV3().SorobanMeta.Events[0].Body.MustV0().Data)
assert.NoError(t, err)
expected = append(expected, EventInfo{
EventType: EventTypeContract,
Ledger: 1,
LedgerClosedAt: now.Format(time.RFC3339),
ContractID: "CAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABSC4",
ID: id,
PagingToken: id,
Topic: []string{value},
Value: EventInfoValue{
XDR: value,
},
EventType: EventTypeContract,
Ledger: 1,
LedgerClosedAt: now.Format(time.RFC3339),
ContractID: "CAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABSC4",
ID: id,
PagingToken: id,
Topic: []string{value},
Value: value,
InSuccessfulContractCall: true,
})
}
Expand Down Expand Up @@ -1039,7 +1035,7 @@ func TestGetEvents(t *testing.T) {
ID: id,
PagingToken: id,
Topic: []string{counterXdr},
Value: EventInfoValue{XDR: expectedXdr},
Value: expectedXdr,
InSuccessfulContractCall: true,
})
}
Expand Down
2 changes: 1 addition & 1 deletion docs/soroban-cli-full-docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -737,7 +737,7 @@ Watch the network for contract events
* `-c`, `--count <COUNT>` — The maximum number of events to display (specify "0" to show all events when using sandbox, or to defer to the server-defined limit if using RPC)

Default value: `10`
* `--id <CONTRACT_IDS>` — A set of (up to 5) contract IDs to filter events on. This parameter can be passed multiple times, e.g. `--id abc --id def`, or passed with multiple parameters, e.g. `--id abd def`
* `--id <CONTRACT_IDS>` — A set of (up to 5) contract IDs to filter events on. This parameter can be passed multiple times, e.g. `--id C123.. --id C456..`, or passed with multiple parameters, e.g. `--id C123 C456`
* `--topic <TOPIC_FILTERS>` — A set of (up to 4) topic filters to filter event topics on. A single topic filter can contain 1-4 different segment filters, separated by commas, with an asterisk (* character) indicating a wildcard segment
* `--type <EVENT_TYPE>` — Specifies which type of contract events to display

Expand Down
Loading