Skip to content

Commit

Permalink
feat: amm swap working
Browse files Browse the repository at this point in the history
  • Loading branch information
samsiegart committed Jul 13, 2022
1 parent 566f1ba commit 16d853c
Show file tree
Hide file tree
Showing 10 changed files with 67 additions and 31 deletions.
12 changes: 7 additions & 5 deletions packages/solo/public/wallet-bridge.html
Original file line number Diff line number Diff line change
Expand Up @@ -154,11 +154,13 @@
});
};

// Start the flow of messages.
if (window.parent !== window) {
window.parent.postMessage({ type: 'walletBridgeLoaded' }, '*');
}
retryWebSocket();
window.addEventListener('load', () => {
// Start the flow of messages.
if (window.parent !== window) {
window.parent.postMessage({ type: 'walletBridgeLoaded' }, '*');
}
retryWebSocket();
});
</script>
</body>
</html>
6 changes: 4 additions & 2 deletions packages/wallet/ui/src/bridge.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ const installDappConnection = ({ addEventListener, parentPost, t0 }) => {
console.debug('bridge: message from dapp->buffer', origin, ev.data);
buffer.push(harden(ev.data));
}
outIx += 1; // ISSUE: overflow?
});

return harden({
Expand All @@ -85,7 +84,10 @@ const installDappConnection = ({ addEventListener, parentPost, t0 }) => {
console.debug('bridge: storage -> message to dapp', origin, payload);
parentPost(payload, '*');
});
setItem = (key, value) => storage.setItem(key, value);
setItem = (key, value) => {
storage.setItem(key, value);
outIx += 1;
};
if (buffer.length) {
console.debug('sending', buffer.length, 'queued messages from', origin);
while (buffer.length) {
Expand Down
3 changes: 2 additions & 1 deletion packages/wallet/ui/src/components/Offer.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,9 @@ const OfferWithoutContext = ({
const {
instancePetname,
instanceHandleBoardId,
requestContext: { date, dappOrigin, origin = 'unknown origin' } = {},
requestContext: { dappOrigin, origin = 'unknown origin' } = {},
id,
meta: { creationStamp: date },
} = offer;
let status = offer.status || 'proposed';

Expand Down
5 changes: 2 additions & 3 deletions packages/wallet/ui/src/components/WalletConnection.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ const WalletConnection = ({
* getAdminBootstrap: (string, unknown) => WalletBridge
* }} WalletConnection
* @typedef {{
* getScopedBridge: (o: unknown) => unknown
* getScopedBridge: (petname: unknown, origin: unknown) => unknown
* }} WalletBridge
*/
const [wc, setWC] = useState(/** @type {WalletConnection|null} */ (null));
Expand Down Expand Up @@ -118,8 +118,7 @@ const WalletConnection = ({
);
ix += 1; // ISSUE: overflow?
};

const makeBoot = () => E(bridge).getScopedBridge(origin);
const makeBoot = () => E(bridge).getScopedBridge(origin, origin);
// console.debug('new capTP connection', { origin, epoch });
conn = makeCapTP(`from ${origin} at ${epoch}`, send, makeBoot);
ix = 0;
Expand Down
6 changes: 4 additions & 2 deletions packages/wallet/ui/src/components/tests/Offer.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,12 @@ const offer = {
decline: jest.fn(),
},
requestContext: {
date: 1636614038901,
dappOrigin: 'https://tokenpalace.app',
origin: 'unknown origin',
},
meta: {
creationStamp: 1636614038901,
},
proposalForDisplay: {
arguments: [],
give: {
Expand Down Expand Up @@ -176,7 +178,7 @@ test('renders the timestamp', () => {
const component = mount(<Offer offer={offer} />);

expect(component.find('.Date').text()).toContain(
formatDateNow(offer.requestContext.date),
formatDateNow(offer.meta.creationStamp),
);
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ jest.mock('@endo/eventual-send', () => ({
}),
}));

jest.mock('@endo/captp', () => ({
makeCapTP: jest.fn(),
}));

jest.mock('@agoric/web-components/react.js', () => {
return {
makeReactAgoricWalletConnection: jest.fn(() => 'wallet-connection'),
Expand Down
19 changes: 16 additions & 3 deletions packages/web-components/src/AgoricIframeMessenger.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable no-underscore-dangle */
// @ts-check
import { LitElement, html } from 'lit';
import { LitElement, html, css } from 'lit';

import { assert } from '@agoric/assert';

Expand All @@ -12,6 +12,19 @@ export const makeAgoricIframeMessenger = (registerThis = _that => {}) =>
return { src: { type: String } };
}

static get styles() {
return css`
:host {
display: block;
}
iframe {
border: none;
overflow: auto;
height: 40px;
}
`;
}

constructor() {
super();
this.src = '';
Expand Down Expand Up @@ -43,6 +56,7 @@ export const makeAgoricIframeMessenger = (registerThis = _that => {}) =>
@load=${this._onLoad}
@abort=${this._onError}
@error=${this._onError}
scrolling="no"
></iframe>
`;
}
Expand All @@ -62,16 +76,15 @@ export const makeAgoricIframeMessenger = (registerThis = _that => {}) =>

_onLoad(event) {
event.preventDefault();
const ev = new CustomEvent('open', { detail: { send: this.send } });
this._origin = new URL(this.src).origin;
this.dispatchEvent(ev);
}

_onMessage(event) {
// console.log('iframe message', event);
if (event.source !== this._contentWindow) {
return;
}

if (this._timeout) {
window.clearTimeout(this._timeout);
this._timeout = undefined;
Expand Down
20 changes: 7 additions & 13 deletions packages/web-components/src/AgoricWalletConnection.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ const RESET_DELAY_MS = 3000;

// TODO: Use something on agoric.app instead.
const DEFAULT_LOCATOR_URL =
'https://local.agoric.com/?append=/wallet/bridge.html';
'https://local.agoric.com/?append=/wallet-bridge.html';

const LOCAL_STORAGE_LOCATOR_URL = 'https://local.agoric.com/?append=/wallet/bridge.html'

const delay = (ms, resolution) =>
new Promise(resolve => setTimeout(resolve, ms, resolution));
Expand All @@ -33,7 +35,7 @@ export const makeAgoricWalletConnection = (makeCapTP = defaultMakeCapTP) =>
return css`
:host {
display: block;
padding: 25px;
padding: 24px;
color: var(--agoric-wallet-connection-text-color, #000);
}
`;
Expand All @@ -42,6 +44,7 @@ export const makeAgoricWalletConnection = (makeCapTP = defaultMakeCapTP) =>
static get properties() {
return {
state: { type: String },
useLocalStorage: { type: Boolean },
};
}

Expand Down Expand Up @@ -140,7 +143,7 @@ export const makeAgoricWalletConnection = (makeCapTP = defaultMakeCapTP) =>

constructor() {
super();

this.useLocalStorage = false;
// This state machine integration is much like lit-robot, but also raises
// state events.
const machine = makeConnectionMachine();
Expand Down Expand Up @@ -182,14 +185,6 @@ export const makeAgoricWalletConnection = (makeCapTP = defaultMakeCapTP) =>
});
}

onOpen(ev) {
console.log(this.state, 'open', ev);
if (this.state === 'bridged') {
assert(this._captp);
this._bridgePK.resolve(this._captp.getBootstrap());
}
}

onLocateMessage(ev) {
console.log(this.state, 'locate', ev);
const { data } = ev.detail;
Expand Down Expand Up @@ -243,8 +238,7 @@ export const makeAgoricWalletConnection = (makeCapTP = defaultMakeCapTP) =>
case 'locating': {
backend = html`
<agoric-iframe-messenger
src=${DEFAULT_LOCATOR_URL}
@open=${this.onOpen}
src=${this.useLocalStorage ? LOCAL_STORAGE_LOCATOR_URL : DEFAULT_LOCATOR_URL}
@message=${this.onLocateMessage}
@error=${this.onError}
></agoric-iframe-messenger>
Expand Down
2 changes: 1 addition & 1 deletion packages/web-components/src/bridge-iframe-connector.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export const makeBridgeIframeConnector = component => {

// Received bridge announcement, so mark the connection as bridged.
component.service.send({ type: 'connected' });
component._bridgePK.resolve(component._captp.getBootstrap());
};

return {
Expand All @@ -52,7 +53,6 @@ export const makeBridgeIframeConnector = component => {
return html`
<agoric-iframe-messenger
src=${url.href}
@open=${component.onOpen}
@message=${onMessage}
@error=${component.onError}
></agoric-iframe-messenger>
Expand Down
21 changes: 20 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7018,6 +7018,11 @@ csstype@^3.0.10, csstype@^3.0.2:
resolved "https://registry.yarnpkg.com/csstype/-/csstype-3.0.10.tgz#2ad3a7bed70f35b965707c092e5f30b327c290e5"
integrity sha512-2u44ZG2OcNUO9HDp/Jl8C07x6pU/eTR3ncV91SiK3dhG9TWvRVsCoJw14Ckx5DgWkzGA3waZWO3d7pgqpUI/XA==

cuint@^0.2.2:
version "0.2.2"
resolved "https://registry.yarnpkg.com/cuint/-/cuint-0.2.2.tgz#408086d409550c2631155619e9fa7bcadc3b991b"
integrity sha512-d4ZVpCW31eWwCMe1YT3ur7mUDnTXbgwyzaL320DrcRT45rfjYxkt5QWLrmOJ+/UEAI2+fQgKe/fCjR8l4TpRgw==

currently-unhandled@^0.4.1:
version "0.4.1"
resolved "https://registry.yarnpkg.com/currently-unhandled/-/currently-unhandled-0.4.1.tgz#988df33feab191ef799a61369dd76c17adf957ea"
Expand Down Expand Up @@ -9654,7 +9659,7 @@ http-proxy-agent@^4.0.1:
agent-base "6"
debug "4"

http-proxy-middleware@^2.0.3:
http-proxy-middleware@^2.0.3, http-proxy-middleware@^2.0.6:
version "2.0.6"
resolved "https://registry.yarnpkg.com/http-proxy-middleware/-/http-proxy-middleware-2.0.6.tgz#e1a4dd6979572c7ab5a4e4b55095d1f32a74963f"
integrity sha512-ya/UeJ6HVBYxrgYotAZo1KvPWlgB48kUJLDePFeneHsVujFaW5WNj2NgWCAE//B1Dl02BIfYlpNgBy8Kf8Rjmw==
Expand Down Expand Up @@ -14553,6 +14558,13 @@ react-app-polyfill@^3.0.0:
regenerator-runtime "^0.13.9"
whatwg-fetch "^3.6.2"

react-app-rewire-multiple-entry@^2.2.3:
version "2.2.3"
resolved "https://registry.yarnpkg.com/react-app-rewire-multiple-entry/-/react-app-rewire-multiple-entry-2.2.3.tgz#50bc510adf215c4610b62c9463186d40f966618e"
integrity sha512-7lGrGLuzLwoINQFQ6ikBvMxZwb58pqmG2gMTsoX8jefSICnj4LWp8Bu3IQ8MZDej8NdshhD8ihk+b/cFWhJzgQ==
dependencies:
xxhashjs "^0.2.2"

react-app-rewired@^2.2.1:
version "2.2.1"
resolved "https://registry.yarnpkg.com/react-app-rewired/-/react-app-rewired-2.2.1.tgz#84901ee1e3f26add0377ebec0b41bcdfce9fc211"
Expand Down Expand Up @@ -17823,6 +17835,13 @@ xtend@^4.0.2, xtend@~4.0.1:
resolved "https://registry.yarnpkg.com/xtend/-/xtend-4.0.2.tgz#bb72779f5fa465186b1f438f674fa347fdb5db54"
integrity sha512-LKYU1iAXJXUgAXn9URjiu+MWhyUXHsvfp7mcuYm9dSUKK0/CjtrUwFAxD82/mCWbtLsGjFIad0wIsod4zrTAEQ==

xxhashjs@^0.2.2:
version "0.2.2"
resolved "https://registry.yarnpkg.com/xxhashjs/-/xxhashjs-0.2.2.tgz#8a6251567621a1c46a5ae204da0249c7f8caa9d8"
integrity sha512-AkTuIuVTET12tpsVIQo+ZU6f/qDmKuRUcjaqR+OIvm+aCBsZ95i7UVY5WJ9TMsSaZ0DA2WxoZ4acu0sPH+OKAw==
dependencies:
cuint "^0.2.2"

y18n@^4.0.0:
version "4.0.0"
resolved "https://registry.yarnpkg.com/y18n/-/y18n-4.0.0.tgz#95ef94f85ecc81d007c264e190a120f0a3c8566b"
Expand Down

0 comments on commit 16d853c

Please sign in to comment.