Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add jams support #55

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 16 additions & 17 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
};
exports.__esModule = true;
exports.putIpfsJson = exports.getIpfsJson = exports.Dapp = exports.PackBuilder = exports.builder = exports.load = void 0;
var fs_1 = require("fs");
var jams_js_1 = require("jams.js");
var builder_1 = require("./src/builder");
exports.PackBuilder = builder_1.PackBuilder;
var dapp_1 = require("./src/dapp");
Expand All @@ -49,26 +51,23 @@ var load = function (arg, _ethers, _signer) {
if (_ethers === void 0) { _ethers = undefined; }
if (_signer === void 0) { _signer = undefined; }
return __awaiter(void 0, void 0, void 0, function () {
var _a;
return __generator(this, function (_b) {
switch (_b.label) {
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
if (!(typeof arg === 'string')) return [3 /*break*/, 4];
if (!((0, ipfs_util_1.isCid)(arg))) return [3 /*break*/, 2];
return [4 /*yield*/, (0, ipfs_util_1.getIpfsJson)(arg)];
case 1:
_a = _b.sent();
return [3 /*break*/, 3];
case 2:
_a = require(arg);
_b.label = 3;
case 3:
arg = _a;
_b.label = 4;
case 4:
if (typeof arg === 'string') {
if ((0, ipfs_util_1.isCid)(arg)) {
arg = (0, ipfs_util_1.getIpfsJson)(arg);
}
else if (arg.split('.').pop() === 'jams') {
arg = (0, jams_js_1.jams)((0, fs_1.readFileSync)(arg));
}
else {
arg = require(arg);
}
}
(0, util_1.need)(typeof arg === 'object' && Object.keys(arg).length, 'Could not find a pack from provided source.');
return [4 /*yield*/, dapp_1.Dapp.loadFromPack(arg, _ethers, _signer)];
case 5: return [2 /*return*/, _b.sent()];
case 1: return [2 /*return*/, _a.sent()];
}
});
});
Expand Down
10 changes: 9 additions & 1 deletion index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
import { readFileSync } from 'fs'
import { jams } from 'jams.js'
import { PackBuilder } from './src/builder'
import { Dapp } from './src/dapp'
import { getIpfsJson, putIpfsJson, isCid } from './src/ipfs-util'
import { need } from './src/util'

export const load = async (arg, _ethers = undefined, _signer = undefined) => {
if (typeof arg === 'string') {
arg = (isCid(arg)) ? await getIpfsJson(arg) : require(arg)
if (isCid(arg)) {
arg = getIpfsJson(arg)
} else if (arg.split('.').pop() === 'jams') {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This 3-way branch is not right, we have 2x2 options,

  • Are we using a CID and fetching it from IPFS, or opening a local file?
  • Is it JAMS or JSON?

Copy link
Contributor Author

@lonerapier lonerapier May 21, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this work?

if (isCid(arg)) {
  arg = getIpfsJson(arg)
} else {
 arg = (arg.split('.').pop() === 'jams')
    ? jams(readFileSync(arg))
    : require(arg)
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one is missing the case that it is a CID that points to a JAMS file, maybe refactor getIpfsJson to just getIpfsFile then branch from there, point is there are 4 cases

arg = jams(readFileSync(arg))
} else {
arg = require(arg)
}
}
need(typeof arg === 'object' && Object.keys(arg).length, 'Could not find a pack from provided source.')
return await Dapp.loadFromPack(arg, _ethers, _signer)
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"debug": "^4.3.2",
"ethers": "^5.4.5",
"ipfs-http-client": "^55.0.0",
"jams.js": "^0.0.7",
"multiformats": "^9.6.4"
},
"devDependencies": {
Expand Down
24 changes: 24 additions & 0 deletions test/data/weth_ropsten.dpack.jams
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
format dpack-1
network ropsten
types {
WETH9 {
typename WETH9
artifact {/ bafkreidsszpx34yqnshrtuszx7n77zxttk2s54kc2m5cftjutaumxe67fa }
nmushegian marked this conversation as resolved.
Show resolved Hide resolved
}
}
objects {
weth9 {
objectname weth
address 0xc778417E063141139Fce010982780140Aa0cD5Ab
typename WETH9
artifact {/ bafkreidsszpx34yqnshrtuszx7n77zxttk2s54kc2m5cftjutaumxe67fa }
}
weth9 {
objectname weth9
address 0xc778417E063141139Fce010982780140Aa0cD5Ab
typename WETH9
artifact {/ bafkreidsszpx34yqnshrtuszx7n77zxttk2s54kc2m5cftjutaumxe67fa }
}
}
}
4 changes: 3 additions & 1 deletion test/dpack-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ before(async () => {

describe('end to end simple example', ()=>{
const packPath = path.join(__dirname, './data/weth_ropsten.dpack.json')
const jamsPath = path.join(__dirname, './data/weth_ropsten.dpack.jams')
let cidStr

it('create weth pack', async () => {
Expand Down Expand Up @@ -55,10 +56,11 @@ describe('end to end simple example', ()=>{
// another network. Packs can be loaded from a CID string, a file path string, or a json object.
const dappFromPack = await load(jsonObj, ethers, signer)
const dappFromPath = await load(packPath, ethers, signer)
const dappFromJams = await load(jamsPath, ethers, signer)
const dappFromCID = await load(cidStr, ethers, signer)

// All methods give the same pack
let packStrings = [JSON.stringify(dappFromPack), JSON.stringify(dappFromPath), JSON.stringify(dappFromCID)];
let packStrings = [JSON.stringify(dappFromPack), JSON.stringify(dappFromPath), JSON.stringify(dappFromJams), JSON.stringify(dappFromCID)];
want((new Set(packStrings)).size).to.equal(1)
})
});
Expand Down
1 change: 1 addition & 0 deletions typings.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
declare module "jams.js"