Skip to content

Commit

Permalink
fix(contract): always store aci in descriptor, override descr by options
Browse files Browse the repository at this point in the history
  • Loading branch information
davidyuk committed Jul 19, 2022
1 parent 34ae285 commit 278a6ff
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 13 deletions.
27 changes: 15 additions & 12 deletions src/actions/contract.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,18 @@ export async function compile(filename, options) {
function getContractParams({
descrPath, contractAddress, contractSource, contractBytecode, contractAci,
}, { dummySource } = {}) {
let descriptor = {};
if (descrPath && fs.existsSync(resolve(descrPath))) {
const { address, ...other } = JSON.parse(readFile(descrPath).toString());
return { contractAddress: address, ...other };
descriptor = JSON.parse(readFile(resolve(descrPath)).toString());
}
return {
contractAddress,
contractAddress: contractAddress ?? descriptor.address,
// TODO: either remove calldata methods in cli or reconsider getContractInstance requirements
source: (contractSource && readFile(contractSource).toString()) ?? (dummySource && 'invalid-source'),
bytecode: contractBytecode && encode(readFile(contractBytecode, null), 'cb'),
aci: contractAci && JSON.parse(readFile(contractAci).toString()),
...dummySource && { source: 'invalid-source' },
...descriptor,
...contractSource && { source: readFile(contractSource).toString() },
...contractBytecode && { bytecode: encode(readFile(contractBytecode, null), 'cb') },
...contractAci && { aci: JSON.parse(readFile(contractAci).toString()) },
};
}

Expand Down Expand Up @@ -82,16 +84,17 @@ export async function deploy(walletPath, args, options) {
// source file or at location provided in descrPath. Multiple deploy of the same contract
// file will generate different deploy descriptors.
const sdk = await initSdkByWalletFile(walletPath, options);
const descriptor = getContractParams(options);
const contract = await sdk.getContractInstance(descriptor);
const contract = await sdk.getContractInstance(getContractParams(options));
const result = await contract.deploy(args, options);
Object.assign(descriptor, {
address: result.address,
bytecode: contract.bytecode,
});
const filename = options.contractSource ?? options.contractBytecode;
options.descrPath ||= path
.resolve(process.cwd(), `${filename}.deploy.${result.address.slice(3)}.json`);
const descriptor = {
address: result.address,
bytecode: contract.bytecode,
// eslint-disable-next-line no-underscore-dangle
aci: contract._aci,
};
fs.writeFileSync(options.descrPath, JSON.stringify(descriptor, undefined, 2));
if (options.json) print({ ...result, descrPath: options.descrPath });
else {
Expand Down
13 changes: 12 additions & 1 deletion test/contract.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ describe('CLI Contract Module', function contractTests() {
const descriptor = JSON.parse(fs.readFileSync(descrPath, 'utf-8'));
expect(descriptor.address).to.satisfy((b) => b.startsWith('ct_'));
expect(descriptor.bytecode).to.satisfy((b) => b.startsWith('cb_'));
expect(descriptor.source).to.satisfy((b) => b.includes('contract Identity'));
expect(descriptor.aci).to.an('object');
fs.unlinkSync(descrPath);
});

Expand Down Expand Up @@ -143,6 +143,17 @@ describe('CLI Contract Module', function contractTests() {
callResponse.decodedResult.should.be.equal('6');
});

it('overrides descriptor\'s address using --contractAddress', async () => {
await expect(executeContract([
'call',
WALLET_NAME, '--password', 'test',
'--json',
'--contractAddress', 'ct_test',
'--descrPath', deployDescriptorFile,
'test', '[1, 2]',
])).to.be.rejectedWith('Invalid name or address: ct_test');
});

it('calls contract static', async () => {
const callResponse = await executeContract([
'call',
Expand Down

0 comments on commit 278a6ff

Please sign in to comment.