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(aztec.js): Support AddressLike parameters #2430

Merged
merged 3 commits into from
Sep 20, 2023
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
8 changes: 7 additions & 1 deletion yarn-project/aztec.js/src/utils/abi_types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { Fr } from '@aztec/circuits.js';
import { AztecAddress, EthAddress, Fr } from '@aztec/circuits.js';

/** Any type that can be converted into a field for a contract call. */
export type FieldLike = Fr | Buffer | bigint | number | { /** Converts to field */ toField: () => Fr };

/** Any type that can be converted into an EthereumAddress Aztec.nr struct. */
export type EthAddressLike = { /** Wrapped address */ address: FieldLike } | EthAddress;

/** Any type that can be converted into an EthereumAddress Aztec.nr struct. */
export type AztecAddressLike = { /** Wrapped address */ address: FieldLike } | AztecAddress;
13 changes: 5 additions & 8 deletions yarn-project/end-to-end/src/canary/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ export const browserTestSuite = (setup: () => Server, pageLogger: AztecJs.DebugL
const owner = (await client.getRegisteredAccounts())[0].address;
const [wallet] = await AztecJs.getSandboxAccountsWallets(client);
const contract = await Contract.at(AztecAddress.fromString(contractAddress), TokenContractAbi, wallet);
const balance = await contract.methods.balance_of_private({ address: owner }).view({ from: owner });
const balance = await contract.methods.balance_of_private(owner).view({ from: owner });
return balance;
},
SANDBOX_URL,
Expand All @@ -143,12 +143,9 @@ export const browserTestSuite = (setup: () => Server, pageLogger: AztecJs.DebugL
const receiver = accounts[1].address;
const [wallet] = await AztecJs.getSandboxAccountsWallets(client);
const contract = await Contract.at(AztecAddress.fromString(contractAddress), TokenContractAbi, wallet);
await contract.methods
.transfer({ address: accounts[0].address }, { address: receiver }, transferAmount, 0)
.send()
.wait();
await contract.methods.transfer(accounts[0].address, receiver, transferAmount, 0).send().wait();
console.log(`Transferred ${transferAmount} tokens to new Account`);
return await contract.methods.balance_of_private({ address: receiver }).view({ from: receiver });
return await contract.methods.balance_of_private(receiver).view({ from: receiver });
},
SANDBOX_URL,
(await getTokenAddress()).toString(),
Expand Down Expand Up @@ -186,11 +183,11 @@ export const browserTestSuite = (setup: () => Server, pageLogger: AztecJs.DebugL
console.log(`Contract Deployed: ${receipt.contractAddress}`);

const token = await Contract.at(receipt.contractAddress!, TokenContractAbi, owner);
await token.methods._initialize({ address: owner.getAddress() }).send().wait();
await token.methods._initialize(owner.getAddress()).send().wait();
const secret = Fr.random();
const secretHash = await computeMessageSecretHash(secret);
await token.methods.mint_private(initialBalance, secretHash).send().wait();
await token.methods.redeem_shield({ address: owner.getAddress() }, initialBalance, secret).send().wait();
await token.methods.redeem_shield(owner.getAddress(), initialBalance, secret).send().wait();

return receipt.txHash.toString();
},
Expand Down
16 changes: 8 additions & 8 deletions yarn-project/end-to-end/src/e2e_2_rpc_servers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,25 +72,25 @@ describe('e2e_2_rpc_servers', () => {

// Then check the balance
const contractWithWallet = await TokenContract.at(tokenAddress, wallet);
const balance = await contractWithWallet.methods.balance_of_private({ address: owner }).view({ from: owner });
const balance = await contractWithWallet.methods.balance_of_private(owner).view({ from: owner });
logger(`Account ${owner} balance: ${balance}`);
expect(balance).toBe(expectedBalance);
};

const deployTokenContract = async (initialBalance: bigint, owner: AztecAddress) => {
logger(`Deploying Token contract...`);
const contract = await TokenContract.deploy(walletA).send().deployed();
expect((await contract.methods._initialize({ address: owner }).send().wait()).status).toBe(TxStatus.MINED);
expect((await contract.methods._initialize(owner).send().wait()).status).toBe(TxStatus.MINED);

const secret = Fr.random();
const secretHash = await computeMessageSecretHash(secret);

expect((await contract.methods.mint_private(initialBalance, secretHash).send().wait()).status).toEqual(
TxStatus.MINED,
);
expect(
(await contract.methods.redeem_shield({ address: owner }, initialBalance, secret).send().wait()).status,
).toEqual(TxStatus.MINED);
expect((await contract.methods.redeem_shield(owner, initialBalance, secret).send().wait()).status).toEqual(
TxStatus.MINED,
);

logger('L2 contract deployed');

Expand Down Expand Up @@ -127,7 +127,7 @@ describe('e2e_2_rpc_servers', () => {
// Transfer funds from A to B via RPC server A
const contractWithWalletA = await TokenContract.at(tokenAddress, walletA);
const receiptAToB = await contractWithWalletA.methods
.transfer({ address: userA.address }, { address: userB.address }, transferAmount1, 0)
.transfer(userA.address, userB.address, transferAmount1, 0)
.send()
.wait();
expect(receiptAToB.status).toBe(TxStatus.MINED);
Expand All @@ -140,7 +140,7 @@ describe('e2e_2_rpc_servers', () => {
// Transfer funds from B to A via RPC server B
const contractWithWalletB = await TokenContract.at(tokenAddress, walletB);
await contractWithWalletB.methods
.transfer({ address: userB.address }, { address: userA.address }, transferAmount2, 0)
.transfer(userB.address, userA.address, transferAmount2, 0)
.send()
.wait({ interval: 0.1 });

Expand Down Expand Up @@ -189,7 +189,7 @@ describe('e2e_2_rpc_servers', () => {

await awaitServerSynchronised(aztecRpcServerA);

const storedValue = await getChildStoredValue({ address: childCompleteAddress.address }, aztecRpcServerB);
const storedValue = await getChildStoredValue(childCompleteAddress, aztecRpcServerB);
expect(storedValue).toBe(newValueToSet);
}, 60_000);
});
12 changes: 3 additions & 9 deletions yarn-project/end-to-end/src/e2e_cross_chain_messaging.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ describe('e2e_cross_chain_messaging', () => {
.methods.claim_private(
bridgeAmount,
secretHashForL2MessageConsumption,
{ address: ethAccount.toField() },
ethAccount,
messageKey,
secretForL2MessageConsumption,
)
Expand All @@ -181,7 +181,7 @@ describe('e2e_cross_chain_messaging', () => {
.methods.claim_private(
bridgeAmount,
secretHashForRedeemingMintedNotes,
{ address: ethAccount.toField() },
ethAccount,
messageKey,
secretForL2MessageConsumption,
)
Expand Down Expand Up @@ -212,13 +212,7 @@ describe('e2e_cross_chain_messaging', () => {
await expect(
l2Bridge
.withWallet(user1Wallet)
.methods.exit_to_l1_private(
{ address: ethAccount.toField() },
{ address: l2Token.address },
withdrawAmount,
{ address: EthAddress.ZERO.toField() },
nonce,
)
.methods.exit_to_l1_private(ethAccount, l2Token.address, withdrawAmount, EthAddress.ZERO, nonce)
.simulate(),
).rejects.toThrowError(`Unknown auth witness for message hash 0x${expectedBurnMessageHash.toString('hex')}`);
});
Expand Down
16 changes: 7 additions & 9 deletions yarn-project/end-to-end/src/e2e_escrow_contract.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,15 @@ describe('e2e_escrow_contract', () => {
// Deploy Private Token contract and mint funds for the escrow contract
token = await TokenContract.deploy(wallet).send().deployed();

expect((await token.methods._initialize({ address: owner }).send().wait()).status).toBe(TxStatus.MINED);
expect((await token.methods._initialize(owner).send().wait()).status).toBe(TxStatus.MINED);

const secret = Fr.random();
const secretHash = await computeMessageSecretHash(secret);

expect((await token.methods.mint_private(100n, secretHash).send().wait()).status).toEqual(TxStatus.MINED);
expect(
(await token.methods.redeem_shield({ address: escrowContract.address }, 100n, secret).send().wait()).status,
).toEqual(TxStatus.MINED);
expect((await token.methods.redeem_shield(escrowContract.address, 100n, secret).send().wait()).status).toEqual(
TxStatus.MINED,
);

logger(`Token contract deployed at ${token.address}`);
}, 100_000);
Expand All @@ -72,7 +72,7 @@ describe('e2e_escrow_contract', () => {
}, 30_000);

const expectBalance = async (who: AztecAddress, expectedBalance: bigint) => {
const balance = await token.methods.balance_of_private({ address: who }).view({ from: who });
const balance = await token.methods.balance_of_private(who).view({ from: who });
logger(`Account ${who} balance: ${balance}`);
expect(balance).toBe(expectedBalance);
};
Expand Down Expand Up @@ -102,14 +102,12 @@ describe('e2e_escrow_contract', () => {
const secretHash = await computeMessageSecretHash(secret);

expect((await token.methods.mint_private(50n, secretHash).send().wait()).status).toEqual(TxStatus.MINED);
expect((await token.methods.redeem_shield({ address: owner }, 50n, secret).send().wait()).status).toEqual(
TxStatus.MINED,
);
expect((await token.methods.redeem_shield(owner, 50n, secret).send().wait()).status).toEqual(TxStatus.MINED);

await expectBalance(owner, 50n);

const actions = [
token.methods.transfer({ address: owner }, { address: recipient }, 10, 0).request(),
token.methods.transfer(owner, recipient, 10, 0).request(),
escrowContract.methods.withdraw(token.address, 20, recipient).request(),
];

Expand Down
10 changes: 4 additions & 6 deletions yarn-project/end-to-end/src/e2e_lending_contract.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,9 @@ describe('e2e_lending_contract', () => {
}

await waitForSuccess(collateralAsset.methods._initialize(accounts[0]).send());
await waitForSuccess(collateralAsset.methods.set_minter({ address: lendingContract.address }, true).send());
await waitForSuccess(collateralAsset.methods.set_minter(lendingContract.address, true).send());
await waitForSuccess(stableCoin.methods._initialize(accounts[0]).send());
await waitForSuccess(stableCoin.methods.set_minter({ address: lendingContract.address }, true).send());
await waitForSuccess(stableCoin.methods.set_minter(lendingContract.address, true).send());

return { priceFeedContract, lendingContract, collateralAsset, stableCoin };
};
Expand Down Expand Up @@ -127,13 +127,11 @@ describe('e2e_lending_contract', () => {
const secret = Fr.random();
const secretHash = await computeMessageSecretHash(secret);

const a = asset.methods.mint_public({ address: lendingAccount.address }, mintAmount).send();
const a = asset.methods.mint_public(lendingAccount.address, mintAmount).send();
const b = asset.methods.mint_private(mintAmount, secretHash).send();

await Promise.all([a, b].map(waitForSuccess));
await waitForSuccess(
asset.methods.redeem_shield({ address: lendingAccount.address }, mintAmount, secret).send(),
);
await waitForSuccess(asset.methods.redeem_shield(lendingAccount.address, mintAmount, secret).send());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,15 @@ describe('e2e_multiple_accounts_1_enc_key', () => {
tokenAddress = token.address;
logger(`Token deployed at ${tokenAddress}`);

expect((await token.methods._initialize({ address: accounts[0] }).send().wait()).status).toBe(TxStatus.MINED);
expect((await token.methods._initialize(accounts[0]).send().wait()).status).toBe(TxStatus.MINED);

const secret = Fr.random();
const secretHash = await computeMessageSecretHash(secret);

expect((await token.methods.mint_private(initialBalance, secretHash).send().wait()).status).toEqual(TxStatus.MINED);
expect(
(await token.methods.redeem_shield({ address: accounts[0] }, initialBalance, secret).send().wait()).status,
).toEqual(TxStatus.MINED);
expect((await token.methods.redeem_shield(accounts[0], initialBalance, secret).send().wait()).status).toEqual(
TxStatus.MINED,
);
}, 100_000);

afterEach(async () => {
Expand All @@ -71,7 +71,7 @@ describe('e2e_multiple_accounts_1_enc_key', () => {

// Then check the balance
const contractWithWallet = await TokenContract.at(tokenAddress, wallet);
const balance = await contractWithWallet.methods.balance_of_private({ address: owner }).view({ from: owner });
const balance = await contractWithWallet.methods.balance_of_private(owner).view({ from: owner });
logger(`Account ${owner} balance: ${balance}`);
expect(balance).toBe(expectedBalance);
};
Expand All @@ -89,10 +89,7 @@ describe('e2e_multiple_accounts_1_enc_key', () => {

const contractWithWallet = await TokenContract.at(tokenAddress, wallets[senderIndex]);

const receipt = await contractWithWallet.methods
.transfer({ address: sender }, { address: receiver }, transferAmount, 0)
.send()
.wait();
const receipt = await contractWithWallet.methods.transfer(sender, receiver, transferAmount, 0).send().wait();
expect(receipt.status).toBe(TxStatus.MINED);

for (let i = 0; i < expectedBalances.length; i++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,27 +151,15 @@ describe('e2e_public_cross_chain_messaging', () => {
await expect(
l2Bridge
.withWallet(user2Wallet)
.methods.claim_public(
{ address: user2Wallet.getAddress() },
bridgeAmount,
{ address: ownerEthAddress.toField() },
messageKey,
secret,
)
.methods.claim_public(user2Wallet.getAddress(), bridgeAmount, ownerEthAddress, messageKey, secret)
.simulate(),
).rejects.toThrow();

// user2 consumes owner's L1-> L2 message on bridge contract and mints public tokens on L2
logger("user2 consumes owner's message on L2 Publicly");
const tx = l2Bridge
.withWallet(user2Wallet)
.methods.claim_public(
{ address: ownerAddress },
bridgeAmount,
{ address: ownerEthAddress.toField() },
messageKey,
secret,
)
.methods.claim_public(ownerAddress, bridgeAmount, ownerEthAddress, messageKey, secret)
.send();
const receipt = await tx.wait();
expect(receipt.status).toBe(TxStatus.MINED);
Expand All @@ -190,12 +178,7 @@ describe('e2e_public_cross_chain_messaging', () => {
await expect(
l2Bridge
.withWallet(ownerWallet)
.methods.exit_to_l1_public(
{ address: ownerEthAddress.toField() },
withdrawAmount,
{ address: EthAddress.ZERO.toField() },
nonce,
)
.methods.exit_to_l1_public(ownerEthAddress, withdrawAmount, EthAddress.ZERO, nonce)
.simulate(),
).rejects.toThrowError('Assertion failed: Message not authorized by account');
});
Expand Down
22 changes: 11 additions & 11 deletions yarn-project/end-to-end/src/e2e_sandbox_example.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,16 +95,16 @@ describe('e2e_sandbox_example', () => {
const tokenContractAlice = await TokenContract.at(contract.address, await accounts[0].getWallet());

// Initialize the contract and add Bob as a minter
await tokenContractAlice.methods._initialize({ address: alice }).send().wait();
await tokenContractAlice.methods.set_minter({ address: bob }, true).send().wait();
await tokenContractAlice.methods._initialize(alice).send().wait();
await tokenContractAlice.methods.set_minter(bob, true).send().wait();

logger(`Contract successfully deployed at address ${contract.address.toShortString()}`);

const secret = Fr.random();
const secretHash = await computeMessageSecretHash(secret);

await tokenContractAlice.methods.mint_private(initialSupply, secretHash).send().wait();
await tokenContractAlice.methods.redeem_shield({ address: alice }, initialSupply, secret).send().wait();
await tokenContractAlice.methods.redeem_shield(alice, initialSupply, secret).send().wait();
// docs:end:Deployment

// ensure that token contract is registered in the rpc
Expand All @@ -118,10 +118,10 @@ describe('e2e_sandbox_example', () => {
// Since we already have a token link, we can simply create a new instance of the contract linked to Bob's wallet
const tokenContractBob = tokenContractAlice.withWallet(await accounts[1].getWallet());

let aliceBalance = await tokenContractAlice.methods.balance_of_private({ address: alice }).view();
let aliceBalance = await tokenContractAlice.methods.balance_of_private(alice).view();
logger(`Alice's balance ${aliceBalance}`);

let bobBalance = await tokenContractBob.methods.balance_of_private({ address: bob }).view();
let bobBalance = await tokenContractBob.methods.balance_of_private(bob).view();
logger(`Bob's balance ${bobBalance}`);

// docs:end:Balance
Expand All @@ -135,13 +135,13 @@ describe('e2e_sandbox_example', () => {
// We will now transfer tokens from ALice to Bob
const transferQuantity = 543n;
logger(`Transferring ${transferQuantity} tokens from Alice to Bob...`);
await tokenContractAlice.methods.transfer({ address: alice }, { address: bob }, transferQuantity, 0).send().wait();
await tokenContractAlice.methods.transfer(alice, bob, transferQuantity, 0).send().wait();

// Check the new balances
aliceBalance = await tokenContractAlice.methods.balance_of_private({ address: alice }).view();
aliceBalance = await tokenContractAlice.methods.balance_of_private(alice).view();
logger(`Alice's balance ${aliceBalance}`);

bobBalance = await tokenContractBob.methods.balance_of_private({ address: bob }).view();
bobBalance = await tokenContractBob.methods.balance_of_private(bob).view();
logger(`Bob's balance ${bobBalance}`);
// docs:end:Transfer

Expand All @@ -155,13 +155,13 @@ describe('e2e_sandbox_example', () => {
const mintQuantity = 10_000n;
logger(`Minting ${mintQuantity} tokens to Bob...`);
await tokenContractBob.methods.mint_private(mintQuantity, secretHash).send().wait();
await tokenContractBob.methods.redeem_shield({ address: bob }, mintQuantity, secret).send().wait();
await tokenContractBob.methods.redeem_shield(bob, mintQuantity, secret).send().wait();

// Check the new balances
aliceBalance = await tokenContractAlice.methods.balance_of_private({ address: alice }).view();
aliceBalance = await tokenContractAlice.methods.balance_of_private(alice).view();
logger(`Alice's balance ${aliceBalance}`);

bobBalance = await tokenContractBob.methods.balance_of_private({ address: bob }).view();
bobBalance = await tokenContractBob.methods.balance_of_private(bob).view();
logger(`Bob's balance ${bobBalance}`);
// docs:end:Mint

Expand Down
Loading