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

Design: use structured response data #101

Open
alpe opened this issue Feb 11, 2022 · 0 comments
Open

Design: use structured response data #101

alpe opened this issue Feb 11, 2022 · 0 comments

Comments

@alpe
Copy link

alpe commented Feb 11, 2022

Background: simple way to access the completion time in confio/tgrade-contracts#152

As a client working with a contract, I can only trust the data field to come from the contract that I am calling. Events are unstructured data and may originate from a subcall so that I need to check for the origin of the event. To parse data from a wasm event, I would have to do in tgrade:

	// parse events for unboundTime
	for _, e := range em.Events() {
		if e.Type != wasmtypes.WasmModuleEventType {
			continue
		}
		var trusted bool
		for _, a := range e.Attributes {
			if string(a.Key) != wasmtypes.AttributeKeyContractAddr || string(a.Value) != contractAddr.String() {
				continue
			}
			trusted = true
			break
		}
		if !trusted {//  filter out other events
			continue
		}
		for _, a := range e.Attributes { 
			if string(a.Key) != "completion_time" {
				continue
			}
			// here I parse the data from the raw string representation
			nanos, err := strconv.ParseInt(string(a.Value), 10, 64)
			if err != nil {
				return nil, err
			}
			completionTime := time.Unix(0, nanos).UTC()
			return &completionTime, nil
		}

	}
	return nil, types.ErrInvalid.Wrap("completion_time not found")

compared to unmarshaling structured json data from the response data field this is quite some overhead

We have this model of structured response data already for queries/ responses and it works well for me.

Note: with submessages the data field could also be overwritten but I would consider the contract to be in charge and taking care as it is orchestrating the workflow.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant