-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: use wallet connection component
- Loading branch information
1 parent
91e461d
commit 4e0ceff
Showing
10 changed files
with
206 additions
and
209 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,156 +1,38 @@ | ||
import { useEffect } from 'react'; | ||
|
||
import './App.css'; | ||
import { | ||
makeAgoricChainStorageWatcher, | ||
AgoricChainStoragePathKind as Kind, | ||
} from '@agoric/rpc'; | ||
import { create } from 'zustand'; | ||
import { | ||
makeAgoricWalletConnection, | ||
suggestChain, | ||
} from '@agoric/web-components'; | ||
import { subscribeLatest } from '@agoric/notifier'; | ||
import { makeCopyBag } from '@agoric/store'; | ||
import { Logos } from './components/Logos'; | ||
import { Inventory } from './components/Inventory'; | ||
import { Trade } from './components/Trade'; | ||
import { ContractProvider } from './providers/Contract'; | ||
import { AgoricProvider } from '@agoric/react-components'; | ||
import { wallets } from 'cosmos-kit'; | ||
import '@agoric/react-components/dist/style.css'; | ||
|
||
const { entries, fromEntries } = Object; | ||
|
||
type Wallet = Awaited<ReturnType<typeof makeAgoricWalletConnection>>; | ||
|
||
const ENDPOINTS = { | ||
RPC: 'http://localhost:26657', | ||
API: 'http://localhost:1317', | ||
}; | ||
|
||
const watcher = makeAgoricChainStorageWatcher(ENDPOINTS.API, 'agoriclocal'); | ||
|
||
interface AppState { | ||
wallet?: Wallet; | ||
offerUpInstance?: unknown; | ||
brands?: Record<string, unknown>; | ||
purses?: Array<Purse>; | ||
} | ||
|
||
const useAppStore = create<AppState>(() => ({})); | ||
|
||
const setup = async () => { | ||
watcher.watchLatest<Array<[string, unknown]>>( | ||
[Kind.Data, 'published.agoricNames.instance'], | ||
instances => { | ||
console.log('got instances', instances); | ||
useAppStore.setState({ | ||
offerUpInstance: instances.find(([name]) => name === 'offerUp')!.at(1), | ||
}); | ||
}, | ||
); | ||
|
||
watcher.watchLatest<Array<[string, unknown]>>( | ||
[Kind.Data, 'published.agoricNames.brand'], | ||
brands => { | ||
console.log('Got brands', brands); | ||
useAppStore.setState({ | ||
brands: fromEntries(brands), | ||
}); | ||
}, | ||
); | ||
}; | ||
|
||
const connectWallet = async () => { | ||
await suggestChain('https://local.agoric.net/network-config'); | ||
const wallet = await makeAgoricWalletConnection(watcher, ENDPOINTS.RPC); | ||
useAppStore.setState({ wallet }); | ||
const { pursesNotifier } = wallet; | ||
for await (const purses of subscribeLatest(pursesNotifier)) { | ||
console.log('got purses', purses); | ||
useAppStore.setState({ purses }); | ||
} | ||
}; | ||
|
||
const makeOffer = (giveValue: bigint, wantChoices: Record<string, bigint>) => { | ||
const { wallet, offerUpInstance, brands } = useAppStore.getState(); | ||
if (!offerUpInstance) throw Error('no contract instance'); | ||
if (!(brands && brands.IST && brands.Item)) | ||
throw Error('brands not available'); | ||
|
||
const value = makeCopyBag(entries(wantChoices)); | ||
const want = { Items: { brand: brands.Item, value } }; | ||
const give = { Price: { brand: brands.IST, value: giveValue } }; | ||
|
||
wallet?.makeOffer( | ||
{ | ||
source: 'contract', | ||
instance: offerUpInstance, | ||
publicInvitationMaker: 'makeTradeInvitation', | ||
}, | ||
{ give, want }, | ||
undefined, | ||
(update: { status: string; data?: unknown }) => { | ||
if (update.status === 'error') { | ||
alert(`Offer error: ${update.data}`); | ||
} | ||
if (update.status === 'accepted') { | ||
alert('Offer accepted'); | ||
} | ||
if (update.status === 'refunded') { | ||
alert('Offer rejected'); | ||
} | ||
}, | ||
); | ||
}; | ||
|
||
function App() { | ||
useEffect(() => { | ||
setup(); | ||
}, []); | ||
|
||
const { wallet, purses } = useAppStore(({ wallet, purses }) => ({ | ||
wallet, | ||
purses, | ||
})); | ||
const istPurse = purses?.find(p => p.brandPetname === 'IST'); | ||
const itemsPurse = purses?.find(p => p.brandPetname === 'Item'); | ||
|
||
const tryConnectWallet = () => { | ||
connectWallet().catch(err => { | ||
switch (err.message) { | ||
case 'KEPLR_CONNECTION_ERROR_NO_SMART_WALLET': | ||
alert( | ||
'no smart wallet at that address; try: yarn docker:make print-key', | ||
); | ||
break; | ||
default: | ||
alert(err.message); | ||
} | ||
}); | ||
}; | ||
|
||
const App = () => { | ||
return ( | ||
<> | ||
<Logos /> | ||
<h1>Items Listed on Offer Up</h1> | ||
|
||
<div className="card"> | ||
<Trade | ||
makeOffer={makeOffer} | ||
istPurse={istPurse as Purse} | ||
walletConnected={!!wallet} | ||
/> | ||
<hr /> | ||
{wallet && istPurse ? ( | ||
<Inventory | ||
address={wallet.address} | ||
istPurse={istPurse} | ||
itemsPurse={itemsPurse as Purse} | ||
/> | ||
) : ( | ||
<button onClick={tryConnectWallet}>Connect Wallet</button> | ||
)} | ||
</div> | ||
</> | ||
<AgoricProvider | ||
wallets={wallets.extension} | ||
defaultNetworkConfig={{ | ||
testChain: { | ||
chainId: 'agoriclocal', | ||
chainName: 'agoric-local', | ||
}, | ||
apis: { | ||
rest: ['http://localhost:1317'], | ||
rpc: ['http://localhost:26657'], | ||
}, | ||
}} | ||
> | ||
<ContractProvider> | ||
<Logos /> | ||
<h1>Items Listed on Offer Up</h1> | ||
<div className="card"> | ||
<Trade /> | ||
<hr /> | ||
<Inventory /> | ||
</div> | ||
</ContractProvider> | ||
</AgoricProvider> | ||
); | ||
} | ||
}; | ||
|
||
export default App; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,53 @@ | ||
import { ConnectWalletButton, useAgoric } from '@agoric/react-components'; | ||
import { stringifyAmountValue } from '@agoric/ui-components'; | ||
import { usePurse } from '../hooks/usePurse'; | ||
import type { CopyBag } from '../types'; | ||
|
||
type InventoryProps = { | ||
address: string; | ||
istPurse: Purse; | ||
itemsPurse: Purse; | ||
}; | ||
const Inventory = () => { | ||
const istPurse = usePurse('IST'); | ||
const itemsPurse = usePurse('Item'); | ||
const { walletConnection } = useAgoric(); | ||
|
||
const Inventory = ({ address, istPurse, itemsPurse }: InventoryProps) => ( | ||
<div className="card"> | ||
<h3>My Wallet</h3> | ||
<div> | ||
return ( | ||
<div className="card"> | ||
<h3>My Wallet</h3> | ||
<div> | ||
<small> | ||
<code>{address}</code> | ||
</small> | ||
</div> | ||
|
||
<div style={{ textAlign: 'left' }}> | ||
<div> | ||
<b>IST: </b> | ||
{stringifyAmountValue( | ||
istPurse.currentAmount, | ||
istPurse.displayInfo.assetKind, | ||
istPurse.displayInfo.decimalPlaces, | ||
)} | ||
</div> | ||
<div> | ||
<b>Items:</b> | ||
{itemsPurse ? ( | ||
<ul style={{ marginTop: 0, textAlign: 'left' }}> | ||
{(itemsPurse.currentAmount.value as CopyBag).payload.map( | ||
([name, number]) => ( | ||
<li key={name}> | ||
{String(number)} {name} | ||
</li> | ||
), | ||
<ConnectWalletButton /> | ||
{walletConnection && ( | ||
<div style={{ textAlign: 'left' }}> | ||
<div> | ||
<b>IST: </b> | ||
{istPurse ? ( | ||
stringifyAmountValue( | ||
istPurse.currentAmount, | ||
istPurse.displayInfo.assetKind, | ||
istPurse.displayInfo.decimalPlaces, | ||
) | ||
) : ( | ||
<i>Fetching balance...</i> | ||
)} | ||
</ul> | ||
) : ( | ||
'None' | ||
)} | ||
</div> | ||
</div> | ||
<div> | ||
<b>Items: </b> | ||
{itemsPurse ? ( | ||
<ul style={{ marginTop: 0, textAlign: 'left' }}> | ||
{(itemsPurse.currentAmount.value as CopyBag).payload.map( | ||
([name, number]) => ( | ||
<li key={name}> | ||
{String(number)} {name} | ||
</li> | ||
), | ||
)} | ||
</ul> | ||
) : ( | ||
'None' | ||
)} | ||
</div> | ||
</div> | ||
)} | ||
</div> | ||
</div> | ||
</div> | ||
); | ||
); | ||
}; | ||
|
||
export { Inventory }; |
Oops, something went wrong.