Skip to content

Commit

Permalink
Merge pull request #103 from Agoric/contract-linting
Browse files Browse the repository at this point in the history
Contract Linting
  • Loading branch information
mujahidkay authored Sep 13, 2024
2 parents 1f75e6a + cfc2b77 commit 4111758
Show file tree
Hide file tree
Showing 10 changed files with 757 additions and 87 deletions.
2 changes: 2 additions & 0 deletions contract/.eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
bundles
dist
19 changes: 0 additions & 19 deletions contract/jsconfig.json

This file was deleted.

39 changes: 31 additions & 8 deletions contract/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"start": "yarn docker:make clean start-contract",
"build": "agoric run scripts/build-contract-deployer.js",
"test": "ava --verbose",
"lint": "eslint '**/*.js'",
"lint": "tsc && eslint '**/*.js'",
"lint:fix": "eslint --fix '**/*.js'",
"make:waitForBlocks": "make wait-for-blocks"
},
Expand All @@ -31,21 +31,22 @@
"eslint": "^8.57.0",
"eslint-config-airbnb-base": "^15.0.0",
"eslint-config-jessie": "^0.0.6",
"eslint-config-prettier": "^9.0.0",
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-ava": "^14.0.0",
"eslint-plugin-github": "^4.10.0",
"eslint-plugin-import": "^2.25.3",
"eslint-plugin-jsdoc": "^46.4.3",
"eslint-plugin-prettier": "^5.0.0",
"eslint-plugin-import": "^2.29.1",
"eslint-plugin-jsdoc": "^48.5.2",
"eslint-plugin-prettier": "^5.1.3",
"import-meta-resolve": "^2.2.1",
"prettier": "^3.0.3",
"prettier-plugin-jsdoc": "^1.0.0",
"type-coverage": "^2.26.3",
"typescript": "~5.2.2",
"typescript-eslint": "^7.13.1"
"typescript": "^5.5.3",
"typescript-eslint": "^7.2.0"
},
"dependencies": {
"@agoric/ertp": "^0.16.3-u16.1",
"@agoric/vats": "dev",
"@agoric/zoe": "^0.26.3-u16.1",
"@endo/far": "^1.1.5",
"@endo/marshal": "^1.5.3",
Expand All @@ -69,13 +70,35 @@
},
"homepage": "https://github.com/Agoric/dapp-offer-up#readme",
"eslintConfig": {
"env": {
"node": true
},
"parser": "@typescript-eslint/parser",
"parserOptions": {
"sourceType": "module",
"ecmaVersion": 2021
},
"ignorePatterns": "bundles/**.js",
"extends": [
"plugin:@typescript-eslint/recommended",
"@agoric"
]
],
"plugins": [
"@typescript-eslint",
"prettier"
],
"rules": {
"prettier/prettier": "warn",
"@typescript-eslint/no-unused-vars": [
"error",
{
"vars": "all",
"args": "all",
"argsIgnorePattern": "^_",
"varsIgnorePattern": "^_"
}
]
}
},
"prettier": {
"trailingComma": "all",
Expand Down
15 changes: 11 additions & 4 deletions contract/src/offer-up-proposal.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ const marshalData = makeMarshal(_val => Fail`data only`);
const IST_UNIT = 1_000_000n;
const CENT = IST_UNIT / 100n;

/**
* @import {ERef} from '@endo/far';
* @import {StorageNode} from '@agoric/internal/src/lib-chainStorage.js';
* @import {BootstrapManifest} from '@agoric/vats/src/core/lib-boot.js';
*/

/**
* Make a storage node for auxilliary data for a value on the board.
*
Expand All @@ -36,7 +42,11 @@ const publishBrandInfo = async (chainStorage, board, brand) => {
await E(node).setValue(JSON.stringify(aux));
};

// TODO get these from agoric-sdk
/** @typedef {Record<string, any>} BootstrapPowers */

/**
*
* Core eval script to start contract
*
* @param {BootstrapPowers} permittedPowers
Expand All @@ -47,19 +57,16 @@ export const startOfferUpContract = async permittedPowers => {
consume: { board, chainStorage, startUpgradable, zoe },
brand: {
consume: { IST: istBrandP },
// @ts-expect-error dynamic extension to promise space
produce: { Item: produceItemBrand },
},
issuer: {
consume: { IST: istIssuerP },
// @ts-expect-error dynamic extension to promise space
produce: { Item: produceItemIssuer },
},
installation: {
consume: { offerUp: offerUpInstallationP },
},
instance: {
// @ts-expect-error dynamic extension to promise space
produce: { offerUp: produceInstance },
},
} = permittedPowers;
Expand Down Expand Up @@ -98,7 +105,7 @@ export const startOfferUpContract = async permittedPowers => {
console.log('offerUp (re)started');
};

/** @type { import("@agoric/vats/src/core/lib-boot").BootstrapManifest } */
/** @type {BootstrapManifest} */
const offerUpManifest = {
[startOfferUpContract.name]: {
consume: {
Expand Down
7 changes: 6 additions & 1 deletion contract/src/offer-up.contract.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,19 @@ import { AmountShape } from '@agoric/ertp/src/typeGuards.js';
import { atomicRearrange } from '@agoric/zoe/src/contractSupport/atomicTransfer.js';
import '@agoric/zoe/exported.js';

/**
* @import {Amount} from '@agoric/ertp/src/types.js';
* @import {CopyBag} from '@endo/patterns';
*
*/
const { Fail, quote: q } = assert;

// #region bag utilities
/** @type { (xs: bigint[]) => bigint } */
const sum = xs => xs.reduce((acc, x) => acc + x, 0n);

/**
* @param {import('@endo/patterns').CopyBag} bag
* @param {CopyBag} bag
* @returns {bigint[]}
*/
const bagCounts = bag => {
Expand Down
9 changes: 8 additions & 1 deletion contract/test/mintStable.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,14 @@ const centralSupplyPath = myRequire.resolve(
'@agoric/vats/src/centralSupply.js',
);

/** @typedef {Installation<typeof import('@agoric/vats/src/centralSupply.js').start>} CentralSupplyInstallation */
/**
* @import {ERef} from '@endo/far';
* @import {Payment} from '@agoric/ertp/src/types.js';
* @import {start} from '@agoric/vats/src/centralSupply.js'
* @import {makeNodeBundleCache} from '@endo/bundle-source/cache.js';
*/

/** @typedef {Installation<typeof start>} CentralSupplyInstallation */
/** @typedef {Awaited<ReturnType<import('@endo/bundle-source/cache.js').makeNodeBundleCache>>} BundleCache */

/**
Expand Down
22 changes: 18 additions & 4 deletions contract/test/test-build-proposal.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,27 @@
/* eslint-disable import/order -- https://github.com/endojs/endo/issues/1235 */
import { test } from './prepare-test-env-ava.js';
import { test as anyTest } from './prepare-test-env-ava.js';
import { execSync } from 'node:child_process';
import { promises as fs } from 'node:fs';
import { makeCompressFile } from './utils.js';

test.before(t => (t.context.compressFile = makeCompressFile(fs.readFile)));
/**
* @typedef {{
* compressFile: (path: string) => Promise<Buffer>,
* }} TestContext
*/

test.only('proposal builder generates compressed bundles less than 1MB', async t => {
const stdout = execSync('agoric run scripts/build-contract-deployer.js', { encoding: 'utf8' });
const test = /** @type {import('ava').TestFn<TestContext>}} */ (anyTest);

test.before(t => {
t.context = {
compressFile: makeCompressFile(fs.readFile),
};
});

test('proposal builder generates compressed bundles less than 1MB', async t => {
const stdout = execSync('agoric run scripts/build-contract-deployer.js', {
encoding: 'utf8',
});
t.log('agoric run stdout:', stdout);
t.truthy(stdout, 'Proposal successfully bundled.');

Expand Down
37 changes: 23 additions & 14 deletions contract/test/test-contract.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { makeCopyBag } from '@endo/patterns';
import { makeNodeBundleCache } from '@endo/bundle-source/cache.js';
import { makeZoeKitForTest } from '@agoric/zoe/tools/setup-zoe.js';
import { AmountMath, makeIssuerKit } from '@agoric/ertp';
import '@agoric/zoe/src/zoeService/types-ambient.js';

import { makeStableFaucet } from './mintStable.js';
import { startOfferUpContract } from '../src/offer-up-proposal.js';
Expand All @@ -22,20 +23,29 @@ import { startOfferUpContract } from '../src/offer-up-proposal.js';
const myRequire = createRequire(import.meta.url);
const contractPath = myRequire.resolve(`../src/offer-up.contract.js`);

/** @type {import('ava').TestFn<Awaited<ReturnType<makeTestContext>>>} */
const test = anyTest;
/** @typedef {Awaited<ReturnType<import('@endo/bundle-source/cache.js').makeNodeBundleCache>>} BundleCache */

const UNIT6 = 1_000_000n;
const CENT = UNIT6 / 100n;
/**
* @typedef {{
* zoe: ZoeService,
* bundle: any,
* bundleCache: BundleCache,
* feeMintAccess: FeeMintAccess
* }} TestContext
*/

const test = /** @type {import('ava').TestFn<TestContext>}} */ (anyTest);

/**
* Tests assume access to the zoe service and that contracts are bundled.
*
* See test-bundle-source.js for basic use of bundleSource().
* Here we use a bundle cache to optimize running tests multiple times.
*
* @param {unknown} _t
* @import {ERef} from '@endo/far';
* @import {ExecutionContext} from 'ava';
* @import {Instance} from '@agoric/zoe/src/zoeService/utils.js';
* @import {Purse} from '@agoric/ertp/src/types.js';
*/

const UNIT6 = 1_000_000n;
const CENT = UNIT6 / 100n;

const makeTestContext = async _t => {
const { zoeService: zoe, feeMintAccess } = makeZoeKitForTest();

Expand Down Expand Up @@ -76,15 +86,14 @@ test('Start the contract', async t => {
/**
* Alice trades by paying the price from the contract's terms.
*
* @param {import('ava').ExecutionContext} t
* @param {ExecutionContext} t
* @param {ZoeService} zoe
* @param {ERef<import('@agoric/zoe/src/zoeService/utils').Instance<AssetContractFn>} instance
* @param {Purse} purse
* @param {ERef<Instance<AssetContractFn>>} instance
* @param {Purse<'nat'>} purse
* @param {string[]} choices
*/
const alice = async (t, zoe, instance, purse, choices = ['map', 'scroll']) => {
const publicFacet = E(zoe).getPublicFacet(instance);
// @ts-expect-error Promise<Instance> seems to work
const terms = await E(zoe).getTerms(instance);
const { issuers, brands, tradePrice } = terms;

Expand Down
27 changes: 27 additions & 0 deletions contract/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"compilerOptions": {
"target": "ESNext",
"module": "NodeNext",
"moduleResolution": "NodeNext",
"lib": [
"ESNext",
"DOM"
],
"checkJs": true,
"strict": true,
"noImplicitAny": false,
"useUnknownInCatchVariables": false,
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true,
"resolveJsonModule": true,
"outDir": "./dist"
},
"include": [
"src",
"test"
],
"exclude": [
"node_modules"
]
}
Loading

0 comments on commit 4111758

Please sign in to comment.