-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' of https://github.com/MeshJS/mesh into feature/ai…
…ken-lib-part-1
- Loading branch information
Showing
13 changed files
with
6,577 additions
and
4,851 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
127 changes: 127 additions & 0 deletions
127
packages/demo/components/pages/apis/resolvers/resolveNativeScriptAddress.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
import { useState } from 'react'; | ||
import Codeblock from '../../../ui/codeblock'; | ||
import Card from '../../../ui/card'; | ||
import SectionTwoCol from '../../../common/sectionTwoCol'; | ||
import RunDemoButton from '../../../common/runDemoButton'; | ||
import RunDemoResult from '../../../common/runDemoResult'; | ||
import { | ||
resolveNativeScriptAddress, | ||
resolvePaymentKeyHash, | ||
resolveSlotNo, | ||
} from '@meshsdk/core'; | ||
import Input from '../../../ui/input'; | ||
import type { NativeScript } from '@meshsdk/core'; | ||
import { demoAddresses } from '../../../../configs/demo'; | ||
|
||
export default function ResolveNativeScriptAddress() { | ||
const [userinput, setUserinput] = useState<string>(demoAddresses.mainnet); | ||
const [userinput2, setUserinput2] = useState<string>('meshtoken'); | ||
|
||
return ( | ||
<SectionTwoCol | ||
sidebarTo="resolveNativeScriptAddress" | ||
header="Resolve Native Script Address" | ||
leftFn={Left(userinput, userinput2)} | ||
rightFn={Right(userinput, setUserinput, userinput2, setUserinput2)} | ||
/> | ||
); | ||
} | ||
|
||
function Left(userinput, userinput2) { | ||
let code1 = `import { resolveNativeScriptHash, resolvePaymentKeyHash, resolveSlotNo } from '@meshsdk/core';\n\n`; | ||
code1 += `const keyHash = resolvePaymentKeyHash('${userinput}');\n`; | ||
code1 += `\n`; | ||
code1 += `let oneYearFromNow = new Date();\n`; | ||
code1 += `oneYearFromNow.setFullYear(oneYearFromNow.getFullYear() + 1);\n`; | ||
code1 += `const slot = resolveSlotNo('mainnet', oneYearFromNow.getTime());\n`; | ||
code1 += `\n`; | ||
code1 += `const nativeScript: NativeScript = {\n`; | ||
code1 += ` type: 'all',\n`; | ||
code1 += ` scripts: [\n`; | ||
code1 += ` {\n`; | ||
code1 += ` type: 'before',\n`; | ||
code1 += ` slot: slot,\n`; | ||
code1 += ` },\n`; | ||
code1 += ` {\n`; | ||
code1 += ` type: 'sig',\n`; | ||
code1 += ` keyHash: keyHash,\n`; | ||
code1 += ` },\n`; | ||
code1 += ` ],\n`; | ||
code1 += `};\n`; | ||
code1 += `\n`; | ||
code1 += `const address = resolveNativeScriptAddress(nativeScript, ${ | ||
userinput.substring(0, 5) === 'addr1' ? 1 : 0 | ||
});\n`; | ||
|
||
return ( | ||
<> | ||
<p> | ||
Converts <code>NativeScript</code> into address. | ||
</p> | ||
<Codeblock data={code1} isJson={false} /> | ||
</> | ||
); | ||
} | ||
|
||
function Right(userinput, setUserinput, userinput2, setUserinput2) { | ||
const [loading, setLoading] = useState<boolean>(false); | ||
const [response, setResponse] = useState<null | any>(null); | ||
const [responseError, setResponseError] = useState<null | any>(null); | ||
|
||
async function runDemo() { | ||
setLoading(true); | ||
setResponse(null); | ||
setResponseError(null); | ||
|
||
try { | ||
const keyHash = resolvePaymentKeyHash(userinput); | ||
|
||
let oneYearFromNow = new Date(); | ||
oneYearFromNow.setFullYear(oneYearFromNow.getFullYear() + 1); | ||
const slot = resolveSlotNo('mainnet', oneYearFromNow.getTime()); | ||
|
||
const nativeScript: NativeScript = { | ||
type: 'all', | ||
scripts: [ | ||
{ | ||
type: 'before', | ||
slot: slot, | ||
}, | ||
{ | ||
type: 'sig', | ||
keyHash: keyHash, | ||
}, | ||
], | ||
}; | ||
|
||
const address = resolveNativeScriptAddress( | ||
nativeScript, | ||
userinput.substring(0, 5) === 'addr1' ? 1 : 0 | ||
); | ||
setResponse(address); | ||
} catch (error) { | ||
setResponseError(`${error}`); | ||
} | ||
setLoading(false); | ||
} | ||
|
||
return ( | ||
<> | ||
<Card> | ||
<Input | ||
value={userinput} | ||
onChange={(e) => setUserinput(e.target.value)} | ||
placeholder="Address" | ||
label="Address" | ||
/> | ||
<RunDemoButton | ||
runDemoFn={runDemo} | ||
loading={loading} | ||
response={response} | ||
/> | ||
<RunDemoResult response={response} /> | ||
<RunDemoResult response={responseError} label="Error" /> | ||
</Card> | ||
</> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
packages/demo/components/pages/apis/transaction/basic/setNativeScriptInput.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import Codeblock from '../../../../ui/codeblock'; | ||
import SectionTwoCol from '../../../../common/sectionTwoCol'; | ||
|
||
export default function SetNativeScriptInput() { | ||
return ( | ||
<SectionTwoCol | ||
sidebarTo="setNativeScriptInput" | ||
header="Set Native Script" | ||
leftFn={Left({})} | ||
rightFn={Right({})} | ||
/> | ||
); | ||
} | ||
|
||
function Left({}) { | ||
let codeSnippet = ``; | ||
codeSnippet += `import { Transaction } from '@meshsdk/core';\n`; | ||
codeSnippet += `\n`; | ||
codeSnippet += `const tx = new Transaction({ initiator: wallet });\n`; | ||
codeSnippet += `tx.setNativeScriptInput(nativeScript, utxo);\n`; | ||
|
||
let codeUtxo = ``; | ||
codeUtxo += `{\n`; | ||
codeUtxo += ` input: {\n`; | ||
codeUtxo += ` outputIndex: number;\n`; | ||
codeUtxo += ` txHash: string;\n`; | ||
codeUtxo += ` };\n`; | ||
codeUtxo += ` output: {\n`; | ||
codeUtxo += ` address: string;\n`; | ||
codeUtxo += ` amount: Asset[];\n`; | ||
codeUtxo += ` dataHash?: string;\n`; | ||
codeUtxo += ` plutusData?: string;\n`; | ||
codeUtxo += ` scriptRef?: string;\n`; | ||
codeUtxo += ` };\n`; | ||
codeUtxo += `}\n`; | ||
|
||
let codeScript = ``; | ||
codeScript += `import type { NativeScript } from '@meshsdk/core';\n`; | ||
codeScript += `\n`; | ||
codeScript += `const nativeScript: NativeScript = {\n`; | ||
codeScript += ` type: 'all',\n`; | ||
codeScript += ` scripts:\n`; | ||
codeScript += ` [\n`; | ||
codeScript += ` {\n`; | ||
codeScript += ` type: 'before',\n`; | ||
codeScript += ` slot: '<insert slot here>'\n`; | ||
codeScript += ` },\n`; | ||
codeScript += ` {\n`; | ||
codeScript += ` type: 'sig',\n`; | ||
codeScript += ` keyHash: '<insert keyHash here>'\n`; | ||
codeScript += ` }\n`; | ||
codeScript += ` ]\n`; | ||
codeScript += `};\n`; | ||
|
||
return ( | ||
<> | ||
<p> | ||
This function allows you to set the Native Script input for the | ||
transaction. | ||
</p> | ||
<Codeblock data={codeSnippet} isJson={false} /> | ||
<p> | ||
where <code>NativeScript</code> has the following format: | ||
</p> | ||
<Codeblock data={codeScript} isJson={false} /> | ||
<p> | ||
where <code>UTxO</code> has the following format (use one of our providers): | ||
</p> | ||
<Codeblock data={codeUtxo} isJson={false} /> | ||
</> | ||
); | ||
} | ||
|
||
function Right({}) { | ||
return <></>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.