Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Submit api redundancy #97

Merged
merged 4 commits into from
Sep 21, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ The `pages/api` directory is mapped to `/api/*`. Files in this directory are tre

* To use it on Cardano Testnet, set `NEXT_PUBLIC_TESTNET=y`. Leave it unset to use the Mainnet.
* To connect it to a GraphQL node, set `NEXT_PUBLIC_GRAPHQL` to the URI of the node.
* To sumbit transactions to a relay, set `NEXT_PUBLIC_SUBMIT` to the URI of the node. **Beware that the server needs a reverse proxy to process CORS request.**
* To sumbit transactions to relays, set `NEXT_PUBLIC_SUBMIT` to the URI of the node, split the URIs with `;`. **Beware that the server needs a reverse proxy to process CORS request.**
* To sync signatures automatically, set `NEXT_PUBLIC_GUN` to the URIs of the peers, split the URIs with `;`. We use [GUN](https://gun.eco) to sync.

## Testing
Expand Down
15 changes: 11 additions & 4 deletions src/cardano/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,20 @@ type QueryAPI = GraphQL
type Config = {
isMainnet: boolean
queryAPI: QueryAPI
submitAPI: string
submitAPI: string[]
gunPeers: string[]
}

const defaultGraphQLMainnet = 'https://d.graphql-api.mainnet.dandelion.link'
const defaultGraphQLTestnet = 'https://d.graphql-api.testnet.dandelion.link'
const defaultSubmitURIMainnet = 'https://adao.panl.org'
const defaultSubmitURITestnet = 'https://testrelay1.panl.org'
const defaultSubmitURIMainnet = [
'https://adao.panl.org',
'https://submit-api.apexpool.info/api/submit/tx'
]
const defaultSubmitURITestnet = [
'https://testrelay1.panl.org',
'https://relay1-testnet.apexpool.info/api/submit/tx'
]

const defaultConfig: Config = {
isMainnet: true,
Expand All @@ -31,7 +37,8 @@ const createConfig = (): Config => {
const defaultGraphQL = isMainnet ? defaultGraphQLMainnet : defaultGraphQLTestnet
const defaultSubmitURI = isMainnet ? defaultSubmitURIMainnet : defaultSubmitURITestnet
const grapQLURI = process.env.NEXT_PUBLIC_GRAPHQL ?? defaultGraphQL
const submitURI = process.env.NEXT_PUBLIC_SUBMIT ?? defaultSubmitURI
const submitEnv = process.env.NEXT_PUBLIC_SUBMIT
const submitURI = submitEnv ? submitEnv.split(';') : defaultSubmitURI
const gunPeers = (process.env.NEXT_PUBLIC_GUN ?? '').split(';')

return {
Expand Down
71 changes: 47 additions & 24 deletions src/components/transaction.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,35 @@ const SignTxButton: FC<{
)
}

const submitTx = (URL: string, transaction: Transaction) => fetch(URL, {
method: 'POST',
headers: { 'Content-Type': 'application/cbor' },
body: transaction.to_bytes()
}).then(async (response) => {
if (!response.ok) {
await response.text().then((message: string): Error => {
if (message.search(/\(ScriptWitnessNotValidatingUTXOW /) !== -1) {
throw {
name: 'InvalidSignatureError',
message: 'The signatures are invalid.'
}
}
if (message.search(/\(BadInputsUTxO /) !== -1) {
throw {
name: 'DuplicatedSpentError',
message: 'The UTxOs have been spent.'
}
}
console.error(message)
throw {
name: 'TxSubmissionError',
message: 'An unknown error. Check the log.'
}
})
}
return response
})

const SubmitTxButton: FC<{
className?: string
children: ReactNode
Expand All @@ -319,35 +348,29 @@ const SubmitTxButton: FC<{
const { notify } = useContext(NotificationContext)
const [isSubmitting, setIsSubmitting] = useState(false)
const [isDisabled, setIsDisabled] = useState(false)
const URL = config.submitAPI

const clickHandle: MouseEventHandler<HTMLButtonElement> = () => {
setIsSubmitting(true)
fetch(URL, {
method: 'POST',
headers: { 'Content-Type': 'application/cbor' },
body: transaction.to_bytes()
})
.then(async (response) => {
if (!response.ok) {
const message = await response.text()
if (message.search(/\(ScriptWitnessNotValidatingUTXOW /) !== -1) {
notify('error', 'The signatures are invalid.')
return
}
if (message.search(/\(BadInputsUTxO /) !== -1) {
notify('error', 'The UTxOs have been spent.')
setIsDisabled(true)
return
}
notify('error', 'Failed to submit.')
return
}
const promises = config.submitAPI.map((URL) => submitTx(URL, transaction))
Promise
.any(promises)
.then(() => {
notify('success', 'The transaction is submitted.')
})
.catch((reason) => {
notify('error', 'Failed to connect.')
console.error(reason)
.catch((reason: AggregateError) => {
const duplicatedSpentError: Error = reason.errors.find((error) => error.name === 'DuplicatedSpentError')
if (duplicatedSpentError) {
setIsDisabled(true)
notify('error', duplicatedSpentError.message)
return
}
const invalidSignatureError: Error = reason.errors.find((error) => error.name === 'InvalidSignatureError')
if (invalidSignatureError) {
notify('error', invalidSignatureError.message)
return
}
const error: Error = reason.errors[0]
notify('error', error.message)
})
.finally(() => setIsSubmitting(false))
}
Expand Down
8 changes: 5 additions & 3 deletions src/pages/config.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,12 @@ const Configure: NextPage = () => {
<span>{config.queryAPI.URI}</span>
</p>
}
<p className='space-x-2'>
{config.submitAPI && <div>
<span>Submit API:</span>
<span>{config.submitAPI}</span>
</p>
<ul>
{config.submitAPI.map((api, index) => <li key={index}>{api}</li>)}
</ul>
</div>}
{config.gunPeers && <div>
<span>GUN Peers:</span>
<ul>
Expand Down