Skip to content

Commit

Permalink
Duplicated api contract to tokens (splitting codebase)
Browse files Browse the repository at this point in the history
  • Loading branch information
aaroncox committed Jan 29, 2025
1 parent c645803 commit 1f26b83
Show file tree
Hide file tree
Showing 16 changed files with 3,287 additions and 0 deletions.
36 changes: 36 additions & 0 deletions contracts/tokens/.clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
---
BasedOnStyle: LLVM
ColumnLimit: 120
---
Language: Cpp
AccessModifierOffset: -3
AlignConsecutiveAssignments: true
AlignConsecutiveDeclarations: true
AllowShortFunctionsOnASingleLine: All
AlwaysBreakTemplateDeclarations: Yes
BinPackParameters: false
BraceWrapping:
AfterCaseLabel: true
AfterClass: true
AfterControlStatement: Never
AfterEnum: true
AfterFunction: true
AfterNamespace: false
AfterStruct: true
AfterUnion: true
AfterExternBlock: true
BeforeCatch: false
BeforeElse: false
IndentBraces: false
SplitEmptyFunction: false
SplitEmptyRecord: false
SplitEmptyNamespace: false
BreakBeforeBraces: Custom
BreakConstructorInitializers: BeforeComma
BreakInheritanceList: BeforeComma
CompactNamespaces: true
ConstructorInitializerAllOnOneLineOrOnePerLine: true
ConstructorInitializerIndentWidth: 1
ContinuationIndentWidth: 3
IndentWidth: 3
PointerAlignment: Left
39 changes: 39 additions & 0 deletions contracts/tokens/.eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{
"root": true,
"ignorePatterns": ["node_modules/**"],
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/eslint-recommended",
"plugin:@typescript-eslint/recommended",
"plugin:prettier/recommended",
],
"rules": {
"prettier/prettier": "warn",
"no-console": "warn",
"sort-imports": [
"error",
{
"ignoreCase": true,
"ignoreDeclarationSort": true,
},
],
"@typescript-eslint/no-empty-interface": "off", // TODO: This should be removed before PR #1
"@typescript-eslint/explicit-module-boundary-types": "off",
"@typescript-eslint/no-explicit-any": "off",
"@typescript-eslint/no-namespace": "off",
"@typescript-eslint/no-non-null-assertion": "off",
"@typescript-eslint/no-empty-function": "warn",
"no-inner-declarations": "off",
},
"settings": {
"import/parsers": {
"@typescript-eslint/parser": [".ts", ".tsx"],
},
"import/resolver": {
"typescript-bun": {
"project": true,
"alwaysTryTypes": true,
},
},
},
}
8 changes: 8 additions & 0 deletions contracts/tokens/.prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
arrowParens: 'always'
bracketSpacing: false
endOfLine: 'lf'
printWidth: 100
semi: false
singleQuote: true
tabWidth: 4
trailingComma: 'es5'
94 changes: 94 additions & 0 deletions contracts/tokens/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
#!/bin/bash
SHELL := /bin/bash
TEST_FILES := $(shell find src -name '*.ts')
BIN := ./node_modules/.bin

SRC_CONTRACT_NAME = api
MAINNET_NODE_URL = https://eos.greymass.com
MAINNET_CONTRACT_ACCOUNT = tokens.gm
TESTNET_NODE_URL = https://jungle4.greymass.com
TESTNET_CONTRACT_ACCOUNT = tokens.gm

build: | build/dir
cdt-cpp -abigen -abigen_output=build/${SRC_CONTRACT_NAME}.abi -o build/${SRC_CONTRACT_NAME}.wasm src/${SRC_CONTRACT_NAME}.cpp -R src -I include -D DEBUG

build/debug: | build/dir
cdt-cpp -abigen -abigen_output=build/${SRC_CONTRACT_NAME}.abi -o build/${SRC_CONTRACT_NAME}.wasm src/${SRC_CONTRACT_NAME}.cpp -R src -I include -D DEBUG

build/production: | build/dir
cdt-cpp -abigen -abigen_output=build/${SRC_CONTRACT_NAME}.abi -o build/${SRC_CONTRACT_NAME}.wasm src/${SRC_CONTRACT_NAME}.cpp -R src -I include

build/dir:
mkdir -p build

clean:
rm -rf build

testnet: build/debug
cleos -u $(TESTNET_NODE_URL) set contract $(TESTNET_CONTRACT_ACCOUNT) \
build/ ${SRC_CONTRACT_NAME}.wasm ${SRC_CONTRACT_NAME}.abi

mainnet: build/production
cleos -u $(MAINNET_NODE_URL) set contract $(MAINNET_CONTRACT_ACCOUNT) \
build/ ${SRC_CONTRACT_NAME}.wasm ${SRC_CONTRACT_NAME}.abi

# mainnet: build/production
# cleos -u $(MAINNET_NODE_URL) set contract \
# --dont-broadcast --skip-sign --expiration 259200 --json-file msig.json \
# ${MAINNET_CONTRACT_ACCOUNT} build/ ${SRC_CONTRACT_NAME}.wasm ${SRC_CONTRACT_NAME}.abi
# cleos -u $(MAINNET_NODE_URL) multisig propose_trx ${MAINNET_MSIG_PROPOSAL} ${MAINNET_MSIG_SIGNERS} \
# msig.json ${MAINNET_MSIG_PROPOSER} -p ${MAINNET_MSIG_PERMISSION}

test: build/debug node_modules build/contract.ts
bun test

build/contract.ts:
npx @wharfkit/cli generate --json ./build/${SRC_CONTRACT_NAME}.abi --file ./test/contracts/${SRC_CONTRACT_NAME}.ts ${SRC_CONTRACT_NAME}

codegen/dir:
mkdir -p codegen

codegen/eosio.ts:
npx @wharfkit/cli generate --url https://jungle4.greymass.com --file ./test/contracts/eosio.ts eosio

codegen/eosio.token.ts:
npx @wharfkit/cli generate --url https://jungle4.greymass.com --file ./test/contracts/eosio.token.ts eosio.token

.PHONY: check
check: cppcheck jscheck

.PHONY: cppcheck
cppcheck:
clang-format --dry-run --Werror src/*.cpp include/${SRC_CONTRACT_NAME}/*.hpp

.PHONY: jscheck
jscheck: node_modules
@${BIN}/eslint test --ext .ts --max-warnings 0 --format unix && echo "Ok"

.PHONY: format
format: cppformat jsformat

.PHONY: cppformat
cppformat:
clang-format -i src/*.cpp include/${SRC_CONTRACT_NAME}/*.hpp

.PHONY: jsformat
jsformat: node_modules
@${BIN}/eslint src --ext .ts --fix

.PHONY: distclean
distclean: clean
rm -rf node_modules/

node_modules:
bun install

.PHONY: testnet/mockdata
testnet/mockdata:
cleos -u $(TESTNET_NODE_URL) push action $(TESTNET_CONTRACT_ACCOUNT) addtokens '{"tokens": [{"contract": "eosio.token", "symbol": "4,EOS"},{"contract": "eosio.token", "symbol": "4,EOS"}]}' -p $(TESTNET_CONTRACT_ACCOUNT)@active
# cleos -u $(TESTNET_NODE_URL) push action $(TESTNET_CONTRACT_ACCOUNT) addtoken '{"contract": "eosio.token", "symbol": "4,EOS"}' -p $(TESTNET_CONTRACT_ACCOUNT)@active
# cleos -u $(TESTNET_NODE_URL) push action $(TESTNET_CONTRACT_ACCOUNT) addtoken '{"contract": "eosio.token", "symbol": "4,JUNGLE"}' -p $(TESTNET_CONTRACT_ACCOUNT)@active

.PHONY: testnet/wipe
testnet/wipe:
cleos -u $(TESTNET_NODE_URL) push action $(TESTNET_CONTRACT_ACCOUNT) wipe '{}' -p $(TESTNET_CONTRACT_ACCOUNT)@active
Empty file added contracts/tokens/README.md
Empty file.
92 changes: 92 additions & 0 deletions contracts/tokens/include/api/api.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
#pragma once

#include <eosio.msig/eosio.msig.hpp>
#include <eosio.system/eosio.system.hpp>
#include <eosio.token/eosio.token.hpp>
#include <eosio/contract.hpp>
#include <eosio/singleton.hpp>

using namespace eosio;
using namespace std;

namespace api {

struct get_account_response
{
name account;
asset balance;
std::vector<eosiosystem::delegated_bandwidth> delegations;
std::vector<eosio::multisig::proposal> proposals;
eosiosystem::refund_request refund;
eosiosystem::rex_balance rexbal;
eosiosystem::rex_fund rexfund;
};

struct token_definition
{
name contract;
symbol symbol;
};

class [[eosio::contract("api")]] api : public contract
{
public:
using contract::contract;

struct [[eosio::table("config")]] config_row
{
name system_contract = name("eosio");
name system_contract_msig = name("eosio.msig");
name system_token_contract = name("eosio.token");
symbol system_token_symbol = symbol("EOS", 4);
};
typedef eosio::singleton<"config"_n, config_row> config_table;

struct [[eosio::table("tokens")]] token_row
{
uint64_t id;
name contract;
symbol symbol;
uint64_t primary_key() const { return id; }
};
typedef eosio::multi_index<"tokens"_n, token_row> token_table;

[[eosio::action]] void addtoken(const token_definition token);
[[eosio::action]] void addtokens(const std::vector<token_definition> tokens);
[[eosio::action]] void removetoken(const uint64_t id);
[[eosio::action]] void setconfig(const name system_contract,
const name system_contract_msig,
const name system_token_contract,
const symbol system_token_symbol);
/**
* getaccount readonly action
*/
[[eosio::action, eosio::read_only]] get_account_response getaccount(const name account);
using getaccount_action = action_wrapper<"getaccount"_n, &api::getaccount>;

[[eosio::action, eosio::read_only]] std::vector<asset> getbalances(const name account,
const std::vector<token_definition> tokens);
using getbalances_action = action_wrapper<"getbalances"_n, &api::getbalances>;

[[eosio::action, eosio::read_only]] std::vector<token_definition> gettokens();
using gettokens_action = action_wrapper<"gettokens"_n, &api::gettokens>;

#ifdef DEBUG
[[eosio::action]] void wipe();
[[eosio::action]] void reset();
#endif

private:
void add_token(const token_definition token);
std::vector<token_definition> get_token_definitions();

#ifdef DEBUG
template <typename T>
void clear_table(T& table, uint64_t rows_to_clear);
void reset_singletons();
void wipe_singletons();
void wipe_tables();
#endif
};

} // namespace api
Loading

0 comments on commit 1f26b83

Please sign in to comment.