Skip to content

Commit 2bc5140

Browse files
author
Weiwu Zhang
authored
Merge pull request #373 from AlphaWallet/correct-violation-of-rfc4912+add-origins-in-activity
Add origins in activity
2 parents 98064be + ea565c9 commit 2bc5140

File tree

5 files changed

+78
-111
lines changed

5 files changed

+78
-111
lines changed

ERCs/ERC20/ERC20-TokenScript.xml

Lines changed: 13 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -32,21 +32,16 @@
3232
<sequence>
3333
<element name="from" ethereum:type="address" ethereum:indexed="true"/>
3434
<element name="to" ethereum:type="address" ethereum:indexed="true"/>
35-
<element name="value" ethereum:type="uint256" ethereum:indexed="false"/>
35+
<element name="amount" ethereum:type="uint256" ethereum:indexed="false"/>
3636
</sequence>
3737
</type>
3838
</namedType>
3939
</asnx:module>
4040
<ts:cards>
4141
<ts:card type="activity" name="sent">
42-
<ts:attribute name="amount">
43-
<ts:type>
44-
<ts:syntax>1.3.6.1.4.1.1466.115.121.1.36</ts:syntax>
45-
</ts:type>
46-
<ts:origins>
47-
<ethereum:event type="Transfer" filter="from=${ownerAddress}" select="value"/>
48-
</ts:origins>
49-
</ts:attribute>
42+
<ts:origins>
43+
<ethereum:event type="Transfer" filter="from=${ownerAddress}"/>
44+
</ts:origins>
5045
<ts:item-view xml:lang="en" xmlns="http://www.w3.org/1999/xhtml">
5146
<style type="text/css">&style;</style>
5247
<script type="text/javascript">&item-view-sent.en;</script>
@@ -57,14 +52,9 @@
5752
</ts:view>
5853
</ts:card>
5954
<ts:card type="activity" name="received">
60-
<ts:attribute name="amount">
61-
<ts:type>
62-
<ts:syntax>1.3.6.1.4.1.1466.115.121.1.36</ts:syntax>
63-
</ts:type>
64-
<ts:origins>
65-
<ethereum:event type="Transfer" filter="to=${ownerAddress}" select="value"/>
66-
</ts:origins>
67-
</ts:attribute>
55+
<ts:origins>
56+
<ethereum:event type="Transfer" filter="to=${ownerAddress}" />
57+
</ts:origins>
6858
<ts:item-view xml:lang="en" xmlns="http://www.w3.org/1999/xhtml">
6959
<style type="text/css">&style;</style>
7060
<script type="text/javascript">&item-view-received.en;</script>
@@ -76,22 +66,9 @@
7666
</ts:card>
7767

7868
<ts:card type="activity" name="ownerApproved">
79-
<ts:attribute name="amount">
80-
<ts:type>
81-
<ts:syntax>1.3.6.1.4.1.1466.115.121.1.36</ts:syntax>
82-
</ts:type>
83-
<ts:origins>
84-
<ethereum:event type="Approval" filter="owner=${ownerAddress}" select="value"/>
85-
</ts:origins>
86-
</ts:attribute>
87-
<ts:attribute name="to">
88-
<ts:type>
89-
<ts:syntax>1.3.6.1.4.1.1466.115.121.1.36</ts:syntax>
90-
</ts:type>
91-
<ts:origins>
92-
<ethereum:event type="Approval" filter="owner=${ownerAddress}" select="spender"/>
93-
</ts:origins>
94-
</ts:attribute>
69+
<ts:origins>
70+
<ethereum:event type="Approval" filter="owner=${ownerAddress}"/>
71+
</ts:origins>
9572
<ts:item-view xml:lang="en" xmlns="http://www.w3.org/1999/xhtml">
9673
<style type="text/css">&style;</style>
9774
<script type="text/javascript">&item-view-ownerApproved.en;</script>
@@ -103,22 +80,9 @@
10380
</ts:card>
10481

10582
<ts:card type="activity" name="approvalObtained">
106-
<ts:attribute name="amount">
107-
<ts:type>
108-
<ts:syntax>1.3.6.1.4.1.1466.115.121.1.36</ts:syntax>
109-
</ts:type>
110-
<ts:origins>
111-
<ethereum:event type="Approval" filter="spender=${ownerAddress}" select="value"/>
112-
</ts:origins>
113-
</ts:attribute>
114-
<ts:attribute name="from">
115-
<ts:type>
116-
<ts:syntax>1.3.6.1.4.1.1466.115.121.1.36</ts:syntax>
117-
</ts:type>
118-
<ts:origins>
119-
<ethereum:event type="Approval" filter="spender=${ownerAddress}" select="owner"/>
120-
</ts:origins>
121-
</ts:attribute>
83+
<ts:origins>
84+
<ethereum:event type="Approval" filter="spender=${ownerAddress}"/>
85+
</ts:origins>
12286
<ts:item-view xml:lang="en" xmlns="http://www.w3.org/1999/xhtml">
12387
<style type="text/css">&style;</style>
12488
<script type="text/javascript">&item-view-approvalObtained.en;</script>

ERCs/ERC721/ERC721-TokenScript.xml

Lines changed: 22 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -21,32 +21,29 @@
2121
<namedType name="Approval">
2222
<type>
2323
<sequence>
24-
<element name="_owner" ethereum:type="address" ethereum:indexed="true"/>
25-
<element name="_approved" ethereum:type="address" ethereum:indexed="true"/>
26-
<element name="_tokenId" ethereum:type="uint256" ethereum:indexed="false"/>
24+
<element name="owner" ethereum:type="address" ethereum:indexed="true"/>
25+
<element name="approved" ethereum:type="address" ethereum:indexed="true"/>
26+
<element name="tokenId" ethereum:type="uint256" ethereum:indexed="false"/>
2727
</sequence>
2828
</type>
2929
</namedType>
3030
<namedType name="Transfer">
3131
<type>
3232
<sequence>
33-
<element name="_from" ethereum:type="address" ethereum:indexed="true"/>
34-
<element name="_to" ethereum:type="address" ethereum:indexed="true"/>
35-
<element name="_tokenId" ethereum:type="uint256" ethereum:indexed="false"/>
33+
<element name="from" ethereum:type="address" ethereum:indexed="true"/>
34+
<element name="to" ethereum:type="address" ethereum:indexed="true"/>
35+
<element name="tokenId" ethereum:type="uint256" ethereum:indexed="false"/>
3636
</sequence>
3737
</type>
3838
</namedType>
3939
</asnx:module>
4040
<ts:cards>
4141
<ts:card type="activity" name="sent">
42-
<ts:attribute name="_tokenId">
43-
<ts:type>
44-
<ts:syntax>1.3.6.1.4.1.1466.115.121.1.36</ts:syntax>
45-
</ts:type>
46-
<ts:origins>
47-
<ethereum:event type="Transfer" filter="${_from}=${ownerAddress}" select="_tokenId"/>
48-
</ts:origins>
49-
</ts:attribute>
42+
<ts:origins>
43+
<!-- this gives you card attributes from, to, tokenID, which is different than the
44+
token's attributes -->
45+
<ethereum:event type="Transfer" filter="from=${ownerAddress}"/>
46+
</ts:origins>
5047
<ts:item-view xml:lang="en" xmlns="http://www.w3.org/1999/xhtml">
5148
<style type="text/css">&style;</style>
5249
<script type="text/javascript">&item-view-sent.en;</script>
@@ -57,14 +54,11 @@
5754
</ts:view>
5855
</ts:card>
5956
<ts:card type="activity" name="received">
60-
<ts:attribute name="_tokenId">
61-
<ts:type>
62-
<ts:syntax>1.3.6.1.4.1.1466.115.121.1.36</ts:syntax>
63-
</ts:type>
64-
<ts:origins>
65-
<ethereum:event type="Transfer" filter="${_to}=${ownerAddress}" select="_tokenId"/>
66-
</ts:origins>
67-
</ts:attribute>
57+
<ts:origins>
58+
<!-- this gives you card attributes from, to, tokenId, which is different than the
59+
token's attributes which also have tokenId -->
60+
<ethereum:event type="Transfer" filter="to=${ownerAddress}"/>
61+
</ts:origins>
6862
<ts:item-view xml:lang="en" xmlns="http://www.w3.org/1999/xhtml">
6963
<style type="text/css">&style;</style>
7064
<script type="text/javascript">&item-view-received.en;</script>
@@ -76,22 +70,9 @@
7670
</ts:card>
7771

7872
<ts:card type="activity" name="ownerApproved">
79-
<ts:attribute name="_tokenId">
80-
<ts:type>
81-
<ts:syntax>1.3.6.1.4.1.1466.115.121.1.36</ts:syntax>
82-
</ts:type>
83-
<ts:origins>
84-
<ethereum:event type="Approval" filter="${_from}=${ownerAddress}" select="_tokenId"/>
85-
</ts:origins>
86-
</ts:attribute>
87-
<ts:attribute name="to">
88-
<ts:type>
89-
<ts:syntax>1.3.6.1.4.1.1466.115.121.1.36</ts:syntax>
90-
</ts:type>
91-
<ts:origins>
92-
<ethereum:event type="Approval" filter="${_to}=${ownerAddress}" select="_to"/>
93-
</ts:origins>
94-
</ts:attribute>
73+
<ts:origins>
74+
<ethereum:event type="Approval" filter="${from}=${ownerAddress}"/>
75+
</ts:origins>
9576
<ts:item-view xml:lang="en" xmlns="http://www.w3.org/1999/xhtml">
9677
<style type="text/css">&style;</style>
9778
<script type="text/javascript">&item-view-ownerApproved.en;</script>
@@ -103,22 +84,9 @@
10384
</ts:card>
10485

10586
<ts:card type="activity" name="approvalObtained">
106-
<ts:attribute name="_tokenId">
107-
<ts:type>
108-
<ts:syntax>1.3.6.1.4.1.1466.115.121.1.36</ts:syntax>
109-
</ts:type>
110-
<ts:origins>
111-
<ethereum:event type="Approval" filter="${_to}=${ownerAddress}" select="_tokenId"/>
112-
</ts:origins>
113-
</ts:attribute>
114-
<ts:attribute name="from">
115-
<ts:type>
116-
<ts:syntax>1.3.6.1.4.1.1466.115.121.1.36</ts:syntax>
117-
</ts:type>
118-
<ts:origins>
119-
<ethereum:event type="Approval" filter="${_to}=${ownerAddress}" select="_from"/>
120-
</ts:origins>
121-
</ts:attribute>
87+
<ts:origins>
88+
<ethereum:event type="Approval" filter="${to}=${ownerAddress}"/>
89+
</ts:origins>
12290
<ts:item-view xml:lang="en" xmlns="http://www.w3.org/1999/xhtml">
12391
<style type="text/css">&style;</style>
12492
<script type="text/javascript">&item-view-approvalObtained.en;</script>

doc/javascript_api.md

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -190,19 +190,18 @@ TokenScript's JavaScript API is designed to be asynchronous.
190190

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

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

197197
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.
198198

199199
A simple way to implement that would be:
200200

201201
```
202-
web3.tokens.dataChanged = (oldTokens, updatedTokens, tokenCardId) => {
203-
const currentTokenInstance = updatedTokens.currentInstance
204-
const domHtml = new Token(currentTokenInstance).render()
205-
document.getElementById(tokenCardId).getElementsByClassName("contents")[0].innerHTML = domHtml
202+
web3.tokens.dataChanged = (old, updated, tokenCardId) => {
203+
const data = updated
204+
document.getElementById(tokenCardId).getElementsByClassName("contents")[0].innerHTML = new Token(data).render()
206205
}
207206
```
208207

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

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

218+
Within the `Token` class, the token data can be access like this:
219+
220+
```
221+
const tokenAttributeValue = this.props.token.tokenAttribute1
222+
```
223+
224+
and the card data like this:
225+
226+
```
227+
const cardAttributeValue = this.props.card.tokenAttribute1
228+
```
229+
230+
And if we are sure the token and card attribute names don't clash, we can pretend they are in the same namespace with:
231+
232+
```
233+
web3.tokens.dataChanged = (old, updated, tokenCardId) => {
234+
const data = Object.assign({}, updated.token, updated.card)
235+
document.getElementById(tokenCardId).getElementsByClassName("contents")[0].innerHTML = new Token(data).render()
236+
}
237+
```
238+
239+
Then this is unchanged:
240+
241+
```
242+
const attributeValue = this.props.tokenAttribute1 //or this.props.cardAttribute1
243+
```
244+
219245
Future
220246
---
221247
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.

schema/ethereum.xsd

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,8 +153,8 @@
153153
<xs:element name="event">
154154
<xs:complexType>
155155
<!-- TODO: this should be whatever ASN.X allows as element name -->
156-
<xs:attribute name="type" type="xs:NCName"/>
157-
<xs:attribute name="contract" type="xs:IDREF"/>
156+
<xs:attribute use="required" name="type" type="xs:NCName"/>
157+
<xs:attribute use="required" name="contract" type="xs:IDREF"/>
158158
<xs:attribute name="filter" type="xs:string"/>
159159
<xs:attribute name="select" type="xs:NCName"/>
160160
</xs:complexType>

schema/tokenscript.xsd

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,14 @@
5858
</sequence>
5959
<attribute name="custodian" type="boolean" default="false"/>
6060
</complexType>
61+
<keyref name="eventTypeRef" refer="ts:namedTypeName">
62+
<selector xpath="ts:cards/ts:card/ts:origins/ethereum:event"></selector>
63+
<field xpath="@type"></field>
64+
</keyref>
65+
<key name="namedTypeName">
66+
<selector xpath="asnx:module/namedType"></selector>
67+
<field xpath="@name"></field>
68+
</key>
6169
<key name="token-attributes">
6270
<selector xpath="ts:attribute"></selector>
6371
<field xpath="@name"></field>
@@ -67,6 +75,7 @@
6775
<complexType>
6876
<sequence>
6977
<element minOccurs="0" name="label" type="ts:text"/>
78+
<element minOccurs="0" ref="ts:origins"/>
7079
<element minOccurs="0" maxOccurs="unbounded" ref="ts:attribute"/>
7180
<element minOccurs="0" maxOccurs="unbounded" ref="ts:selection"/>
7281
<!-- consider putting input and output in transaction

0 commit comments

Comments
 (0)