-
Notifications
You must be signed in to change notification settings - Fork 266
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
refactor: remove native token #2280
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
841e6c7
refactor: remove the native and private token from dapp_testing
LHerskind 50d03cd
fix: update storage slots
LHerskind ab181a0
fix: wrong order of parameters
LHerskind 6c086f2
fix: using secretHash instead of secret
LHerskind f73a742
fix: add unencrypted log check
LHerskind dbae7f1
fix: update refs in docs
LHerskind 371a6b2
fix: delete native_token_contract
LHerskind 20ee877
fix: remove native token abi from cli_docs_sandbox
LHerskind e284b94
refactor: remove expects in dapp_teesting for status
LHerskind File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,13 +5,16 @@ import { | |
CheatCodes, | ||
Fr, | ||
L2BlockL2Logs, | ||
computeMessageSecretHash, | ||
createAccount, | ||
createAztecRpcClient, | ||
getSandboxAccountsWallets, | ||
waitForSandbox, | ||
} from '@aztec/aztec.js'; | ||
import { toBigIntBE } from '@aztec/foundation/bigint-buffer'; | ||
import { NativeTokenContract, PrivateTokenContract, TestContract } from '@aztec/noir-contracts/types'; | ||
import { TestContract, TokenContract } from '@aztec/noir-contracts/types'; | ||
|
||
const { SANDBOX_URL = 'http://localhost:8080', ETHEREUM_HOST = 'http://localhost:8545' } = process.env; | ||
|
||
describe('guides/dapp/testing', () => { | ||
describe('on in-proc sandbox', () => { | ||
|
@@ -20,86 +23,91 @@ describe('guides/dapp/testing', () => { | |
let stop: () => Promise<void>; | ||
let owner: AccountWallet; | ||
let recipient: AccountWallet; | ||
let token: PrivateTokenContract; | ||
let token: TokenContract; | ||
|
||
beforeAll(async () => { | ||
// docs:start:in-proc-sandbox | ||
({ rpcServer: rpc, stop } = await createSandbox()); | ||
// docs:end:in-proc-sandbox | ||
owner = await createAccount(rpc); | ||
recipient = await createAccount(rpc); | ||
token = await PrivateTokenContract.deploy(owner, 100n, owner.getAddress()).send().deployed(); | ||
token = await TokenContract.deploy(owner).send().deployed(); | ||
await token.methods._initialize({ address: owner.getAddress() }).send().wait(); | ||
}, 60_000); | ||
|
||
// docs:start:stop-in-proc-sandbox | ||
afterAll(() => stop()); | ||
// docs:end:stop-in-proc-sandbox | ||
|
||
it('increases recipient funds on transfer', async () => { | ||
expect(await token.methods.getBalance(recipient.getAddress()).view()).toEqual(0n); | ||
await token.methods.transfer(20n, recipient.getAddress()).send().wait(); | ||
expect(await token.methods.getBalance(recipient.getAddress()).view()).toEqual(20n); | ||
it('increases recipient funds on mint', async () => { | ||
expect(await token.methods.balance_of_private({ address: recipient.getAddress() }).view()).toEqual(0n); | ||
const secret = Fr.random(); | ||
const secretHash = await computeMessageSecretHash(secret); | ||
await token.methods.mint_private(20n, secretHash).send().wait(); | ||
await token.methods.redeem_shield({ address: recipient.getAddress() }, 20n, secret).send().wait(); | ||
expect(await token.methods.balance_of_private({ address: recipient.getAddress() }).view()).toEqual(20n); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('on local sandbox', () => { | ||
beforeAll(async () => { | ||
const { SANDBOX_URL = 'http://localhost:8080' } = process.env; | ||
const rpc = createAztecRpcClient(SANDBOX_URL); | ||
await waitForSandbox(rpc); | ||
}); | ||
|
||
// docs:start:sandbox-example | ||
describe('private token contract', () => { | ||
const { SANDBOX_URL = 'http://localhost:8080' } = process.env; | ||
|
||
let rpc: AztecRPC; | ||
let owner: AccountWallet; | ||
let recipient: AccountWallet; | ||
let token: PrivateTokenContract; | ||
let token: TokenContract; | ||
|
||
beforeEach(async () => { | ||
rpc = createAztecRpcClient(SANDBOX_URL); | ||
owner = await createAccount(rpc); | ||
recipient = await createAccount(rpc); | ||
token = await PrivateTokenContract.deploy(owner, 100n, owner.getAddress()).send().deployed(); | ||
token = await TokenContract.deploy(owner).send().deployed(); | ||
await token.methods._initialize({ address: owner.getAddress() }).send().wait(); | ||
}, 30_000); | ||
|
||
it('increases recipient funds on transfer', async () => { | ||
expect(await token.methods.getBalance(recipient.getAddress()).view()).toEqual(0n); | ||
await token.methods.transfer(20n, recipient.getAddress()).send().wait(); | ||
expect(await token.methods.getBalance(recipient.getAddress()).view()).toEqual(20n); | ||
it('increases recipient funds on mint', async () => { | ||
expect(await token.methods.balance_of_private({ address: recipient.getAddress() }).view()).toEqual(0n); | ||
const secret = Fr.random(); | ||
const secretHash = await computeMessageSecretHash(secret); | ||
await token.methods.mint_private(20n, secretHash).send().wait(); | ||
await token.methods.redeem_shield({ address: recipient.getAddress() }, 20n, secret).send().wait(); | ||
expect(await token.methods.balance_of_private({ address: recipient.getAddress() }).view()).toEqual(20n); | ||
}); | ||
}); | ||
// docs:end:sandbox-example | ||
|
||
describe('private token contract with initial accounts', () => { | ||
const { SANDBOX_URL = 'http://localhost:8080' } = process.env; | ||
|
||
let rpc: AztecRPC; | ||
let owner: AccountWallet; | ||
let recipient: AccountWallet; | ||
let token: PrivateTokenContract; | ||
let token: TokenContract; | ||
|
||
beforeEach(async () => { | ||
// docs:start:use-existing-wallets | ||
rpc = createAztecRpcClient(SANDBOX_URL); | ||
[owner, recipient] = await getSandboxAccountsWallets(rpc); | ||
token = await PrivateTokenContract.deploy(owner, 100n, owner.getAddress()).send().deployed(); | ||
token = await TokenContract.deploy(owner).send().deployed(); | ||
await token.methods._initialize({ address: owner.getAddress() }).send().wait(); | ||
// docs:end:use-existing-wallets | ||
}, 30_000); | ||
|
||
it('increases recipient funds on transfer', async () => { | ||
expect(await token.methods.getBalance(recipient.getAddress()).view()).toEqual(0n); | ||
await token.methods.transfer(20n, recipient.getAddress()).send().wait(); | ||
expect(await token.methods.getBalance(recipient.getAddress()).view()).toEqual(20n); | ||
it('increases recipient funds on mint', async () => { | ||
expect(await token.methods.balance_of_private({ address: recipient.getAddress() }).view()).toEqual(0n); | ||
const secret = Fr.random(); | ||
const secretHash = await computeMessageSecretHash(secret); | ||
await token.methods.mint_private(20n, secretHash).send().wait(); | ||
await token.methods.redeem_shield({ address: recipient.getAddress() }, 20n, secret).send().wait(); | ||
expect(await token.methods.balance_of_private({ address: recipient.getAddress() }).view()).toEqual(20n); | ||
}); | ||
}); | ||
|
||
describe('cheats', () => { | ||
const { SANDBOX_URL = 'http://localhost:8080', ETHEREUM_HOST = 'http://localhost:8545' } = process.env; | ||
|
||
let rpc: AztecRPC; | ||
let owner: AccountWallet; | ||
let testContract: TestContract; | ||
|
@@ -122,29 +130,32 @@ describe('guides/dapp/testing', () => { | |
}); | ||
|
||
describe('assertions', () => { | ||
const { SANDBOX_URL = 'http://localhost:8080', ETHEREUM_HOST = 'http://localhost:8545' } = process.env; | ||
|
||
let rpc: AztecRPC; | ||
let owner: AccountWallet; | ||
let recipient: AccountWallet; | ||
let token: PrivateTokenContract; | ||
let nativeToken: NativeTokenContract; | ||
let testContract: TestContract; | ||
let token: TokenContract; | ||
let cheats: CheatCodes; | ||
let ownerSlot: Fr; | ||
|
||
beforeAll(async () => { | ||
rpc = createAztecRpcClient(SANDBOX_URL); | ||
owner = await createAccount(rpc); | ||
recipient = await createAccount(rpc); | ||
token = await PrivateTokenContract.deploy(owner, 100n, owner.getAddress()).send().deployed(); | ||
nativeToken = await NativeTokenContract.deploy(owner, 100n, owner.getAddress()).send().deployed(); | ||
testContract = await TestContract.deploy(owner).send().deployed(); | ||
token = await TokenContract.deploy(owner).send().deployed(); | ||
await token.methods._initialize({ address: owner.getAddress() }).send().wait(); | ||
const secret = Fr.random(); | ||
const secretHash = await computeMessageSecretHash(secret); | ||
await token.methods.mint_private(100n, secretHash).send().wait(); | ||
await token.methods.redeem_shield({ address: owner.getAddress() }, 100n, secret).send().wait(); | ||
|
||
// docs:start:calc-slot | ||
cheats = await CheatCodes.create(ETHEREUM_HOST, rpc); | ||
// The balances mapping is defined on storage slot 1 and is indexed by user address | ||
ownerSlot = cheats.aztec.computeSlotInMap(1n, owner.getAddress()); | ||
// The balances mapping is defined on storage slot 3 and is indexed by user address | ||
ownerSlot = cheats.aztec.computeSlotInMap(3n, owner.getAddress()); | ||
// docs:end:calc-slot | ||
}, 30_000); | ||
}, 60_000); | ||
|
||
it('checks private storage', async () => { | ||
// docs:start:private-storage | ||
|
@@ -157,40 +168,61 @@ describe('guides/dapp/testing', () => { | |
|
||
it('checks public storage', async () => { | ||
// docs:start:public-storage | ||
await nativeToken.methods.owner_mint_pub(owner.getAddress(), 100n).send().wait(); | ||
const ownerPublicBalanceSlot = cheats.aztec.computeSlotInMap(4n, owner.getAddress()); | ||
const balance = await rpc.getPublicStorageAt(nativeToken.address, ownerPublicBalanceSlot); | ||
await token.methods.mint_public({ address: owner.getAddress() }, 100n).send().wait(); | ||
const ownerPublicBalanceSlot = cheats.aztec.computeSlotInMap(6n, owner.getAddress()); | ||
const balance = await rpc.getPublicStorageAt(token.address, ownerPublicBalanceSlot); | ||
expect(toBigIntBE(balance!)).toEqual(100n); | ||
// docs:end:public-storage | ||
}); | ||
|
||
it('checks unencrypted logs', async () => { | ||
it('checks unencrypted logs, [Kinda broken with current implementation]', async () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. issue to fix? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Created #2306 for it, will update |
||
// docs:start:unencrypted-logs | ||
const tx = await nativeToken.methods.owner_mint_pub(owner.getAddress(), 100n).send().wait(); | ||
const value = Fr.fromString('ef'); // Only 1 bytes will make its way in there :( so no larger stuff | ||
const tx = await testContract.methods.emit_unencrypted(value).send().wait(); | ||
const logs = await rpc.getUnencryptedLogs(tx.blockNumber!, 1); | ||
const textLogs = L2BlockL2Logs.unrollLogs(logs).map(log => log.toString('ascii')); | ||
expect(textLogs).toEqual(['Coins minted']); | ||
const log = L2BlockL2Logs.unrollLogs(logs)[0]; | ||
expect(Fr.fromBuffer(log)).toEqual(value); | ||
// docs:end:unencrypted-logs | ||
}); | ||
|
||
it('asserts a local transaction simulation fails by calling simulate', async () => { | ||
// docs:start:local-tx-fails | ||
const call = token.methods.transfer(200n, recipient.getAddress()); | ||
const call = token.methods.transfer( | ||
{ address: owner.getAddress() }, | ||
{ address: recipient.getAddress() }, | ||
200n, | ||
0, | ||
); | ||
await expect(call.simulate()).rejects.toThrowError(/Balance too low/); | ||
// docs:end:local-tx-fails | ||
}); | ||
|
||
it('asserts a local transaction simulation fails by calling send', async () => { | ||
// docs:start:local-tx-fails-send | ||
const call = token.methods.transfer(200n, recipient.getAddress()); | ||
const call = token.methods.transfer( | ||
{ address: owner.getAddress() }, | ||
{ address: recipient.getAddress() }, | ||
200n, | ||
0, | ||
); | ||
await expect(call.send().wait()).rejects.toThrowError(/Balance too low/); | ||
// docs:end:local-tx-fails-send | ||
}); | ||
|
||
it('asserts a transaction is dropped', async () => { | ||
// docs:start:tx-dropped | ||
const call1 = token.methods.transfer(80n, recipient.getAddress()); | ||
const call2 = token.methods.transfer(50n, recipient.getAddress()); | ||
const call1 = token.methods.transfer( | ||
{ address: owner.getAddress() }, | ||
{ address: recipient.getAddress() }, | ||
80n, | ||
0, | ||
); | ||
const call2 = token.methods.transfer( | ||
{ address: owner.getAddress() }, | ||
{ address: recipient.getAddress() }, | ||
50n, | ||
0, | ||
); | ||
|
||
await call1.simulate(); | ||
await call2.simulate(); | ||
|
@@ -202,14 +234,24 @@ describe('guides/dapp/testing', () => { | |
|
||
it('asserts a simulation for a public function call fails', async () => { | ||
// docs:start:local-pub-fails | ||
const call = nativeToken.methods.transfer_pub(recipient.getAddress(), 1000n); | ||
await expect(call.simulate()).rejects.toThrowError(/Balance too low/); | ||
const call = token.methods.transfer_public( | ||
{ address: owner.getAddress() }, | ||
{ address: recipient.getAddress() }, | ||
1000n, | ||
0, | ||
); | ||
await expect(call.simulate()).rejects.toThrowError(/Underflow/); | ||
// docs:end:local-pub-fails | ||
}); | ||
|
||
it('asserts a transaction with a failing public call is dropped (until we get public reverts)', async () => { | ||
// docs:start:pub-dropped | ||
const call = nativeToken.methods.transfer_pub(recipient.getAddress(), 1000n); | ||
const call = token.methods.transfer_public( | ||
{ address: owner.getAddress() }, | ||
{ address: recipient.getAddress() }, | ||
1000n, | ||
0, | ||
); | ||
await expect(call.send({ skipPublicSimulation: true }).wait()).rejects.toThrowError(/dropped/); | ||
// docs:end:pub-dropped | ||
}); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 0 additions & 10 deletions
10
yarn-project/noir-contracts/src/contracts/native_token_contract/Nargo.toml
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As this is being used in the example we should probably limit the level of chaining in the calls
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ye, just wasted a lot of space otherwise, but maybe fine to include the
waitForSuccess
or just do the wait and ignore the status as we are doing in quite a few places anyway 🤷