Skip to content

Commit

Permalink
Experimental caching of commitments
Browse files Browse the repository at this point in the history
  • Loading branch information
KaffinPX committed Sep 16, 2024
1 parent fbed8d4 commit 14efa0d
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ function App() {
</Button>
</div>
<div className='flex bg-card rounded-md gap-1 px-1'>
<Select value={address} onValueChange={(address) => {
<Select value={address} disabled={!account?.addresses} onValueChange={(address) => {
setAddress(address)
}}>
<SelectTrigger className="w-[160px] border-0">
Expand Down
3 changes: 1 addition & 2 deletions src/contexts/Indexer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,7 @@ export function IndexerProvider ({ children }: {
}, [ indexer ])

useEffect(() => {
if (indexer.url === '') return setTokens({})

setTokens({})
refresh()
}, [ indexer, refresh ])

Expand Down
14 changes: 11 additions & 3 deletions src/pages/Account/Mintage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ function Mintage () {
if (commitAddress) setCommitAddress(undefined)

if (!ticker) return
if (tokens[ticker].state === 'finished') {
if (tokens[ticker]?.state === 'finished') {
toast.error(`${ticker} token minting is no longer available as it has been completed.`)
return
}
Expand All @@ -37,8 +37,12 @@ function Mintage () {

inscription.write(script, XOnlyPublicKey.fromAddress(new Address(address!)).toString())

const scriptAddress = addressFromScriptPublicKey(script.createPayToScriptHashScript(), networkId!)!.toString()
const commitment = localStorage.getItem(scriptAddress)

setScript(script.toString())
setCommitAddress(addressFromScriptPublicKey(script.createPayToScriptHashScript(), networkId!)!.toString())
if (commitment) setCommit(commitment)
setCommitAddress(scriptAddress)
}, [ address, ticker ])

useEffect(() => {
Expand All @@ -55,7 +59,7 @@ function Mintage () {
</CardHeader>
<CardContent className="grid grid-cols-6 items-center">
<Label htmlFor="ticker" className='font-bold'>Ticker:</Label>
<Select value={ticker} onValueChange={(ticker) => {
<Select value={ticker} disabled={Object.keys(tokens).length === 0 || !!commit} onValueChange={(ticker) => {
setTicker(ticker)
}}>
<SelectTrigger className="col-span-5" id='ticker'>
Expand All @@ -75,7 +79,9 @@ function Mintage () {
if (!commit) {
invoke('transact', [[[ commitAddress!, '0.2' ]]]).then(commitment => {
const transaction = JSON.parse(commitment)

setCommit(transaction.id)
localStorage.setItem(commitAddress!, transaction.id)

toast.success('Committed token mint request succesfully!', {
action: {
Expand All @@ -95,7 +101,9 @@ function Mintage () {
script: script
}]]).then((reveal) => {
const transaction = JSON.parse(reveal)

setCommit(undefined)
localStorage.removeItem(commitAddress!)

toast.success('Revealed and completed token mint request succesfully!', {
action: {
Expand Down

0 comments on commit 14efa0d

Please sign in to comment.