Skip to content

Commit

Permalink
allow access list to be used
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronmgdr committed Jun 6, 2024
1 parent f2be773 commit 9708d53
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 9 deletions.
23 changes: 14 additions & 9 deletions src/lib/transactions.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
AccessList,
accessListify,
AccessListish,
assertArgument,
Expand Down Expand Up @@ -54,6 +55,10 @@ export enum TxTypeToPrefix {
eip1559 = 0x02,
}

function formatAccessList(value: AccessListish): Array<[ string, Array<string> ]> {
return accessListify(value).map((set) => [ set.address, set.storageKeys ]);
}

interface Field {
maxLength?: number;
length?: number;
Expand Down Expand Up @@ -106,6 +111,7 @@ export const celoTransactionFields: Record<CeloFieldName, Field> = {
maxFeePerGas: { maxLength: 32, numeric: true } as Field,
maxPriorityFeePerGas: { maxLength: 32, numeric: true } as Field,
maxFeeInFeeCurrency: { maxLength: 32, numeric: true } as Field,

} as const;

function formatCeloField(name: CeloFieldName, value: any) {
Expand Down Expand Up @@ -170,8 +176,7 @@ function prepareEncodeTx(tx: CeloTransaction, signature?: Signature): RlpStructu
tx.to || "0x",
tx.value ? toBeHex(tx.value) : "0x",
tx.data || "0x",
// @ts-expect-error
tx.accessList || [],
formatAccessList(tx.accessList || []),
(tx as CeloTransactionCip66).feeCurrency || "0x",
(tx as CeloTransactionCip66).maxFeeInFeeCurrency ? toBeHex((tx as CeloTransactionCip66).maxFeeInFeeCurrency) : "0x",
];
Expand All @@ -188,8 +193,7 @@ function prepareEncodeTx(tx: CeloTransaction, signature?: Signature): RlpStructu
tx.to || "0x",
tx.value ? toBeHex(tx.value) : "0x",
tx.data || "0x",
// @ts-expect-error
tx.accessList || [],
formatAccessList(tx.accessList || []),
(tx as CeloTransactionCip64).feeCurrency || "0x",
];
break;
Expand All @@ -205,8 +209,7 @@ function prepareEncodeTx(tx: CeloTransaction, signature?: Signature): RlpStructu
tx.to || "0x",
tx.value ? toBeHex(tx.value) : "0x",
tx.data || "0x",
// @ts-expect-error
tx.accessList || [],
formatAccessList(tx.accessList || []),
];
break;
default:
Expand Down Expand Up @@ -258,6 +261,8 @@ export function serializeCeloTransaction(
if (fieldName in celoTransactionFields) {
// @ts-expect-error
txArgs[fieldName as CeloFieldName] = formatCeloField(fieldName as CeloFieldName, fieldValue);
} else if (fieldName === 'accessList') {
txArgs[fieldName] = fieldValue
}
});

Expand Down Expand Up @@ -339,7 +344,7 @@ export function parseCeloTransaction(rawTransaction: BytesLike): CeloTransaction
to: handleAddress(transaction[5] as string),
value: handleBigInt(transaction[6] as string),
data: transaction[7] as string,
accessList: handleAccessList(transaction[8] as string),
accessList: handleAccessList(transaction[8]),
feeCurrency: handleAddress(transaction[9] as string),
maxFeeInFeeCurrency: handleBigInt(transaction[10] as string),
} as CeloTransactionCip66;
Expand Down Expand Up @@ -469,12 +474,12 @@ function handleBigInt(value: string): bigint {
return getBigInt(value);
}

function handleAccessList(value: string): AccessListish | "0x" {
function handleAccessList(value: any): AccessList {
if (value === "0x") {
return accessListify([]);
}
// TODO: use value
return accessListify([]);
return accessListify(value);
}

const baseTxLengths = {
Expand Down
28 changes: 28 additions & 0 deletions tests/transactions.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,34 @@ describe('serializeCeloTransaction', () => {

expect(serializeCeloTransaction(tx)).toMatchInlineSnapshot(`"0x7af84b82a4ec01847735940084773594008094f653a42ef024d174bb4be98b67690e5886d01f5f880de0b6b3a764000080c094765de816845861e75a25fca122bb6898b8b1282a860b3a4b56fa00"`);
});
it('can handle accessList', () => {
const transaction = serializeCeloTransaction({
to: "0xF653A42ef024d174Bb4bE98B67690E5886d01F5F",
chainId: 42220,
nonce: 1,
accessList: [
{
address: '0x0000000000000000000000000000000000000000',
storageKeys: [
'0x0000000000000000000000000000000000000000000000000000000000000001',
'0x60fdd29ff912ce880cd3edaf9f932dc61d3dae823ea77e0323f94adb9f6a72fe',
],
},
],
})
expect(transaction).toMatchInlineSnapshot(`"0x02f87b82a4ec0180808094f653a42ef024d174bb4be98b67690e5886d01f5f8080f85bf859940000000000000000000000000000000000000000f842a00000000000000000000000000000000000000000000000000000000000000001a060fdd29ff912ce880cd3edaf9f932dc61d3dae823ea77e0323f94adb9f6a72fe"`)
expect(parseCeloTransaction(transaction).accessList).toMatchInlineSnapshot(`
[
{
"address": "0x0000000000000000000000000000000000000000",
"storageKeys": [
"0x0000000000000000000000000000000000000000000000000000000000000001",
"0x60fdd29ff912ce880cd3edaf9f932dc61d3dae823ea77e0323f94adb9f6a72fe",
],
},
]
`)
})
});
describe('parseCeloTransaction', () => {
it('serializes CIP-66 transaction', () => {
Expand Down

0 comments on commit 9708d53

Please sign in to comment.