Skip to content

Instantly share code, notes, and snippets.

@williamcotton
williamcotton / money.fs
Last active October 10, 2024 01:26
F# Money Types
open System
// Define units of measurement
[<Measure>] type USD
[<Measure>] type BTC
[<Measure>] type percent
let btcUsdRate = 62500m<USD/BTC>
// Money type alias
@williamcotton
williamcotton / ResultWriter.fsx
Last active May 17, 2024 20:32
A ResultWriter Computation Expression in F#
// Define types
type Result<'T> =
| Success of 'T
| Error of string
type LogState = { Logs: string list }
type ResultLog<'T> = Result<'T> * LogState
#ifndef MEMORY_MANAGER_H
#define MEMORY_MANAGER_H
#define HEAP_SIZE (10 * 1024 * 1024)
#include <stdlib.h>
typedef struct memory_manager_malloc_t {
void *ptr;
} memory_manager_malloc_t;
[
"/earth/me/me/kittery-point/03905",
"/earth/me/me",
"/earth/us/me",
"/earth/me/me/kittery",
"/earth/us/me/south-eliot",
"/earth/us/co",
"/earth/us/ca/huntington-beach/92647",
"/earth/us/ca/huntington-beach/92648",
"/earth/us/ca/huntington-beach/92646",
var blockcast = require("blockcast");
document.getElementById("scan-address").addEventListener("click", function(event) {
getTransaction = function(txHash, callback) {
xhr("https://www.blockai.com/chain/testnet/tx/" + txHash, function(err, res, body) {
var rawTx = JSON.parse(body);
var rawOutputs = rawTx.outputs;
var rawInputs = rawTx.inputs;
var outputs = [];
var openpublish = require("openpublish");
document.getElementById("open-publish-file").addEventListener("click", function(event) {
getUnspentOutputs(function(err, unspentOutputs) {
openpublish.register({
file: file,
uri: uri,
address: address,
unspentOutputs: unspentOutputs,
document.getElementById("sign-and-post-transaction").addEventListener("click", function(event) {
signedTx = wallet.signWith(newTx, [address]);
signedTxHex = signedTx.toHex();
xhr({
uri: '/chain/:blockchainType/transactions/send',
method: 'POST',
json: {
transactionHex: signedTxHex
document.getElementById("create-transaction").addEventListener("click", function(event) {
newTx = wallet.createTx(bitstoreDepositAddress, 100000, 1000, address);
newTxJSON = JSON.stringify({
inputCount: newTx.ins.length,
outputCount: newTx.outs.length,
txHash: newTx.getId()
}, null, 4);
document.getElementById("new-tx-json").innerHTML = newTxJSON;
document.getElementById("get-unspents").addEventListener("click", function(event) {
getUnspentOutputs = function(callback) {
xhr("https://www.blockai.com/chain/testnet/addresses/" + address + "/unspents", function(err, res, body) {
var unspentOutputs = JSON.parse(body);
unspentOutputs.forEach(function(utxo) {
utxo.txHash = utxo.hash;
utxo.index = utxo.outputIndex;
});
callback(err, unspentOutputs);
var xhr = require('xhr');
document.getElementById("get-wallet").addEventListener("click", function(event) {
xhr("https://www.blockai.com/chain/testnet/addresses/" + address, function(err, res, body) {
var data = JSON.parse(body);
var balance = data.balance;
document.getElementById("balance").innerHTML = balance;
});