Skip to content

Commit

Permalink
fixup! feat: StatusManager scaffold
Browse files Browse the repository at this point in the history
  • Loading branch information
0xpatrickdev committed Nov 6, 2024
1 parent a9216ce commit 4cb1978
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 17 deletions.
3 changes: 2 additions & 1 deletion packages/fast-usdc/src/exos/settler.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ export const prepareSettler = (zone, { statusManager }) => {
}

const hasPendingSettlement = statusManager.hasPendingSettlement(
tx.sender,
// given the sourceChannel check, we can be certain of this cast
/** @type {NobleAddress} */ (tx.sender),
BigInt(tx.amount),
);
if (!hasPendingSettlement) {
Expand Down
25 changes: 16 additions & 9 deletions packages/fast-usdc/src/exos/status-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,20 @@ import { TxStatus } from '../constants.js';
* @import {CctpTxEvidence, NobleAddress, StatusManagerKey, StatusManagerValue} from '../types.js';
*/

/**
* @param {NobleAddress} addr
* @param {bigint} amount
*/
export const toStatusKey = (addr, amount) =>
/** @type {StatusManagerKey} */ (JSON.stringify([addr, String(amount)]));

/**
* @param {CctpTxEvidence} evidence
* @returns {StatusManagerKey}
*/
export const getStatusKey = evidence => {
const { amount, forwardingAddress } = evidence.tx;
return `${forwardingAddress}-${amount}`;
return toStatusKey(forwardingAddress, amount);
};

/**
Expand Down Expand Up @@ -85,12 +92,12 @@ export const prepareStatusManager = zone => {
/**
* Mark an `OBSERVED` transaction as `ADVANCED`.
*
* @param {string} address
* @param {NobleAddress} address
* @param {bigint} amount
* @param {number} index
*/
advance(address, amount, index) {
const key = /** @type {StatusManagerKey} */ (`${address}-${amount}`);
const key = toStatusKey(address, amount);
const mutableEntries = [...txs.get(key)];
if (!mutableEntries[index]) {
throw makeError(`Cannot find ${q(key)} entry at index ${q(index)}`);
Expand All @@ -109,12 +116,12 @@ export const prepareStatusManager = zone => {
/**
* Mark an `OBSERVED` transaction as `SKIPPED`.
*
* @param {string} address
* @param {NobleAddress} address
* @param {bigint} amount
* @param {number} index
*/
skip(address, amount, index) {
const key = /** @type {StatusManagerKey} */ (`${address}-${amount}`);
const key = toStatusKey(address, amount);
const mutableEntries = [...txs.get(key)];
if (!mutableEntries[index]) {
throw makeError(`Cannot find ${q(key)} entry at index ${q(index)}`);
Expand All @@ -134,12 +141,12 @@ export const prepareStatusManager = zone => {
/**
* Find an `ADVANCED` or `SKIPPED` tx waiting to be `SETTLED`
*
* @param {string} address
* @param {NobleAddress} address
* @param {bigint} amount
* @returns {boolean}
*/
hasPendingSettlement(address, amount) {
const key = /** @type {StatusManagerKey} */ (`${address}-${amount}`);
const key = toStatusKey(address, amount);
const entries = txs.get(key);
const unsettledIdx = getPendingIdx(entries);
return unsettledIdx > -1;
Expand All @@ -153,7 +160,7 @@ export const prepareStatusManager = zone => {
* @param {bigint} amount
*/
settle(address, amount) {
const key = /** @type {StatusManagerKey} */ (`${address}-${amount}`);
const key = toStatusKey(address, amount);
const mutableEntries = [...txs.get(key)];
const unsettledIdx = getPendingIdx(mutableEntries);
if (unsettledIdx === -1) {
Expand Down Expand Up @@ -187,7 +194,7 @@ export const prepareStatusManager = zone => {
* @param {number} [index] - optional index to return single entry
*/
view(address, amount, index) {
const key = /** @type {StatusManagerKey} */ (`${address}-${amount}`);
const key = toStatusKey(address, amount);
const entries = txs.get(key);
return index === undefined ? entries : entries[index];
},
Expand Down
2 changes: 1 addition & 1 deletion packages/fast-usdc/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,6 @@ export interface StatusManagerValue extends CctpTxEvidence {
status: TxStatus;
}

export type StatusManagerKey = `${NobleAddress}-${bigint}`;
export type StatusManagerKey = `"["${NobleAddress}",${string}]"`;

export type * from './constants.js';
12 changes: 6 additions & 6 deletions packages/fast-usdc/test/exos/status-manager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ test('cannot ADVANCE without OBSERVE first', t => {
),
{
message:
'key "noble1x0ydg69dh6fqvr27xjvp6maqmrldam6yfelqkd-150000000" not found in collection "txs"',
'key "[[\\"noble1x0ydg69dh6fqvr27xjvp6maqmrldam6yfelqkd\\",\\"150000000\\"]]" not found in collection "txs"',
},
);
});
Expand All @@ -155,7 +155,7 @@ test('cannot SETTLE without ADVANCE first', t => {
statusManager.settle(evidence.tx.forwardingAddress, evidence.tx.amount),
{
message:
'No unsettled entry for "noble1x0ydg69dh6fqvr27xjvp6maqmrldam6yfelqkd-150000000"',
'No unsettled entry for "[[\\"noble1x0ydg69dh6fqvr27xjvp6maqmrldam6yfelqkd\\",\\"150000000\\"]]"',
},
);
});
Expand All @@ -181,7 +181,7 @@ test('cannot ADVANCE an entry that is already SETTLED or ADVANCED', t => {
),
{
message:
'Cannot advance "noble1x0ydg69dh6fqvr27xjvp6maqmrldam6yfelqkd-150000000" entry 0 with status: "ADVANCED"',
'Cannot advance "[[\\"noble1x0ydg69dh6fqvr27xjvp6maqmrldam6yfelqkd\\",\\"150000000\\"]]" entry 0 with status: "ADVANCED"',
},
);

Expand All @@ -196,7 +196,7 @@ test('cannot ADVANCE an entry that is already SETTLED or ADVANCED', t => {
),
{
message:
'Cannot find "noble1x0ydg69dh6fqvr27xjvp6maqmrldam6yfelqkd-150000000" entry at index 0',
'Cannot find "[[\\"noble1x0ydg69dh6fqvr27xjvp6maqmrldam6yfelqkd\\",\\"150000000\\"]]" entry at index 0',
},
);
});
Expand All @@ -222,7 +222,7 @@ test('cannot SKIP an entry that is already SETTLED or ADVANCED', t => {
),
{
message:
'Cannot skip "noble1x0ydg69dh6fqvr27xjvp6maqmrldam6yfelqkd-150000000" entry 0 with status: "ADVANCED"',
'Cannot skip "[[\\"noble1x0ydg69dh6fqvr27xjvp6maqmrldam6yfelqkd\\",\\"150000000\\"]]" entry 0 with status: "ADVANCED"',
},
);

Expand All @@ -237,7 +237,7 @@ test('cannot SKIP an entry that is already SETTLED or ADVANCED', t => {
),
{
message:
'Cannot find "noble1x0ydg69dh6fqvr27xjvp6maqmrldam6yfelqkd-150000000" entry at index 0',
'Cannot find "[[\\"noble1x0ydg69dh6fqvr27xjvp6maqmrldam6yfelqkd\\",\\"150000000\\"]]" entry at index 0',
},
);
});
Expand Down

0 comments on commit 4cb1978

Please sign in to comment.