-
Notifications
You must be signed in to change notification settings - Fork 0
/
+page.svelte
75 lines (66 loc) · 2.03 KB
/
+page.svelte
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
<!-- Svelte demo code for the "Using avatars" section of the Circles SDK documentation -->
<script lang="ts">
import {Sdk, type ChainConfig, type SdkContractRunner, type AvatarInterface} from "@circles-sdk/sdk";
import {BrowserProvider} from "ethers";
import {onMount} from "svelte";
let avatar: AvatarInterface | undefined;
let sdk: Sdk | undefined;
let error: Error | undefined;
const chainConfig: ChainConfig = {
pathfinderUrl: 'https://pathfinder.aboutcircles.com',
circlesRpcUrl: "https://rpc.helsinki.aboutcircles.com",
v1HubAddress: "0x29b9a7fbb8995b2423a71cc17cf9810798f6c543"
};
async function getRunner(): Promise<SdkContractRunner> {
const w: any = window;
const browserProvider = new BrowserProvider(w.ethereum);
const signer = await browserProvider.getSigner();
const address = await signer.getAddress();
return {
runner: signer,
address: address
};
}
async function getSdk() {
const runner = await getRunner();
return new Sdk(chainConfig, runner);
}
onMount(async () => {
sdk = await getSdk();
});
async function registerAvatar() {
error = undefined;
try {
avatar = await sdk?.registerHuman();
// If you want to sign up an organization:
// avatar = await sdk?.registerOrganization();
} catch (e) {
error = <any>e;
}
}
async function loadAvatar() {
error = undefined;
try {
avatar = await sdk?.getAvatar(sdk?.contractRunner.address);
} catch (e) {
error = <any>e;
}
}
</script>
{#if error}
<p style="color: darkred">
Error: {error.message}
</p>
{/if}
{#if !avatar}
<button on:click={registerAvatar}>
Register Human
</button>
<button on:click={loadAvatar}>
Load avatar
</button>
{:else}
<p>
Avatar {avatar.avatarInfo?.avatar} is {avatar.avatarInfo?.type}
</p>
{/if}