Skip to content

Instantly share code, notes, and snippets.

@radio-alice
Last active October 30, 2019 16:07
Show Gist options
  • Save radio-alice/3b8d61f3324aa9b4d4ab589ca08421c0 to your computer and use it in GitHub Desktop.
Save radio-alice/3b8d61f3324aa9b4d4ab589ca08421c0 to your computer and use it in GitHub Desktop.
examples of how to use the quasar api to pin files via http endpoints and ethereum smart contracts
const quasarUrl = 'http://quasar.yourdomain.com' // wherever you are hosting the server
// tell quasar to listen for pin events from a smart contract at contractAddress
async function addStorageContract(contractAddress) {
return await fetch(`${quasarUrl}/api/v0/contracts`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ contractAddress })
})
}
// get a list of storage contracts that quasar is listening to for pin events
async function getStorageContracts() {
const response = await fetch(`${quasarUrl}/api/v0/contracts`, {
method: 'GET'
})
return await response.json()
}
// quasar optimistically pins your dag, then returns a hash for you to pass to a storage contract
// if the storage contract is registered with quasar, the pin is confirmed
// if the pin is never confirmed, quasar removes it after a week
async function dagPut(dag) {
const response = await fetch(`${quasarUrl}/api/v0/dag/put`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(dag)
})
const hash = await response.text()
storageContract.invokePinMethod(hash)
return hash
}
// same as above, but files instead of dags
async function addFile(file) {
const response = await fetch(`${quasarUrl}/api/v0/files/add`, {
method: 'POST',
headers: {
'Content-Type': 'multipart/form-data',
},
file
})
const hash = await response.text()
storageContract.invokePinMethod(hash)
return hash
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment