Skip to content

Commit 9d271b5

Browse files
committed
pool+accounts: add renewAccount action to accountStore
1 parent 628d9f4 commit 9d271b5

File tree

4 files changed

+71
-0
lines changed

4 files changed

+71
-0
lines changed

app/src/__tests__/store/accountStore.spec.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,25 @@ describe('AccountStore', () => {
194194
});
195195
});
196196

197+
it('should renew an Account', async () => {
198+
await store.fetchAccounts();
199+
const txid = await store.renewAccount(100, 253);
200+
expect(txid).toEqual(poolCloseAccount.closeTxid);
201+
});
202+
203+
it('should handle errors renewing an Account', async () => {
204+
await store.fetchAccounts();
205+
grpcMock.unary.mockImplementationOnce(() => {
206+
throw new Error('test-err');
207+
});
208+
expect(rootStore.appView.alerts.size).toBe(0);
209+
await store.renewAccount(100, 253);
210+
await waitFor(() => {
211+
expect(rootStore.appView.alerts.size).toBe(1);
212+
expect(values(rootStore.appView.alerts)[0].message).toBe('test-err');
213+
});
214+
});
215+
197216
it('should deposit funds into an account', async () => {
198217
await store.fetchAccounts();
199218
const txid = await store.deposit(1);

app/src/api/pool.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,23 @@ class PoolApi extends BaseApi<PoolEvents> {
7070
return res.toObject();
7171
}
7272

73+
/**
74+
* call the pool `RenewAccount` RPC and return the response
75+
*/
76+
async renewAccount(
77+
traderKey: string,
78+
expiryBlocks: number,
79+
feeRateSatPerKw: number,
80+
): Promise<POOL.RenewAccountResponse.AsObject> {
81+
const req = new POOL.RenewAccountRequest();
82+
req.setAccountKey(b64(traderKey));
83+
req.setRelativeExpiry(expiryBlocks);
84+
req.setFeeRateSatPerKw(feeRateSatPerKw);
85+
86+
const res = await this._grpc.request(Trader.RenewAccount, req, this._meta);
87+
return res.toObject();
88+
}
89+
7390
/**
7491
* call the pool `CloseAccount` RPC and return the response
7592
*/

app/src/store/stores/accountStore.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,35 @@ export default class AccountStore {
129129
}
130130
}
131131

132+
/**
133+
* Renews an account via the pool API
134+
*/
135+
async renewAccount(expiryBlocks: number, feeRate: number) {
136+
try {
137+
const acct = this.activeAccount;
138+
this._store.log.info(
139+
`renewing account ${acct.traderKey} to expire in ${expiryBlocks} blocks`,
140+
);
141+
142+
const res = await this._store.api.pool.renewAccount(
143+
acct.traderKey,
144+
expiryBlocks,
145+
feeRate,
146+
);
147+
runInAction(() => {
148+
// the account should always be defined but if not, fetch all accounts as a fallback
149+
if (res.account) {
150+
acct.update(res.account);
151+
} else {
152+
this.fetchAccounts();
153+
}
154+
});
155+
return res.renewalTxid;
156+
} catch (error) {
157+
this._store.appView.handleError(error, 'Unable to renew the account');
158+
}
159+
}
160+
132161
/**
133162
* queries the pool api to fetch the list of accounts and stores them
134163
* in the state

app/src/util/tests/sampleData.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,11 @@ export const poolCloseAccount: POOL.CloseAccountResponse.AsObject = {
377377
closeTxid: '+BQm/hnM0SleT2NxS7bdw0JNDuvIMhL4qxLUkdbCJdo=',
378378
};
379379

380+
export const poolRenewAccount: POOL.RenewAccountResponse.AsObject = {
381+
renewalTxid: '+BQm/hnM0SleT2NxS7bdw0JNDuvIMhL4qxLUkdbCJdo=',
382+
account: poolInitAccount,
383+
};
384+
380385
export const poolListAccounts: POOL.ListAccountsResponse.AsObject = {
381386
accountsList: [
382387
poolInitAccount,
@@ -759,6 +764,7 @@ export const sampleApiResponses: Record<string, any> = {
759764
'poolrpc.Trader.QuoteAccount': poolQuoteAccount,
760765
'poolrpc.Trader.InitAccount': poolInitAccount,
761766
'poolrpc.Trader.CloseAccount': poolCloseAccount,
767+
'poolrpc.Trader.RenewAccount': poolRenewAccount,
762768
'poolrpc.Trader.DepositAccount': poolDepositAccount,
763769
'poolrpc.Trader.WithdrawAccount': poolWithdrawAccount,
764770
'poolrpc.Trader.ListOrders': poolListOrders,

0 commit comments

Comments
 (0)