Skip to content

Commit

Permalink
[ECO-1698] Refactor Event types and query results to not use classes (
Browse files Browse the repository at this point in the history
#73)

Co-authored-by: matt <[email protected]>
  • Loading branch information
xbtmatt and matt authored May 21, 2024
1 parent 054095b commit c5e7be3
Show file tree
Hide file tree
Showing 40 changed files with 3,760 additions and 974 deletions.
1 change: 0 additions & 1 deletion .github/workflows/ts-run-tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ env:
4bab58978ec1b1bef032eeb285ad47a6a9b997d646c19b598c35f46b26ff9ece
PUBLISHER_PK: |-
29479e9e5fe47ba9a8af509dd6da1f907510bcf8917bfb19b7079d8c63c0b720
RANDOMIZE_PUBLISHER: 'false'
START_LOCAL_NODE_FOR_TEST: 'true'
TS_DIR: 'src/typescript'
jobs:
Expand Down
3 changes: 2 additions & 1 deletion cfg/cspell-config.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
"ignorePaths": [
"/project-words.txt",
"**/package.json",
"**/pnpm-lock.yaml"
"**/pnpm-lock.yaml",
"../src/typescript/sdk/src/emoji_data/symbol-emojis.json"
],
"overrides": [
{
Expand Down
12 changes: 6 additions & 6 deletions src/python/move_emojis/scripts/generate_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@

BASE_EMOJIS_URL = "https://unicode.org/Public/emoji/15.1/emoji-test.txt"
ZWJ_EMOJIS_URL = "https://unicode.org/Public/emoji/15.1/emoji-zwj-sequences.txt"
SYMBOL_EMOJI_MOVE_CONSTS_FILE = "data/move_consts_symbol_emojis.txt"
CHAT_EMOJI_MOVE_CONSTS_FILE = "data/move_consts_chat_emojis.txt"
BASE_EMOJIS_FILE = "data/base_emojis.json"
ZWJ_EMOJIS_FILE = "data/zwj_emojis.json"
SYMBOL_EMOJIS_FILE = "data/symbol_emojis.json"
CHAT_EMOJIS_FILE = "data/chat_emojis.json"
SYMBOL_EMOJI_MOVE_CONSTS_FILE = "data/move-consts-symbol-emojis.txt"
CHAT_EMOJI_MOVE_CONSTS_FILE = "data/move-consts-chat-emojis.txt"
BASE_EMOJIS_FILE = "data/base-emojis.json"
ZWJ_EMOJIS_FILE = "data/zwj-emojis.json"
SYMBOL_EMOJIS_FILE = "data/symbol-emojis.json"
CHAT_EMOJIS_FILE = "data/chat-emojis.json"
TAB = " " * 4


Expand Down
6 changes: 0 additions & 6 deletions src/typescript/sdk/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,6 @@ PUBLISHER_PK="29479e9e5fe47ba9a8af509dd6da1f907510bcf8917bfb19b7079d8c63c0b720"
# Derived from `PUBLISHER_PK`.
MODULE_ADDRESS="4bab58978ec1b1bef032eeb285ad47a6a9b997d646c19b598c35f46b26ff9ece"

# Randomize the publisher for ease of use when testing. This facilitates "wiping"
# the contract state without having to reset the local Aptos node.
# Note that this will override the `PUBLISHER_PK` and `MODULE_ADDRESS` values if
# set to "true".
RANDOMIZE_PUBLISHER="false"

# URL of the Inbox deployment
# Warning: no / at the end
INBOX_URL="http://localhost:3000"
2 changes: 2 additions & 0 deletions src/typescript/sdk/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ module.exports = {
plugins: ["@typescript-eslint", "unused-imports", "import"],
rules: {
quotes: ["error", "double"],
// We disable the following rule to distinguish Move/JSON types from TS types.
"@typescript-eslint/naming-convention": "off",
"max-len": ["error", 100],
"import/extensions": "off",
"import/no-commonjs": ["error", { allowRequire: false, allowPrimitiveModules: false }],
Expand Down
83 changes: 83 additions & 0 deletions src/typescript/sdk/src/const.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import { Account, AccountAddress, Ed25519PrivateKey } from "@aptos-labs/ts-sdk";
import dotenv from "dotenv";
import path from "path";
import { getGitRoot } from "../tests/utils/helpers";

// Load environment variables from .env file if we're not running a deployment on Vercel.
const VERCEL = process.env.VERCEL === "1";
if (!VERCEL) {
// For local development and GitHub Actions CI/CD.
const envPath = path.join(getGitRoot(), "src", "typescript", "sdk", ".env");
dotenv.config({ path: envPath });
// If the publisher private key is not set by now, throw an error.
if (!process.env.PUBLISHER_PK) {
throw new Error("Missing PUBLISHER_PK environment variable for test.");
}

// Get the derived account from the private key.
const derivedAccount = Account.fromPrivateKey({
privateKey: new Ed25519PrivateKey(process.env.PUBLISHER_PK),
});

// Update the MODULE_ADDRESS env variable to the derived account address.
process.env.MODULE_ADDRESS = derivedAccount.accountAddress.toString();
} else {
// For Vercel deployments.
if (!process.env.NEXT_PUBLIC_MODULE_ADDRESS) {
throw new Error("Missing NEXT_PUBLIC_MODULE_ADDRESS environment variable.");
}
// TODO: Verify this works as expected.
// Regardless, it will likely error out if it isn't loaded first.
process.env.MODULE_ADDRESS = process.env.NEXT_PUBLIC_MODULE_ADDRESS;
}

// Import this in `pre-test.js` to force it to load the environment variables before running tests.
export const getPublisherPKForTest = () => process.env.PUBLISHER_PK;

if (!process.env.MODULE_ADDRESS) {
throw new Error("Missing MODULE_ADDRESS environment variable");
} else if (!process.env.INBOX_URL) {
throw new Error("Missing INBOX_URL environment variable");
}
export const MODULE_ADDRESS = (() => AccountAddress.from(process.env.MODULE_ADDRESS))();
export const INBOX_URL = process.env.INBOX_URL!;

export const ONE_APT = 1 * 10 ** 8;
export const MAX_GAS_FOR_PUBLISH = 1500000;
export const COIN_FACTORY_MODULE_NAME = "coin_factory";
export const EMOJICOIN_DOT_FUN_MODULE_NAME = "emojicoin_dot_fun";
export const DEFAULT_REGISTER_MARKET_GAS_OPTIONS = {
maxGasAmount: ONE_APT / 100,
gasUnitPrice: 100,
};
export const MARKET_CAP = 4_500_000_000_000n;
export const EMOJICOIN_REMAINDER = 10_000_000_000_000_000n;
export const EMOJICOIN_SUPPLY = 45_000_000_000_000_000n;
export const LP_TOKENS_INITIAL = 100_000_000_000_000n;
export const BASE_REAL_FLOOR = 0n;
export const QUOTE_REAL_FLOOR = 0n;
export const BASE_REAL_CEILING = 35_000_000_000_000_000n;
export const QUOTE_REAL_CEILING = 1_000_000_000_000n;
export const BASE_VIRTUAL_FLOOR = 14_000_000_000_000_000n;
export const QUOTE_VIRTUAL_FLOOR = 400_000_000_000n;
export const BASE_VIRTUAL_CEILING = 49_000_000_000_000_000n;
export const QUOTE_VIRTUAL_CEILING = 1_400_000_000_000n;
export const POOL_FEE_RATE_BPS = 25;
export const MARKET_REGISTRATION_FEE = 100_000_000n;

// For the emoji coin.
export const DECIMALS = 8;

// Emoji sequence length constraints.
export const MAX_CHAT_MESSAGE_LENGTH = 100;
export const MAX_SYMBOL_LENGTH = 10;

export enum CandlestickResolution {
PERIOD_1M = "60000000",
PERIOD_5M = "300000000",
PERIOD_15M = "900000000",
PERIOD_30M = "1800000000",
PERIOD_1H = "3600000000",
PERIOD_4H = "14400000000",
PERIOD_1D = "86400000000",
}
39 changes: 39 additions & 0 deletions src/typescript/sdk/src/emoji_data/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Reproducing the TypeScript `symbol-emojis.json` data

To reproduce the data in the TypeScript JSON data file `symbol-emojis.json`
`cd` to the root of the repo then run the following commands.

```shell
cd src/python/move_emojis
poetry install
poetry run python -m scripts.generate_code
```

You should now have emoji data in
`src/python/move_emojis/data/symbol-emojis.json`.

Ensure you're in `src/python/move_emojis` still.

To get the new data run the following python code:

```python
import json

fp = 'data/symbol-emojis.json'
new_fp = 'data/symbol-emojis-less-data.json'

emoji_data = json.load(open(fp, 'r'))

# Use the name as the key still but only keep the encoded UTF-8 emoji field as
# each entry's value.
new_data = { k: v['emoji'] for k, v in emoji_data.items() }

# Sort the dictionary by the key.
new_data = sorted(new_data.items(), key=lambda x: x[0])

# Rewrite it back into a dictionary type.
new_data = dict(list(new_data))

# Prettify and dump the new dictionary data to a new file.
json.dump(new_data, open(new_fp, 'w'), indent=2)
```
3 changes: 3 additions & 0 deletions src/typescript/sdk/src/emoji_data/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from "./symbol-data";
export * from "./types";
export * from "./utils";
33 changes: 33 additions & 0 deletions src/typescript/sdk/src/emoji_data/symbol-data.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { normalizeHex } from "../utils";
import AllSymbolEmojiJSON from "./symbol-emojis.json";
import { type SymbolEmojiData } from "./types";

const encoder = new TextEncoder();
const values: SymbolEmojiData[] = Object.entries(AllSymbolEmojiJSON).map(([name, emoji]) => {
const bytes = encoder.encode(emoji);
const hex = normalizeHex(bytes);
return {
name,
hex,
bytes,
emoji,
};
});

const nameMap = new Map<string, SymbolEmojiData>(values.map((v) => [v.name, v]));
const hexMap = new Map<`0x${string}`, SymbolEmojiData>(values.map((v) => [v.hex, v]));
const emojiMap = new Map<string, SymbolEmojiData>(values.map((v) => [v.emoji, v]));

export const SYMBOL_DATA = {
byName: (v: string) => nameMap.get(v),
byHex: (v: `0x${string}`) => hexMap.get(v),
byEmoji: (v: string) => emojiMap.get(v),
hasName: (v: string) => nameMap.has(v),
hasHex: (v: `0x${string}`) => hexMap.has(v),
hasEmoji: (v: string) => emojiMap.has(v),
};

export const getRandomEmoji = (): SymbolEmojiData => {
const randomIndex = Math.floor(values.length * Math.random());
return values[randomIndex];
};
Loading

0 comments on commit c5e7be3

Please sign in to comment.