Skip to content

Instantly share code, notes, and snippets.

Avatar

Heri Sim heri16

View GitHub Profile
@heri16
heri16 / README.md
Last active Oct 19, 2021
Binary encoding of OUCH messages in Elixir
View README.md

Reuse logic in Elixir

Here are commons ways to reuse logic in Elixir:

1a. Move Function to other module (guards, pattern-matching) 1b. Protocol - Polymorphism

  1. Behaviour Dynamic-func - with def method_name(implemetation, args)
@heri16
heri16 / flatten_array.ex
Created Aug 26, 2021
Elixir Flatten array (with tail recursion)
View flatten_array.ex
defmodule FlattenArray do
@doc """
Accept a list and return the list flattened without nil values.
## Examples
iex> FlattenArray.flatten([1, [2], 3, nil])
[1,2,3]
iex> FlattenArray.flatten([nil, nil])
@heri16
heri16 / .README.md
Last active Oct 2, 2021
Install Elixir on macOs
View .README.md

Instructions

brew update
brew install asdf openssl@1.1
EDITOR=nano brew edit wxmac # Edit the file according to wxmac.patch
brew install wxmac --build-from-source

asdf plugin add erlang
asdf plugin add elixir
@heri16
heri16 / playground.go
Last active Sep 9, 2021
Nasdaq OUCH_4.2 golang
View playground.go
package main
import (
"bytes"
"encoding"
"encoding/binary"
"io"
"fmt"
"stockbit.com/proto/ouch42"
@heri16
heri16 / .README.md
Last active Aug 30, 2021
Build & Install eixx C++ library
View .README.md

macOS / darwin

Install Dependencies

# Build-time Deps
brew install cmake
# brew install gcc@10
pip3 install gil
@heri16
heri16 / slice.go
Created Jul 26, 2021
Golang - convert slice such as []string to []interface{}
View slice.go
module slice
func InterfaceSlice(slice interface{}) []interface{} {
switch slice := slice.(type) {
case []string:
new := make([]interface{}, len(slice))
for i, v := range slice {
new[i] = v
}
return new
@heri16
heri16 / yup.ts
Last active Jun 29, 2021
Yup validate input for too many decimals to prevent ethers.js BigNumber / BigInt underflow
View yup.ts
let decimals = 4;
const regexpDecimals = new RegExp(`^\\d*\\.{0,1}\\d{0,${decimals}}$`);
const SignupSchema = Yup.object().shape({
decimal: Yup.number().test(
'is-decimal',
'too many decimals',
value => (value + "").match(regexpDecimals),
),
});
@heri16
heri16 / ethersjs-token-bridge.ts
Last active Jun 29, 2021
Trigger Polygon / MATIC Token Bridge via ethers.js
View ethersjs-token-bridge.ts
// See: https://github.com/maticnetwork/matic.js/blob/master/examples/POS-client/config.js
const posRootChainManager = '0xBbD7cBFA79faee899Eaf900F13C9065bF03B1A74';
const posERC20Predicate = '0xdD6596F2029e6233DEFfaCa316e6A95217d4Dc34';
const accounts = useAccounts();
const userAddress = accounts?.[0]
const rootDaiContract = useContractContext(DAIDummyContext, 0);
const { send: approve } = useContractFunc(rootDaiContract, "approve");
@heri16
heri16 / button.tsx
Created Jun 29, 2021
Add network or switch network button for Metamask
View button.tsx
interface AddEthereumChainParameter {
chainId: string; // A 0x-prefixed hexadecimal string
chainName: string;
nativeCurrency: {
name: string;
symbol: string; // 2-6 characters long
decimals: 18;
};
rpcUrls: string[];
blockExplorerUrls?: string[];
@heri16
heri16 / Structs.md
Created Jun 21, 2021
WIP: Solidity Object Structures for Insured Finance
View Structs.md

Enums

  enum CoverType { SMART_PROTOCOL_FAILURE, STABLECOIN_DEVALUATION, CUSTODIAN_FAILURE, RUGPULL_LIQUIDITY_SCAM }
  enum CurrencyType { USDT, DAI, USDC }
  enum InsuredSumRule { PARTIAL, FULL }
  // enum ChainType { NON_EVM , EVM }

CoinId Decimals & CurrencyDecimals