-
Notifications
You must be signed in to change notification settings - Fork 0
/
+page.svelte
107 lines (95 loc) · 3.01 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
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
<!-- Svelte demo code for the "Interact with other avatars" section of the Circles SDK documentation -->
<script lang="ts">
import {Sdk, type ChainConfig, type SdkContractRunner, Avatar} from "@circles-sdk/sdk";
import {BrowserProvider} from "ethers";
import {onMount} from "svelte";
let avatar: Avatar | undefined;
let sdk: Sdk | undefined;
let error: Error | undefined;
let trustAddress: string = "";
let transferRecipientAddress: string = "";
let transferAmount: number = 0;
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);
}
async function handleError<T>(fn: () => Promise<T>): Promise<T | undefined> {
try {
return await fn();
} catch (e) {
error = <any>e;
return undefined;
}
}
onMount(async () => {
sdk = await getSdk();
await handleError(async () => {
avatar = await sdk?.getAvatar(sdk?.contractRunner.address);
});
});
async function trust() {
await handleError(async () => {
await avatar?.trust(trustAddress);
});
}
async function untrust() {
await handleError(async () => {
await avatar?.untrust(trustAddress);
});
}
async function mint() {
await handleError(async () => {
await avatar?.personalMint();
});
}
async function transfer() {
await handleError(async () => {
await avatar?.transfer(transferRecipientAddress, transferAmount);
});
}
</script>
{#if error}
<p style="color: darkred">
Error: {error.message}
</p>
{/if}
{#if !avatar}
<p>
Loading avatar...
</p>
{:else}
<p>
Avatar {avatar.avatarInfo?.avatar} is {avatar.avatarInfo?.type}
</p>
<p>
Add/remove trust relations:<br/>
<input type="text" bind:value={trustAddress} placeholder="0x.."/>
<button on:click={trust}>Trust</button>
<button on:click={untrust}>Untrust</button>
</p>
<p>
Mint personal tokens:<br/>
<button on:click={mint}>Mint</button>
</p>
<p>
Transfer Circles:<br/>
<input type="text" bind:value={transferRecipientAddress} placeholder="0x.."/>
<input type="number" bind:value={transferAmount} placeholder="Amount"/>
<button on:click={transfer}>Transfer</button>
</p>
{/if}