Skip to content

Commit

Permalink
Initial commit to rework for Unicove
Browse files Browse the repository at this point in the history
  • Loading branch information
aaroncox committed Jan 22, 2025
1 parent e179806 commit 7c1d647
Show file tree
Hide file tree
Showing 17 changed files with 87 additions and 83 deletions.
28 changes: 14 additions & 14 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,28 @@ SHELL := /bin/bash

# CONTRACT BUILD

build: build/contractname/production
build: build/api/production

build/debug: build/contractname/debug
build/debug: build/api/debug

build/production: build/contractname/production
build/production: build/api/production

build/contractname:
make -C contracts/contractname build
build/api:
make -C contracts/api build

build/contractname/debug:
make -C contracts/contractname build/debug
build/api/debug:
make -C contracts/api build/debug

build/contractname/production:
make -C contracts/contractname build/production
build/api/production:
make -C contracts/api build/production

# UNIT TESTS

test/contractname: build/contractname/debug node_modules codegen
test/api: build/api/debug node_modules codegen
bun test

node_modules:
make -C contracts/contractname node_modules
make -C contracts/api node_modules

.PHONY: jscheck
jscheck: node_modules
Expand All @@ -36,7 +36,7 @@ test: build/debug codegen node_modules
# CODEGEN

.PHONY: codegen
codegen: codegen/contractname
codegen: codegen/api

codegen/contractname:
npx @wharfkit/cli generate --json ./contracts/contractname/build/contractname.abi --file ./codegen/contractname.ts contractname
codegen/api:
npx @wharfkit/cli generate --json ./contracts/api/build/api.abi --file ./codegen/api.ts api
18 changes: 9 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
Setup

1. Rename `src/contractname` folder to name of contract.
2. Rename `src/contractname/include/contractname` to name of contract.
3. Rename `src/contractname/include/contractname/contractname.hpp` to name of contract.
4. Rename `src/contractname/src/contractname.cpp` to name of contract.
5. Rename `test/contractname` to name of contract.
6. Rename `test/contractname/contractname.test.ts` to name of contract.
7. Rename `src/contractname/contractname.clauses.md` to name of contract.
8. Rename `src/contractname/contractname.contracts.md` to name of contract.
9. Search and replace in project to replace `contractname` with name of contract.
1. Rename `src/api` folder to name of contract.
2. Rename `src/apiapi` to napiact.
3. Rename `src/apiapi/api.hapioapi
4. Rename `src/apicpp` apiontract.
5. Rename `test/api of contract.
6. Rename `test/api.apiof contract.
7. Rename `src/apisapime of contract.
8. Rename `src/apirapiname of contract.
9. Search and replace in project to replace `apime of contract.
File renamed without changes.
File renamed without changes.
File renamed without changes.
6 changes: 3 additions & 3 deletions contracts/contractname/Makefile → contracts/api/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ SHELL := /bin/bash
TEST_FILES := $(shell find src -name '*.ts')
BIN := ./node_modules/.bin

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

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
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,23 @@
using namespace eosio;
using namespace std;

namespace contractname {
namespace api {

class [[eosio::contract("contractname")]] contractname : public contract
struct get_account_response
{
name account;
};

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

/**
* Example Action
* getaccount readonly action
*/
[[eosio::action]] void foo(const string bar);
using foo_action = action_wrapper<"foo"_n, &contractname::foo>;
[[eosio::action]] get_account_response getaccount(const name account);
using getaccount_action = action_wrapper<"getaccount"_n, &api::getaccount>;

// DEBUG (used to help testing)
#ifdef DEBUG
Expand All @@ -35,4 +40,4 @@ class [[eosio::contract("contractname")]] contractname : public contract
#endif
};

} // namespace contractname
} // namespace api
3 changes: 3 additions & 0 deletions contracts/api/src/api.clauses.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<h1 class="clause">api</h1>

api
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ icon: https://avatars.githubusercontent.com/u/147292861?s=400&u=3b1af66e90dd851f

---

<h1 class="contract">foo</h1>
<h1 class="contract">getaccount</h1>

---

spec_version: "0.2.0"
title: foo
summary: 'DEBUG: foo action'
title: getaccount
summary: 'Read-only API to get account data'
icon: https://avatars.githubusercontent.com/u/147292861?s=400&u=3b1af66e90dd851f4d7c096ed6a2fbb4b9e190da

---
Expand Down
15 changes: 15 additions & 0 deletions contracts/api/src/api.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#include "api/api.hpp"

namespace api {

[[eosio::action, eosio::read_only]] get_account_response api::getaccount(const name account)
{
return get_account_response{.account = account};
}

} // namespace api

// DEBUG (used on testnet)
#ifdef DEBUG
#include "debug.cpp"
#endif
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
namespace contractname {
namespace api {

// @debug
template <typename T>
void contractname::clear_table(T& table, uint64_t rows_to_clear)
void api::clear_table(T& table, uint64_t rows_to_clear)
{
auto itr = table.begin();
while (itr != table.end() && rows_to_clear--) {
Expand All @@ -12,16 +12,16 @@ void contractname::clear_table(T& table, uint64_t rows_to_clear)

// @debug
[[eosio::action]] void
contractname::cleartable(const name table_name, const optional<name> scope, const optional<uint64_t> max_rows)
api::cleartable(const name table_name, const optional<name> scope, const optional<uint64_t> max_rows)
{
require_auth(get_self());
const uint64_t rows_to_clear = (!max_rows || *max_rows == 0) ? -1 : *max_rows;
const uint64_t value = scope ? scope->value : get_self().value;

// list out all the tables that could be cleared
//
// contractname::table1 _table1(get_self(), value);
// contractname::table2 _table2(get_self(), value);
// api::table1 _table1(get_self(), value);
// api::table2 _table2(get_self(), value);

// Use an if/else if/else chain to determine which table to clear
//
Expand All @@ -34,19 +34,19 @@ contractname::cleartable(const name table_name, const optional<name> scope, cons
}

// @debug
[[eosio::action]] void contractname::wipe()
[[eosio::action]] void api::wipe()
{
require_auth(get_self());

// Define the tables to wipe
//
// contractname::table1 _table1(get_self(), get_self().value);
// contractname::table2 _table2(get_self(), get_self().value);
// api::table1 _table1(get_self(), get_self().value);
// api::table2 _table2(get_self(), get_self().value);

// Call clear on the tables to wipe
//
// clear_table(_table1, -1);
// clear_table(_table2, -1);
}

} // namespace contractname
} // namespace api
3 changes: 0 additions & 3 deletions contracts/contractname/src/contractname.clauses.md

This file was deleted.

12 changes: 0 additions & 12 deletions contracts/contractname/src/contractname.cpp

This file was deleted.

15 changes: 15 additions & 0 deletions test/api/api.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import {beforeEach, describe, test} from 'bun:test'

import {apiContract, contracts, resetContracts} from '../helpers'

describe(apiContract, () => {
beforeEach(async () => {
await resetContracts()
})

describe('action::getaccount', () => {
test('success', async () => {
await contracts.api.actions.getaccount(['bar']).send()
})
})
})
15 changes: 0 additions & 15 deletions test/contractname/contractname.test.ts

This file was deleted.

14 changes: 5 additions & 9 deletions test/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,23 @@
import {Blockchain} from '@proton/vert'
import {TimePointSec} from '@greymass/eosio'

import * as ContractNameContract from '../codegen/contractname.ts'
import * as ApiContract from '../codegen/api.ts'

export const ContractName = ContractNameContract
export const api = ApiContract

export const blockchain = new Blockchain()
export const alice = 'alice'
export const bob = 'bob'
blockchain.createAccounts(bob, alice)

export const contractnameContract = 'contractname'
export const apiContract = 'api'

export const contracts = {
contractname: blockchain.createContract(
contractnameContract,
`./contracts/contractname/build/contractname`,
true
),
api: blockchain.createContract(apiContract, `./contracts/api/build/api`, true),
}

export async function resetContracts() {
await contracts.contractname.actions.wipe().send()
await contracts.api.actions.wipe().send()
}

export function advanceTime(seconds: number) {
Expand Down

0 comments on commit 7c1d647

Please sign in to comment.