Skip to content

Commit

Permalink
fix(contract): detect if ACI doesn't match called contract
Browse files Browse the repository at this point in the history
  • Loading branch information
davidyuk committed Apr 11, 2024
1 parent 82e5794 commit 8c4287f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/contract/Contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,9 @@ class Contract<M extends ContractMethodsBase> {
break;
case 'error':
message = decode(returnValue).toString();
if (/Expected \d+ arguments, got \d+/.test(message)) {
throw new ContractError(`ACI doesn't match called contract. Error provided by node: ${message}`);
}
break;
default:
throw new InternalError(`Unknown return type: ${returnType}`);
Expand Down
13 changes: 13 additions & 0 deletions test/integration/contract-aci.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
NoSuchContractFunctionError,
ConsensusProtocolVersion,
InvalidTxError,
ContractError,
} from '../../src';
import { getSdk } from '.';
import {
Expand Down Expand Up @@ -362,6 +363,18 @@ describe('Contract instance', () => {
expect((await contract.intFn(3)).decodedResult).to.be.equal(3n);
});

it('fails if aci doesn\'t match called contract', async () => {
const aci = structuredClone(testContractAci);
const fn = aci.at(-1)?.contract?.functions.find(({ name }) => name === 'intFn');
assertNotNull(fn);
fn.arguments.push(fn.arguments[0]);
const contract = await aeSdk.initializeContract<{
intFn: (a: InputNumber, b: InputNumber) => bigint;
}>({ aci, address: testContract.$options.address });
await expect(contract.intFn(3, 2)).to.be
.rejectedWith(ContractError, 'ACI doesn\'t match called contract. Error provided by node: Expected 1 arguments, got 2');
});

it('deploys and calls by bytecode and aci', async () => {
const contract = await aeSdk.initializeContract<TestContractApi>(
{ bytecode: testContractBytecode, aci: testContractAci },
Expand Down

0 comments on commit 8c4287f

Please sign in to comment.