Skip to content

Commit

Permalink
refactor: extract @agoric/contracts package
Browse files Browse the repository at this point in the history
  • Loading branch information
turadg committed Apr 12, 2023
1 parent 6861f5e commit 4b2f52c
Show file tree
Hide file tree
Showing 148 changed files with 297 additions and 290 deletions.
12 changes: 12 additions & 0 deletions packages/SwingSet/src/kernel/state/storageHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ export function* enumeratePrefixedKeys(kvStore, prefix, exclusiveEnd) {
}
}

/**
* @param {KVStore} kvStore
* @param {string} prefix
*/
// NOTE: awkward naming: the thing that returns a stream of keys is named
// "enumerate..." while the thing that returns a stream of values is named
// "get..."
Expand All @@ -43,12 +47,20 @@ function* enumerateNumericPrefixedKeys(kvStore, prefix) {
}
}

/**
* @param {KVStore} kvStore
* @param {string} prefix
*/
export function* getPrefixedValues(kvStore, prefix) {
for (const key of enumerateNumericPrefixedKeys(kvStore, prefix)) {
yield kvStore.get(key) || Fail`enumerate ensures get`;
}
}

/**
* @param {KVStore} kvStore
* @param {string} prefix
*/
export function deletePrefixedKeys(kvStore, prefix) {
// this is kind of like a deleteRange() would be, but can be implemented
// efficiently without backend DB support because it only looks at numeric
Expand Down
5 changes: 5 additions & 0 deletions packages/contracts/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Contracts Support

This package contains utilities that contracts tend to need.

It also includes sample contracts to learn from and play with.
12 changes: 12 additions & 0 deletions packages/contracts/jsconfig.build.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"extends": "./jsconfig.json",
"compilerOptions": {
"noEmit": false,
"declaration": true,
"emitDeclarationOnly": true,
"declarationMap": true
},
"exclude": [
"test/"
]
}
17 changes: 17 additions & 0 deletions packages/contracts/jsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// This file can contain .js-specific Typescript compiler config.
{
"extends": "../../tsconfig.json",
"compilerOptions": {
// Disable b/c @endo/init can't pass noImplicitAny
"checkJs": false,
"noImplicitAny": true,
},
"include": [
"*.js",
"*.ts",
"src/**/*.js",
"src/**/*.ts",
"test/**/*.js",
"test/**/*.ts"
]
}
49 changes: 49 additions & 0 deletions packages/contracts/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
{
"name": "@agoric/contracts",
"version": "0.0.1",
"description": "Utilities and sample contracts to support contract development",
"type": "module",
"main": "src/index.js",
"engines": {
"node": ">=14.15.0"
},
"scripts": {
"build": "exit 0",
"prepack": "tsc --build jsconfig.build.json",
"postpack": "git clean -f '*.d.ts*'",
"test": "ava",
"test:nyc": "exit 0",
"test:xs": "exit 0",
"lint-fix": "yarn lint:eslint --fix",
"lint": "run-s --continue-on-error lint:*",
"lint:eslint": "eslint .",
"lint:types": "tsc -p jsconfig.json"
},
"dependencies": {
"@agoric/assert": "^0.5.1",
"@agoric/ertp": "^0.15.3",
"@agoric/internal": "^0.2.1",
"@agoric/notifier": "^0.5.1",
"@agoric/store": "^0.8.3",
"@agoric/vat-data": "^0.4.3",
"@agoric/zoe": "^0.25.3",
"@endo/eventual-send": "^0.16.8",
"@endo/marshal": "^0.8.1",
"@endo/nat": "^4.1.0",
"@endo/promise-kit": "^0.2.52",
"anylogger": "^0.21.0",
"jessie.js": "^0.3.2"
},
"devDependencies": {
"@endo/init": "^0.5.52",
"ava": "^5.2.0"
},
"author": "Agoric",
"license": "Apache-2.0",
"files": [
"src"
],
"publishConfig": {
"access": "public"
}
}
Original file line number Diff line number Diff line change
@@ -1,23 +1,8 @@
import { mustMatch, M } from '@agoric/store';
import { assertRightsConserved } from '../contractFacet/rightsConservation.js';
import { AmountKeywordRecordShape, SeatShape } from '../typeGuards.js';
import { assertRightsConserved } from '@agoric/zoe/src/contractFacet/rightsConservation.js';

const { Fail, quote: q } = assert;

export const TransferPartShape = M.splitArray(
harden([M.opt(SeatShape), M.opt(SeatShape), M.opt(AmountKeywordRecordShape)]),
harden([M.opt(AmountKeywordRecordShape)]),
);

/**
* @typedef {[
* fromSeat?: ZCFSeat,
* toSeat?: ZCFSeat,
* fromAmounts?: AmountKeywordRecord,
* toAmounts?: AmountKeywordRecord
* ]} TransferPart
*/

/**
* Asks Zoe (via zcf) to rearrange the allocations among the seats
* mentioned. This is a set of changes to allocations that must satisfy
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,28 +32,10 @@ export {
offerTo,
} from './zoeHelpers.js';

export {
makeRatio,
makeRatioFromAmounts,
floorMultiplyBy,
floorDivideBy,
ceilMultiplyBy,
ceilDivideBy,
assertIsRatio,
invertRatio,
oneMinus,
addRatios,
multiplyRatios,
ratiosSame,
quantize,
ratioGTE,
subtractRatios,
ratioToNumber,
} from './ratio.js';

export * from './durability.js';
export * from './priceAuthority.js';
export * from './priceQuote.js';
export * from './statistics.js';
export * from './ratio.js';
export * from './recorder.js';
export * from './statistics.js';
export * from './topics.js';
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { E } from '@endo/eventual-send';
import { makePromiseKit } from '@endo/promise-kit';
import { AssetKind } from '@agoric/ertp';
import { fromUniqueEntries } from '@agoric/internal';
import { satisfiesWant } from '../contractFacet/offerSafety.js';
import { satisfiesWant } from '@agoric/zoe/src/contractFacet/offerSafety.js';
import {
atomicRearrange,
atomicTransfer,
Expand Down
4 changes: 4 additions & 0 deletions packages/contracts/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// XXX get ambients
import '@agoric/zoe/tools/types-ambient.js';

export * from './contractSupport/index.js';
2 changes: 1 addition & 1 deletion packages/governance/src/committee.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { makeStoredPublishKit } from '@agoric/notifier';
import { makeScalarMapStore, makeExo, M } from '@agoric/store';
import { natSafeMath } from '@agoric/zoe/src/contractSupport/index.js';
import { natSafeMath } from '@agoric/contracts';
import { E } from '@endo/eventual-send';

import { makeHandle } from '@agoric/zoe/src/makeHandle.js';
Expand Down
2 changes: 1 addition & 1 deletion packages/governance/src/contractGovernance/assertions.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { isRemotable } from '@endo/marshal';
import { assertIsRatio } from '@agoric/zoe/src/contractSupport/ratio.js';
import { assertIsRatio } from '@agoric/contracts';
import { mustMatch } from '@agoric/store';
import { RelativeTimeRecordShape, TimestampRecordShape } from '@agoric/time';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { test } from '@agoric/zoe/tools/prepare-test-env-ava.js';
import { AmountMath, AssetKind, makeIssuerKit } from '@agoric/ertp';
import { makeStoredPublisherKit } from '@agoric/notifier';
import { keyEQ } from '@agoric/store';
import { makeRatio } from '@agoric/zoe/src/contractSupport/index.js';
import { makeRatio } from '@agoric/contracts';
import { makeHandle } from '@agoric/zoe/src/makeHandle.js';
import { setupZCFTest } from '@agoric/zoe/test/unitTests/zcf/setupZcfTest.js';
import { E } from '@endo/eventual-send';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { test } from '@agoric/zoe/tools/prepare-test-env-ava.js';
import '@agoric/zoe/exported.js';
import { AmountMath, AssetKind, makeIssuerKit } from '@agoric/ertp';
import { makeRatio } from '@agoric/zoe/src/contractSupport/index.js';
import { makeRatio } from '@agoric/contracts';

import { makeHandle } from '@agoric/zoe/src/makeHandle.js';
import { makeParamChangePositions } from '../../src/index.js';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { test } from '@agoric/zoe/tools/prepare-test-env-ava.js';
import { makeIssuerKit, AmountMath } from '@agoric/ertp';
import { makeStoredPublisherKit } from '@agoric/notifier';
import { makeRatio } from '@agoric/zoe/src/contractSupport/index.js';
import { makeRatio } from '@agoric/contracts';
import { setupZCFTest } from '@agoric/zoe/test/unitTests/zcf/setupZcfTest.js';
import { E } from '@endo/eventual-send';
import { Far } from '@endo/marshal';
Expand Down
1 change: 1 addition & 0 deletions packages/inter-protocol/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"homepage": "https://github.com/Agoric/agoric-sdk#readme",
"dependencies": {
"@agoric/assert": "^0.5.1",
"@agoric/contracts": "^0.0.1",
"@agoric/ertp": "^0.15.3",
"@agoric/governance": "^0.9.1",
"@agoric/internal": "^0.2.1",
Expand Down
2 changes: 1 addition & 1 deletion packages/inter-protocol/src/auction/auctionBook.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
makeRatioFromAmounts,
multiplyRatios,
ratioGTE,
} from '@agoric/zoe/src/contractSupport/index.js';
} from '@agoric/contracts';
import { E } from '@endo/captp';
import {
observeNotifier,
Expand Down
6 changes: 3 additions & 3 deletions packages/inter-protocol/src/auction/auctioneer.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
provideEmptySeat,
ceilMultiplyBy,
ceilDivideBy,
} from '@agoric/zoe/src/contractSupport/index.js';
} from '@agoric/contracts';
import { FullProposalShape } from '@agoric/zoe/src/typeGuards.js';
import { E } from '@endo/eventual-send';
import { Far } from '@endo/marshal';
Expand Down Expand Up @@ -84,7 +84,7 @@ const distributeProportionalShares = (

const collShare = makeRatioFromAmounts(collateralReturn, totalCollDeposited);
const currShare = makeRatioFromAmounts(currencyRaised, totalCollDeposited);
/** @type {import('@agoric/zoe/src/contractSupport/atomicTransfer.js').TransferPart[]} */
/** @type {TransferPart[]} */
const transfers = [];
let currencyLeft = currencyRaised;
let collateralLeft = collateralReturn;
Expand Down Expand Up @@ -237,7 +237,7 @@ export const distributeProportionalSharesWithLimits = (
// collateral to reach their share. Then see what's left, and allocate it
// among the remaining depositors. Escape to distributeProportionalShares if
// anything doesn't work.
/** @type {import('@agoric/zoe/src/contractSupport/atomicTransfer.js').TransferPart[]} */
/** @type {TransferPart[]} */
const transfers = [];
let currencyLeft = currencyRaised;
let collateralLeft = collateralReturn;
Expand Down
2 changes: 1 addition & 1 deletion packages/inter-protocol/src/auction/scheduleMath.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { TimeMath } from '@agoric/time';
import { natSafeMath } from '@agoric/zoe/src/contractSupport/index.js';
import { natSafeMath } from '@agoric/contracts';
import { makeTracer } from '@agoric/internal';

const { subtract, multiply, floorDivide } = natSafeMath;
Expand Down
5 changes: 1 addition & 4 deletions packages/inter-protocol/src/auction/sortedOffers.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
import {
makeRatio,
ratioToNumber,
} from '@agoric/zoe/src/contractSupport/index.js';
import { makeRatio, ratioToNumber } from '@agoric/contracts';
import { M, mustMatch } from '@agoric/store';
import { RatioShape } from '@agoric/ertp';

Expand Down
2 changes: 1 addition & 1 deletion packages/inter-protocol/src/auction/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {
makeRatioFromAmounts,
multiplyRatios,
ratioGTE,
} from '@agoric/zoe/src/contractSupport/index.js';
} from '@agoric/contracts';

/**
* Constants for Auction State.
Expand Down
2 changes: 1 addition & 1 deletion packages/inter-protocol/src/clientSupport.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import { Fail } from '@agoric/assert';
import { AmountMath } from '@agoric/ertp';
import { assertAllDefined } from '@agoric/internal';
import { parseRatio } from '@agoric/zoe/src/contractSupport/ratio.js';
import { parseRatio } from '@agoric/contracts';

// XXX support other decimal places
const COSMOS_UNIT = 1_000_000n;
Expand Down
2 changes: 1 addition & 1 deletion packages/inter-protocol/src/collectFees.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { atomicTransfer } from '@agoric/zoe/src/contractSupport/index.js';
import { atomicTransfer } from '@agoric/contracts';

/**
* Provide shared support for providing access to fees from a service contract.
Expand Down
2 changes: 1 addition & 1 deletion packages/inter-protocol/src/interest-math.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
invertRatio,
multiplyRatios,
ratiosSame,
} from '@agoric/zoe/src/contractSupport/ratio.js';
} from '@agoric/contracts';

/**
*
Expand Down
8 changes: 4 additions & 4 deletions packages/inter-protocol/src/interest.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { AmountMath } from '@agoric/ertp';
import { natSafeMath } from '@agoric/zoe/src/contractSupport/index.js';
import { Fail } from '@agoric/assert';
import {
makeRatio,
multiplyRatios,
natSafeMath,
quantize,
} from '@agoric/zoe/src/contractSupport/ratio.js';
import { Fail } from '@agoric/assert';
} from '@agoric/contracts';
import { AmountMath } from '@agoric/ertp';
import { TimeMath } from '@agoric/time';

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/inter-protocol/src/price/fluxAggregator.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { makeScalarBigMapStore } from '@agoric/vat-data';
import {
makeOnewayPriceAuthorityKit,
makeStorageNodePathProvider,
} from '@agoric/zoe/src/contractSupport/index.js';
} from '@agoric/contracts';
import { E } from '@endo/eventual-send';
import { Far } from '@endo/marshal';
import { makeOracleAdminKit } from './priceOracleKit.js';
Expand Down
5 changes: 1 addition & 4 deletions packages/inter-protocol/src/price/roundsManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@ import {
makeKindHandle,
makeScalarBigMapStore,
} from '@agoric/vat-data';
import {
calculateMedian,
natSafeMath,
} from '@agoric/zoe/src/contractSupport/index.js';
import { calculateMedian, natSafeMath } from '@agoric/contracts';
import { E } from '@endo/eventual-send';
import { Far } from '@endo/marshal';
import { UnguardedHelperI } from '../typeGuards.js';
Expand Down
4 changes: 2 additions & 2 deletions packages/inter-protocol/src/proposals/addAssetToVault.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { AmountMath, AssetKind } from '@agoric/ertp';
import { makeRatio } from '@agoric/zoe/src/contractSupport/index.js';
import { makeRatio } from '@agoric/contracts';
import { deeplyFulfilledObject } from '@agoric/internal';
import { Stable } from '@agoric/vats/src/tokens.js';
import { E } from '@endo/far';
import { parseRatio } from '@agoric/zoe/src/contractSupport/ratio.js';
import { parseRatio } from '@agoric/contracts';
import { reserveThenGetNames } from './utils.js';

export * from './startPSM.js';
Expand Down
2 changes: 1 addition & 1 deletion packages/inter-protocol/src/proposals/econ-behaviors.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import '@agoric/vats/exported.js';
import '@agoric/vats/src/core/types.js';
import { Stable, Stake } from '@agoric/vats/src/tokens.js';
import { makeStorageNodeChild } from '@agoric/internal/src/lib-chainStorage.js';
import { makeRatio } from '@agoric/zoe/src/contractSupport/index.js';
import { makeRatio } from '@agoric/contracts';
import { E } from '@endo/far';
import { LienBridgeId, makeStakeReporter } from '../my-lien.js';
import { makeReserveTerms } from '../reserve/params.js';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
} from '@agoric/internal/src/lib-chainStorage.js';
import { deeplyFulfilledObject, makeTracer } from '@agoric/internal';

import { unitAmount } from '@agoric/zoe/src/contractSupport/priceQuote.js';
import { unitAmount } from '@agoric/contracts';
import { CONTRACT_ELECTORATE, ParamTypes } from '@agoric/governance';
import { reserveThenDeposit, reserveThenGetNames } from './utils.js';

Expand Down
2 changes: 1 addition & 1 deletion packages/inter-protocol/src/proposals/startPSM.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { AmountMath, AssetKind } from '@agoric/ertp';
import { CONTRACT_ELECTORATE, ParamTypes } from '@agoric/governance';
import { makeStorageNodeChild } from '@agoric/internal/src/lib-chainStorage.js';
import { makeRatio } from '@agoric/zoe/src/contractSupport/index.js';
import { makeRatio } from '@agoric/contracts';
import { E } from '@endo/far';
import { Stable } from '@agoric/vats/src/tokens.js';
import { deeplyFulfilledObject } from '@agoric/internal';
Expand Down
Loading

0 comments on commit 4b2f52c

Please sign in to comment.