Skip to content
This repository has been archived by the owner on Aug 16, 2024. It is now read-only.

Commit

Permalink
feat: add get button methods function
Browse files Browse the repository at this point in the history
  • Loading branch information
xstelea committed Dec 1, 2022
1 parent f17d2e1 commit 6caf742
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 4 deletions.
30 changes: 26 additions & 4 deletions examples/index.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,39 @@
import '../src/connect-button'
import { configure, setState } from '../src/connect-button'
import { configure, setState, getMethods } from '../src/connect-button'

const { getWalletData, destroy } = configure({
const { getWalletData, sendTransaction, destroy } = configure({
dAppId: 'dashboard',
logLevel: 'DEBUG',
onConnect: async () => {
setState({ loading: true, connected: false })
const result = await getWalletData({
await getWalletData({
oneTimeAccountsWithoutProofOfOwnership: {},
})
}).andThen(({ oneTimeAccounts }) =>
sendTransaction({
version: 1,
transactionManifest: `
CREATE_RESOURCE Enum("Fungible", 18u8) Map<String, String>("description", "Dedo test token", "name", "Dedo", "symbol", "DEDO") Map<Enum, Tuple>() Some(Enum("Fungible", Decimal("15000")));
CALL_METHOD ComponentAddress("${oneTimeAccounts[0].address}") "deposit_batch" Expression("ENTIRE_WORKTOP");`,
}).andThen(sendTx)
)
setState({ loading: false, connected: true })
},
onDestroy: () => {
destroy()
},
})

const sendTx = () => {
return getMethods()
.getWalletData({
oneTimeAccountsWithoutProofOfOwnership: {},
})
.andThen(({ oneTimeAccounts }) =>
sendTransaction({
version: 1,
transactionManifest: `
CREATE_RESOURCE Enum("Fungible", 18u8) Map<String, String>("description", "Dedo test token", "name", "Dedo", "symbol", "DEDO") Map<Enum, Tuple>() Some(Enum("Fungible", Decimal("15000")));
CALL_METHOD ComponentAddress("${oneTimeAccounts[0].address}") "deposit_batch" Expression("ENTIRE_WORKTOP");`,
}).map(() => {})
)
}
18 changes: 18 additions & 0 deletions src/connect-button.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ let walletSdk: WalletSdkType
const onConnect$ = onConnectSubject.asObservable()
const onDestroy$ = onDestroySubject.asObservable()

let buttonApi: ReturnType<typeof configure> | undefined

export const configure = (
input: Parameters<typeof WalletSdk>[0] & {
onConnect: () => void
Expand All @@ -32,16 +34,32 @@ export const configure = (
.subscribe()
)

buttonApi = {
getWalletData: walletSdk.request,
sendTransaction: walletSdk.sendTransaction,
destroy: () => {
subscriptions?.unsubscribe()
walletSdk.destroy()
},
}

return {
getWalletData: walletSdk.request,
sendTransaction: walletSdk.sendTransaction,
destroy: () => {
subscriptions?.unsubscribe()
walletSdk.destroy()
buttonApi = undefined
},
}
}

export const getMethods = () => {
if (!buttonApi) throw new Error('connect button has not been configured')

return buttonApi
}

const getConnectButtonElement = () => {
const connectButtonElement = document.querySelector('connect-button')
if (!connectButtonElement) {
Expand Down

0 comments on commit 6caf742

Please sign in to comment.