Skip to content

Instantly share code, notes, and snippets.

@pizzarob
pizzarob / xhr.js
Created Aug 21, 2016
Helper Functions for Making XHR Requests in JavaScript
View xhr.js
function post(url, data) {
return new Promise((resolve, reject) => {
let xhr = new XMLHttpRequest();
xhr.open('POST', url);
xhr.setRequestHeader('Content-Type', 'application/json; charset=UTF-8');
xhr.addEventListener('load', () => {
let { response, status } = xhr;
let res = JSON.parse(response);
if(status >= 200 && status < 400){
View starter_03.js
// Update this with your crate ID
const CRATE_ID = '98';
// Initialize cargo using the development network. This will
// tell Cargo to use it's contracts on the Rinkeby network.
const cargo = new Cargo({
network: 'development',
});
const run = async () => {
View starter_06.js
const CRATE_ID = '98';
// Initialize cargo using the development network. This will
// tell Cargo to use it's contracts on the Rinkeby network.
const cargo = new Cargo({
network: 'development',
});
const run = async () => {
// This will fetch Cargo contract information
View starter_08.js
const CRATE_ID = '98';
// Initialize cargo using the development network. This will
// tell Cargo to use it's contracts on the Rinkeby network.
const cargo = new Cargo({
network: 'development',
});
const getResaleItems = async () => {
// Get all the vendors in your crate. The creator of the crate (you) is
View starter_01.js
// Initialize cargo using the development network. This will
// tell Cargo to use it's contracts on the Rinkeby network.
const cargo = new Cargo({
network: 'development',
});
const run = async () => {
// This will fetch Cargo contract information
// so we can begin interacting with those contracts when we are ready.
await cargo.init();
View starter_07.js
const tokenRows = document.querySelector('[data-id="token-rows"]');
const txConfirmation = document.querySelector('[data-id="tx-confirmation"]');
// This listener checks to see if we clicked the buy now button
tokenRows.addEventListener('click', async evt => {
// If the target is the buy now button
if (evt.target.dataset.id === 'buy-now') {
// We want to get the resale ID and price we set on the button via
// data attributes.
View starter_05.js
function displayContractContent(contracts, resaleItems) {
let template = '';
contracts.forEach(contract => {
const { name, symbol, tokenAddress, tokenContractId } = contract;
const currentResaleItems = resaleItems[tokenContractId];
let tokenMarkup = '';
currentResaleItems.forEach(token => {
const { metadata, price } = token;
View starter_04.js
// Get the resale items for our contracts. This method
// takes a list of token contract IDs, not addresses.
// The IDs are assigned by Cargo and used internally
const { data: resaleItems} = await cargo.api.getContractResaleItems(
contracts.map(contract => contract.tokenContractId)
);
// Pass our contract data and resale items to a function that will create some markup
// and add it to the page.
displayContractContent(contracts, resaleItems);
View starter_02.js
if (isEnabled) {
// Get all the vendors in your crate. The creator of the crate (you) is
// added as a default vendor.
const { data: vendors } = await cargo.api.getCrateVendors(CRATE_ID);
// Map through each vendor and get the token contracts they created.
const contractResponses = await Promise.all(
vendors.map(({ vendorId }) => cargo.api.getVendorTokenContracts(vendorId))
);
@pizzarob
pizzarob / MongooseSingletonModel.js
Last active Dec 4, 2017
Mongoose Singleton Model
View MongooseSingletonModel.js
schema.statics = {
getSingleton: function (cb) {
this.findOne()
.sort({ updated: -1 })
.limit(1)
.exec((err, model) => {
if (err) {
cb(err, null);
} else if (!model) {
cb(err, new this());