Skip to content

Instantly share code, notes, and snippets.

Avatar

Harry Scholes harryscholes

View GitHub Profile
@harryscholes
harryscholes / terra.graphql
Last active Nov 25, 2021
Terra graphql query for tx log event KV pairs
View terra.graphql
{
tx {
byHeight(height: 6708762) {
tx {
body {
messages
}
}
logs {
events {
@harryscholes
harryscholes / clone.rs
Created Nov 5, 2021
Rust: how to clone a type that is not Clone
View clone.rs
use std::sync::Arc;
struct Foo();
fn main() {
let x = Foo();
// x.clone(); // `Foo` is not `Clone`
let x = Arc::new(x); // Wrap in `Arc`
x.clone(); // Works because `Arc` is `Clone`
@harryscholes
harryscholes / option.rs
Created Nov 4, 2021
Rust custom Option type
View option.rs
enum MyOption<T> {
MySome(T),
MyNone,
}
use MyOption::{MyNone, MySome};
fn main() {
let mut x = MySome(1i32);
x = MyNone;
@harryscholes
harryscholes / price_holder.rs
Last active Oct 31, 2021
Rust threadsafe hashmap with notification of state changes via the observer pattern
View price_holder.rs
use std::collections::HashMap;
use std::sync::mpsc::{sync_channel, Receiver, SyncSender};
use std::sync::{Arc, Mutex};
use num::Unsigned;
struct Price<T> {
value: Option<T>,
waiters: Option<Vec<SyncSender<T>>>,
}
View terracli_config.sh
terracli config node http://public-node.terra.dev:26657
terracli config chain-id columbus-4
terracli config trust-node false
@harryscholes
harryscholes / lib.rs
Last active Aug 11, 2021
Thread safe price holder in Rust
View lib.rs
use std::{
collections::HashMap,
sync::{
mpsc::{self, RecvError, Sender},
Arc, Mutex,
},
};
struct Price {
value: Option<u128>,
@harryscholes
harryscholes / error_conversion.rs
Last active Jul 24, 2021
Rust automatic error conversion
View error_conversion.rs
use thiserror::Error;
#[derive(Debug, Error)]
enum A {
#[error("")]
ErrorA {},
}
#[derive(Debug, Error)]
enum B {
@harryscholes
harryscholes / terra_core_apple_silicon.sh
Created Jul 19, 2021
Install terra core on Apple Silicon
View terra_core_apple_silicon.sh
# On an Apple Silicon computer, install Rosetta 2 and the amd64 version of Go, then
arch -x86_64 make install
@harryscholes
harryscholes / length.rs
Last active Jun 8, 2021
Length traits in Rust
View length.rs
use std::ops::{Add, AddAssign, Mul};
#[derive(Debug)]
struct Millimeters<T>(T);
struct Centimeters<T>(Millimeters<T>);
struct Meters<T>(Millimeters<T>);
/// Length represents a length in millimeters
View set.rs
use std::{collections::HashMap, hash::Hash};
#[derive(Debug)]
struct Set<T> {
m: HashMap<T, ()>,
}
impl<T> Set<T>
where
T: Eq + Hash + Copy,