-
Notifications
You must be signed in to change notification settings - Fork 236
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes #1450
- Loading branch information
Showing
7 changed files
with
361 additions
and
35 deletions.
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
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 |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { ABIParameterVisibility, ContractAbi, FunctionType } from '@aztec/foundation/abi'; | ||
|
||
export const mockContractAbi: ContractAbi = { | ||
name: 'MockContract', | ||
functions: [ | ||
{ | ||
name: 'constructor', | ||
functionType: FunctionType.SECRET, | ||
isInternal: false, | ||
parameters: [ | ||
{ | ||
name: 'constructorParam1', | ||
type: { | ||
kind: 'field', | ||
}, | ||
visibility: ABIParameterVisibility.SECRET, | ||
}, | ||
], | ||
returnTypes: [], | ||
bytecode: 'constructorBytecode', | ||
}, | ||
{ | ||
name: 'mockFunction', | ||
functionType: FunctionType.SECRET, | ||
isInternal: false, | ||
parameters: [ | ||
{ | ||
name: 'fieldParam', | ||
type: { kind: 'field' }, | ||
visibility: ABIParameterVisibility.SECRET, | ||
}, | ||
{ | ||
name: 'boolParam', | ||
type: { kind: 'boolean' }, | ||
visibility: ABIParameterVisibility.SECRET, | ||
}, | ||
{ | ||
name: 'integerParam', | ||
type: { kind: 'integer', sign: 'signed', width: 32 }, | ||
visibility: ABIParameterVisibility.SECRET, | ||
}, | ||
{ | ||
name: 'arrayParam', | ||
type: { kind: 'array', length: 3, type: { kind: 'field' } }, | ||
visibility: ABIParameterVisibility.SECRET, | ||
}, | ||
{ | ||
name: 'structParam', | ||
type: { | ||
kind: 'struct', | ||
fields: [ | ||
{ name: 'subField1', type: { kind: 'field' } }, | ||
{ name: 'subField2', type: { kind: 'boolean' } }, | ||
], | ||
}, | ||
visibility: ABIParameterVisibility.SECRET, | ||
}, | ||
], | ||
returnTypes: [{ kind: 'boolean' }], | ||
bytecode: 'mockBytecode', | ||
}, | ||
], | ||
}; |
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 |
---|---|---|
@@ -0,0 +1,133 @@ | ||
import { AztecAddress, Fr } from '@aztec/aztec.js'; | ||
import { AztecRPC, CompleteAddress } from '@aztec/types'; | ||
|
||
import { MockProxy, mock } from 'jest-mock-extended'; | ||
|
||
import { encodeArgs } from '../encoding.js'; | ||
import { getTxSender } from '../utils.js'; | ||
import { mockContractAbi } from './mocks.js'; | ||
|
||
describe('CLI Utils', () => { | ||
let client: MockProxy<AztecRPC>; | ||
|
||
// test values | ||
const addr1 = AztecAddress.random(); | ||
const addr2 = AztecAddress.random(); | ||
const addr3 = AztecAddress.random(); | ||
const fieldArray = [addr1.toString(), addr2.toString(), addr3.toString()]; | ||
const num = 33; | ||
const field = Fr.random(); | ||
const struct = { | ||
subField1: field.toString(), | ||
subField2: 'true', | ||
}; | ||
beforeEach(() => { | ||
client = mock<AztecRPC>(); | ||
}); | ||
it('Gets a txSender correctly or throw error', async () => { | ||
// returns a parsed Aztec Address | ||
const aztecAddress = AztecAddress.random(); | ||
const result = await getTxSender(client, aztecAddress.toString()); | ||
expect(client.getAccounts).toHaveBeenCalledTimes(0); | ||
expect(result).toEqual(aztecAddress); | ||
|
||
// returns an address found in the aztec client | ||
const completeAddress = await CompleteAddress.random(); | ||
client.getAccounts.mockResolvedValueOnce([completeAddress]); | ||
const resultWithoutString = await getTxSender(client); | ||
expect(client.getAccounts).toHaveBeenCalled(); | ||
expect(resultWithoutString).toEqual(completeAddress.address); | ||
|
||
// throws when invalid parameter passed | ||
const errorAddr = 'foo'; | ||
await expect( | ||
(async () => { | ||
await getTxSender(client, errorAddr); | ||
})(), | ||
).rejects.toThrow(`Invalid option 'from' passed: ${errorAddr}`); | ||
|
||
// Throws error when no string is passed & no accounts found in RPC | ||
client.getAccounts.mockResolvedValueOnce([]); | ||
await expect( | ||
(async () => { | ||
await getTxSender(client); | ||
})(), | ||
).rejects.toThrow('No accounts found in Aztec RPC instance.'); | ||
}); | ||
|
||
it('Encodes args correctly', () => { | ||
const args = [addr1.toString(), 'false', num.toString(), `${JSON.stringify(fieldArray)}`, JSON.stringify(struct)]; | ||
const result = encodeArgs(args, mockContractAbi.functions[1].parameters); | ||
const exp = [ | ||
addr1.toBigInt(), | ||
false, | ||
33n, | ||
addr1.toBigInt(), | ||
addr2.toBigInt(), | ||
addr3.toBigInt(), | ||
field.toBigInt(), | ||
true, | ||
]; | ||
expect(result).toEqual(exp); | ||
}); | ||
|
||
it('Errors on invalid inputs', () => { | ||
// invalid number of args | ||
const args1 = [field.toString(), 'false']; | ||
expect(() => encodeArgs(args1, mockContractAbi.functions[1].parameters)).toThrow( | ||
'Invalid number of args provided. Expected: 5, received: 2', | ||
); | ||
|
||
// invalid array length | ||
const invalidArray = fieldArray.concat([Fr.random().toString()]); | ||
const args2 = [ | ||
addr1.toString(), | ||
'false', | ||
num.toString(), | ||
`${JSON.stringify(invalidArray)}`, | ||
JSON.stringify(struct), | ||
]; | ||
expect(() => encodeArgs(args2, mockContractAbi.functions[1].parameters)).toThrow( | ||
'Invalid array length passed for arrayParam. Expected 3, received 4.', | ||
); | ||
|
||
// invalid struct | ||
const invalidStruct = { | ||
subField1: Fr.random().toString(), | ||
}; | ||
const args3 = [ | ||
addr1.toString(), | ||
'false', | ||
num.toString(), | ||
`${JSON.stringify(fieldArray)}`, | ||
JSON.stringify(invalidStruct), | ||
]; | ||
expect(() => encodeArgs(args3, mockContractAbi.functions[1].parameters)).toThrow( | ||
'Expected field subField2 not found in struct structParam.', | ||
); | ||
|
||
// invalid bool | ||
const args4 = [ | ||
addr1.toString(), | ||
'foo', | ||
num.toString(), | ||
`${JSON.stringify(fieldArray)}`, | ||
JSON.stringify(invalidStruct), | ||
]; | ||
expect(() => encodeArgs(args4, mockContractAbi.functions[1].parameters)).toThrow( | ||
'Invalid boolean value passed for boolParam: foo.', | ||
); | ||
|
||
// invalid field | ||
const args5 = ['foo', 'false', num.toString(), `${JSON.stringify(fieldArray)}`, JSON.stringify(invalidStruct)]; | ||
expect(() => encodeArgs(args5, mockContractAbi.functions[1].parameters)).toThrow( | ||
'Invalid value passed for fieldParam. Could not parse foo as a field.', | ||
); | ||
|
||
// invalid int | ||
const args6 = [addr1.toString(), 'false', 'foo', `${JSON.stringify(fieldArray)}`, JSON.stringify(invalidStruct)]; | ||
expect(() => encodeArgs(args6, mockContractAbi.functions[1].parameters)).toThrow( | ||
'Invalid value passed for integerParam. Could not parse foo as an integer.', | ||
); | ||
}); | ||
}); |
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
Oops, something went wrong.