Skip to content

Commit

Permalink
feat: wallet deploy contract and patches
Browse files Browse the repository at this point in the history
  • Loading branch information
tabaktoni committed Oct 18, 2023
1 parent 9921406 commit dbf53b6
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 85 deletions.
95 changes: 54 additions & 41 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 3 additions & 37 deletions src/account/default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ import {
} from '../types';
import { CallData } from '../utils/calldata';
import { extractContractHashes, isSierra } from '../utils/contract';
import { starkCurve } from '../utils/ec';
import { parseUDCEvent } from '../utils/events';
import {
calculateContractAddressFromHash,
Expand All @@ -55,8 +54,8 @@ import {
} from '../utils/hash';
import { toBigInt, toCairoBool } from '../utils/num';
import { parseContract } from '../utils/provider';
import { estimatedFeeToMaxFee, formatSignature, randomAddress } from '../utils/stark';
import { getExecuteCalldata } from '../utils/transaction';
import { estimatedFeeToMaxFee, formatSignature } from '../utils/stark';
import { buildUDCCall, getExecuteCalldata } from '../utils/transaction';
import { getMessageHash } from '../utils/typedData';
import { AccountInterface } from './interface';

Expand Down Expand Up @@ -366,40 +365,7 @@ export class Account extends Provider implements AccountInterface {
payload: UniversalDeployerContractPayload | UniversalDeployerContractPayload[],
details?: InvocationsDetails | undefined
): Promise<MultiDeployContractResponse> {
const params = [].concat(payload as []).map((it) => {
const {
classHash,
salt,
unique = true,
constructorCalldata = [],
} = it as UniversalDeployerContractPayload;

const compiledConstructorCallData = CallData.compile(constructorCalldata);
const deploySalt = salt ?? randomAddress();

return {
call: {
contractAddress: UDC.ADDRESS,
entrypoint: UDC.ENTRYPOINT,
calldata: [
classHash,
deploySalt,
toCairoBool(unique),
compiledConstructorCallData.length,
...compiledConstructorCallData,
],
},
address: calculateContractAddressFromHash(
unique ? starkCurve.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 { calls, addresses } = buildUDCCall(payload, this.address);
const invokeResponse = await this.execute(calls, undefined, details);

return {
Expand Down
21 changes: 17 additions & 4 deletions src/account/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
} from 'get-starknet-core';

import { StarknetChainId } from '../constants';
import { buildUDCCall } from '../utils/transaction';
// eslint-disable-next-line import/no-cycle
import {
AllowArray,
Expand Down Expand Up @@ -146,6 +147,11 @@ export class WalletAccount /* implements AccountInterface */ {
abi: json.stringify(pContract.abi),
};

// Check FIx
if (!declareContractPayload.compiledClassHash) {
throw Error('compiledClassHash is required');
}

const rpcCall: RpcCall = {
type: 'starknet_addDeclareTransaction',
params: {
Expand All @@ -159,16 +165,23 @@ export class WalletAccount /* implements AccountInterface */ {
public async deploy(
payload: UniversalDeployerContractPayload | UniversalDeployerContractPayload[]
): Promise<MultiDeployContractResponse> {
// TODO: Create UDC PRocedure using invoke()
return new Promise((e) => false);
const { calls, addresses } = buildUDCCall(payload, this.address);
const invokeResponse = await this.execute(calls);

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

public async deployAccount(payload: DeployAccountContractPayload) {
const rpcCall: RpcCall = {
type: 'starknet_addDeployAccountTransaction',
params: {
contract_address_salt: payload.addressSalt?.toString(),
constructor_calldata: CallData.compile(payload.constructorCalldata),
contract_address_salt: payload.addressSalt?.toString() || '0',
constructor_calldata: payload.constructorCalldata
? CallData.compile(payload.constructorCalldata)
: [],
class_hash: payload.classHash,
},
};
Expand Down
59 changes: 56 additions & 3 deletions src/utils/transaction.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
import { BigNumberish, CairoVersion, Call, CallStruct, Calldata, ParsedStruct } from '../types';
import { UDC } from '../constants';
import {
BigNumberish,
CairoVersion,
Call,
CallStruct,
Calldata,
ParsedStruct,
UniversalDeployerContractPayload,
} from '../types';
import { CallData } from './calldata';
import { getSelectorFromName } from './hash';
import { toBigInt } from './num';
import { starkCurve } from './ec';
import { calculateContractAddressFromHash, getSelectorFromName } from './hash';
import { toBigInt, toCairoBool } from './num';
import { randomAddress } from './stark';

/**
* Transforms a list of Calls, each with their own calldata, into
Expand Down Expand Up @@ -81,3 +92,45 @@ export const getExecuteCalldata = (calls: Call[], cairoVersion: CairoVersion = '
}
return fromCallsToExecuteCalldata(calls);
};

export function buildUDCCall(
payload: UniversalDeployerContractPayload | UniversalDeployerContractPayload[],
address: string
) {
const params = [].concat(payload as []).map((it) => {
const {
classHash,
salt,
unique = true,
constructorCalldata = [],
} = it as UniversalDeployerContractPayload;

const compiledConstructorCallData = CallData.compile(constructorCalldata);
const deploySalt = salt ?? randomAddress();

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

return {
calls: params.map((it) => it.call),
addresses: params.map((it) => it.address),
};
}

0 comments on commit dbf53b6

Please sign in to comment.