Skip to content

Commit

Permalink
feat: improve usage by extending RawCalldata to RawArgs in all major …
Browse files Browse the repository at this point in the history
…flows
  • Loading branch information
tabaktoni committed May 4, 2023
1 parent 8f28462 commit 0ef1ec8
Show file tree
Hide file tree
Showing 16 changed files with 2,037 additions and 1,938 deletions.
3,664 changes: 1,844 additions & 1,820 deletions __mocks__/Account.json

Large diffs are not rendered by default.

100 changes: 84 additions & 16 deletions __tests__/account.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Signature } from 'micro-starknet';

import typedDataExample from '../__mocks__/typedDataExample.json';
import { Account, Contract, Provider, TransactionStatus, ec, stark } from '../src';
import { Account, Contract, Provider, TransactionStatus, ec, hash, stark } from '../src';
import { uint256 } from '../src/utils/calldata/cairo';
import { parseUDCEvent } from '../src/utils/events';
import { calculateContractAddressFromHash, feeTransactionVersion } from '../src/utils/hash';
Expand Down Expand Up @@ -69,7 +69,79 @@ describe('deploy and test Wallet', () => {
innerInvokeEstFeeSpy.mockClear();
});

describeIfDevnetSequencer('Estimate Fee Bulk on Sequencer', () => {
describeIfDevnetSequencer('Test on Devnet Sequencer', () => {
test('deployAccount with rawArgs - test on devnet', async () => {
const priKey = stark.randomAddress();
const pubKey = ec.starkCurve.getStarkKey(priKey);

const calldata = { publicKey: pubKey };

// declare account
const declareAccount = await account.declare({
contract: compiledOpenZeppelinAccount,
});
const accountClassHash = declareAccount.class_hash;
await account.waitForTransaction(declareAccount.transaction_hash);

// fund new account
const tobeAccountAddress = hash.calculateContractAddressFromHash(
pubKey,
accountClassHash,
calldata,
0
);
const devnetERC20Address =
'0x49D36570D4E46F48E99674BD3FCC84644DDD6B96F7C741B1562B82F9E004DC7';
const { transaction_hash } = await account.execute({
contractAddress: devnetERC20Address,
entrypoint: 'transfer',
calldata: {
recipient: tobeAccountAddress,
amount: uint256(1_000_000_000_000_000),
},
});
await account.waitForTransaction(transaction_hash);

// deploy account
const accountOZ = new Account(provider, tobeAccountAddress, priKey);
const deployed = await accountOZ.deploySelf({
classHash: accountClassHash,
constructorCalldata: calldata,
addressSalt: pubKey,
});
const receipt = await account.waitForTransaction(deployed.transaction_hash);
expect(receipt).toMatchSchemaRef('GetTransactionReceiptResponse');
});

test('deploy with rawArgs', async () => {
const deployment = await account.deploy({
classHash: erc20ClassHash,
constructorCalldata: {
name: 'Token',
symbol: 'ERC20',
recipient: account.address,
},
});
expect(deployment).toMatchSchemaRef('MultiDeployContractResponse');
});

test('multideploy with rawArgs', async () => {
const deployments = await account.deploy([
{
classHash: '0x04367b26fbb92235e8d1137d19c080e6e650a6889ded726d00658411cc1046f5',
},
{
classHash: erc20ClassHash,
constructorCalldata: {
name: 'Token',
symbol: 'ERC20',
recipient: account.address,
},
},
]);
expect(deployments).toMatchSchemaRef('MultiDeployContractResponse');
});

test('estimate fee bulk', async () => {
const innerInvokeEstFeeSpy = jest.spyOn(account.signer, 'signTransaction');
const estimatedFeeBulk = await account.estimateFeeBulk([
Expand Down Expand Up @@ -98,9 +170,7 @@ describe('deploy and test Wallet', () => {
expect(innerInvokeEstFeeSpy.mock.calls[0][1].version).toBe(feeTransactionVersion);
innerInvokeEstFeeSpy.mockClear();
});
});

describeIfDevnetSequencer('Simulate transaction on Sequencer', () => {
test('simulate transaction', async () => {
const innerInvokeEstFeeSpy = jest.spyOn(account.signer, 'signTransaction');
const res = await account.simulateTransaction({
Expand Down Expand Up @@ -396,7 +466,6 @@ describe('deploy and test Wallet', () => {
beforeAll(async () => {
const declareAccount = await account.declare({
contract: compiledOpenZeppelinAccount,
classHash: '0x058d97f7d76e78f44905cc30cb65b91ea49a4b908a76703c54197bca90f81773',
});
accountClassHash = declareAccount.class_hash;
await provider.waitForTransaction(declareAccount.transaction_hash);
Expand All @@ -406,7 +475,7 @@ describe('deploy and test Wallet', () => {
precalculatedAddress = calculateContractAddressFromHash(
starkKeyPub,
accountClassHash,
[starkKeyPub],
{ publicKey: starkKeyPub },
0
);
newAccount = new Account(provider, precalculatedAddress, privateKey);
Expand All @@ -416,7 +485,7 @@ describe('deploy and test Wallet', () => {
const { transaction_hash } = await account.execute({
contractAddress: erc20Address,
entrypoint: 'transfer',
calldata: [precalculatedAddress, '10', '0'],
calldata: [precalculatedAddress, uint256(10)],
});
await provider.waitForTransaction(transaction_hash);

Expand All @@ -425,7 +494,7 @@ describe('deploy and test Wallet', () => {
type: 'DEPLOY_ACCOUNT',
payload: {
classHash: accountClassHash,
constructorCalldata: [starkKeyPub],
constructorCalldata: { publicKey: starkKeyPub },
addressSalt: starkKeyPub,
contractAddress: precalculatedAddress,
},
Expand All @@ -436,12 +505,12 @@ describe('deploy and test Wallet', () => {
{
contractAddress: erc20Address,
entrypoint: 'approve',
calldata: [account.address, '10', '0'],
calldata: { address: account.address, amount: uint256(10) },
},
{
contractAddress: erc20Address,
entrypoint: 'transfer',
calldata: [account.address, '10', '0'],
calldata: [account.address, uint256(10)],
},
],
},
Expand All @@ -465,11 +534,7 @@ describe('deploy and test Wallet', () => {
type: 'DEPLOY',
payload: {
classHash: '0x54328a1075b8820eb43caf0caa233923148c983742402dcfc38541dd843d01a',
constructorCalldata: [
encodeShortString('Token'),
encodeShortString('ERC20'),
account.address,
],
constructorCalldata: ['Token', 'ERC20', account.address],
},
},
{
Expand All @@ -478,7 +543,10 @@ describe('deploy and test Wallet', () => {
{
contractAddress: erc20Address,
entrypoint: 'approve',
calldata: [erc20Address, '10', '0'],
calldata: {
address: erc20Address,
amount: uint256(10),
},
},
{
contractAddress: erc20Address,
Expand Down
8 changes: 1 addition & 7 deletions __tests__/contract.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,13 +208,7 @@ describe('contract module', () => {
});
test('deployment of new contract', async () => {
const factory = new ContractFactory(compiledErc20, classHash, account);
const erc20 = await factory.deploy(
CallData.compile({
name: encodeShortString('Token'),
symbol: encodeShortString('ERC20'),
recipient: wallet,
})
);
const erc20 = await factory.deploy('Token', 'ERC20', wallet);
expect(erc20 instanceof Contract);
});
test('wait for deployment transaction', async () => {
Expand Down
4 changes: 2 additions & 2 deletions __tests__/utils/__snapshots__/stark.test.ts.snap

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion __tests__/utils/hash.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ describe('Hash Tester', () => {
const classHash = computeContractClassHash(compiledOpenZeppelinAccount);

expect(classHash).toMatchInlineSnapshot(
`"0x58d97f7d76e78f44905cc30cb65b91ea49a4b908a76703c54197bca90f81773"`
`"0x36c7e49a16f8fc760a6fbdf71dde543d98be1fee2eda5daff59a0eeae066ed9"`
);
});

Expand Down
4 changes: 2 additions & 2 deletions __tests__/utils/stark.browser.test.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
/**
* @jest-environment jsdom
*/
import fs from 'fs';
import { TextEncoder } from 'util';

import * as constants from '../../src/constants';
import * as json from '../../src/utils/json';
import { compiledOpenZeppelinAccount } from '../fixtures';

const { IS_BROWSER } = constants;

const compiledAccount = json.parse(fs.readFileSync('./__mocks__/Account.json').toString('ascii'));
const compiledAccount = compiledOpenZeppelinAccount;

test('isBrowser', () => {
expect(IS_BROWSER).toBe(true);
Expand Down
17 changes: 12 additions & 5 deletions src/account/default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,8 @@ export class Account extends Provider implements AccountInterface {
return { declare: { ...declare, class_hash }, deploy };
}

public deploySelf = this.deployAccount;

public async deployAccount(
{
classHash,
Expand All @@ -455,14 +457,19 @@ export class Account extends Provider implements AccountInterface {
(await this.getSuggestedMaxFee(
{
type: TransactionType.DEPLOY_ACCOUNT,
payload: { classHash, constructorCalldata, addressSalt, contractAddress },
payload: {
classHash,
constructorCalldata: compiledCalldata,
addressSalt,
contractAddress,
},
},
transactionsDetail
));

const signature = await this.signer.signDeployAccountTransaction({
classHash,
constructorCalldata,
constructorCalldata: compiledCalldata,
contractAddress,
addressSalt,
chainId,
Expand Down Expand Up @@ -542,7 +549,7 @@ export class Account extends Provider implements AccountInterface {
}

/**
* @deprecated will be renamed to buildDeclareContractTransaction
* will be renamed to buildDeclareContractTransaction
*/
public async buildDeclarePayload(
payload: DeclareContractPayload,
Expand Down Expand Up @@ -590,13 +597,13 @@ export class Account extends Provider implements AccountInterface {
version,
nonce,
addressSalt,
constructorCalldata,
constructorCalldata: compiledCalldata,
});

return {
classHash,
addressSalt,
constructorCalldata,
constructorCalldata: compiledCalldata,
signature,
};
}
Expand Down
6 changes: 3 additions & 3 deletions src/provider/rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import { CallData } from '../utils/calldata';
import fetch from '../utils/fetchPonyfill';
import { getSelectorFromName } from '../utils/hash';
import { stringify } from '../utils/json';
import { BigNumberish, bigNumberishArrayToHexadecimalStringArray, toHex } from '../utils/num';
import { BigNumberish, toHex } from '../utils/num';
import { wait } from '../utils/provider';
import { RPCResponseParser } from '../utils/responseParser/rpc';
import { signatureToHexArray } from '../utils/stark';
Expand Down Expand Up @@ -300,7 +300,7 @@ export class RpcProvider implements ProviderInterface {
return this.fetchEndpoint('starknet_estimateFee', {
request: {
type: RPC.TransactionType.DEPLOY_ACCOUNT,
constructor_calldata: bigNumberishArrayToHexadecimalStringArray(constructorCalldata || []),
constructor_calldata: CallData.toHex(constructorCalldata || []),
class_hash: toHex(classHash),
contract_address_salt: toHex(addressSalt || 0),
signature: signatureToHexArray(signature),
Expand Down Expand Up @@ -351,7 +351,7 @@ export class RpcProvider implements ProviderInterface {
): Promise<DeployContractResponse> {
return this.fetchEndpoint('starknet_addDeployAccountTransaction', {
deploy_account_transaction: {
constructor_calldata: bigNumberishArrayToHexadecimalStringArray(constructorCalldata || []),
constructor_calldata: CallData.toHex(constructorCalldata || []),
class_hash: toHex(classHash),
contract_address_salt: toHex(addressSalt || 0),
type: RPC.TransactionType.DEPLOY_ACCOUNT,
Expand Down
8 changes: 4 additions & 4 deletions src/provider/sequencer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ export class SequencerProvider implements ProviderInterface {
// sender_address: contractAddress,
contract_address: contractAddress,
entry_point_selector: getSelectorFromName(entryPointSelector),
calldata,
calldata: CallData.compile(calldata),
}
).then(this.responseParser.parseCallContractResponse);
}
Expand Down Expand Up @@ -445,7 +445,7 @@ export class SequencerProvider implements ProviderInterface {
{
type: TransactionType.INVOKE,
sender_address: invocation.contractAddress,
calldata: invocation.calldata ?? [],
calldata: CallData.compile(invocation.calldata ?? []),
signature: signatureToDecimalArray(invocation.signature),
version: toHex(invocationDetails?.version || 1),
nonce: toHex(invocationDetails.nonce),
Expand Down Expand Up @@ -520,7 +520,7 @@ export class SequencerProvider implements ProviderInterface {
res = {
type: invocation.type,
sender_address: invocation.contractAddress,
calldata: invocation.calldata ?? [],
calldata: CallData.compile(invocation.calldata ?? []),
};
} else if (invocation.type === 'DECLARE') {
res = {
Expand Down Expand Up @@ -648,7 +648,7 @@ export class SequencerProvider implements ProviderInterface {
{
type: 'INVOKE_FUNCTION',
sender_address: invocation.contractAddress,
calldata: invocation.calldata ?? [],
calldata: CallData.compile(invocation.calldata ?? []),
signature: signatureToDecimalArray(invocation.signature),
version: toHex(invocationDetails?.version || 1),
nonce: toHex(invocationDetails.nonce),
Expand Down
3 changes: 2 additions & 1 deletion src/signer/default.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Abi, Call, DeclareSignerDetails, InvocationsSignerDetails, Signature } from '../types';
import { DeployAccountSignerDetails } from '../types/signer';
import { CallData } from '../utils/calldata';
import { starkCurve } from '../utils/ec';
import { buf2hex } from '../utils/encode';
import {
Expand Down Expand Up @@ -65,7 +66,7 @@ export class Signer implements SignerInterface {
const msgHash = calculateDeployAccountTransactionHash(
contractAddress,
classHash,
constructorCalldata,
CallData.compile(constructorCalldata),
addressSalt,
version,
maxFee,
Expand Down
7 changes: 5 additions & 2 deletions src/types/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ export type UniversalDeployerContractPayload = {
constructorCalldata?: RawArgs;
};

/**
* @deprecated deprecated due to no direct deploy, unused - can be removed
*/
export type DeployContractPayload = {
contract: CompiledContract | string;
constructorCalldata?: RawCalldata;
Expand All @@ -46,7 +49,7 @@ export type DeployContractPayload = {

export type DeployAccountContractPayload = {
classHash: string;
constructorCalldata?: RawCalldata;
constructorCalldata?: RawArgs;
addressSalt?: BigNumberish;
contractAddress?: string;
};
Expand Down Expand Up @@ -84,7 +87,7 @@ export type DeclareContractTransaction = {

export type CallDetails = {
contractAddress: string;
calldata?: RawCalldata;
calldata?: RawArgs;
};

export type Invocation = CallDetails & { signature?: Signature };
Expand Down
Loading

0 comments on commit 0ef1ec8

Please sign in to comment.