Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Deserialize AztecAddress when contract's view function returns it in Aztec.js #3641 #4224

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 10 additions & 20 deletions yarn-project/end-to-end/src/e2e_card_game.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@ const cardToField = (card: Card): bigint => {
};

interface PlayerGameEntry {
address: {
inner: bigint;
};
address: AztecAddress;
deck_strength: bigint;
points: bigint;
}
Expand Down Expand Up @@ -183,16 +181,12 @@ describe('e2e_card_game', () => {
expect((await contract.methods.view_game(GAME_ID).view({ from: firstPlayer })) as Game).toMatchObject({
players: [
{
address: {
inner: firstPlayer.toBigInt(),
},
address: firstPlayer,
deck_strength: expect.anything(),
points: 0n,
},
{
address: {
inner: 0n,
},
address: AztecAddress.ZERO,
deck_strength: 0n,
points: 0n,
},
Expand Down Expand Up @@ -227,16 +221,12 @@ describe('e2e_card_game', () => {
expect((await contract.methods.view_game(GAME_ID).view({ from: firstPlayer })) as Game).toMatchObject({
players: expect.arrayContaining([
{
address: {
inner: firstPlayer.toBigInt(),
},
address: firstPlayer,
deck_strength: expect.anything(),
points: 0n,
},
{
address: {
inner: secondPlayer.toBigInt(),
},
address: secondPlayer,
deck_strength: expect.anything(),
points: 0n,
},
Expand Down Expand Up @@ -281,16 +271,16 @@ describe('e2e_card_game', () => {

async function playGame(playerDecks: { address: AztecAddress; deck: Card[] }[], id = GAME_ID) {
const initialGameState = (await contract.methods.view_game(id).view({ from: firstPlayer })) as Game;
const players = initialGameState.players.map(player => player.address.inner);
const players = initialGameState.players.map(player => player.address);
const cards = players.map(
player => playerDecks.find(playerDeckEntry => playerDeckEntry.address.toBigInt() === player)!.deck,
player => playerDecks.find(playerDeckEntry => playerDeckEntry.address.equals(player))!.deck,
);

for (let roundIndex = 0; roundIndex < cards.length; roundIndex++) {
for (let playerIndex = 0; playerIndex < players.length; playerIndex++) {
const player = players[playerIndex];
const card = cards[playerIndex][roundIndex];
await contractFor(AztecAddress.fromBigInt(player)).methods.play_card(id, card).send().wait();
await contractFor(player).methods.play_card(id, card).send().wait();
}
}

Expand All @@ -315,8 +305,8 @@ describe('e2e_card_game', () => {
]);

const sortedByPoints = game.players.sort((a, b) => Number(b.points - a.points));
const winner = AztecAddress.fromBigInt(sortedByPoints[0].address.inner);
const loser = AztecAddress.fromBigInt(sortedByPoints[1].address.inner);
const winner = sortedByPoints[0].address;
const loser = sortedByPoints[1].address;

await expect(
contractFor(loser).methods.claim_cards(GAME_ID, game.rounds_cards.map(cardToField)).send().wait(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,7 @@ export async function deployAndInitializeTokenAndBridgeContracts(
throw new Error(`Token admin is not ${owner}`);
}

// TODO(#3641) - Fix deserialization and compare AztecAddress directly
if ((await bridge.methods.token().view()).inner !== token.address.toBigInt()) {
if (!(await bridge.methods.token().view()).equals(token.address)) {
throw new Error(`Bridge token is not ${token.address}`);
}

Expand Down
8 changes: 7 additions & 1 deletion yarn-project/foundation/src/abi/decoder.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { AztecAddress } from '../aztec-address/index.js';
import { Fr } from '../fields/index.js';
import { ABIParameter, type ABIType, ABIVariable, FunctionArtifact } from './abi.js';
import { isAztecAddressStruct } from './utils.js';

/**
* The type of our decoded ABI.
*/
export type DecodedReturn = bigint | boolean | DecodedReturn[] | { [key: string]: DecodedReturn };
export type DecodedReturn = bigint | boolean | AztecAddress | DecodedReturn[] | { [key: string]: DecodedReturn };

/**
* Decodes return values from a function call.
Expand Down Expand Up @@ -38,6 +40,10 @@ class ReturnValuesDecoder {
}
case 'struct': {
const struct: { [key: string]: DecodedReturn } = {};
if (isAztecAddressStruct(abiType)) {
return new AztecAddress(this.getNextField().toBuffer());
}

for (const field of abiType.fields) {
struct[field.name] = this.decodeReturn(field.type);
}
Expand Down
Loading