Skip to content

Commit

Permalink
Created addtokens helper + script to generate ESR.
Browse files Browse the repository at this point in the history
  • Loading branch information
aaroncox committed Jan 28, 2025
1 parent 4f14fde commit c0283a4
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,4 @@ test: build/debug codegen node_modules
codegen: codegen/api

codegen/api:
npx @wharfkit/cli generate --json ./contracts/api/build/api.abi --file ./codegen/api.ts api
npx @wharfkit/cli generate --json ./contracts/api/build/api.abi --file ./codegen/api.ts unicove.gm
5 changes: 3 additions & 2 deletions contracts/api/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,9 @@ node_modules:

.PHONY: testnet/mockdata
testnet/mockdata:
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
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:
Expand Down
11 changes: 10 additions & 1 deletion contracts/api/include/api/api.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ struct get_account_response
eosiosystem::rex_fund rexfund;
};

struct token_definition
{
name contract;
symbol symbol;
};

class [[eosio::contract("api")]] api : public contract
{
public:
Expand All @@ -45,7 +51,8 @@ class [[eosio::contract("api")]] api : public contract
};
typedef eosio::multi_index<"tokens"_n, token_row> token_table;

[[eosio::action]] void addtoken(const name contract, const symbol symbol);
[[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,
Expand All @@ -66,6 +73,8 @@ class [[eosio::contract("api")]] api : public contract
#endif

private:
void add_token(const token_definition token);

#ifdef DEBUG
template <typename T>
void clear_table(T& table, uint64_t rows_to_clear);
Expand Down
16 changes: 13 additions & 3 deletions contracts/api/src/api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,16 +88,26 @@ namespace api {
return balances;
}

[[eosio::action]] void api::addtoken(const name contract, const symbol symbol)
void api::add_token(const token_definition token)
{
require_auth(get_self());
token_table tokens(get_self(), get_self().value);
tokens.emplace(get_self(), [&](auto& row) {
row.id = tokens.available_primary_key();
row.contract = contract;
row.symbol = symbol;
row.contract = token.contract;
row.symbol = token.symbol;
});
}

[[eosio::action]] void api::addtoken(const token_definition token) { add_token(token); }

[[eosio::action]] void api::addtokens(const std::vector<token_definition> tokens)
{
for (const auto& token : tokens) {
add_token(token);
}
}

[[eosio::action]] void api::removetoken(const uint64_t id)
{
require_auth(get_self());
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
"@types/bun": "^1.0.4",
"@typescript-eslint/eslint-plugin": "^7.8.0",
"@typescript-eslint/parser": "^6.20.0",
"@wharfkit/abicache": "^1.2.2",
"@wharfkit/signing-request": "^3.2.0",
"eslint": "^8.56.0",
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-prettier": "^5.1.3",
Expand Down
73 changes: 73 additions & 0 deletions scripts/tokens/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import {APIClient, Asset} from '@wharfkit/antelope'
import {SigningRequest} from '@wharfkit/signing-request'
import zlib from 'pako'

import {Contract} from '../../codegen/api'
import ABICache from '@wharfkit/abicache'

const client = new APIClient({
url: 'https://eos.greymass.com',
})

const contract = new Contract({
client,
})

interface Token {
key: string
symbol: string
account: string
chain: string
supply: {
circulating: number
max: number
precision: number
last_update: string
}
metadata: {
name: string
website: string
logo: string
created_at: string
desc: string
}
chain_rank: string
}

async function fetchTokens(): Promise<Token[]> {
const url =
'https://raw.githubusercontent.com/greymass/antelope-tokens.json/refs/heads/main/tokens.json'
const response = await fetch(url)
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`)
}
return response.json()
}

const tokens = await fetchTokens()

const action = {
tokens: [],
}

tokens
.filter((token) => {
return token.chain === 'eos'
})
.forEach((token) => {
action.tokens.push({
contract: token.account,
symbol: Asset.Symbol.from(`${token.supply.precision},${token.symbol.toUpperCase()}`),
})
})

const abiProvider = new ABICache(client)

const request = await SigningRequest.create(
{
action: contract.action('addtokens', action),
},
{abiProvider, zlib}
)

console.log(String(request))

0 comments on commit c0283a4

Please sign in to comment.