Skip to content

Add origins in activity #373

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

Merged
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
62 changes: 13 additions & 49 deletions ERCs/ERC20/ERC20-TokenScript.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,21 +32,16 @@
<sequence>
<element name="from" ethereum:type="address" ethereum:indexed="true"/>
<element name="to" ethereum:type="address" ethereum:indexed="true"/>
<element name="value" ethereum:type="uint256" ethereum:indexed="false"/>
<element name="amount" ethereum:type="uint256" ethereum:indexed="false"/>
</sequence>
</type>
</namedType>
</asnx:module>
<ts:cards>
<ts:card type="activity" name="sent">
<ts:attribute name="amount">
<ts:type>
<ts:syntax>1.3.6.1.4.1.1466.115.121.1.36</ts:syntax>
</ts:type>
<ts:origins>
<ethereum:event type="Transfer" filter="from=${ownerAddress}" select="value"/>
</ts:origins>
</ts:attribute>
<ts:origins>
<ethereum:event type="Transfer" filter="from=${ownerAddress}"/>
</ts:origins>
<ts:item-view xml:lang="en" xmlns="http://www.w3.org/1999/xhtml">
<style type="text/css">&style;</style>
<script type="text/javascript">&item-view-sent.en;</script>
Expand All @@ -57,14 +52,9 @@
</ts:view>
</ts:card>
<ts:card type="activity" name="received">
<ts:attribute name="amount">
<ts:type>
<ts:syntax>1.3.6.1.4.1.1466.115.121.1.36</ts:syntax>
</ts:type>
<ts:origins>
<ethereum:event type="Transfer" filter="to=${ownerAddress}" select="value"/>
</ts:origins>
</ts:attribute>
<ts:origins>
<ethereum:event type="Transfer" filter="to=${ownerAddress}" />
</ts:origins>
<ts:item-view xml:lang="en" xmlns="http://www.w3.org/1999/xhtml">
<style type="text/css">&style;</style>
<script type="text/javascript">&item-view-received.en;</script>
Expand All @@ -76,22 +66,9 @@
</ts:card>

<ts:card type="activity" name="ownerApproved">
<ts:attribute name="amount">
<ts:type>
<ts:syntax>1.3.6.1.4.1.1466.115.121.1.36</ts:syntax>
</ts:type>
<ts:origins>
<ethereum:event type="Approval" filter="owner=${ownerAddress}" select="value"/>
</ts:origins>
</ts:attribute>
<ts:attribute name="to">
<ts:type>
<ts:syntax>1.3.6.1.4.1.1466.115.121.1.36</ts:syntax>
</ts:type>
<ts:origins>
<ethereum:event type="Approval" filter="owner=${ownerAddress}" select="spender"/>
</ts:origins>
</ts:attribute>
<ts:origins>
<ethereum:event type="Approval" filter="owner=${ownerAddress}"/>
</ts:origins>
<ts:item-view xml:lang="en" xmlns="http://www.w3.org/1999/xhtml">
<style type="text/css">&style;</style>
<script type="text/javascript">&item-view-ownerApproved.en;</script>
Expand All @@ -103,22 +80,9 @@
</ts:card>

<ts:card type="activity" name="approvalObtained">
<ts:attribute name="amount">
<ts:type>
<ts:syntax>1.3.6.1.4.1.1466.115.121.1.36</ts:syntax>
</ts:type>
<ts:origins>
<ethereum:event type="Approval" filter="spender=${ownerAddress}" select="value"/>
</ts:origins>
</ts:attribute>
<ts:attribute name="from">
<ts:type>
<ts:syntax>1.3.6.1.4.1.1466.115.121.1.36</ts:syntax>
</ts:type>
<ts:origins>
<ethereum:event type="Approval" filter="spender=${ownerAddress}" select="owner"/>
</ts:origins>
</ts:attribute>
<ts:origins>
<ethereum:event type="Approval" filter="spender=${ownerAddress}"/>
</ts:origins>
<ts:item-view xml:lang="en" xmlns="http://www.w3.org/1999/xhtml">
<style type="text/css">&style;</style>
<script type="text/javascript">&item-view-approvalObtained.en;</script>
Expand Down
76 changes: 22 additions & 54 deletions ERCs/ERC721/ERC721-TokenScript.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,32 +21,29 @@
<namedType name="Approval">
<type>
<sequence>
<element name="_owner" ethereum:type="address" ethereum:indexed="true"/>
<element name="_approved" ethereum:type="address" ethereum:indexed="true"/>
<element name="_tokenId" ethereum:type="uint256" ethereum:indexed="false"/>
<element name="owner" ethereum:type="address" ethereum:indexed="true"/>
<element name="approved" ethereum:type="address" ethereum:indexed="true"/>
<element name="tokenId" ethereum:type="uint256" ethereum:indexed="false"/>
</sequence>
</type>
</namedType>
<namedType name="Transfer">
<type>
<sequence>
<element name="_from" ethereum:type="address" ethereum:indexed="true"/>
<element name="_to" ethereum:type="address" ethereum:indexed="true"/>
<element name="_tokenId" ethereum:type="uint256" ethereum:indexed="false"/>
<element name="from" ethereum:type="address" ethereum:indexed="true"/>
<element name="to" ethereum:type="address" ethereum:indexed="true"/>
<element name="tokenId" ethereum:type="uint256" ethereum:indexed="false"/>
</sequence>
</type>
</namedType>
</asnx:module>
<ts:cards>
<ts:card type="activity" name="sent">
<ts:attribute name="_tokenId">
<ts:type>
<ts:syntax>1.3.6.1.4.1.1466.115.121.1.36</ts:syntax>
</ts:type>
<ts:origins>
<ethereum:event type="Transfer" filter="${_from}=${ownerAddress}" select="_tokenId"/>
</ts:origins>
</ts:attribute>
<ts:origins>
<!-- this gives you card attributes from, to, tokenID, which is different than the
token's attributes -->
<ethereum:event type="Transfer" filter="from=${ownerAddress}"/>
</ts:origins>
<ts:item-view xml:lang="en" xmlns="http://www.w3.org/1999/xhtml">
<style type="text/css">&style;</style>
<script type="text/javascript">&item-view-sent.en;</script>
Expand All @@ -57,14 +54,11 @@
</ts:view>
</ts:card>
<ts:card type="activity" name="received">
<ts:attribute name="_tokenId">
<ts:type>
<ts:syntax>1.3.6.1.4.1.1466.115.121.1.36</ts:syntax>
</ts:type>
<ts:origins>
<ethereum:event type="Transfer" filter="${_to}=${ownerAddress}" select="_tokenId"/>
</ts:origins>
</ts:attribute>
<ts:origins>
<!-- this gives you card attributes from, to, tokenId, which is different than the
token's attributes which also have tokenId -->
<ethereum:event type="Transfer" filter="to=${ownerAddress}"/>
</ts:origins>
<ts:item-view xml:lang="en" xmlns="http://www.w3.org/1999/xhtml">
<style type="text/css">&style;</style>
<script type="text/javascript">&item-view-received.en;</script>
Expand All @@ -76,22 +70,9 @@
</ts:card>

<ts:card type="activity" name="ownerApproved">
<ts:attribute name="_tokenId">
<ts:type>
<ts:syntax>1.3.6.1.4.1.1466.115.121.1.36</ts:syntax>
</ts:type>
<ts:origins>
<ethereum:event type="Approval" filter="${_from}=${ownerAddress}" select="_tokenId"/>
</ts:origins>
</ts:attribute>
<ts:attribute name="to">
<ts:type>
<ts:syntax>1.3.6.1.4.1.1466.115.121.1.36</ts:syntax>
</ts:type>
<ts:origins>
<ethereum:event type="Approval" filter="${_to}=${ownerAddress}" select="_to"/>
</ts:origins>
</ts:attribute>
<ts:origins>
<ethereum:event type="Approval" filter="${from}=${ownerAddress}"/>
</ts:origins>
<ts:item-view xml:lang="en" xmlns="http://www.w3.org/1999/xhtml">
<style type="text/css">&style;</style>
<script type="text/javascript">&item-view-ownerApproved.en;</script>
Expand All @@ -103,22 +84,9 @@
</ts:card>

<ts:card type="activity" name="approvalObtained">
<ts:attribute name="_tokenId">
<ts:type>
<ts:syntax>1.3.6.1.4.1.1466.115.121.1.36</ts:syntax>
</ts:type>
<ts:origins>
<ethereum:event type="Approval" filter="${_to}=${ownerAddress}" select="_tokenId"/>
</ts:origins>
</ts:attribute>
<ts:attribute name="from">
<ts:type>
<ts:syntax>1.3.6.1.4.1.1466.115.121.1.36</ts:syntax>
</ts:type>
<ts:origins>
<ethereum:event type="Approval" filter="${_to}=${ownerAddress}" select="_from"/>
</ts:origins>
</ts:attribute>
<ts:origins>
<ethereum:event type="Approval" filter="${to}=${ownerAddress}"/>
</ts:origins>
<ts:item-view xml:lang="en" xmlns="http://www.w3.org/1999/xhtml">
<style type="text/css">&style;</style>
<script type="text/javascript">&item-view-approvalObtained.en;</script>
Expand Down
38 changes: 32 additions & 6 deletions doc/javascript_api.md
Original file line number Diff line number Diff line change
Expand Up @@ -190,19 +190,18 @@ TokenScript's JavaScript API is designed to be asynchronous.

There are 3 args supplied to the `web3.tokens.dataChanged` callback:

* oldTokens — this contains the value of the token instance before this change
* updatedTokens — this contains the value of the token instance after this change
* old data — this contains the data — token instance and card attribute values — before the change
* updated data — this contains the data — token instance and card attribute values — after the change
* tokenCardId — the CSS ID of the wrapper `<div>` to place content in

Developers can use this callback and the arguments to figure out what has changed. We might point them to a good JSON-diff library; but since the purpose here is just to render a static layout, they can just re-render the whole DOM as we do in our examples.

A simple way to implement that would be:

```
web3.tokens.dataChanged = (oldTokens, updatedTokens, tokenCardId) => {
const currentTokenInstance = updatedTokens.currentInstance
const domHtml = new Token(currentTokenInstance).render()
document.getElementById(tokenCardId).getElementsByClassName("contents")[0].innerHTML = domHtml
web3.tokens.dataChanged = (old, updated, tokenCardId) => {
const data = updated
document.getElementById(tokenCardId).getElementsByClassName("contents")[0].innerHTML = new Token(data).render()
}
```

Expand All @@ -216,6 +215,33 @@ document.getElementById(tokenCardId).innerHTML = replacementDomHtml

The above-mentioned `Token.render()` function generates a DOM from a dictionary of token attributes.

Within the `Token` class, the token data can be access like this:

```
const tokenAttributeValue = this.props.token.tokenAttribute1
```

and the card data like this:

```
const cardAttributeValue = this.props.card.tokenAttribute1
```

And if we are sure the token and card attribute names don't clash, we can pretend they are in the same namespace with:

```
web3.tokens.dataChanged = (old, updated, tokenCardId) => {
const data = Object.assign({}, updated.token, updated.card)
document.getElementById(tokenCardId).getElementsByClassName("contents")[0].innerHTML = new Token(data).render()
}
```

Then this is unchanged:

```
const attributeValue = this.props.tokenAttribute1 //or this.props.cardAttribute1
```

Future
---
In (A), we can also stuff the entire list of tokens in the user's Ethereum wallet in there (in a future iteration) under the `all` key. We might have to key them by wallet/networks too. Performance is a concern, but this simple approach has quite a number of advantages. Perhaps it can be partially mitigated by adding a permission call that TokenScript developers have to make to make `tokens` accessible, maybe as part of the permission granted via https://eips.ethereum.org/EIPS/eip-1102 (which we should implement anyway) or a new function call.
Expand Down
4 changes: 2 additions & 2 deletions schema/ethereum.xsd
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,8 @@
<xs:element name="event">
<xs:complexType>
<!-- TODO: this should be whatever ASN.X allows as element name -->
<xs:attribute name="type" type="xs:NCName"/>
<xs:attribute name="contract" type="xs:IDREF"/>
<xs:attribute use="required" name="type" type="xs:NCName"/>
<xs:attribute use="required" name="contract" type="xs:IDREF"/>
<xs:attribute name="filter" type="xs:string"/>
<xs:attribute name="select" type="xs:NCName"/>
</xs:complexType>
Expand Down
9 changes: 9 additions & 0 deletions schema/tokenscript.xsd
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,14 @@
</sequence>
<attribute name="custodian" type="boolean" default="false"/>
</complexType>
<keyref name="eventTypeRef" refer="ts:namedTypeName">
<selector xpath="ts:cards/ts:card/ts:origins/ethereum:event"></selector>
<field xpath="@type"></field>
</keyref>
<key name="namedTypeName">
<selector xpath="asnx:module/namedType"></selector>
<field xpath="@name"></field>
</key>
<key name="token-attributes">
<selector xpath="ts:attribute"></selector>
<field xpath="@name"></field>
Expand All @@ -67,6 +75,7 @@
<complexType>
<sequence>
<element minOccurs="0" name="label" type="ts:text"/>
<element minOccurs="0" ref="ts:origins"/>
<element minOccurs="0" maxOccurs="unbounded" ref="ts:attribute"/>
<element minOccurs="0" maxOccurs="unbounded" ref="ts:selection"/>
<!-- consider putting input and output in transaction
Expand Down