-
Notifications
You must be signed in to change notification settings - Fork 4
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: pay ui #45
feat: pay ui #45
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
start-sell-concert-tickets-permit.json | ||
start-sell-concert-tickets.js | ||
bundles/ | ||
,tx.json |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,16 +12,18 @@ export const meta = harden({ | |
// compatibility with an earlier contract metadata API | ||
export const { customTermsShape } = meta; | ||
|
||
let issuerNumber = 1; | ||
const IssuerShape = M.remotable('Issuer'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. import from ertp? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. I did a deep import because of a comment in the swap contract about minimizing bundle size. |
||
|
||
/** | ||
* @typedef {object} PostalSvcTerms | ||
* @property {import('@agoric/vats').NameHub} namesByAddress | ||
*/ | ||
|
||
/** @param {ZCF<PostalSvcTerms>} zcf */ | ||
export const start = zcf => { | ||
const { namesByAddress, issuers } = zcf.getTerms(); | ||
const { namesByAddress } = zcf.getTerms(); | ||
mustMatch(namesByAddress, M.remotable('namesByAddress')); | ||
console.log('postal-service issuers', Object.keys(issuers)); | ||
|
||
/** | ||
* @param {string} addr | ||
|
@@ -38,9 +40,19 @@ export const start = zcf => { | |
*/ | ||
const sendTo = (addr, pmt) => E(getDepositFacet(addr)).receive(pmt); | ||
|
||
/** @param {string} recipient */ | ||
const makeSendInvitation = recipient => { | ||
/** | ||
* @param {string} recipient | ||
* @param {Issuer[]} issuers | ||
*/ | ||
const makeSendInvitation = (recipient, issuers) => { | ||
assert.typeof(recipient, 'string'); | ||
mustMatch(issuers, M.arrayOf(IssuerShape)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Did you consider making the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The UI is always just going to send the issuers anyway so it doesn't seem worth the complexity. We'd have to query and check which issuers are already added first, I'm not sure if they're published anywhere either. |
||
|
||
for (const i of issuers) { | ||
if (!Object.values(zcf.getTerms().issuers).includes(i)) { | ||
zcf.saveIssuer(i, `Issuer${(issuerNumber += 1)}`); | ||
} | ||
} | ||
|
||
/** @type {OfferHandler} */ | ||
const handleSend = async seat => { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,13 +7,11 @@ | |
*/ | ||
// @ts-check | ||
|
||
import { E } from '@endo/far'; | ||
import { fixHub } from './fixHub.js'; | ||
import { | ||
installContract, | ||
startContract, | ||
} from './platform-goals/start-contract.js'; | ||
import { allValues } from './objectTools.js'; | ||
|
||
const { Fail } = assert; | ||
|
||
|
@@ -23,17 +21,15 @@ const contractName = 'postalService'; | |
* @param {BootstrapPowers} powers | ||
* @param {{ options?: { postalService: { | ||
* bundleID: string; | ||
* issuerNames?: string[]; | ||
* }}}} [config] | ||
*/ | ||
export const startPostalService = async (powers, config) => { | ||
const { | ||
consume: { namesByAddressAdmin, agoricNames }, | ||
consume: { namesByAddressAdmin }, | ||
} = powers; | ||
const { | ||
// must be supplied by caller or template-replaced | ||
bundleID = Fail`no bundleID`, | ||
issuerNames = ['IST', 'Invitation', 'BLD', 'ATOM'], | ||
} = config?.options?.[contractName] ?? {}; | ||
|
||
const installation = await installContract(powers, { | ||
|
@@ -44,15 +40,9 @@ export const startPostalService = async (powers, config) => { | |
const namesByAddress = await fixHub(namesByAddressAdmin); | ||
const terms = harden({ namesByAddress }); | ||
|
||
const issuerKeywordRecord = await allValues( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This regression is a little surprising. But I guess I don't mind too much. |
||
Object.fromEntries( | ||
issuerNames.map(n => [n, E(agoricNames).lookup('issuer', n)]), | ||
), | ||
); | ||
|
||
await startContract(powers, { | ||
name: contractName, | ||
startArgs: { installation, issuerKeywordRecord, terms }, | ||
startArgs: { installation, terms }, | ||
}); | ||
}; | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
import { useAgoric } from '@agoric/react-components'; | ||
import ProposalAmountsBox from '../ProposalAmountsBox'; | ||
import RecipientInput from '../RecipientInput'; | ||
import { queryPurses } from '../../utils/queryPurses'; | ||
import { useContext, useEffect, useState } from 'react'; | ||
import type { Amount } from '@agoric/web-components'; | ||
import { useDisplayInfo } from '../../store/displayInfo'; | ||
import { NotificationContext } from '../../context/NotificationContext'; | ||
import { queryIssuers } from '../../utils/queryIssuers'; | ||
|
||
const Pay = () => { | ||
const { addNotification } = useContext(NotificationContext); | ||
const { purses, chainStorageWatcher, makeOffer } = useAgoric(); | ||
const [recipientAddr, setRecipientAddr] = useState(''); | ||
const [recipientError, setRecipientError] = useState(''); | ||
const [myAmounts, setMyAmounts] = useState<Amount[]>([]); | ||
const { brandToDisplayInfo } = useDisplayInfo(({ brandToDisplayInfo }) => ({ | ||
brandToDisplayInfo, | ||
})); | ||
|
||
useEffect(() => { | ||
let isCancelled = false; | ||
const checkRecipientSmartWallet = async () => { | ||
if (chainStorageWatcher && recipientAddr) { | ||
try { | ||
await queryPurses(chainStorageWatcher, recipientAddr); | ||
} catch (e) { | ||
if (!isCancelled) { | ||
setRecipientError('Failed to fetch recipient wallet.'); | ||
} | ||
} | ||
} | ||
}; | ||
|
||
if (!recipientAddr.length) { | ||
setRecipientError(''); | ||
} else if ( | ||
recipientAddr.startsWith('agoric') && | ||
recipientAddr.length === 45 | ||
) { | ||
setRecipientError(''); | ||
checkRecipientSmartWallet(); | ||
} else { | ||
setRecipientError('Invalid address format'); | ||
} | ||
|
||
return () => { | ||
isCancelled = true; | ||
}; | ||
}, [chainStorageWatcher, recipientAddr]); | ||
|
||
const sendOffer = async () => { | ||
assert(chainStorageWatcher && makeOffer); | ||
|
||
assert(chainStorageWatcher && makeOffer); | ||
try { | ||
const brandPetnameToIssuer = await queryIssuers(chainStorageWatcher); | ||
const issuers = new Set( | ||
[...myAmounts].map(amount => { | ||
const { petname } = brandToDisplayInfo.get(amount.brand)!; | ||
return brandPetnameToIssuer.get(petname); | ||
}), | ||
); | ||
|
||
const invitationSpec = { | ||
source: 'agoricContract', | ||
instancePath: ['postalService'], | ||
callPipe: [['makeSendInvitation', [recipientAddr, [...issuers]]]], | ||
}; | ||
|
||
const gives = myAmounts.map(amount => { | ||
const { petname } = brandToDisplayInfo.get(amount.brand)!; | ||
return [petname, amount]; | ||
}); | ||
const proposal = { | ||
give: { ...Object.fromEntries(gives) }, | ||
want: {}, | ||
}; | ||
|
||
makeOffer( | ||
invitationSpec, | ||
proposal, | ||
undefined, | ||
(update: { status: string; data?: unknown }) => { | ||
if (update.status === 'error') { | ||
addNotification!({ | ||
text: `Payment Error: ${update.data}`, | ||
status: 'error', | ||
}); | ||
} | ||
if (update.status === 'accepted') { | ||
addNotification!({ | ||
text: 'Payment Sent', | ||
status: 'success', | ||
}); | ||
} | ||
if (update.status === 'refunded') { | ||
addNotification!({ | ||
text: 'Payment Refunded', | ||
status: 'warning', | ||
}); | ||
} | ||
}, | ||
); | ||
} catch (e) { | ||
addNotification!({ | ||
text: `Offer error: ${e}`, | ||
status: 'error', | ||
}); | ||
} | ||
}; | ||
|
||
const isButtonDisabled = !makeOffer || !recipientAddr || !myAmounts.length; | ||
|
||
return ( | ||
<div className="items-top flex w-full flex-col justify-around lg:flex-row"> | ||
<div> | ||
<h2 className="daisyui-card-title mb-2 w-full">Send Payment</h2> | ||
<div className="daisyui-card h-fit w-96 bg-base-300 px-4 py-4 shadow-xl"> | ||
<RecipientInput | ||
address={recipientAddr} | ||
onChange={addr => setRecipientAddr(addr)} | ||
error={recipientError} | ||
/> | ||
<div className="my-1"> | ||
<h2 className="mb-2 text-lg font-medium">Give</h2> | ||
<ProposalAmountsBox | ||
actionLabel="Add from Your Purse" | ||
amounts={myAmounts} | ||
purses={purses} | ||
onChange={setMyAmounts} | ||
warning={purses ? undefined : 'Wallet Not Connected'} | ||
/> | ||
</div> | ||
<button | ||
onClick={sendOffer} | ||
disabled={isButtonDisabled} | ||
className="daisyui-btn daisyui-btn-primary mt-4 w-full self-center text-lg" | ||
> | ||
Send Payment | ||
</button> | ||
</div> | ||
</div> | ||
</div> | ||
); | ||
}; | ||
|
||
export default Pay; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
move this inside
start()
, please. No static mutable state.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I considered making it durable, but saw that this was what the swap contract does already ,so I figured it's not critical to preserve the issuer numbers. Is no static mutable state a hard rule? I can update it in both contracts if needed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please do.
It's part of OCap Discipline that isn't (yet?) enforced by our platform.
SES lockdown docs say:
But that's not quite true: SES doesn't (yet?) get rid of module-level static mutable state such as
So one program could do
and another could do
cc @kriskowal @erights
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nothing in the postal service contract is durable, so that would be above end beyond the call of duty here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah I see what you mean, move it into the function scope but don't necessarily make it durable. Done.