Skip to content

Commit

Permalink
fix: supply calldata as hexadecimal string array
Browse files Browse the repository at this point in the history
Co-authored-by: dkillen <david@davidkillen.net>
  • Loading branch information
ivpavici and dkillen committed Sep 1, 2022
1 parent f4fab69 commit 44cb4c5
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 6 deletions.
31 changes: 29 additions & 2 deletions __tests__/rpcProvider.test.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,44 @@
import { RpcProvider } from '../src';
import { describeIfRpc, getTestProvider } from './fixtures';
import { Account, RpcProvider, ec } from '../src';
import {
compiledOpenZeppelinAccount,
describeIfRpc,
getTestAccount,
getTestProvider,
} from './fixtures';

describeIfRpc('RPCProvider', () => {
let rpcProvider: RpcProvider;
let accountPublicKey: string;

beforeAll(async () => {
rpcProvider = getTestProvider() as RpcProvider;
const account = getTestAccount(rpcProvider);

expect(account).toBeInstanceOf(Account);

const accountKeyPair = ec.genKeyPair();
accountPublicKey = ec.getStarkKey(accountKeyPair);
});

describe('RPC methods', () => {
test('getChainId', async () => {
const chainId = await rpcProvider.getChainId();
expect(chainId).toBe('0x534e5f474f45524c49');
});

test('deployContract', async () => {
const { contract_address, transaction_hash } = await rpcProvider.deployContract({
contract: compiledOpenZeppelinAccount,
constructorCalldata: [accountPublicKey],
addressSalt: accountPublicKey,
});
await rpcProvider.waitForTransaction(transaction_hash);
expect(contract_address).toBeTruthy();
expect(transaction_hash).toBeTruthy();
});

test.todo('getEstimateFee');

test.todo('invokeFunction');
});
});
13 changes: 9 additions & 4 deletions src/provider/rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,12 @@ import { RPC } from '../types/api';
import fetch from '../utils/fetchPonyfill';
import { getSelectorFromName } from '../utils/hash';
import { stringify } from '../utils/json';
import { BigNumberish, bigNumberishArrayToDecimalStringArray, toBN, toHex } from '../utils/number';
import {
BigNumberish,
bigNumberishArrayToHexadecimalStringArray,
toBN,
toHex,
} from '../utils/number';
import { parseCalldata, parseContract, wait } from '../utils/provider';
import { RPCResponseParser } from '../utils/responseParser/rpc';
import { randomAddress } from '../utils/stark';
Expand Down Expand Up @@ -166,7 +171,7 @@ export class RpcProvider implements ProviderInterface {
contract_address: invocation.contractAddress,
entry_point_selector: getSelectorFromName(invocation.entrypoint),
calldata: parseCalldata(invocation.calldata),
signature: bigNumberishArrayToDecimalStringArray(invocation.signature || []),
signature: bigNumberishArrayToHexadecimalStringArray(invocation.signature || []),
version: toHex(toBN(invocationDetails?.version || 0)),
},
blockIdentifier,
Expand Down Expand Up @@ -197,7 +202,7 @@ export class RpcProvider implements ProviderInterface {

return this.fetchEndpoint('starknet_addDeployTransaction', [
addressSalt ?? randomAddress(),
bigNumberishArrayToDecimalStringArray(constructorCalldata ?? []),
bigNumberishArrayToHexadecimalStringArray(constructorCalldata ?? []),
{
program: contractDefinition.program,
entry_points_by_type: contractDefinition.entry_points_by_type,
Expand All @@ -215,7 +220,7 @@ export class RpcProvider implements ProviderInterface {
entry_point_selector: getSelectorFromName(functionInvocation.entrypoint),
calldata: parseCalldata(functionInvocation.calldata),
},
bigNumberishArrayToDecimalStringArray(functionInvocation.signature || []),
bigNumberishArrayToHexadecimalStringArray(functionInvocation.signature || []),
toHex(toBN(details.maxFee || 0)),
toHex(toBN(details.version || 0)),
]).then(this.responseParser.parseInvokeFunctionResponse);
Expand Down
4 changes: 4 additions & 0 deletions src/utils/number.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,7 @@ export function assertInRange(
export function bigNumberishArrayToDecimalStringArray(rawCalldata: BigNumberish[]): string[] {
return rawCalldata.map((x) => toBN(x).toString(10));
}

export function bigNumberishArrayToHexadecimalStringArray(rawCalldata: BigNumberish[]): string[] {
return rawCalldata.map((x) => toHex(toBN(x)));
}
6 changes: 6 additions & 0 deletions www/docs/API/utils.md
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,12 @@ const signature = await this.signer.signTransaction(transactions, signerDetails)
}
```

### bigNumberishArrayToHexadecimalStringArray

`bigNumberishArrayToHexadecimalStringArray(rawCalldata: BigNumberish[]): string[]`

Convert BigNumberish array to hexadecimal string array. Used for signature conversion.

<hr />

## **uint256**
Expand Down

0 comments on commit 44cb4c5

Please sign in to comment.