Skip to content

Commit

Permalink
feat: add support for cancelling intents (#547)
Browse files Browse the repository at this point in the history
* Add cancel tx intent endpoint

* Add proxy cancel tx functions

* rename to generic transaction
  • Loading branch information
shahnami authored Sep 5, 2024
1 parent 55ff4bd commit 7c7431b
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 1 deletion.
7 changes: 7 additions & 0 deletions examples/relayer-signer-actions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ async function replace(id) {
console.log('txResponse', JSON.stringify(txResponse, null, 2));
}

async function cancel(id) {
const txResponse = await client.relaySigner.cancelTransactionById(id);
console.log('txResponse', JSON.stringify(txResponse, null, 2));
}

async function sign(msg) {
if (!msg) throw new Error(`Missing msg to sign`);
const signResponse = await client.relaySigner.sign({ message: msg });
Expand Down Expand Up @@ -91,6 +96,8 @@ async function main() {
return await send();
case 'replace':
return await replace(process.argv[3]);
case 'cancel':
return await cancel(process.argv[3]);
case 'sign':
return await sign(process.argv[3]);
case 'balance':
Expand Down
11 changes: 11 additions & 0 deletions packages/relay-signer/src/action/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,17 @@ describe('ActionRelayer', () => {
});
});

describe('cancelTransactionById', () => {
test('passes txId to the API', async () => {
await relayer.cancelTransactionById('123-456-abc');
expect(relayer.lambda.invoke).toHaveBeenCalledWith({
FunctionName: 'arn',
InvocationType: 'RequestResponse',
Payload: '{"action":"cancel-tx","id":"123-456-abc"}',
});
});
});

describe('getRelayer', () => {
test('passes correct arguments to the API', async () => {
await relayer.getRelayer();
Expand Down
11 changes: 10 additions & 1 deletion packages/relay-signer/src/action/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { ActionRelayerParams, IRelayer, RelayerGetResponse, RelayerStatus } from '../models/relayer';
import { ListTransactionsRequest, RelayerTransaction, RelayerTransactionPayload } from '../models/transactions';
import {
ListTransactionsRequest,
RelayerTransaction,
RelayerTransactionPayload,
TransactionDeleteResponse,
} from '../models/transactions';
import {
JsonRpcRequest,
JsonRpcResponse,
Expand Down Expand Up @@ -60,6 +65,10 @@ export class ActionRelayer extends BaseActionClient implements IRelayer {
return this.execute({ action: 'replace-tx', payload });
}

public async cancelTransactionById(id: string): Promise<TransactionDeleteResponse> {
return this.execute({ action: 'cancel-tx', id });
}

public async getRelayer(): Promise<RelayerGetResponse> {
return this.execute({
action: 'get-self' as const,
Expand Down
7 changes: 7 additions & 0 deletions packages/relay-signer/src/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
PaginatedTransactionResponse,
RelayerTransaction,
RelayerTransactionPayload,
TransactionDeleteResponse,
} from '../models/transactions';
import { JsonRpcResponse, SignMessagePayload, SignTypedDataPayload, SignedMessagePayload } from '../models/rpc';
import { AuthType } from '@openzeppelin/defender-sdk-base-client/lib/api/auth-v2';
Expand Down Expand Up @@ -66,6 +67,12 @@ export class RelaySignerClient extends BaseApiClient implements IRelayer {
});
}

public async cancelTransactionById(id: string): Promise<TransactionDeleteResponse> {
return this.apiCall(async (api) => {
return (await api.delete(`/relayers/self/txs/${id}`)) as TransactionDeleteResponse;
});
}

public async signTypedData(payload: SignTypedDataPayload): Promise<SignedMessagePayload> {
return this.apiCall(async (api) => {
return (await api.post('/relayers/self/sign-typed-data', payload)) as SignedMessagePayload;
Expand Down
2 changes: 2 additions & 0 deletions packages/relay-signer/src/models/relayer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
PrivateTransactionMode,
RelayerTransaction,
RelayerTransactionPayload,
TransactionDeleteResponse,
} from './transactions';
import { JsonRpcResponse, SignMessagePayload, SignTypedDataPayload, SignedMessagePayload } from './rpc';

Expand Down Expand Up @@ -74,6 +75,7 @@ export interface IRelayer {
sendTransaction(payload: RelayerTransactionPayload): Promise<RelayerTransaction>;
replaceTransactionById(id: string, payload: RelayerTransactionPayload): Promise<RelayerTransaction>;
replaceTransactionByNonce(nonce: number, payload: RelayerTransactionPayload): Promise<RelayerTransaction>;
cancelTransactionById(id: string): Promise<TransactionDeleteResponse>;
getTransaction(id: string): Promise<RelayerTransaction>;
listTransactions(criteria?: ListTransactionsRequest): Promise<RelayerTransaction[] | PaginatedTransactionResponse>;
sign(payload: SignMessagePayload): Promise<SignedMessagePayload>;
Expand Down
4 changes: 4 additions & 0 deletions packages/relay-signer/src/models/transactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,7 @@ export type PaginatedTransactionResponse = {
items: RelayerTransaction[];
next?: string;
};

export type TransactionDeleteResponse = {
message: string;
};
5 changes: 5 additions & 0 deletions packages/relay-signer/src/relayer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
PaginatedTransactionResponse,
RelayerTransaction,
RelayerTransactionPayload,
TransactionDeleteResponse,
} from './models/transactions';
import { isApiCredentials, isActionCredentials, validatePayload } from './ethers/utils';
import { RelaySignerClient } from './api';
Expand Down Expand Up @@ -111,6 +112,10 @@ export class Relayer implements IRelayer {
return this.relayer.replaceTransactionByNonce(nonce, payload);
}

public cancelTransactionById(id: string): Promise<TransactionDeleteResponse> {
return this.relayer.cancelTransactionById(id);
}

public getTransaction(id: string): Promise<RelayerTransaction> {
return this.relayer.getTransaction(id);
}
Expand Down

0 comments on commit 7c7431b

Please sign in to comment.