Skip to content

Commit

Permalink
feat: return precalculated address from deploy
Browse files Browse the repository at this point in the history
  • Loading branch information
tabaktoni committed Dec 6, 2022
1 parent 9d85e25 commit 5e40224
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 22 deletions.
30 changes: 27 additions & 3 deletions __tests__/account.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { isBN } from 'bn.js';

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 { encodeShortString } from '../src/utils/shortString';
Expand Down Expand Up @@ -265,7 +266,7 @@ describe('deploy and test Wallet', () => {
expect(deployResponse.salt).toBeDefined();
});

test('UDC Deploy', async () => {
test('UDC Deploy unique', async () => {
const salt = randomAddress(); // use random salt

const deployment = await account.deploy({
Expand All @@ -276,12 +277,35 @@ describe('deploy and test Wallet', () => {
account.address,
],
salt,
unique: true, // Using true here so as not to clash with normal erc20 deploy in account and provider test
unique: true,
});
expect(deployment).toHaveProperty('transaction_hash');

// check pre-calculated address
const txReceipt = await provider.waitForTransaction(deployment.transaction_hash);
const udcEvent = parseUDCEvent(txReceipt);
expect(deployment.contract_address).toBe(udcEvent.contract_address);
});

await provider.waitForTransaction(deployment.transaction_hash);
test('UDC Deploy non-unique', async () => {
const salt = randomAddress(); // use random salt

const deployment = await account.deploy({
classHash: erc20ClassHash,
constructorCalldata: [
encodeShortString('Token'),
encodeShortString('ERC20'),
account.address,
],
salt,
unique: false,
});
expect(deployment).toHaveProperty('transaction_hash');

// check pre-calculated address
const txReceipt = await provider.waitForTransaction(deployment.transaction_hash);
const udcEvent = parseUDCEvent(txReceipt);
expect(deployment.contract_address).toBe(udcEvent.contract_address);
});
});
});
49 changes: 30 additions & 19 deletions src/account/default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,15 @@ import {
InvocationsSignerDetails,
InvokeFunctionResponse,
KeyPair,
MultiDeployContractResponse,
Signature,
UniversalDeployerContractPayload,
} from '../types';
import { parseUDCEvent } from '../utils/events';
import {
calculateContractAddressFromHash,
feeTransactionVersion,
pedersen,
transactionVersion,
} from '../utils/hash';
import { BigNumberish, hexToDecimalString, toBN, toCairoBool } from '../utils/number';
Expand Down Expand Up @@ -329,29 +331,39 @@ export class Account extends Provider implements AccountInterface {
additionalCalls = [],
}: UniversalDeployerContractPayload,
invocationsDetails: InvocationsDetails = {}
): Promise<InvokeFunctionResponse> {
): Promise<DeployContractResponse | MultiDeployContractResponse> {
const compiledConstructorCallData = compileCalldata(constructorCalldata);
const callsArray = Array.isArray(additionalCalls) ? additionalCalls : [additionalCalls];
const deploySalt = salt ?? randomAddress();

return this.execute(
[
{
contractAddress: UDC.ADDRESS,
entrypoint: UDC.ENTRYPOINT,
calldata: [
classHash,
deploySalt,
toCairoBool(unique),
compiledConstructorCallData.length,
...compiledConstructorCallData,
],
},
...callsArray,
],
undefined,
invocationsDetails
const calls = [
{
contractAddress: UDC.ADDRESS,
entrypoint: UDC.ENTRYPOINT,
calldata: [
classHash,
deploySalt,
toCairoBool(unique),
compiledConstructorCallData.length,
...compiledConstructorCallData,
],
},
...callsArray,
];

const invokeResponse = await this.execute(calls, undefined, invocationsDetails);

const calculated_address = calculateContractAddressFromHash(
unique ? pedersen([this.address, deploySalt]) : deploySalt,
classHash,
compiledConstructorCallData,
unique ? UDC.ADDRESS : 0
);

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

public async deployContract(
Expand Down Expand Up @@ -385,7 +397,6 @@ export class Account extends Provider implements AccountInterface {
return { declare: { ...declare, class_hash: classHash }, deploy };
}


public async deployAccount(
{
classHash,
Expand Down
5 changes: 5 additions & 0 deletions src/types/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ export interface DeployContractResponse {
transaction_hash: string;
}

export interface MultiDeployContractResponse {
contract_address: Array<string>;
transaction_hash: string;
}

export interface DeployContractUDCResponse extends DeployContractResponse {
address: string;
deployer: string;
Expand Down

0 comments on commit 5e40224

Please sign in to comment.