Skip to content

Commit

Permalink
feat(sendAnywhere): handle failed IBC transfer
Browse files Browse the repository at this point in the history
  • Loading branch information
0xpatrickdev committed Aug 27, 2024
1 parent 1339584 commit 97c708a
Show file tree
Hide file tree
Showing 5 changed files with 186 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ const contract = async (
const orchFns = orchestrateAll(flows, {
zcf,
contractState,
localTransfer: zoeTools.localTransfer,
zoeTools,
});

const publicFacet = zone.exo(
Expand Down
30 changes: 19 additions & 11 deletions packages/orchestration/src/examples/sendAnywhere.flows.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { NonNullish } from '@agoric/internal';
import { Fail, q } from '@endo/errors';
import { M, mustMatch } from '@endo/patterns';

/**
* @import {GuestOf} from '@agoric/async-flow';
* @import {GuestInterface} from '@agoric/async-flow';
* @import {ZoeTools} from '../utils/zoe-tools.js';
* @import {Orchestrator, LocalAccountMethods, OrchestrationAccountI, OrchestrationFlow} from '../types.js';
*/
Expand All @@ -17,13 +18,13 @@ const { entries } = Object;
* @param {Orchestrator} orch
* @param {object} ctx
* @param {{ localAccount?: OrchestrationAccountI & LocalAccountMethods }} ctx.contractState
* @param {GuestOf<ZoeTools['localTransfer']>} ctx.localTransfer
* @param {GuestInterface<ZoeTools>} ctx.zoeTools
* @param {ZCFSeat} seat
* @param {{ chainName: string; destAddr: string }} offerArgs
*/
export const sendIt = async (
orch,
{ contractState, localTransfer },
{ contractState, zoeTools: { localTransfer, withdrawToSeat } },
seat,
offerArgs,
) => {
Expand Down Expand Up @@ -51,14 +52,21 @@ export const sendIt = async (

await localTransfer(seat, contractState.localAccount, give);

await contractState.localAccount.transfer(
{ denom, value: amt.value },
{
value: destAddr,
encoding: 'bech32',
chainId,
},
);
try {
await contractState.localAccount.transfer(
{ denom, value: amt.value },
{
value: destAddr,
encoding: 'bech32',
chainId,
},
);
} catch (e) {
await withdrawToSeat(contractState.localAccount, seat, give);
seat.exit();
throw Fail`IBC Transfer failed ${q(e)}`;
}

seat.exit();
};
harden(sendIt);
167 changes: 160 additions & 7 deletions packages/orchestration/test/examples/sendAnywhere.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,13 @@ import { setUpZoeForTest } from '@agoric/zoe/tools/setup-zoe.js';
import { E } from '@endo/far';
import path from 'path';
import { mustMatch } from '@endo/patterns';
import { makeIssuerKit } from '@agoric/ertp';
import {
eventLoopIteration,
inspectMapStore,
} from '@agoric/internal/src/testing-utils.js';
import { inspect } from 'util';
import { AmountMath, makeIssuerKit } from '@agoric/ertp';
import { eventLoopIteration, inspectMapStore } from '@agoric/internal/src/testing-utils.js';

Check warning on line 8 in packages/orchestration/test/examples/sendAnywhere.test.ts

View workflow job for this annotation

GitHub Actions / lint-rest

Replace `·eventLoopIteration,·inspectMapStore·` with `⏎··eventLoopIteration,⏎··inspectMapStore,⏎`
import { SIMULATED_ERRORS } from '@agoric/vats/tools/fake-bridge.js';
import { CosmosChainInfo, IBCConnectionInfo } from '../../src/cosmos-api.js';
import { commonSetup } from '../supports.js';
import { SingleAmountRecord } from '../../src/examples/sendAnywhere.contract.js';
import { registerChain } from '../../src/chain-info.js';
import { buildVTransferEvent } from '../../tools/ibc-mocks.js';

const dirname = path.dirname(new URL(import.meta.url).pathname);

Expand Down Expand Up @@ -253,3 +249,160 @@ test('baggage', async t => {
const tree = inspectMapStore(contractBaggage);
t.snapshot(tree, 'contract baggage after start');
});

test('failed ibc transfer returns give', async t => {
t.log('bootstrap, orchestration core-eval');
const {
bootstrap,
commonPrivateArgs,
brands: { ist },
utils: { inspectLocalBridge, pourPayment, inspectBankBridge },
} = await commonSetup(t);
const vt = bootstrap.vowTools;

const { zoe, bundleAndInstall } = await setUpZoeForTest();

t.log('contract coreEval', contractName);

const installation: Installation<StartFn> =
await bundleAndInstall(contractFile);

const storageNode = await E(bootstrap.storage.rootNode).makeChildNode(
contractName,
);
const sendKit = await E(zoe).startInstance(
installation,
{ Stable: ist.issuer },
{},
{ ...commonPrivateArgs, storageNode },
);

t.log('client sends an ibc transfer we expect will timeout');

const publicFacet = await E(zoe).getPublicFacet(sendKit.instance);
const inv = E(publicFacet).makeSendInvitation();
const amt = await E(zoe).getInvitationDetails(inv);
t.is(amt.description, 'send');

const anAmt = ist.make(SIMULATED_ERRORS.TIMEOUT);
const Send = await pourPayment(anAmt);
const userSeat = await E(zoe).offer(
inv,
{ give: { Send: anAmt } },
{ Send },
{ destAddr: 'cosmos1destAddr', chainName: 'cosmoshub' },
);

await eventLoopIteration();
await vt.when(E(userSeat).tryExit());
const payouts = await vt.when(E(userSeat).getPayouts());
t.log('Failed offer payouts', payouts);
const amountReturned = await ist.issuer.getAmountOf(payouts.Send);
t.log('Failed offer Send amount', amountReturned);
t.true(AmountMath.isEqual(anAmt, amountReturned), 'give is returned');

await t.throwsAsync(vt.when(E(userSeat).getOfferResult()), {
message:
'IBC Transfer failed "[Error: simulated unexpected MsgTransfer packet timeout]"',
});

t.log('ibc MsgTransfer was attempted from a local chain account');
const history = inspectLocalBridge();
t.like(history, [
{ type: 'VLOCALCHAIN_ALLOCATE_ADDRESS' },
{ type: 'VLOCALCHAIN_EXECUTE_TX' },
]);
const [_alloc, { messages, address: execAddr }] = history;
t.is(messages.length, 1);
const [txfr] = messages;
t.log('local bridge', txfr);
t.like(txfr, {
'@type': '/ibc.applications.transfer.v1.MsgTransfer',
sender: execAddr,
sourcePort: 'transfer',
token: { amount: '504', denom: 'uist' },
});

t.log('deposit to and withdrawal from LCA is observed in bank bridge');
const bankHistory = inspectBankBridge();
t.log('bank bridge', bankHistory);
t.deepEqual(
bankHistory[bankHistory.length - 2],
{
type: 'VBANK_GIVE',
recipient: 'agoric1fakeLCAAddress',
denom: 'uist',
amount: '504',
},
'funds sent to LCA',
);
t.deepEqual(
bankHistory[bankHistory.length - 1],
{
type: 'VBANK_GRAB',
sender: 'agoric1fakeLCAAddress',
denom: 'uist',
amount: '504',
},
'funds withdrawn from LCA in catch block',
);
});

test('non-vbank asset is returned (deposit fails)', async t => {
t.log('bootstrap, orchestration core-eval');
const {
bootstrap,
commonPrivateArgs,
brands: { ist },
utils: { inspectLocalBridge, pourPayment, inspectBankBridge },
} = await commonSetup(t);
const vt = bootstrap.vowTools;

const { zoe, bundleAndInstall } = await setUpZoeForTest();

t.log('contract coreEval', contractName);

const installation: Installation<StartFn> =
await bundleAndInstall(contractFile);

const storageNode = await E(bootstrap.storage.rootNode).makeChildNode(
contractName,
);
const sendKit = await E(zoe).startInstance(
installation,
{ Stable: ist.issuer },
{},
{ ...commonPrivateArgs, storageNode },
);

t.log('client sends an ibc transfer we expect will timeout');

const publicFacet = await E(zoe).getPublicFacet(sendKit.instance);
const inv = E(publicFacet).makeSendInvitation();
const amt = await E(zoe).getInvitationDetails(inv);
t.is(amt.description, 'send');

const anAmt = ist.make(SIMULATED_ERRORS.TIMEOUT);
const Send = await pourPayment(anAmt);
const userSeat = await E(zoe).offer(
inv,
{ give: { Send: anAmt } },
{ Send },
{ destAddr: 'cosmos1destAddr', chainName: 'cosmoshub' },
);

const payouts = await vt.when(E(userSeat).getPayouts());
t.log('Failed offer payouts', payouts);
const amountReturned = await ist.issuer.getAmountOf(payouts.Send);
t.log('Failed offer Send amount', amountReturned);
t.true(AmountMath.isEqual(anAmt, amountReturned), 'give is returned');

await t.throwsAsync(vt.when(E(userSeat).getOfferResult()), {
message:
'IBC Transfer failed "[Error: simulated unexpected MsgTransfer packet timeout]"',
});
});
test.todo(
'non-vbank asset and vbank asset presented in same offer (partial failure)',
);
test.todo('multiple vbank assets are presented');
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,12 @@ Generated by [AVA](https://avajs.dev).
0: {
contractState_kindHandle: 'Alleged: kind',
contractState_singleton: 'Alleged: contractState',
localTransfer_kindHandle: 'Alleged: kind',
localTransfer_singleton: 'Alleged: localTransfer',
zoeTools: {
localTransfer_kindHandle: 'Alleged: kind',
localTransfer_singleton: 'Alleged: localTransfer',
withdrawToSeat_kindHandle: 'Alleged: kind',
withdrawToSeat_singleton: 'Alleged: withdrawToSeat',
},
},
},
},
Expand Down
Binary file not shown.

0 comments on commit 97c708a

Please sign in to comment.