-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.ts
140 lines (119 loc) · 5.21 KB
/
main.ts
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
import './style.css'
import { web3Accounts, web3Enable, web3FromSource } from '@polkadot/extension-dapp'
import { ApiPromise, WsProvider } from '@polkadot/api'
import { InjectedAccountWithMeta } from '@polkadot/extension-inject/types'
import { MultiAddress, pas } from '@polkadot-api/descriptors'
import { createClient } from 'polkadot-api'
import { getWsProvider } from 'polkadot-api/ws-provider/web'
import { withPolkadotSdkCompat } from 'polkadot-api/polkadot-sdk-compat'
import {
getInjectedExtensions,
connectInjectedExtension,
InjectedPolkadotAccount,
InjectedExtension
} from 'polkadot-api/pjs-signer'
import { JSONprint } from './utils'
const PASEO_WS_PROVIDER = 'wss://rpc.ibp.network/paseo'
const EXAMPLE_DAPP_NAME = 'example-dapp'
let injectedAccountsPjs: InjectedAccountWithMeta[] = []
let injectedAccountsPapi: InjectedPolkadotAccount[] = []
const client = createClient(withPolkadotSdkCompat(getWsProvider(PASEO_WS_PROVIDER)))
const papi = client.getTypedApi(pas)
document
.querySelector<HTMLButtonElement>('#connect-accounts-papi')!
.addEventListener('click', async () => {
// Get the list of installed extensions
const extensions: string[] = getInjectedExtensions()
document.querySelector<HTMLDivElement>('#all-extensions')!.innerHTML =
JSON.stringify(extensions)
if (extensions.length === 0) {
document.querySelector<HTMLDivElement>('#injected-error')!.innerHTML = 'rejected'
} else {
try {
// Connect to an extension
const selectedExtension: InjectedExtension = await connectInjectedExtension(
extensions[0],
EXAMPLE_DAPP_NAME
)
// Get accounts registered in the extension
injectedAccountsPapi = selectedExtension.getAccounts()
document.querySelector<HTMLDivElement>('#all-accounts')!.innerHTML =
JSON.stringify(injectedAccountsPapi)
} catch (error: any) {
document.querySelector<HTMLDivElement>('#injected-error')!.innerHTML = error.toString()
}
}
})
document
.querySelector<HTMLButtonElement>('#connect-accounts-pjs')!
.addEventListener('click', async () => {
// returns an array of all the injected sources
// (this needs to be called first, before other requests)
const allInjected = await web3Enable(EXAMPLE_DAPP_NAME)
if (allInjected.length === 0) {
document.querySelector<HTMLDivElement>('#injected-error')!.innerHTML = 'rejected'
} else {
document.querySelector<HTMLDivElement>('#injected')!.innerHTML = JSON.stringify(allInjected)
// returns an array of { address, meta: { name, source } }
// meta.source contains the name of the wallet that provides this account
const allAccounts = await web3Accounts()
document.querySelector<HTMLDivElement>('#all-accounts')!.innerHTML =
JSON.stringify(allAccounts)
injectedAccountsPjs = allAccounts
}
})
document.querySelector<HTMLButtonElement>('#send-tx-pjs')!.addEventListener('click', async () => {
const provider = new WsProvider(PASEO_WS_PROVIDER)
const pjsApi = await ApiPromise.create({ provider })
const account = injectedAccountsPjs[0]
if (injectedAccountsPjs.length === 0) {
console.error('No injected account')
return
}
const amount = document.querySelector<HTMLInputElement>('#amount-input')?.value
const transferExtrinsic = pjsApi.tx.balances.transferKeepAlive(account.address, amount)
const injector = await web3FromSource(account.meta.source)
transferExtrinsic
.signAndSend(account.address, { signer: injector.signer }, ({ events, txHash, status }) => {
document.querySelector<HTMLDivElement>('#tx-hash')!.innerHTML = txHash.toString()
if (status.isInBlock) {
events.forEach(({ event: { method, section } }) => {
const li = document.createElement('li')
li.innerHTML = `${section}.${method}`
document.querySelector<HTMLPreElement>('#tx-events')!.appendChild(li)
})
}
})
.catch((error: any) => {
document.querySelector<HTMLDivElement>('#tx-error')!.innerHTML = error
console.log(':( transaction failed', error)
})
})
document.querySelector<HTMLButtonElement>('#send-tx-papi')!.addEventListener('click', async () => {
const account = injectedAccountsPapi[0]
if (injectedAccountsPapi.length === 0) {
console.error('No injected account')
return
}
const amount = document.querySelector<HTMLInputElement>('#amount-input')?.value
const transferExtrinsic = papi.tx.Balances.transfer_keep_alive({
dest: MultiAddress.Id(account.address),
value: amount ? BigInt(amount) : 0n
})
transferExtrinsic.signSubmitAndWatch(account.polkadotSigner, { at: 'best' }).subscribe({
next: (event) => {
document.querySelector<HTMLDivElement>('#tx-hash')!.innerHTML = event.txHash.toString()
if (event.type === 'txBestBlocksState' && event.found) {
event.events.forEach((ev) => {
const li = document.createElement('li')
li.innerHTML = JSONprint(ev)
document.querySelector<HTMLPreElement>('#tx-events')!.appendChild(li)
})
}
},
error: (error: any) => {
document.querySelector<HTMLDivElement>('#tx-error')!.innerHTML = error.toString()
console.log(':( transaction failed', error)
}
})
})