Skip to content

Commit a018b92

Browse files
authored
Merge pull request #23 from seanjameshan/feature/allow-invokeFunction-transactions
feat: enable invokeFunction transactions
2 parents 4214294 + d349968 commit a018b92

File tree

14 files changed

+33413
-257
lines changed

14 files changed

+33413
-257
lines changed

.eslintrc

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"node": true,
66
"jest": true
77
},
8-
"extends": ["airbnb-base", "airbnb-typescript/base", "prettier", "plugin:prettier/recommended", ],
8+
"extends": ["airbnb-base", "airbnb-typescript/base", "prettier", "plugin:prettier/recommended"],
99
"globals": {
1010
"Atomics": "readonly",
1111
"SharedArrayBuffer": "readonly"
@@ -16,5 +16,9 @@
1616
"sourceType": "module",
1717
"project": "./tsconfig.eslint.json"
1818
},
19-
"plugins": ["@typescript-eslint"]
19+
"plugins": ["@typescript-eslint"],
20+
"rules": {
21+
"import/prefer-default-export": 0,
22+
"@typescript-eslint/naming-convention": 0
23+
}
2024
}

__mocks__/ERC20.json

Lines changed: 32727 additions & 0 deletions
Large diffs are not rendered by default.

__tests__/contracts.test.ts

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import { BigNumber } from '@ethersproject/bignumber';
2+
import fs from 'fs';
3+
import {
4+
CompiledContract,
5+
Contract,
6+
deployContract,
7+
JsonParser,
8+
randomAddress,
9+
waitForTx,
10+
} from '../src';
11+
12+
const compiledERC20: CompiledContract = JsonParser.parse(
13+
fs.readFileSync('./__mocks__/ERC20.json').toString('ascii')
14+
);
15+
16+
describe('class Contract {}', () => {
17+
const address = randomAddress();
18+
const wallet = randomAddress();
19+
const contract = new Contract(compiledERC20.abi, address);
20+
beforeAll(async () => {
21+
const { code, tx_id } = await deployContract(compiledERC20, address);
22+
// I want to show the tx number to the tester, so he/she can trace the transaction in the explorer.
23+
// eslint-disable-next-line no-console
24+
console.log('deployed erc20 contract', tx_id);
25+
expect(code).toBe('TRANSACTION_RECEIVED');
26+
await waitForTx(tx_id);
27+
});
28+
test('read initial balance of that account', async () => {
29+
const response = await contract.call('balance_of', {
30+
user: wallet,
31+
});
32+
expect(BigNumber.from(response.res)).toStrictEqual(BigNumber.from(0));
33+
});
34+
test('add 10 test ERC20 to account', async () => {
35+
const response = await contract.invoke('mint', {
36+
recipient: wallet,
37+
amount: '10',
38+
});
39+
expect(response.code).toBe('TRANSACTION_RECEIVED');
40+
41+
// I want to show the tx number to the tester, so he/she can trace the transaction in the explorer.
42+
// eslint-disable-next-line no-console
43+
console.log('txId:', response.tx_id, ', funded wallet:', wallet);
44+
await waitForTx(response.tx_id);
45+
});
46+
test('read balance after mint of that account', async () => {
47+
const response = await contract.call('balance_of', {
48+
user: wallet,
49+
});
50+
51+
expect(BigNumber.from(response.res)).toStrictEqual(BigNumber.from(10));
52+
});
53+
});

__tests__/index.test.ts

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,18 @@
11
import fs from 'fs';
2-
import { compressProgram, randomAddress, makeAddress, JsonParser } from '../src/utils';
3-
import { CompiledContract } from '../src/types';
4-
import * as starknet from '../src/index';
2+
import {
3+
CompiledContract,
4+
compressProgram,
5+
randomAddress,
6+
JsonParser,
7+
getContractAddresses,
8+
getBlock,
9+
getCode,
10+
getStorageAt,
11+
getTransactionStatus,
12+
getTransaction,
13+
addTransaction,
14+
deployContract,
15+
} from '../src';
516

617
const compiledArgentAccount = JsonParser.parse(
718
fs.readFileSync('./__mocks__/ArgentAccount.json').toString('ascii')
@@ -10,31 +21,27 @@ const compiledArgentAccount = JsonParser.parse(
1021
describe('starknet endpoints', () => {
1122
describe('feeder gateway endpoints', () => {
1223
test('getContractAddresses()', () => {
13-
return expect(starknet.getContractAddresses()).resolves.not.toThrow();
24+
return expect(getContractAddresses()).resolves.not.toThrow();
1425
});
1526
xtest('callContract()', () => {});
1627
test('getBlock()', () => {
17-
return expect(starknet.getBlock(46500)).resolves.not.toThrow();
28+
return expect(getBlock(46500)).resolves.not.toThrow();
1829
});
1930
test('getCode()', () => {
2031
return expect(
21-
starknet.getCode('0x5f778a983bf8760ad37868f4c869d70247c5546044a7f0386df96d8043d4e9d', 46500)
32+
getCode('0x5f778a983bf8760ad37868f4c869d70247c5546044a7f0386df96d8043d4e9d', 46500)
2233
).resolves.not.toThrow();
2334
});
2435
test('getStorageAt()', () => {
2536
return expect(
26-
starknet.getStorageAt(
27-
'0x5f778a983bf8760ad37868f4c869d70247c5546044a7f0386df96d8043d4e9d',
28-
0,
29-
46500
30-
)
37+
getStorageAt('0x5f778a983bf8760ad37868f4c869d70247c5546044a7f0386df96d8043d4e9d', 0, 46500)
3138
).resolves.not.toThrow();
3239
});
3340
test('getTransactionStatus()', () => {
34-
return expect(starknet.getTransactionStatus(286136)).resolves.not.toThrow();
41+
return expect(getTransactionStatus(286136)).resolves.not.toThrow();
3542
});
3643
test('getTransaction()', () => {
37-
return expect(starknet.getTransaction(286136)).resolves.not.toThrow();
44+
return expect(getTransaction(286136)).resolves.not.toThrow();
3845
});
3946
});
4047

@@ -47,7 +54,7 @@ describe('starknet endpoints', () => {
4754
program: compressProgram(inputContract.program),
4855
};
4956

50-
const response = await starknet.addTransaction({
57+
const response = await addTransaction({
5158
type: 'DEPLOY',
5259
contract_address: randomAddress(),
5360
contract_definition: contractDefinition,
@@ -59,15 +66,11 @@ describe('starknet endpoints', () => {
5966
// eslint-disable-next-line no-console
6067
console.log('txId:', response.tx_id);
6168
});
62-
xtest('type: "INVOKE_FUNCTION"', () => {});
6369

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

67-
const response = await starknet.deployContract(
68-
inputContract,
69-
makeAddress('0x20b5B1b8aFd65F1FCB755a449000cFC4aBCA0D40')
70-
);
73+
const response = await deployContract(inputContract);
7174
expect(response.code).toBe('TRANSACTION_RECEIVED');
7275
expect(response.tx_id).toBeGreaterThan(0);
7376

__tests__/utils.browser.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*/
44

55
import fs from 'fs';
6-
import { compressProgram, isBrowser, JsonParser } from '../src/utils';
6+
import { compressProgram, isBrowser, JsonParser } from '../src';
77

88
const compiledArgentAccount = JsonParser.parse(
99
fs.readFileSync('./__mocks__/ArgentAccount.json').toString('ascii')

__tests__/utils.test.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import fs from 'fs';
2-
import { compressProgram, makeAddress, isBrowser, JsonParser } from '../src/utils';
2+
import { compressProgram, makeAddress, isBrowser, JsonParser, getSelectorFromName } from '../src';
33

44
const compiledArgentAccount = JsonParser.parse(
55
fs.readFileSync('./__mocks__/ArgentAccount.json').toString('ascii')
@@ -33,3 +33,20 @@ describe('makeAddress()', () => {
3333
expect(starkAddress).toBe('0xdfd0f27fce99b50909de0bdd328aed6eabe76bc5');
3434
});
3535
});
36+
describe('starknetKeccak()', () => {
37+
test('hash works for value="test"', () => {
38+
expect(getSelectorFromName('test')).toBe(
39+
'0x22ff5f21f0b81b113e63f7db6da94fedef11b2119b4088b89664fb9a3cb658'
40+
);
41+
});
42+
test('hash works for value="initialize"', () => {
43+
expect(getSelectorFromName('initialize')).toBe(
44+
'0x79dc0da7c54b95f10aa182ad0a46400db63156920adb65eca2654c0945a463'
45+
);
46+
});
47+
test('hash works for value="mint"', () => {
48+
expect(getSelectorFromName('mint')).toBe(
49+
'0x02f0b3c5710379609eb5495f1ecd348cb28167711b73609fe565a72734550354'
50+
);
51+
});
52+
});

0 commit comments

Comments
 (0)