-
Notifications
You must be signed in to change notification settings - Fork 0
/
share.html
214 lines (190 loc) · 6.44 KB
/
share.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
<!doctype html>
<!-- <form id="form"> -->
<p>This will share all signals present and historical (privileges 1, 3, 4), as well as data streaming (6), until the given Unix timestamp.
<p>Share to address: <input type="text" id="share_to">
<p>Your vehicle: <select id="vehicle"></select>
<p>Share until <a href="https://www.unixtimestamp.com">Unix timestamp</a>: <input type="number" id="expiry">
<p><button id="clicker">Share</button>
<p><button id="stream">Create Stream</button>
<p id="status">
<!-- </form> -->
<script type="module">
import { createWalletClient, createPublicClient, http, custom } from 'https://esm.sh/viem';
import { polygon } from 'https://esm.sh/viem/chains';
export const publicClient = createPublicClient({
chain: polygon,
transport: http()
})
export const walletClient = createWalletClient({
chain: polygon,
transport: custom(window.ethereum),
});
const [account] = await walletClient.requestAddresses()
console.log(account);
const resp = await fetch("https://identity-api.dimo.zone/query", {
"method": "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({
query: `
query MyVehicles($me: Address!)
{
vehicles(first: 10, filterBy: {owner: $me}) {
nodes {
tokenId
definition {
make
model
year
}
}
}
}`,
variables: {
me: account,
}
})
})
const respBody = await resp.json();
for (const veh of respBody.data.vehicles.nodes) {
const thisCar = document.createElement("option");
thisCar.value = veh.tokenId;
thisCar.innerText = `Token id ${veh.tokenId}: ${veh.definition.year} ${veh.definition.make} ${veh.definition.model}`;
document.getElementById("vehicle").appendChild(thisCar);
}
await walletClient.switchChain({ id: polygon.id })
const streamrAbi = [{
"inputs": [
{
"internalType": "uint256",
"name": "vehicleId",
"type": "uint256"
}
],
"name": "getVehicleStream",
"outputs": [
{
"internalType": "string",
"name": "streamId",
"type": "string"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
"internalType": "uint256",
"name": "vehicleId",
"type": "uint256"
}
],
"name": "createVehicleStream",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
}
]
const abi = [{
"inputs": [
{
"components": [
{
"internalType": "uint256",
"name": "tokenId",
"type": "uint256"
},
{
"internalType": "uint256",
"name": "privId",
"type": "uint256"
},
{
"internalType": "address",
"name": "user",
"type": "address"
},
{
"internalType": "uint256",
"name": "expires",
"type": "uint256"
}
],
"internalType": "struct MultiPrivilege.SetPrivilegeData[]",
"name": "privData",
"type": "tuple[]"
}
],
"name": "setPrivileges",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
}]
async function callback() {
const shareTo = document.getElementById("share_to").value;
const tokenId = parseInt(document.getElementById("vehicle").value);
const expiry = parseInt(document.getElementById("expiry").value);
console.log(shareTo, tokenId)
const { request } = await publicClient.simulateContract({
address: '0xbA5738a18d83D41847dfFbDC6101d37C69c9B0cF',
abi: abi,
functionName: 'setPrivileges',
args: [[
{
tokenId: tokenId,
privId: 1,
user: shareTo,
expires: expiry
},
{
tokenId: tokenId,
privId: 3,
user: shareTo,
expires: expiry
},
{
tokenId: tokenId,
privId: 4,
user: shareTo,
expires: expiry
},
{
tokenId: tokenId,
privId: 6,
user: shareTo,
expires: expiry
}]
],
account
})
const txHash = await walletClient.writeContract(request);
document.getElementById("status").innerHTML = `Shared: <a href="https://polygonscan.com/tx/${txHash}">transaction <code>${txHash}</code></a>`;
}
async function stream() {
const tokenId = parseInt(document.getElementById("vehicle").value);
const readRes = await publicClient.readContract({
address: "0xFA8beC73cebB9D88FF88a2f75E7D7312f2Fd39EC",
abi: streamrAbi,
functionName: 'getVehicleStream',
args: [tokenId],
})
if (readRes === "") {
const { request } = await publicClient.simulateContract({
address: '0xFA8beC73cebB9D88FF88a2f75E7D7312f2Fd39EC',
abi: streamrAbi,
functionName: 'createVehicleStream',
args: [tokenId],
account
})
const txHash = await walletClient.writeContract(request);
document.getElementById("status").innerHTML = `Stream creation: <a href="https://polygonscan.com/tx/${txHash}">transaction</a>`;
} else {
const escaped = encodeURIComponent(readRes);
document.getElementById("status").innerHTML = `Vehicle already has stream <a href="https://streamr.network/hub/streams/${escaped}"><code>${readRes}</code></a>`;
}
}
document.getElementById("clicker").onclick=callback;
document.getElementById("stream").onclick=stream;
</script>