Skip to content

Commit

Permalink
fix: handle oneTime requests
Browse files Browse the repository at this point in the history
  • Loading branch information
xstelea committed Feb 10, 2023
1 parent 84547f1 commit cc7a5dc
Show file tree
Hide file tree
Showing 11 changed files with 704 additions and 563 deletions.
2 changes: 1 addition & 1 deletion examples/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<meta charset="UTF-8" />
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Vite + Lit + TS</title>
<title>Example dApp</title>
<script type="module" src="/index.ts"></script>
</head>
<body></body>
Expand Down
191 changes: 144 additions & 47 deletions examples/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,45 @@ import { RadixDappToolkit } from '../src/radix-dapp-toolkit'
import { Logger } from 'tslog'
import { html, render, LitElement, css } from 'lit'
import { customElement, state } from 'lit/decorators.js'
import { Persona, State } from '../src/_types'
import { Account } from '@radixdlt/connect-button'

const free_funds = (account_component_address: string) => `
CALL_METHOD
ComponentAddress("component_tdx_22_1qgehpqdhhr62xh76wh6gppnyn88a0uau68epljprvj3s7s5gc3")
"free";
const update_metadata = (account_component_address: string) =>
`SET_METADATA ComponentAddress("${account_component_address}") "name" "test name";
SET_METADATA ComponentAddress("${account_component_address}") "description" "test description";
SET_METADATA ComponentAddress("${account_component_address}") "domain" "test.domain";
SET_METADATA ComponentAddress("${account_component_address}") "account_type" "dapp definition";`.trim()

const create_token = (account_component_address: string) => `
CREATE_FUNGIBLE_RESOURCE
18u8
Map<String, String>(
"name", "MyResource", # Resource Name
"symbol", "RSRC", # Resource Symbol
"description", "A very innovative and important resource" # Resource Description
)
Map<Enum, Tuple>(
Enum("ResourceMethodAuthKey::Withdraw"), Tuple(Enum("AccessRule::AllowAll"), Enum("AccessRule::DenyAll")),
Enum("ResourceMethodAuthKey::Deposit"), Tuple(Enum("AccessRule::AllowAll"), Enum("AccessRule::DenyAll"))
)
None;
CALL_METHOD
ComponentAddress("${account_component_address}")
"deposit_batch"
Expression("ENTIRE_WORKTOP");`
Expression("ENTIRE_WORKTOP");
`

const update_metadata = (account_component_address: string) => `
SET_METADATA
ComponentAddress("${account_component_address}") "name" "test name";
SET_METADATA
ComponentAddress("${account_component_address}") "description" "test description";
const transfer_token = (payer: string, payee: string) => `
CALL_METHOD
ComponentAddress("${payer}")
"withdraw_by_amount"
Decimal("100")
ResourceAddress("resource_tdx_22_1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqj3nwpk");
SET_METADATA
ComponentAddress("${account_component_address}") "domain" "test.domain";
SET_METADATA
ComponentAddress("${account_component_address}") "account_type" "dapp definition";`
CALL_METHOD
ComponentAddress("${payee}")
"deposit_batch"
Expression("ENTIRE_WORKTOP");`

@customElement('example-dapp')
// @ts-ignore
Expand Down Expand Up @@ -54,6 +68,13 @@ class ExampleDapp extends LitElement {
margin-bottom: 2rem;
}
pre {
text-align: left;
background: black;
color: greenyellow;
padding: 1rem;
}
h1 {
font-size: 1.5rem;
padding: 0;
Expand All @@ -77,28 +98,26 @@ class ExampleDapp extends LitElement {
accounts: Account[] = []

@state()
persona?: Persona
response: any

private rdt = RadixDappToolkit(
{ dAppDefinitionAddress: 'acc_123abc', dAppName: 'Test dApp' },
(requestData) => {
requestData({
accounts: { quantifier: 'atLeast', quantity: 1 },
}).map(({ data: { accounts, persona } }) => {
this.accounts = accounts
this.persona = persona
}).map((response) => {
this.accounts = response.data.accounts
this.response = response
})
},
{
logger: new Logger(),
networkId: 34,
onDisconnect: () => {
this.accounts = []
this.persona = undefined
},
onInit: ({ accounts, persona }) => {
onInit: ({ accounts }) => {
this.accounts = accounts ?? []
this.persona = persona
},
explorer: {
baseUrl: 'https://hammunet-dashboard.rdx-works-main.extratools.works/',
Expand All @@ -108,49 +127,116 @@ class ExampleDapp extends LitElement {
}
)

private sendTransactionTemplate() {
return this.persona
private oneTimeRequest() {
return html`<radix-button
fullWidth
@click=${() => {
this.rdt
.requestData({
accounts: { quantifier: 'exactly', quantity: 1, oneTime: true },
})
.map((response) => {
this.accounts = response.accounts
this.response = response
})
.mapErr((response) => {
this.response = response
})
}}
>
OneTimeRequest
</radix-button>`
}

private createTokenRequest() {
return this.accounts.length
? html`<radix-button
fullWidth
@click=${() => {
this.rdt.sendTransaction({
version: 1,
transactionManifest: free_funds(this.accounts[0].address),
})
this.rdt
.sendTransaction({
transactionManifest: create_token(this.accounts[0].address),
version: 1,
})
.map((response) => {
this.response = response
})
.mapErr((response) => {
this.response = response
})
}}
>
Get free XRD
Create token
</radix-button>`
: html`<h2>Connect wallet to use dApp</h2>`
: ''
}

private oneTimeRequest() {
return this.persona
private ongoingRequest() {
return html`<radix-button
fullWidth
@click=${() => {
this.rdt
.requestData({
accounts: { quantifier: 'exactly', quantity: 2 },
})
.map((response) => {
this.accounts = response.accounts
this.response = response
})
.mapErr((response) => {
this.response = response
})
}}
>
OngoingRequest
</radix-button>`
}

private updateMetadata() {
return this.accounts.length
? html`<radix-button
fullWidth
@click=${() => {
this.rdt.requestData({
accounts: { quantifier: 'atLeast', quantity: 1 },
})
this.rdt
.sendTransaction({
version: 1,
transactionManifest: update_metadata(this.accounts[0].address),
})
.map((response) => {
this.response = response
})
.mapErr((response) => {
this.response = response
})
}}
>
OneTimeRequest
Update metadata
</radix-button>`
: ''
}

private updateMetadata() {
return this.persona
private transferToken() {
return this.accounts.length
? html`<radix-button
fullWidth
@click=${() => {
this.rdt.sendTransaction({
version: 1,
transactionManifest: update_metadata(this.accounts[0].address),
})
this.rdt
.sendTransaction({
version: 1,
transactionManifest: transfer_token(
this.accounts[0].address,
this.accounts[0].address
),
})
.map((response) => {
this.response = response
})
.mapErr((response) => {
this.response = response
})
}}
>
Update metadata
Transfer token
</radix-button>`
: ''
}
Expand All @@ -162,11 +248,22 @@ class ExampleDapp extends LitElement {
</header>`
}

private walletResponseTemplate() {
return this.response
? html`<pre>
Wallet response
${JSON.stringify(this.response, null, 2)}</pre
>`
: ''
}

render() {
return html`<div>
${this.headerTemplate()}
<div class="content">
${this.sendTransactionTemplate()} ${this.updateMetadata()}
${this.oneTimeRequest()} ${this.ongoingRequest()}
${this.createTokenRequest()} ${this.transferToken()}
${this.updateMetadata()} ${this.walletResponseTemplate()}
</div>
</div>`
}
Expand Down
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@
"dev": "vite serve examples",
"build": "tsc && vite build",
"prepare": "npx husky install",
"test": "node --experimental-vm-modules node_modules/.bin/jest",
"test:watch": "node --experimental-vm-modules node_modules/.bin/jest --watch"
"test": "node --experimental-vm-modules node_modules/jest/bin/jest.js",
"test:watch": "node --experimental-vm-modules node_modules/jest/bin/jest.js --watch"
},
"dependencies": {
"@radixdlt/connect-button": "0.11.0-alpha.8",
"@radixdlt/wallet-sdk": "0.6.0-beta.10",
"@radixdlt/wallet-sdk": "0.6.0-beta.11",
"neverthrow": "^6.0.0",
"rxjs": "^7.8.0",
"tslog": "^4.7.2"
Expand All @@ -50,8 +50,8 @@
"@types/jest": "^29.4.0",
"babel-preset-vite": "^1.1.0",
"husky": "^8.0.3",
"jest": "^29.4.1",
"jest-environment-jsdom": "^29.4.1",
"jest": "^29.4.2",
"jest-environment-jsdom": "^29.4.2",
"lit": "^2.6.1",
"ts-jest": "^29.0.5",
"ts-node": "^10.9.1",
Expand Down
Loading

0 comments on commit cc7a5dc

Please sign in to comment.