Skip to content

Instantly share code, notes, and snippets.

@daverickdunn
daverickdunn / costs.ts
Created July 17, 2023 10:47
Get Individual Lambda Costs
import {
CloudWatchLogsClient,
StartQueryCommand,
GetQueryResultsCommand,
LogGroup,
DescribeLogGroupsCommand,
ResultField
} from '@aws-sdk/client-cloudwatch-logs';
import { writeFileSync } from 'fs';
import { DateTime } from 'luxon';
@daverickdunn
daverickdunn / base64.ts
Last active May 14, 2021 13:06
Base64 URL Encode Decode Typescript
export function encode(unencoded: string) {
return Buffer.from(unencoded).toString('base64');
};
export function decode(encoded: string) {
return Buffer.from(encoded, 'base64').toString('utf8');
};
export function urlEncode(unencoded: string) {
let encoded = encode(unencoded);
@daverickdunn
daverickdunn / ddb.replace-sets.ts
Last active November 11, 2020 10:38
Replace ECMA Sets with DocumentClient Sets
import { DocumentClient } from 'aws-sdk/clients/dynamodb';
function ReplaceSets(item: any) {
const keys = Object.keys(item);
for (let index = 0; index < keys.length; index++) {
const element = item[keys[index]];
if (typeof element === 'object' && element instanceof Set) {
if (element.size === 0){
item[keys[index]] = null;
} else {
@daverickdunn
daverickdunn / put-batch.js
Last active April 21, 2020 23:52
DynamoDB - Dynamically Map-Reduce a Batch Update Request in to Blocks of 25 Items
// Your DocClient ...
function SplitBatch(params) {
const max_items = 25;
let tables = Object.entries(params.RequestItems);
let transactions = {};
for (const [table, requests] of tables) {
for (let index = 0; index < requests.length; index += max_items) {
let block = requests.slice(index, index + max_items);
@daverickdunn
daverickdunn / update-example.js
Last active March 27, 2023 11:45
DynamoDB - Dynamically Build an Update Expression
const generateUpdateQuery = (fields) => {
let exp = {
UpdateExpression: 'set',
ExpressionAttributeNames: {},
ExpressionAttributeValues: {}
}
Object.entries(fields).forEach(([key, item]) => {
exp.UpdateExpression += ` #${key} = :${key},`;
exp.ExpressionAttributeNames[`#${key}`] = key;
exp.ExpressionAttributeValues[`:${key}`] = item
@daverickdunn
daverickdunn / output.txt
Last active September 8, 2020 01:56
Convert a simplified directory structure (tree) to a set of clustered documents. Parameterised to control cluster sizes.
{ id: 'A',
chn:
[ { id: 'B', chn: [ { id: 'D', chn: [] } ] },
{ id: 'C',
chn:
[ { id: 'E', chn: [] },
{ id: 'F', chn: [] },
{ id: 'G',
chn:
[ { id: 'H', chn: [ { id: 'I', chn: [] }, { id: 'J', chn: [] } ] } ] } ] } ] }
@daverickdunn
daverickdunn / create_and_trust.js
Last active June 27, 2022 20:19
Stellar JS SDK create account and set trust-line
const StellarSdk = require('stellar-sdk');
StellarSdk.Network.useTestNetwork(); // StellarSdk.Network.usePublicNetwork();
const server = new StellarSdk.Server('https://horizon-testnet.stellar.org'); // const server = new StellarSdk.Server('https://horizon.stellar.org');
// an arbitary list of trusted assets
const ASSETS = {
'XPORT': new StellarSdk.Asset('XPORT', 'GCC4HAL7SOR7AZBF53SUTVAJT4OHYLBHOUJMM3IAF5YLFF7OVVAWLVFT'),
'USDT': new StellarSdk.Asset('USDT', 'GCQTGZQQ5G4PTM2GL7CDIFKUBIPEC52BROAQIAPW53XBRJVN6ZJVTG6V'),
'MOBI' : new StellarSdk.Asset('MOBI', 'GA6HCMBLTZS5VYYBCATRBRZ3BZJMAFUDKYYF6AH6MVCMGWMRDNSWJPIH'),
@daverickdunn
daverickdunn / stream_listener.js
Last active August 22, 2021 22:34
Stellar JS SDK listen to server streamed EventSource events
const StellarSdk = require('stellar-sdk');
StellarSdk.Network.useTestNetwork(); // StellarSdk.Network.usePublicNetwork();
const server = new StellarSdk.Server('https://horizon-testnet.stellar.org'); // const server = new StellarSdk.Server('https://horizon.stellar.org');
// convenience method: returns a PaymentCallBuilder
// https://stellar.github.io/js-stellar-sdk/PaymentCallBuilder_PaymentCallBuilder.html#stream
// there are similar CallBuilders for other network events, this one is specific to payments
var stream = server
.payments()
@daverickdunn
daverickdunn / make_payment.js
Last active December 17, 2019 12:32
Stellar JS SDK make a payment
const StellarSdk = require('stellar-sdk');
StellarSdk.Network.useTestNetwork(); // StellarSdk.Network.usePublicNetwork();
const server = new StellarSdk.Server('https://horizon-testnet.stellar.org'); // const server = new StellarSdk.Server('https://horizon.stellar.org');
// an arbitary list of trusted assets
const ASSETS = {
'XPORT': new StellarSdk.Asset('XPORT', 'GCC4HAL7SOR7AZBF53SUTVAJT4OHYLBHOUJMM3IAF5YLFF7OVVAWLVFT'),
'USDT': new StellarSdk.Asset('USDT', 'GCQTGZQQ5G4PTM2GL7CDIFKUBIPEC52BROAQIAPW53XBRJVN6ZJVTG6V'),
'MOBI' : new StellarSdk.Asset('MOBI', 'GA6HCMBLTZS5VYYBCATRBRZ3BZJMAFUDKYYF6AH6MVCMGWMRDNSWJPIH'),
@daverickdunn
daverickdunn / see_balance.js
Last active August 22, 2022 13:39
Stellar JS SDK view balance.
const StellarSdk = require('stellar-sdk');
StellarSdk.Network.useTestNetwork(); // StellarSdk.Network.usePublicNetwork();
const server = new StellarSdk.Server('https://horizon-testnet.stellar.org'); // const server = new StellarSdk.Server('https://horizon.stellar.org');
// Note: this solution trusts the accounts asset codes alone.
// For general accounts you may need to verify the issuing account id: b.asset_issuer
const getBalance = (account, currency) => {
let balance = 0;
if (currency == 'XLM') {
balance = Number.parseFloat(account.balances.find(b => b.asset_type == 'native').balance);