Skip to content

Commit

Permalink
Include code for Offer up Dapp (#10)
Browse files Browse the repository at this point in the history
* feat(ci): Included ui/ and contract/ from offer-up-dapp (with changes)

* refactor: moved ui/ and contract/ to tools/ folder
  • Loading branch information
frazarshad authored Mar 4, 2024
1 parent dbc5fe0 commit f35b3fe
Show file tree
Hide file tree
Showing 45 changed files with 1,964 additions and 0 deletions.
3 changes: 3 additions & 0 deletions tools/contract/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
start-offer-up-permit.json
start-offer-up.js
bundles/
121 changes: 121 additions & 0 deletions tools/contract/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
CHAINID=agoriclocal
USER1ADDR=$(shell agd keys show user1 -a --keyring-backend="test")
ACCT_ADDR=$(USER1ADDR)
BLD=000000ubld

ATOM_DENOM=ibc/BA313C4A19DFBF943586C0387E6B11286F9E416B4DD27574E6909CABE0E342FA
ATOM=000000$(ATOM_DENOM)

.PHONY: list
# https://stackoverflow.com/a/73159833/7963
list:
@make -npq : 2> /dev/null | grep -v PHONY |\
awk -v RS= -F: '$$1 ~ /^[^#%]+$$/ { print $$1 }'

balance-q:
agd keys show user1 -a --keyring-backend="test"
agd query bank balances $(ACCT_ADDR)

GAS_ADJUSTMENT=1.2
SIGN_BROADCAST_OPTS=--keyring-backend=test --chain-id=$(CHAINID) \
--gas=auto --gas-adjustment=$(GAS_ADJUSTMENT) \
--yes -b block

mint100:
make FUNDS=1000$(ATOM) fund-acct
cd /usr/src/agoric-sdk && \
yarn --silent agops vaults open --wantMinted 100 --giveCollateral 100 >/tmp/want-ist.json && \
yarn --silent agops perf satisfaction --executeOffer /tmp/want-ist.json --from user1 --keyring-backend=test

# https://agoric.explorers.guru/proposal/61
lower-bundle-cost: bundles/lower-bundle-cost.json ./scripts/voteLatestProposalAndWait.sh
agd tx gov submit-proposal param-change bundles/lower-bundle-cost.json \
$(SIGN_BROADCAST_OPTS) \
--from user1
./scripts/voteLatestProposalAndWait.sh
# agd query swingset params


bundles/swingset-params.json:
mkdir -p bundles/
agd query swingset params -o json >$@

.ONESHELL:
bundles/lower-bundle-cost.json: bundles/swingset-params.json
@read PARAMS < bundles/swingset-params.json; export PARAMS
node - <<- EOF >$@
const storageByte = '20000000';
const paramChange = {
title: 'Lower Bundle Cost to 0.02 IST/Kb (a la mainnet 61)',
description: '0.02 IST/Kb',
deposit: '10000000ubld',
changes: [{
subspace: 'swingset',
key: 'beans_per_unit',
value: '...',
}],
};
const params = JSON.parse(process.env.PARAMS);
const ix = params.beans_per_unit.findIndex(({key}) => key === 'storageByte');
params.beans_per_unit[ix].beans = storageByte;
paramChange.changes[0].value = params.beans_per_unit;
console.log(JSON.stringify(paramChange, null, 2));
EOF

# Keep mint4k around a while for compatibility
mint4k:
make FUNDS=1000$(ATOM) fund-acct
cd /usr/src/agoric-sdk && \
yarn --silent agops vaults open --wantMinted 4000 --giveCollateral 1000 >/tmp/want4k.json && \
yarn --silent agops perf satisfaction --executeOffer /tmp/want4k.json --from user1 --keyring-backend=test

FUNDS=321$(BLD)
fund-acct:
agd tx bank send validator $(ACCT_ADDR) $(FUNDS) \
$(SIGN_BROADCAST_OPTS) \
-o json >,tx.json
jq '{code: .code, height: .height}' ,tx.json

gov-q:
agd query gov proposals --output json | \
jq -c '.proposals[] | [.proposal_id,.voting_end_time,.status]'

gov-voting-q:
agd query gov proposals --status=voting_period --output json | \
jq -c '.proposals[].proposal_id'

PROPOSAL=1
VOTE_OPTION=yes
vote:
agd tx gov vote $(PROPOSAL) $(VOTE_OPTION) --from=validator \
$(SIGN_BROADCAST_OPTS) \
-o json >,tx.json
jq '{code: .code, height: .height}' ,tx.json

instance-q:
agd query vstorage data published.agoricNames.instance -o json

print-key: /root/.agoric/user1.key
@echo Import the following mnemonic into Keplr:
@cat $<
@echo
@echo -n 'The resulting address should be: '
@agd keys show user1 -a --keyring-backend="test"
@echo

SCRIPT=start-offer-up.js
PERMIT=start-offer-up-permit.json
start-contract: $(SCRIPT) $(PERMIT) install-bundles
scripts/propose-start-contract.sh

install-bundles: bundles/bundle-list
./scripts/install-bundles.sh

build-proposal: bundles/bundle-list

bundles/bundle-list $(SCRIPT) $(PERMIT):
./scripts/build-proposal.sh


clean:
@rm -rf $(SCRIPT) $(PERMIT) bundles/
19 changes: 19 additions & 0 deletions tools/contract/jsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// This file can contain .js-specific Typescript compiler config.
{
"compilerOptions": {
"target": "esnext",
"module": "ES2022",

"noEmit": true,
/*
// The following flags are for creating .d.ts files:
"noEmit": false,
"declaration": true,
"emitDeclarationOnly": true,
*/
"downlevelIteration": true,
"strictNullChecks": true,
"moduleResolution": "node"
},
"include": ["src/**/*.js", "test/**/*.js", "exported.js", "globals.d.ts"]
}
85 changes: 85 additions & 0 deletions tools/contract/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
{
"name": "demo2-contract",
"version": "0.1.0",
"private": true,
"description": "Offer Up Contract",
"type": "module",
"scripts": {
"start:docker": "docker compose up -d",
"docker:logs": "docker compose logs --tail 200 -f",
"docker:bash": "docker compose exec agd bash",
"docker:make": "docker compose exec agd make -C /workspace/contract",
"make:help": "make list",
"start": "yarn docker:make clean start-contract print-key",
"build": "agoric run scripts/build-contract-deployer.js",
"test": "ava --verbose",
"lint": "eslint '**/*.js'",
"lint:fix": "eslint --fix '**/*.js'"
},
"devDependencies": {
"@agoric/deploy-script-support": "^0.10.4-u12.0",
"@agoric/eslint-config": "dev",
"@endo/bundle-source": "^2.8.0",
"@endo/eslint-plugin": "^0.5.2",
"@endo/init": "^0.5.60",
"@endo/promise-kit": "0.2.56",
"@endo/ses-ava": "^0.2.44",
"@jessie.js/eslint-plugin": "^0.4.0",
"@typescript-eslint/eslint-plugin": "^6.7.0",
"@typescript-eslint/parser": "^6.7.0",
"agoric": "^0.21.2-u12.0",
"ava": "^5.3.0",
"eslint": "^8.47.0",
"eslint-config-airbnb-base": "^15.0.0",
"eslint-config-jessie": "^0.0.6",
"eslint-config-prettier": "^9.0.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",
"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"
},
"dependencies": {
"@agoric/ertp": "^0.16.3-u12.0",
"@agoric/zoe": "^0.26.3-u12.0",
"@endo/far": "^0.2.22",
"@endo/marshal": "^0.8.9",
"@endo/patterns": "^0.2.5"
},
"ava": {
"files": [
"test/**/test-*.js"
],
"timeout": "10m"
},
"keywords": [],
"repository": {
"type": "git",
"url": "git+https://github.com/Agoric/dapp-offer-up"
},
"author": "Agoric",
"license": "Apache-2.0",
"bugs": {
"url": "https://github.com/Agoric/dapp-offer-up/issues"
},
"homepage": "https://github.com/Agoric/dapp-offer-up#readme",
"eslintConfig": {
"parserOptions": {
"sourceType": "module",
"ecmaVersion": 2021
},
"extends": [
"@agoric"
]
},
"prettier": {
"trailingComma": "all",
"arrowParens": "avoid",
"singleQuote": true
}
}
42 changes: 42 additions & 0 deletions tools/contract/scripts/build-contract-deployer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/**
* @file Permission Contract Deployment builder
*
* Creates files for starting an instance of the contract:
* * contract source and instantiation proposal bundles to be published via
* `agd tx swingset install-bundle`
* * start-offer-up-permit.json and start-offer-up.js to submit the
* instantiation proposal via `agd tx gov submit-proposal swingset-core-eval`
*
* Usage:
* agoric run build-contract-deployer.js
*/

import { makeHelpers } from '@agoric/deploy-script-support';
import { getManifestForOfferUp } from '../src/offer-up-proposal.js';

/** @type {import('@agoric/deploy-script-support/src/externalTypes.js').ProposalBuilder} */
export const offerUpProposalBuilder = async ({ publishRef, install }) => {
return harden({
sourceSpec: '../src/offer-up-proposal.js',
getManifestCall: [
getManifestForOfferUp.name,
{
offerUpRef: publishRef(
install(
'../src/offer-up.contract.js',
'../bundles/bundle-offer-up.js',
{
persist: true,
},
),
),
},
],
});
};

/** @type {DeployScriptFunction} */
export default async (homeP, endowments) => {
const { writeCoreProposal } = await makeHelpers(homeP, endowments);
await writeCoreProposal('start-offer-up', offerUpProposalBuilder);
};
11 changes: 11 additions & 0 deletions tools/contract/scripts/build-proposal.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/bin/sh
# NOTE: intended to run _inside_ the agd container

cd /workspace/contract

mkdir -p bundles
(agoric run ./scripts/build-contract-deployer.js )>/tmp/,run.log
./scripts/parseProposals.mjs </tmp/,run.log \
| jq -r '.bundles[]' | sort -u > bundles/bundle-list


24 changes: 24 additions & 0 deletions tools/contract/scripts/install-bundles.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/bin/bash
# NOTE: intended to run _inside_ the agd container

set -xueo pipefail

cd /workspace/contract

# TODO: try `agoric publish` to better track outcome
install_bundle() {
ls -sh "$1"
agd tx swingset install-bundle --compress "@$1" \
--from user1 --keyring-backend=test --gas=auto --gas-adjustment=1.2 \
--chain-id=agoriclocal -bblock --yes -o json
}

# exit fail if bundle-list is emtpy
[ -s bundles/bundle-list ] || exit 1

make balance-q # do we have enough IST?

for b in $(cat bundles/bundle-list); do
echo installing $b
install_bundle $b
done
41 changes: 41 additions & 0 deletions tools/contract/scripts/parseProposals.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#!/usr/bin/env node

import fs from 'fs';

const Fail = (template, ...args) => {
throw Error(String.raw(template, ...args.map(val => String(val))));
};

/**
* Parse output of `agoric run proposal-builder.js`
*
* @param {string} txt
*
* adapted from packages/boot/test/bootstrapTests/supports.js
*/
const parseProposalParts = txt => {
const evals = [
...txt.matchAll(/swingset-core-eval (?<permit>\S+) (?<script>\S+)/g),
].map(m => {
if (!m.groups) throw Fail`Invalid proposal output ${m[0]}`;
const { permit, script } = m.groups;
return { permit, script };
});
evals.length || Fail`No swingset-core-eval found in proposal output: ${txt}`;

const bundles = [...txt.matchAll(/swingset install-bundle @([^\n]+)/gm)].map(
([, bundle]) => bundle,
);
bundles.length || Fail`No bundles found in proposal output: ${txt}`;

return { evals, bundles };
};

const main = (stdin, readFileSync) => {
const input = readFileSync(stdin.fd).toString();
const parts = parseProposalParts(input);
// relies on console.log printing to stdout unmodified
console.log(JSON.stringify(parts));
};

main(process.stdin, fs.readFileSync);
22 changes: 22 additions & 0 deletions tools/contract/scripts/propose-start-contract.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/bin/bash
set -xueo pipefail

cd /workspace/contract

SCRIPT=start-offer-up.js
PERMIT=start-offer-up-permit.json
ls -sh "$SCRIPT" "$PERMIT"

PROPOSAL=$(agd query gov proposals --output json | jq -c '.proposals | length | .+1')

make fund-acct

agd tx gov submit-proposal swingset-core-eval "$PERMIT" "$SCRIPT" \
--title="Start Offer Up Contract" --description="Evaluate $SCRIPT" \
--deposit=10000000ubld --gas=auto --gas-adjustment=1.2 \
--from user1 --chain-id agoriclocal --keyring-backend=test \
--yes -b block

set +x # not so noisy for this part
. /usr/src/upgrade-test-scripts/env_setup.sh
voteLatestProposalAndWait
Loading

0 comments on commit f35b3fe

Please sign in to comment.