Skip to content

Commit

Permalink
feat: multi deploy reimplemented
Browse files Browse the repository at this point in the history
  • Loading branch information
tabaktoni committed Dec 6, 2022
1 parent 5e40224 commit 10e609a
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 74 deletions.
26 changes: 23 additions & 3 deletions __tests__/account.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import typedDataExample from '../__mocks__/typedDataExample.json';
import { Account, Contract, Provider, number, stark } from '../src';
import { parseUDCEvent } from '../src/utils/events';
import { feeTransactionVersion } from '../src/utils/hash';
import { hexToDecimalString, toBN } from '../src/utils/number';
import { cleanHex, hexToDecimalString, toBN } from '../src/utils/number';
import { encodeShortString } from '../src/utils/shortString';
import { randomAddress } from '../src/utils/stark';
import {
Expand Down Expand Up @@ -284,7 +284,7 @@ describe('deploy and test Wallet', () => {
// check pre-calculated address
const txReceipt = await provider.waitForTransaction(deployment.transaction_hash);
const udcEvent = parseUDCEvent(txReceipt);
expect(deployment.contract_address).toBe(udcEvent.contract_address);
expect(cleanHex(deployment.contract_address[0])).toBe(cleanHex(udcEvent.contract_address));
});

test('UDC Deploy non-unique', async () => {
Expand All @@ -305,7 +305,27 @@ describe('deploy and test Wallet', () => {
// check pre-calculated address
const txReceipt = await provider.waitForTransaction(deployment.transaction_hash);
const udcEvent = parseUDCEvent(txReceipt);
expect(deployment.contract_address).toBe(udcEvent.contract_address);
expect(cleanHex(deployment.contract_address[0])).toBe(cleanHex(udcEvent.contract_address));
});

test('UDC multi Deploy', async () => {
// TODO: add test with multiple deploys
const deployments = await account.deploy([
{
classHash: '0x04367b26fbb92235e8d1137d19c080e6e650a6889ded726d00658411cc1046f5',
},
{
classHash: erc20ClassHash,
constructorCalldata: [
encodeShortString('Token'),
encodeShortString('ERC20'),
account.address,
],
},
]);
expect(deployments).toHaveProperty('transaction_hash');
expect(deployments.contract_address[0]).toBeDefined();
expect(deployments.contract_address[1]).toBeDefined();
});
});
});
132 changes: 62 additions & 70 deletions src/account/default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,35 +220,32 @@ export class Account extends Provider implements AccountInterface {
}

public async estimateDeployFee(
{
classHash,
salt = '0',
unique = true,
constructorCalldata = [],
additionalCalls = [],
}: UniversalDeployerContractPayload,
payload: UniversalDeployerContractPayload | UniversalDeployerContractPayload[],
transactionsDetail?: InvocationsDetails | undefined
): Promise<EstimateFee> {
const compiledConstructorCallData = compileCalldata(constructorCalldata);
const calls = [].concat(payload as []).map((it) => {
const {
classHash,
salt = '0',
unique = true,
constructorCalldata = [],
} = it as UniversalDeployerContractPayload;
const compiledConstructorCallData = compileCalldata(constructorCalldata);

return {
contractAddress: UDC.ADDRESS,
entrypoint: UDC.ENTRYPOINT,
calldata: [
classHash,
salt,
toCairoBool(unique),
compiledConstructorCallData.length,
...compiledConstructorCallData,
],
};
});

const callsArray = Array.isArray(additionalCalls) ? additionalCalls : [additionalCalls];
return this.estimateInvokeFee(
[
{
contractAddress: UDC.ADDRESS,
entrypoint: UDC.ENTRYPOINT,
calldata: [
classHash,
salt,
toCairoBool(unique),
compiledConstructorCallData.length,
...compiledConstructorCallData,
],
},
...callsArray,
],
transactionsDetail
);
return this.estimateInvokeFee(calls, transactionsDetail);
}

public async execute(
Expand Down Expand Up @@ -323,46 +320,48 @@ export class Account extends Provider implements AccountInterface {
}

public async deploy(
{
classHash,
salt,
unique = true,
constructorCalldata = [],
additionalCalls = [],
}: UniversalDeployerContractPayload,
invocationsDetails: InvocationsDetails = {}
): Promise<DeployContractResponse | MultiDeployContractResponse> {
const compiledConstructorCallData = compileCalldata(constructorCalldata);
const callsArray = Array.isArray(additionalCalls) ? additionalCalls : [additionalCalls];
const deploySalt = salt ?? randomAddress();

const calls = [
{
contractAddress: UDC.ADDRESS,
entrypoint: UDC.ENTRYPOINT,
calldata: [
payload: UniversalDeployerContractPayload | UniversalDeployerContractPayload[],
details: InvocationsDetails = {}
): Promise<MultiDeployContractResponse> {
const params = [].concat(payload as []).map((it) => {
const {
classHash,
salt,
unique = true,
constructorCalldata = [],
} = it as UniversalDeployerContractPayload;

const compiledConstructorCallData = compileCalldata(constructorCalldata);
const deploySalt = salt ?? randomAddress();

return {
call: {
contractAddress: UDC.ADDRESS,
entrypoint: UDC.ENTRYPOINT,
calldata: [
classHash,
deploySalt,
toCairoBool(unique),
compiledConstructorCallData.length,
...compiledConstructorCallData,
],
},
address: calculateContractAddressFromHash(
unique ? pedersen([this.address, deploySalt]) : deploySalt,
classHash,
deploySalt,
toCairoBool(unique),
compiledConstructorCallData.length,
...compiledConstructorCallData,
],
},
...callsArray,
];

const invokeResponse = await this.execute(calls, undefined, invocationsDetails);
compiledConstructorCallData,
unique ? UDC.ADDRESS : 0
),
};
});

const calculated_address = calculateContractAddressFromHash(
unique ? pedersen([this.address, deploySalt]) : deploySalt,
classHash,
compiledConstructorCallData,
unique ? UDC.ADDRESS : 0
);
const calls = params.map((it) => it.call);
const addresses = params.map((it) => it.address);
const invokeResponse = await this.execute(calls, undefined, details);

return {
...invokeResponse,
contract_address: calculated_address,
contract_address: addresses,
};
}

Expand All @@ -378,20 +377,13 @@ export class Account extends Provider implements AccountInterface {
}

public async declareDeploy(
{
classHash,
contract,
constructorCalldata,
salt,
unique,
additionalCalls,
}: DeclareDeployContractPayload,
{ classHash, contract, constructorCalldata, salt, unique }: DeclareDeployContractPayload,
details?: InvocationsDetails
) {
const { transaction_hash } = await this.declare({ contract, classHash }, details);
const declare = await this.waitForTransaction(transaction_hash, undefined, ['ACCEPTED_ON_L2']);
const deploy = await this.deployContract(
{ classHash, salt, unique, constructorCalldata, additionalCalls },
{ classHash, salt, unique, constructorCalldata },
details
);
return { declare: { ...declare, class_hash: classHash }, deploy };
Expand Down
1 change: 0 additions & 1 deletion src/types/lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ export type UniversalDeployerContractPayload = {
salt?: string;
unique?: boolean;
constructorCalldata?: RawArgs;
additionalCalls?: AllowArray<Call>; // support multicall
};

export type DeployContractPayload = {
Expand Down

0 comments on commit 10e609a

Please sign in to comment.