Skip to content

Commit

Permalink
feat: new signer and provider interface
Browse files Browse the repository at this point in the history
  • Loading branch information
janek26 committed Feb 9, 2022
1 parent be2ec9e commit 4b2a71c
Show file tree
Hide file tree
Showing 18 changed files with 427 additions and 305 deletions.
3 changes: 2 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"plugins": ["@typescript-eslint"],
"rules": {
"import/prefer-default-export": 0,
"@typescript-eslint/naming-convention": 0
"@typescript-eslint/naming-convention": 0,
"@typescript-eslint/no-unused-vars": ["error", { "argsIgnorePattern": "^_" }]
}
}
17 changes: 8 additions & 9 deletions __tests__/account.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,24 +49,23 @@ describe('deploy and test Wallet', () => {
let erc20: Contract;
let erc20Address: string;
beforeAll(async () => {
const { code: codeErc20, address: erc20AddressLocal } = await defaultProvider.deployContract(
compiledErc20,
[]
);
const { code: codeErc20, address: erc20AddressLocal } = await defaultProvider.deployContract({
contract: compiledErc20,
});
erc20Address = erc20AddressLocal;
erc20 = new Contract(compiledErc20.abi, erc20Address);

expect(codeErc20).toBe('TRANSACTION_RECEIVED');

const { code, address: walletAddressLocal } = await defaultProvider.deployContract(
compiledArgentAccount,
compileCalldata({
const { code, address: walletAddressLocal } = await defaultProvider.deployContract({
contract: compiledArgentAccount,
constructorCalldata: compileCalldata({
signer: starkKeyPub,
guardian: '0',
L1_address: '0',
}),
starkKeyPub
);
addressSalt: starkKeyPub,
});
walletAddress = walletAddressLocal;
wallet = new Contract(compiledArgentAccount.abi, walletAddress);
expect(code).toBe('TRANSACTION_RECEIVED');
Expand Down
4 changes: 3 additions & 1 deletion __tests__/contract.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ describe('class Contract {}', () => {
code,
transaction_hash,
address: erc20address,
} = await defaultProvider.deployContract(compiledERC20, []);
} = await defaultProvider.deployContract({
contract: compiledERC20,
});

contract = new Contract(compiledERC20.abi, erc20address);

Expand Down
9 changes: 9 additions & 0 deletions __tests__/jest.setup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import axios from 'axios';
import * as AxiosLogger from 'axios-logger';

jest.setTimeout(50 * 60 * 1000);

if (process.env.DEBUG === 'true') {
axios.interceptors.request.use(AxiosLogger.requestLogger, AxiosLogger.errorLogger);
axios.interceptors.response.use(AxiosLogger.responseLogger, AxiosLogger.errorLogger);
}
36 changes: 6 additions & 30 deletions __tests__/provider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@ describe('defaultProvider', () => {
test('callContract()', () => {
return expect(
defaultProvider.callContract({
contract_address: '0x9ff64f4ab0e1fe88df4465ade98d1ea99d5732761c39279b8e1374fa943e9b',
entry_point_selector: stark.getSelectorFromName('balance_of'),
contractAddress: '0x9ff64f4ab0e1fe88df4465ade98d1ea99d5732761c39279b8e1374fa943e9b',
entrypoint: 'balance_of',
calldata: compileCalldata({
user: '0x9ff64f4ab0e1fe88df4465ade98d1ea99d5732761c39279b8e1374fa943e9b',
}),
Expand All @@ -99,45 +99,21 @@ describe('defaultProvider', () => {
});

describe('addTransaction()', () => {
test('type: "DEPLOY"', async () => {
test('deployContract()', async () => {
const inputContract = compiledArgentAccount as unknown as CompiledContract;

const contractDefinition = {
...inputContract,
program: stark.compressProgram(inputContract.program),
};

const response = await defaultProvider.addTransaction({
type: 'DEPLOY',
contract_address_salt: stark.randomAddress(),
constructor_calldata: compileCalldata({
const response = await defaultProvider.deployContract({
contract: inputContract,
constructorCalldata: compileCalldata({
signer: stark.randomAddress(),
guardian: '0',
L1_address: '0',
}),
contract_definition: contractDefinition,
});

expect(response.code).toBe('TRANSACTION_RECEIVED');
expect(response.transaction_hash).toBeDefined();
expect(response.address).toBeDefined();
});

test('deployContract()', async () => {
const inputContract = compiledArgentAccount as unknown as CompiledContract;

const response = await defaultProvider.deployContract(
inputContract,
compileCalldata({
signer: stark.randomAddress(),
guardian: '0',
L1_address: '0',
})
);

expect(response.code).toBe('TRANSACTION_RECEIVED');
expect(response.transaction_hash).toBeDefined();
expect(response.address).toBeDefined();
});
});
});
46 changes: 22 additions & 24 deletions __tests__/signer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
number,
stark,
} from '../src';
import { toBN } from '../src/utils/number';

const compiledArgentAccount: CompiledContract = json.parse(
fs.readFileSync('./__mocks__/ArgentAccount.json').toString('ascii')
Expand All @@ -31,24 +32,23 @@ describe('deploy and test Wallet', () => {
let signer: Signer;

beforeAll(async () => {
const { code: codeErc20, address: erc20AddressLocal } = await defaultProvider.deployContract(
compiledErc20,
[]
);
const { code: codeErc20, address: erc20AddressLocal } = await defaultProvider.deployContract({
contract: compiledErc20,
});
erc20Address = erc20AddressLocal;
erc20 = new Contract(compiledErc20.abi, erc20Address);

expect(codeErc20).toBe('TRANSACTION_RECEIVED');

const { code, address: walletAddressLocal } = await defaultProvider.deployContract(
compiledArgentAccount,
compileCalldata({
const { code, address: walletAddressLocal } = await defaultProvider.deployContract({
contract: compiledArgentAccount,
constructorCalldata: compileCalldata({
signer: starkKeyPub,
guardian: '0',
L1_address: '0',
}),
starkKeyPub
);
addressSalt: starkKeyPub,
});
walletAddress = walletAddressLocal;
expect(code).toBe('TRANSACTION_RECEIVED');

Expand All @@ -68,8 +68,8 @@ describe('deploy and test Wallet', () => {
});
test('read nonce', async () => {
const { result } = await signer.callContract({
contract_address: signer.address,
entry_point_selector: stark.getSelectorFromName('get_nonce'),
contractAddress: signer.address,
entrypoint: 'get_nonce',
});
const nonce = result[0];

Expand All @@ -83,11 +83,10 @@ describe('deploy and test Wallet', () => {
expect(number.toBN(res as string).toString()).toStrictEqual(number.toBN(1000).toString());
});
test('execute by wallet owner', async () => {
const { code, transaction_hash } = await signer.addTransaction({
type: 'INVOKE_FUNCTION',
contract_address: erc20Address,
entry_point_selector: stark.getSelectorFromName('transfer'),
calldata: [erc20Address, '10'],
const { code, transaction_hash } = await signer.invokeFunction({
contractAddress: erc20Address,
entrypoint: 'transfer',
calldata: [toBN(erc20Address).toString(), '10'],
});

expect(code).toBe('TRANSACTION_RECEIVED');
Expand All @@ -102,15 +101,14 @@ describe('deploy and test Wallet', () => {
});
test('execute with custom nonce', async () => {
const { result } = await signer.callContract({
contract_address: signer.address,
entry_point_selector: stark.getSelectorFromName('get_nonce'),
contractAddress: signer.address,
entrypoint: 'get_nonce',
});
const nonce = parseInt(result[0], 10);
const { code, transaction_hash } = await signer.addTransaction({
type: 'INVOKE_FUNCTION',
contract_address: erc20Address,
entry_point_selector: stark.getSelectorFromName('transfer'),
calldata: [erc20Address, '10'],
const nonce = toBN(result[0]).toNumber();
const { code, transaction_hash } = await signer.invokeFunction({
contractAddress: erc20Address,
entrypoint: 'transfer',
calldata: [toBN(erc20Address).toString(), '10'],
nonce,
});

Expand Down
35 changes: 35 additions & 0 deletions package-lock.json

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

8 changes: 7 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
"@types/url-join": "^4.0.1",
"@typescript-eslint/eslint-plugin": "^5.0.0",
"@typescript-eslint/parser": "^5.0.0",
"axios-logger": "^2.6.0",
"eslint": "^7.32.0",
"eslint-config-airbnb-base": "^14.2.1",
"eslint-config-airbnb-typescript": "^14.0.1",
Expand Down Expand Up @@ -76,7 +77,12 @@
"*.{ts,js,md,yml,json}": "prettier --write"
},
"jest": {
"testTimeout": 800000
"testMatch": [
"**/__tests__/**/(*.)+(spec|test).[jt]s?(x)"
],
"setupFilesAfterEnv": [
"./__tests__/jest.setup.ts"
]
},
"importSort": {
".js, .jsx, .ts, .tsx": {
Expand Down
19 changes: 7 additions & 12 deletions src/contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,12 @@ import assert from 'minimalistic-assert';

import { Provider, defaultProvider } from './provider';
import { BlockIdentifier } from './provider/utils';
import { Abi, AbiEntry, FunctionAbi, Signature, StructAbi } from './types';
import { Abi, AbiEntry, FunctionAbi, RawCalldata, Signature, StructAbi } from './types';
import { BigNumberish, toBN } from './utils/number';
import { getSelectorFromName } from './utils/stark';

export type Args = {
[inputName: string]: string | string[] | { type: 'struct'; [k: string]: BigNumberish };
};
export type Calldata = string[];

function parseFelt(candidate: string): BN {
try {
Expand All @@ -29,7 +27,7 @@ function isFelt(candidate: string): boolean {
}
}

export function compileCalldata(args: Args): Calldata {
export function compileCalldata(args: Args): RawCalldata {
return Object.values(args).flatMap((value) => {
if (Array.isArray(value))
return [toBN(value.length).toString(), ...value.map((x) => toBN(x).toString())];
Expand Down Expand Up @@ -155,15 +153,13 @@ export class Contract {
this.validateMethodAndArgs('INVOKE', method, args);

// compile calldata
const entrypointSelector = getSelectorFromName(method);
const calldata = compileCalldata(args);

return this.provider.addTransaction({
type: 'INVOKE_FUNCTION',
contract_address: this.connectedTo,
return this.provider.invokeFunction({
contractAddress: this.connectedTo,
signature,
calldata,
entry_point_selector: entrypointSelector,
entrypoint: method,
});
}

Expand All @@ -175,15 +171,14 @@ export class Contract {
this.validateMethodAndArgs('CALL', method, args);

// compile calldata
const entrypointSelector = getSelectorFromName(method);
const calldata = compileCalldata(args);

return this.provider
.callContract(
{
contract_address: this.connectedTo,
contractAddress: this.connectedTo,
entrypoint: method,
calldata,
entry_point_selector: entrypointSelector,
},
blockIdentifier
)
Expand Down
Loading

0 comments on commit 4b2a71c

Please sign in to comment.