Skip to content

Commit

Permalink
Merge pull request #2 from Yuripetusko/github-package
Browse files Browse the repository at this point in the history
GitHub package
  • Loading branch information
Yuripetusko authored Mar 13, 2021
2 parents 4194a48 + 3970b8c commit 7be940d
Show file tree
Hide file tree
Showing 51 changed files with 3,413 additions and 1,233 deletions.
Binary file added .DS_Store
Binary file not shown.
15 changes: 15 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"presets": [
"@babel/typescript",
[
"@babel/preset-env",
{
"targets": { "browsers": "defaults, not ie 11", "node": true },
"modules": false,
"useBuiltIns": false,
"loose": true
}
]
],
"plugins": ["@babel/plugin-proposal-optional-chaining", "@babel/plugin-transform-modules-commonjs"]
}
46 changes: 46 additions & 0 deletions .github/workflows/cd.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
name: NPM publish CD workflow

on:
release:
# This specifies that the build will be triggered when we publish a release
types: [published]

jobs:
build:

# Run on latest version of ubuntu
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
with:
# "ref" specifies the branch to check out.
# "github.event.release.target_commitish" is a global variable and specifies the branch the release targeted
ref: ${{ github.event.release.target_commitish }}
# install Node.js
- name: Use Node.js 14
uses: actions/setup-node@v1
with:
node-version: 14
# Specifies the registry, this field is required!
registry-url: https://npm.pkg.github.com/
scope: "@Yuripetusko"
# clean install of your projects' deps. We use "npm ci" to avoid package lock changes
- run: yarn install
# set up git since we will later push to the repo
- run: git config --global user.email "[email protected]" && git config --global user.name "$GITHUB_ACTOR"
# upgrade npm version in package.json to the tag used in the release.
- run: yarn version ${{ github.event.release.tag_name }}
# build the project
- run: yarn build
# run tests just in case
- run: yarn test
# publish to NPM -> there is one caveat, continue reading for the fix
- run: yarn publish
env:
NODE_AUTH_TOKEN: ${{secrets.GITHUB_TOKEN}}
# push the version changes to GitHub
- run: git push
env:
# The secret is passed automatically. Nothing to configure.
github-token: ${{ secrets.GITHUB_TOKEN }}
15 changes: 15 additions & 0 deletions .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
name: Tests CI
on: [push, pull_request]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Run unit tests
uses: actions/setup-node@v1
with:
node-version: '14'
- run: yarn install
- run: yarn test


5 changes: 3 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ build
archive
.vscode
.idea
.npmrc
dist

/dist/rmrk.js
/remarks*.json
/dump-*.json
throwaway.js
throwaway.js
50 changes: 47 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,52 @@

Typescript implementation of the [RMRK spec](https://github.com/Swader/rmrk-spec/).

![Tests](https://github.com/Swader/rmrk-tools/actions/workflows/run-tests.yml/badge.svg)

## Installation

> Note: NodeJS 14+ is required. Please install with [NVM](https://nvm.sh).
`yarn install git+https://github.com/Swader/rmrk-tools`

## Usage

### ESM / Typescript

```
import { fetchRemarks, utils, Consolidator } from 'rmrk-tools';
import { ApiPromise, WsProvider } from '@polkadot/api';
const wsProvider = new WsProvider('wss://node.rmrk.app');
const fetchAndConsolidate = async () => {
try {
const api = await ApiPromise.create({ provider: wsProvider });
const to = await utils.getLatestFinalizedBlock(api);
const remarkBlocks = await fetchRemarks(api, 6431422, to, ['']);
if (remarkBlocks && !isEmpty(remarkBlocks)) {
const remarks = utils.getRemarksFromBlocks(remarkBlocks);
const consolidator = new Consolidator();
const { nfts, collections } = consolidator.consolidate(remarks);
console.log('Consolidated nfts:', nfts);
console.log('Consolidated collections:', collections);
}
} catch (error) {
console.log(error)
}
}
```

### Browser

```
<script src="node_modules/rmrk-tools"></script>
<script>
const { c100, n100, Consolidator, fetchRemarks, utils } = window.rmrkTools;
</script>
```

TBD

## Helper Tools
Expand All @@ -15,7 +59,7 @@ Grabs all `system.remark` extrinsics in a block range and logs an array of them
Export functionality will be added soon (SQL and file, total and in chunks).

```bash
yarn fetch
yarn cli:fetch
```

Optional parameters:
Expand Down Expand Up @@ -61,7 +105,7 @@ The return data will look like this:
Takes as input a JSON file and processes all remarks within it to reach a final state of the NFT ecosystem based on that JSON.

```bash
yarn consolidate --json=dumps/remarks-4892957-5437981-0x726d726b.json
yarn cli:consolidate --json=dumps/remarks-4892957-5437981-0x726d726b.json
```

Todo:
Expand All @@ -78,7 +122,7 @@ Todo:
A local chain must be running in `--dev` mode for this to work.

```bash
yarn seed --folder=[folder]
yarn cli:seed --folder=[folder]
```

When running a local chain, you can run `yarn seed` to populate the chain with pre-written NFT configurations. This is good for testing UIs, wallets, etc. It will use the unlocked ALICE, BOB, and CHARLIE accounts so `--dev` is required here.
Expand Down
76 changes: 58 additions & 18 deletions dist/index.html → browser/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<body>
<style>
#collection_to_remark input[type="text"],
#nft_to_remark input[type="text"],
button.submit,
textarea {
width: 100%;
Expand Down Expand Up @@ -67,7 +68,7 @@ <h2>Minting a collection</h2>
<div>
<label for="coll_symbol">Symbol*:</label
><input
onkeyup="refreshShownId()"
onblur="refreshShownId(this)"
type="text"
name="symbol"
id="coll_symbol"
Expand All @@ -87,7 +88,8 @@ <h2>Minting a collection</h2>
autocomplete="off"
/>
</div>
<div><button type="submit" class="submit">Turn into Remark</button></div>
<div><button type="submit" class="submit turn_into_remark">Turn into Remark</button></div>
<div><button type="submit" data-textarea="collection_remark" class="submit_remark">Submit Remark</button></div>
<div>
<textarea
name="collection_remark"
Expand Down Expand Up @@ -167,7 +169,8 @@ <h2>Minting an NFT</h2>
autocomplete="off"
/>
</div>
<div><button type="submit" class="submit">Turn into Remark</button></div>
<div><button type="submit" class="submit turn_into_remark">Turn into Remark</button></div>
<div><button type="submit" data-textarea="nft_remark" class="submit_remark">Submit Remark</button></div>
<div id="nft_id">
ID: <span><b></b></span>.
<em
Expand All @@ -187,16 +190,25 @@ <h2>Minting an NFT</h2>
</div>
</form>
<hr />
<script src="rmrk.js"></script>
<script src="rmrk-tools.js"></script>
<script src="polkadot-utils.js"></script>
<script>
function initWeb3Login() {
dappex.web3Enable("RMRK Test").then(function () {
if (!dappex.isWeb3Injected) {
const {extensionDapps, utilCrypto, util, api} = window.polkadotUtils;
start();

async function start() {
let myApi = await initWeb3Connection();
await initWeb3Login();
}

async function initWeb3Login() {
extensionDapps.web3Enable("RMRK Test").then(function () {
if (!extensionDapps.isWeb3Injected) {
alert(
"You need a Web3 enabled browser to log in with Web3. The easiest solution is probably to install the Polkadot{js} extension."
);
} else {
dappex.web3Accounts().then(async (accounts) => {
extensionDapps.web3Accounts().then(async (accounts) => {
console.log(accounts);
let picker = document.querySelector("#account_picker");
let options = "";
Expand All @@ -206,15 +218,15 @@ <h2>Minting an NFT</h2>
}</option>`;
}
picker.innerHTML = options;
initRest();
await initRest();
});
}
});
}

function initRest() {
async function initRest() {
document
.querySelector("#collection_to_remark button.submit")
.querySelector("#collection_to_remark button.submit.turn_into_remark")
.addEventListener("click", function (e) {
e.preventDefault();

Expand All @@ -230,7 +242,7 @@ <h2>Minting an NFT</h2>
return false;
}

const c1 = new c100.Collection(
const c1 = new rmrkTools.c100(
0,
name,
max,
Expand All @@ -246,7 +258,7 @@ <h2>Minting an NFT</h2>
});

document
.querySelector("#nft_to_remark button.submit")
.querySelector("#nft_to_remark button.submit.turn_into_remark")
.addEventListener("click", function (e) {
e.preventDefault();

Expand All @@ -266,7 +278,7 @@ <h2>Minting an NFT</h2>

if (!transferable) transferable = 1;

const n1 = new n100.NFT(
const n1 = new rmrkTools.n100(
0,
coll,
name,
Expand All @@ -282,6 +294,15 @@ <h2>Minting an NFT</h2>
).innerText = `xxxxxx-${coll}-${instance}-${sn}`;
});

for (let b of document.querySelectorAll(".submit_remark")) {
b.addEventListener("click", function(e) {
e.preventDefault();
const remark = document.querySelector(`#${e.currentTarget.dataset.textarea}`).value;
console.log("Will submit " + remark);
submitRemark(remark);
})
}

document
.querySelector("#current_account_is_issuer")
.addEventListener("click", function (e) {
Expand All @@ -293,16 +314,17 @@ <h2>Minting an NFT</h2>
return document.querySelector("#account_picker").value;
}
function pubkeyForAddress() {
return util_crypto.decodeAddress(selectedAddress());
return utilCrypto.decodeAddress(selectedAddress());
}
function generateCollectionId(symbol, pubkey) {
return c100.Collection.generateId(util.u8aToHex(pubkey), symbol);
return rmrkTools.c100.generateId(util.u8aToHex(pubkey), symbol);
}
function getJson(s) {
let encoded = s.split("::").pop();
return decodeURIComponent(encoded);
}
function refreshShownId() {
function refreshShownId(target) {
sluggify(target);
const id = generateCollectionId(
document.querySelector("#coll_symbol").value,
pubkeyForAddress()
Expand Down Expand Up @@ -342,7 +364,25 @@ <h2>Minting an NFT</h2>
console.log(target);
target.value = string_to_slug(target.value).toUpperCase();
}
initWeb3Login();

async function submitRemark(remark) {
if (myApi) {
console.log("Submitting from " + selectedAddress());
let acc = await extensionDapps.web3FromAddress(selectedAddress());
console.log(acc);
myApi.tx.system.remark(remark).signAndSend(selectedAddress(), {signer: acc.signer})
} else {
console.error("No connection");
}
}
async function initWeb3Connection() {
try {
const wsProvider = new api.WsProvider('ws://127.0.0.1:9944');
myApi = await api.ApiPromise.create({ provider: wsProvider });
} catch (e) {
console.error(e);
}
}
</script>
</body>
</html>
2 changes: 2 additions & 0 deletions browser/polkadot-utils.js

Large diffs are not rendered by default.

34 changes: 34 additions & 0 deletions browser/polkadot-utils.js.LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*!
* The buffer module from node.js, for the browser.
*
* @author Feross Aboukhadijeh <https://feross.org>
* @license MIT
*/

/*! *****************************************************************************
Copyright (c) Microsoft Corporation.

Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
***************************************************************************** */

/*! ieee754. BSD-3-Clause License. Feross Aboukhadijeh <https://feross.org/opensource> */

/*! safe-buffer. MIT License. Feross Aboukhadijeh <https://feross.org/opensource> */

/**
* [js-sha3]{@link https://github.com/emn178/js-sha3}
*
* @version 0.8.0
* @author Chen, Yi-Cyuan [[email protected]]
* @copyright Chen, Yi-Cyuan 2015-2018
* @license MIT
*/
4 changes: 4 additions & 0 deletions browser/polkadot-utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export * as extensionDapps from "@polkadot/extension-dapp";
export * as utilCrypto from "@polkadot/util-crypto";
export * as util from "@polkadot/util";
export * as api from "@polkadot/api";
2 changes: 2 additions & 0 deletions browser/rmrk-tools.js

Large diffs are not rendered by default.

Loading

0 comments on commit 7be940d

Please sign in to comment.