Skip to content

Commit 530289b

Browse files
committed
feat(portfolio-contract): creatorFacet.withdrawFees
1 parent 357406d commit 530289b

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 = {
@@ -408,6 +411,9 @@ export const contract = async (
408411
M.string(),
409412
M.remotable('Instance'),
410413
).returns(),
414+
withdrawFees: M.callWhen(M.string())
415+
.optional(M.record())
416+
.returns(M.record()),
411417
}),
412418
{
413419
makeResolverInvitation() {
@@ -448,6 +454,24 @@ export const contract = async (
448454
await E(pfP).deliverPayment(address, invitation);
449455
trace('delivered planner invitation');
450456
},
457+
/**
458+
* Withdraw from contractAccount; for example, before terminating the contract
459+
*
460+
* @param toAccount
461+
* @param optAmount - defaults to BLD balance
462+
*/
463+
async withdrawFees(toAccount: AccountId, optAmount?: DenomAmount) {
464+
const traceWithdraw = trace.sub('withdrawFees');
465+
traceWithdraw('to', toAccount);
466+
// LCA operations are prompt
467+
const { when } = vowTools;
468+
const acct = await when(contractAccountV);
469+
const amount = await (optAmount || when(acct.getBalance('ubld')));
470+
traceWithdraw('amount', amount);
471+
await when(acct.send(toAccount, amount));
472+
traceWithdraw({ amount, from: acct.getAddress().value, toAccount });
473+
return amount;
474+
},
451475
},
452476
);
453477

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

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1084,3 +1084,66 @@ test('withdraw using planner', async t => {
10841084
]);
10851085
t.is(833332500n, (3333330000n * 25n) / 100n);
10861086
});
1087+
1088+
test('creatorFacet.withdrawFees', async t => {
1089+
const { started, common } = await setupTrader(t);
1090+
const { storage } = common.bootstrap;
1091+
const { inspectLocalBridge } = common.utils;
1092+
1093+
const { contractAccount } = storage
1094+
.getDeserialized(`${ROOT_STORAGE_PATH}`)
1095+
.at(-1) as unknown as { contractAccount: string };
1096+
t.log('contractAddress for fees', contractAccount);
1097+
1098+
const { bld } = common.brands;
1099+
const bld2k = bld.units(2_000);
1100+
{
1101+
const pmt = await common.utils.pourPayment(bld2k);
1102+
const { bankManager } = common.bootstrap;
1103+
const bank = E(bankManager).getBankForAddress(contractAccount);
1104+
const purse = E(bank).getPurse(bld2k.brand);
1105+
await E(purse).deposit(pmt);
1106+
t.log('deposited', bld2k, 'for fees');
1107+
}
1108+
1109+
const { creatorFacet } = started;
1110+
1111+
const dest = `cosmos:agoric-3:${makeTestAddress(5)}` as const;
1112+
1113+
{
1114+
const actual = await E(creatorFacet).withdrawFees(dest, {
1115+
denom: 'ubld',
1116+
value: 100n,
1117+
});
1118+
t.log('withdrew some', actual);
1119+
1120+
t.deepEqual(actual, { denom: 'ubld', value: 100n });
1121+
1122+
const [tx] = inspectLocalBridge().filter(
1123+
obj => obj.type === 'VLOCALCHAIN_EXECUTE_TX',
1124+
);
1125+
t.like(tx.messages[0], {
1126+
'@type': '/cosmos.bank.v1beta1.MsgSend',
1127+
fromAddress: 'agoric1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqp7zqht',
1128+
toAddress: 'agoric1q5qqqqqqqqqqqqqqqqqqqqqqqqqqqqqq8ee25y',
1129+
amount: [{ denom: 'ubld', amount: '100' }],
1130+
});
1131+
}
1132+
1133+
{
1134+
const amt = await E(creatorFacet).withdrawFees(dest);
1135+
1136+
t.log('withdrew all', amt);
1137+
t.deepEqual(amt, { denom: 'ubld', value: bld2k.value });
1138+
1139+
const [_, tx] = inspectLocalBridge().filter(
1140+
obj => obj.type === 'VLOCALCHAIN_EXECUTE_TX',
1141+
);
1142+
t.like(tx.messages[0], {
1143+
'@type': '/cosmos.bank.v1beta1.MsgSend',
1144+
fromAddress: 'agoric1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqp7zqht',
1145+
toAddress: 'agoric1q5qqqqqqqqqqqqqqqqqqqqqqqqqqqqqq8ee25y',
1146+
amount: [{ denom: 'ubld', amount: '2000000000' }],
1147+
});
1148+
}
1149+
});

0 commit comments

Comments
 (0)