Skip to content

Commit 42501e2

Browse files
Update send-anywhere.md (#1261)
* Update send-anywhere.md Various discrepancies have crept in between the docs and the code. This updates to match the code in agoric-sdk. * chore: make lint happy
1 parent 30637d4 commit 42501e2

File tree

1 file changed

+32
-26
lines changed

1 file changed

+32
-26
lines changed

main/guides/orchestration/contract-walkthroughs/send-anywhere.md

Lines changed: 32 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ The high-level flow of the contract is:
1010
- Creates or ensures the existence of a local account (held by the contract) to handle temporary holding.
1111
- Transfers the asset from the source address to the local account.
1212
- Initiates a transfer to the destination address, which could reside on the remote blockchain.
13-
- Gracefully handles the failures.
13+
- Gracefully handles failures.
1414

1515
The contract is implemented in two separate files:
1616

@@ -24,12 +24,12 @@ Let us walk through these files one by one.
2424

2525
The contract begins by importing various modules and utilities necessary for its functionality. These include:
2626

27-
- **State management**: `makeSharedStateRecord` is imported to create and manage the state across contract incarnations.
28-
- **Type validation**: `AmountShape` and `InvitationShape` ensure that the contract works with correct data types, such as amounts and invitations.
27+
- **State management**: `makeSharedStateRecord` is imported to create and manage state across contract incarnations.
28+
- **Type validation**: `AnyNatAmountShape` and `InvitationShape` ensure that the contract works with correct data types, such as amounts and invitations.
2929
- **Orchestration utilities**: `withOrchestration` is imported to facilitate interactions with Orchestration functions.
3030
- **Flows**: The Orchestration flows for handling transfers are imported from `send-anywhere.flows.js` to be made available to Zoe.
3131

32-
These imports set up the contract for the validation, orchestration, and execution of transfers through Zoe API.
32+
These imports set up the contract for the validation, orchestration, and execution of transfers through the Zoe API.
3333

3434
### Single Amount Record Validation
3535

@@ -52,16 +52,14 @@ This validation ensures that the proposal shape submitted by users contains exac
5252
The contract defines a shared state record as below:
5353

5454
```js
55-
const contractState = makeSharedStateRecord(
56-
/** @type {{ account: OrchestrationAccount<any> | undefined }} */ {
57-
localAccount: undefined
58-
}
55+
const sharedLocalAccountP = zone.makeOnce('localAccount', () =>
56+
makeLocalAccount()
5957
);
6058
```
6159

62-
This state keeps track of the local account that will hold the transferred assets temporarily before they are sent to the destination
63-
address. The state starts with an undefined `localAccount`. This account will be created later during the offer handling process if
64-
needed.
60+
`sharedLocalAccountP` stores the local account that will hold the transferred assets temporarily before they
61+
are sent to the destination address. Since this is initialized with a promise (`makeOnce` returns a promise, so
62+
we add a `P` to the variable name), uses of `sharedLocalAccountP` will have to await it before making use of it.
6563

6664
### Logging setup
6765

@@ -81,8 +79,8 @@ these functions with the necessary context (such as the contract state, logging,
8179

8280
```js
8381
const orchFns = orchestrateAll(flows, {
84-
contractState,
8582
log,
83+
sharedLocalAccountP,
8684
zoeTools
8785
});
8886
```
@@ -111,7 +109,7 @@ const publicFacet = zone.exo(
111109
```
112110

113111
The `makeSendInvitation` method creates an invitation for users, allowing them to initiate a transfer by submitting a proposal. The
114-
proposal must match the structure defined by the `SingleNatAmountRecord`, ensuring that only one asset is transferred per transaction.
112+
proposal must match the structure defined by `SingleNatAmountRecord`, ensuring that only one asset is transferred per transaction.
115113
The invitation is connected to the `sendIt` function (explained later), which performs the asset transfer.
116114

117115
## 2. `send-anywhere.flows.js`
@@ -122,22 +120,26 @@ remote chains. The parameters passed to this function include:
122120

123121
- `orch`: The orchestrator object for managing chain/account interactions.
124122
- `ctx`: The contract state and utility functions, including:
125-
- `contractState`: Holds the local account for intermediate storage.
126-
- `localTransfer`: The transfer function for moving assets between local accounts.
123+
- `sharedLocalAccountP`: Holds the local account for intermediate storage.
124+
- `log`: access to logging
125+
- zoeTools: Helper functions for transferring assets. Contains
126+
- `localTransfer`: Support for moving assets between local accounts.
127+
- `withdrawToSeat`: Support for moving assets from a local account to an account on a remote chain.
127128
- `seat`: The Zoe seat representing the assets to be transferred.
128129
- `offerArgs`: Includes details about the destination chain and address.
129130

130-
The `sendIt` function performs following important steps:
131+
The `sendIt` function performs the following important steps:
131132

132133
### Offer Validation and Setup
133134

134135
Upon receiving an offer, the `sendIt` function:
135136

136137
- Validates the offer arguments using [endo's pattern-matching library](https://github.com/endojs/endo/tree/master/packages/patterns) to ensure the correct structure is submitted.
137-
- Retrieves the `proposal` from the seat, extracting the asset (`brand` and `amount`) being transferred.
138-
- The contract ensures that the asset brand is registered on the local chain by querying the chain’s asset registry (`vbank`). If not
139-
the contract throws an error and exits the transaction.
140-
- If a local account for the contract doesn’t already exist, the function creates one.
138+
- Retrieves the `proposal` from the seat, extracting (from `give`) the asset (`brand` and `amount`) being transferred.
139+
- The contract ensures that the asset brand is registered on the local chain by querying the chain’s asset registry
140+
(`vbank`). If not the contract throws an error and exits the transaction. It also gets the `denom` for the
141+
destination chain from the `vbank`.
142+
- retrieves the `chainId` for the remote chain.
141143

142144
```js
143145
mustMatch(offerArgs, harden({ chainName: M.scalar(), destAddr: M.string() }));
@@ -149,17 +151,20 @@ void log(`sending {${amt.value}} from ${chainName} to ${destAddr}`);
149151
const agoric = await orch.getChain('agoric');
150152
const assets = await agoric.getVBankAssetInfo();
151153
void log(`got info for denoms: ${assets.map(a => a.denom).join(', ')}`);
154+
152155
const { denom } = NonNullish(
153156
assets.find(a => a.brand === amt.brand),
154157
`${amt.brand} not registered in vbank`
155158
);
156-
157-
if (!contractState.localAccount) {
158-
contractState.localAccount = await agoric.makeAccount();
159-
}
159+
const chain = await orch.getChain(chainName);
160+
const info = await chain.getChainInfo();
161+
const { chainId } = info;
162+
assert(typeof chainId === 'string', 'bad chainId');
163+
void log(`got info for chain: ${chainName} ${chainId}`);
160164
```
161165

162-
This setup phase ensures the transaction is valid and the contract is prepared to handle it.
166+
This setup phase ensures the transaction is valid and the contract is prepared to handle it. Notice that all checks
167+
are complete before any transfers are started.
163168

164169
### Assets Transfer
165170

@@ -168,7 +173,8 @@ Once everything is validated, the contract performs the following steps:
168173
- **Local transfer**: The assets are first transferred from the Zoe seat to the contract’s local account using `localTransfer` API.
169174

170175
```js
171-
await localTransfer(seat, contractState.localAccount, give);
176+
const sharedLocalAccount = await sharedLocalAccountP;
177+
await localTransfer(seat, sharedLocalAccount, give);
172178
```
173179

174180
- **Remote transfer**: The contract initiates the transfer to the destination address on the remote chain. This transfer includes

0 commit comments

Comments
 (0)