Skip to content

Commit 18c7332

Browse files
committed
feat(portfolio-contract): creatorFacet.withdrawFees
1 parent 5af168a commit 18c7332

File tree

2 files changed

+87
-0
lines changed

2 files changed

+87
-0
lines changed

packages/portfolio-contract/src/portfolio.contract.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,11 @@ import {
2121
OrchestrationPowersShape,
2222
registerChainsAndAssets,
2323
withOrchestration,
24+
type AccountId,
2425
type Bech32Address,
2526
type ChainInfo,
2627
type Denom,
28+
type DenomAmount,
2729
type DenomDetail,
2830
type OrchestrationPowers,
2931
type OrchestrationTools,
@@ -265,6 +267,7 @@ export const contract = async (
265267
void vowTools.when(contractAccountV, acct => {
266268
const addr = acct.getAddress();
267269
publishStatus(storageNode, harden({ contractAccount: addr.value }));
270+
trace('published contractAccount', addr.value);
268271
});
269272

270273
const ctx1: flows.PortfolioInstanceContext = {
@@ -406,6 +409,9 @@ export const contract = async (
406409
M.string(),
407410
M.remotable('Instance'),
408411
).returns(),
412+
withdrawFees: M.callWhen(M.string())
413+
.optional(M.record())
414+
.returns(M.record()),
409415
}),
410416
{
411417
makeResolverInvitation() {
@@ -446,6 +452,24 @@ export const contract = async (
446452
await E(pfP).deliverPayment(address, invitation);
447453
trace('delivered planner invitation');
448454
},
455+
/**
456+
* Withdraw from contractAccount; for example, before terminating the contract
457+
*
458+
* @param toAccount
459+
* @param optAmount - defaults to BLD balance
460+
*/
461+
async withdrawFees(toAccount: AccountId, optAmount?: DenomAmount) {
462+
const traceWithdraw = trace.sub('withdrawFees');
463+
traceWithdraw('to', toAccount);
464+
// LCA operations are prompt
465+
const { when } = vowTools;
466+
const acct = await when(contractAccountV);
467+
const amount = await (optAmount || when(acct.getBalance('ubld')));
468+
traceWithdraw('amount', amount);
469+
await when(acct.send(toAccount, amount));
470+
traceWithdraw({ amount, from: acct.getAddress().value, toAccount });
471+
return amount;
472+
},
449473
},
450474
);
451475

packages/portfolio-contract/test/portfolio.contract.test.ts

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1045,3 +1045,66 @@ test('request rebalance - send same targetAllocation', async t => {
10451045
rebalanceCount: 1,
10461046
});
10471047
});
1048+
1049+
test('creatorFacet.withdrawFees', async t => {
1050+
const { started, common } = await setupTrader(t);
1051+
const { storage } = common.bootstrap;
1052+
const { inspectLocalBridge } = common.utils;
1053+
1054+
const { contractAccount } = storage
1055+
.getDeserialized(`${ROOT_STORAGE_PATH}`)
1056+
.at(-1) as unknown as { contractAccount: string };
1057+
t.log('contractAddress for fees', contractAccount);
1058+
1059+
const { bld } = common.brands;
1060+
const bld2k = bld.units(2_000);
1061+
{
1062+
const pmt = await common.utils.pourPayment(bld2k);
1063+
const { bankManager } = common.bootstrap;
1064+
const bank = E(bankManager).getBankForAddress(contractAccount);
1065+
const purse = E(bank).getPurse(bld2k.brand);
1066+
await E(purse).deposit(pmt);
1067+
t.log('deposited', bld2k, 'for fees');
1068+
}
1069+
1070+
const { creatorFacet } = started;
1071+
1072+
const dest = `cosmos:agoric-3:${makeTestAddress(5)}` as const;
1073+
1074+
{
1075+
const actual = await E(creatorFacet).withdrawFees(dest, {
1076+
denom: 'ubld',
1077+
value: 100n,
1078+
});
1079+
t.log('withdrew some', actual);
1080+
1081+
t.deepEqual(actual, { denom: 'ubld', value: 100n });
1082+
1083+
const [tx] = inspectLocalBridge().filter(
1084+
obj => obj.type === 'VLOCALCHAIN_EXECUTE_TX',
1085+
);
1086+
t.like(tx.messages[0], {
1087+
'@type': '/cosmos.bank.v1beta1.MsgSend',
1088+
fromAddress: 'agoric1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqp7zqht',
1089+
toAddress: 'agoric1q5qqqqqqqqqqqqqqqqqqqqqqqqqqqqqq8ee25y',
1090+
amount: [{ denom: 'ubld', amount: '100' }],
1091+
});
1092+
}
1093+
1094+
{
1095+
const amt = await E(creatorFacet).withdrawFees(dest);
1096+
1097+
t.log('withdrew all', amt);
1098+
t.deepEqual(amt, { denom: 'ubld', value: bld2k.value });
1099+
1100+
const [_, tx] = inspectLocalBridge().filter(
1101+
obj => obj.type === 'VLOCALCHAIN_EXECUTE_TX',
1102+
);
1103+
t.like(tx.messages[0], {
1104+
'@type': '/cosmos.bank.v1beta1.MsgSend',
1105+
fromAddress: 'agoric1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqp7zqht',
1106+
toAddress: 'agoric1q5qqqqqqqqqqqqqqqqqqqqqqqqqqqqqq8ee25y',
1107+
amount: [{ denom: 'ubld', amount: '2000000000' }],
1108+
});
1109+
}
1110+
});

0 commit comments

Comments
 (0)