Skip to content

Commit

Permalink
feat: update to alpha3
Browse files Browse the repository at this point in the history
  • Loading branch information
janek26 committed Oct 29, 2021
1 parent 643cd40 commit 1cf4616
Show file tree
Hide file tree
Showing 11 changed files with 70,579 additions and 65,154 deletions.
102,294 changes: 52,141 additions & 50,153 deletions __mocks__/ArgentAccount.json

Large diffs are not rendered by default.

33,203 changes: 18,275 additions & 14,928 deletions __mocks__/ERC20.json

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions __tests__/__snapshots__/utils.browser.test.ts.snap

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions __tests__/__snapshots__/utils.test.ts.snap

Large diffs are not rendered by default.

20 changes: 13 additions & 7 deletions __tests__/contracts.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,22 @@ const compiledERC20: CompiledContract = parse(
);

describe('class Contract {}', () => {
const address = randomAddress();
const wallet = randomAddress();
const contract = new Contract(compiledERC20.abi, address);
let contract: Contract;
beforeAll(async () => {
const { code, tx_id } = await deployContract(compiledERC20, address);
const {
code,
transaction_hash,
address: erc20address,
} = await deployContract(compiledERC20, []);
console.log(erc20address);
contract = new Contract(compiledERC20.abi, erc20address);
// I want to show the tx number to the tester, so he/she can trace the transaction in the explorer.
// eslint-disable-next-line no-console
console.log('deployed erc20 contract', tx_id);
console.log('deployed erc20 contract', transaction_hash);
expect(code).toBe('TRANSACTION_RECEIVED');
await waitForTx(tx_id);

await waitForTx(transaction_hash);
});
test('read initial balance of that account', async () => {
const response = await contract.call('balance_of', {
Expand All @@ -39,8 +45,8 @@ describe('class Contract {}', () => {

// I want to show the tx number to the tester, so he/she can trace the transaction in the explorer.
// eslint-disable-next-line no-console
console.log('txId:', response.tx_id, ', funded wallet:', wallet);
await waitForTx(response.tx_id);
console.log('txId:', response.transaction_hash, ', funded wallet:', wallet);
await waitForTx(response.transaction_hash);
});
test('read balance after mint of that account', async () => {
const response = await contract.call('balance_of', {
Expand Down
60 changes: 47 additions & 13 deletions __tests__/starknet.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import fs from 'fs';

import {
CompiledContract,
Contract,
addTransaction,
callContract,
deployContract,
getBlock,
getCode,
Expand All @@ -12,6 +14,7 @@ import {
getTransactionStatus,
utils,
} from '../src';
import { getSelectorFromName } from '../src/utils/starknet';

const {
json: { parse },
Expand All @@ -28,36 +31,51 @@ describe('starknet endpoints', () => {
return expect(getContractAddresses()).resolves.not.toThrow();
});
test('getBlock()', () => {
return expect(getBlock(46500)).resolves.not.toThrow();
return expect(getBlock(870)).resolves.not.toThrow();
});
test('getBlock(blockId=null)', () => {
return expect(getBlock()).resolves.not.toThrow();
});
test('getCode()', () => {
return expect(
getCode('0x5f778a983bf8760ad37868f4c869d70247c5546044a7f0386df96d8043d4e9d', 46500)
getCode('0x163a1542a64402ffc93e39a4962eec51ce126f2e634631d3f1f6770a76e3a61', 870)
).resolves.not.toThrow();
});
test('getCode(blockId=null)', () => {
return expect(
getCode('0x5f778a983bf8760ad37868f4c869d70247c5546044a7f0386df96d8043d4e9d')
getCode('0x163a1542a64402ffc93e39a4962eec51ce126f2e634631d3f1f6770a76e3a61')
).resolves.not.toThrow();
});
test('getStorageAt()', () => {
return expect(
getStorageAt('0x5f778a983bf8760ad37868f4c869d70247c5546044a7f0386df96d8043d4e9d', 0, 46500)
getStorageAt('0x163a1542a64402ffc93e39a4962eec51ce126f2e634631d3f1f6770a76e3a61', 0, 870)
).resolves.not.toThrow();
});
test('getStorageAt(blockId=null)', () => {
return expect(
getStorageAt('0x5f778a983bf8760ad37868f4c869d70247c5546044a7f0386df96d8043d4e9d', 0)
getStorageAt('0x163a1542a64402ffc93e39a4962eec51ce126f2e634631d3f1f6770a76e3a61', 0)
).resolves.not.toThrow();
});
test('getTransactionStatus()', () => {
return expect(getTransactionStatus(286136)).resolves.not.toThrow();
return expect(
getTransactionStatus('0x2086ff26645fb0e31a3e252302f3cb1e7612c60389102e5473dfc89758a3aa9')
).resolves.not.toThrow();
});
test('getTransaction()', () => {
return expect(getTransaction(286136)).resolves.not.toThrow();
return expect(
getTransaction('0x2086ff26645fb0e31a3e252302f3cb1e7612c60389102e5473dfc89758a3aa9')
).resolves.not.toThrow();
});
test('callContract()', () => {
return expect(
callContract({
contract_address: '0x58bceda58a83a5a100117ddc893234bad9c84a6833c2008f0f1ca90150149af',
entry_point_selector: getSelectorFromName('balance_of'),
calldata: Contract.compileCalldata({
user: '0x58bceda58a83a5a100117ddc893234bad9c84a6833c2008f0f1ca90150149af',
}),
})
).resolves.not.toThrow();
});
});

Expand All @@ -72,27 +90,43 @@ describe('starknet endpoints', () => {

const response = await addTransaction({
type: 'DEPLOY',
contract_address: randomAddress(),
contract_address_salt: randomAddress(),
constructor_calldata: Contract.compileCalldata({
signer: randomAddress(),
guardian: '0',
L1_address: '0',
}),
contract_definition: contractDefinition,
});

expect(response.code).toBe('TRANSACTION_RECEIVED');
expect(response.tx_id).toBeGreaterThan(0);
expect(response.transaction_hash).toBeDefined();
expect(response.address).toBeDefined();

// I want to show the tx number to the tester, so he/she can trace the transaction in the explorer.
// eslint-disable-next-line no-console
console.log('txId:', response.tx_id);
console.log('txHash:', response.transaction_hash);
});

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

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

expect(response.code).toBe('TRANSACTION_RECEIVED');
expect(response.tx_id).toBeGreaterThan(0);
expect(response.transaction_hash).toBeDefined();
expect(response.address).toBeDefined();

// I want to show the tx number to the tester, so he/she can trace the transaction in the explorer.
// eslint-disable-next-line no-console
console.log('txId:', response.tx_id);
console.log('txHash:', response.transaction_hash);
});
});
});
70 changes: 41 additions & 29 deletions __tests__/wallet.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,36 +53,45 @@ describe('deploy and test Wallet', () => {
console.log('PK:', pk);

const starkKeyPair = getKeyPair(pk);
const walletAddress = getStarkKey(starkKeyPair);
const wallet = new Contract(compiledArgentAccount.abi, walletAddress);
const erc20Address = getStarkKey(getKeyPair(randomAddress()));
const erc20 = new Contract(compiledErc20.abi, erc20Address);
const starkKeyPub = getStarkKey(starkKeyPair);
let wallet: Contract;
let walletAddress: string;
let erc20: Contract;
let erc20Address: string;
beforeAll(async () => {
const { code: codeErc20, tx_id: txErc20 } = await deployContract(compiledErc20, erc20Address);
const {
code: codeErc20,
transaction_hash: txErc20,
address: erc20AddressLocal,
} = await deployContract(compiledErc20, []);
erc20Address = erc20AddressLocal;
erc20 = new Contract(compiledErc20.abi, erc20Address);
// I want to show the tx number to the tester, so he/she can trace the transaction in the explorer.
// eslint-disable-next-line no-console
console.log('deployed erc20', txErc20);
expect(codeErc20).toBe('TRANSACTION_RECEIVED');

const { code, tx_id } = await deployContract(compiledArgentAccount, walletAddress);
const {
code,
transaction_hash,
address: walletAddressLocal,
} = await deployContract(
compiledArgentAccount,
Contract.compileCalldata({
signer: starkKeyPub,
guardian: '0',
L1_address: '0',
}),
starkKeyPub
);
walletAddress = walletAddressLocal;
wallet = new Contract(compiledArgentAccount.abi, walletAddress);
// I want to show the tx number to the tester, so he/she can trace the transaction in the explorer.
// eslint-disable-next-line no-console
console.log('deployed wallet', tx_id);
console.log('deployed wallet', transaction_hash);
expect(code).toBe('TRANSACTION_RECEIVED');

const { code: code2, tx_id: txId2 } = await wallet.invoke('initialize', {
signer: walletAddress,
guardian: '0',
L1_address: '0',
self_address: walletAddress,
});

// I want to show the tx number to the tester, so he/she can trace the transaction in the explorer.
// eslint-disable-next-line no-console
console.log('initialized wallet', txId2);
expect(code2).toBe('TRANSACTION_RECEIVED');

const { code: codeErc20Mint, tx_id: txErc20Mint } = await erc20.invoke('mint', {
const { code: codeErc20Mint, transaction_hash: txErc20Mint } = await erc20.invoke('mint', {
recipient: walletAddress,
amount: '1000',
});
Expand Down Expand Up @@ -119,20 +128,23 @@ describe('deploy and test Wallet', () => {
);

const { r, s } = sign(starkKeyPair, msgHash);
const { code, tx_id } = await wallet.invoke('execute', {
to: erc20Address,
selector: getSelectorFromName('transfer'),
calldata: [erc20Address, '10'],
nonce: nonce.toString(),
sig: [toHex(r), toHex(s)],
});
const { code, transaction_hash } = await wallet.invoke(
'execute',
{
to: erc20Address,
selector: getSelectorFromName('transfer'),
calldata: [erc20Address, '10'],
nonce: nonce.toString(),
},
[toHex(r), toHex(s)]
);

// I want to show the tx number to the tester, so he/she can trace the transaction in the explorer.
// eslint-disable-next-line no-console
console.log('transfer erc20 using wallet execute', tx_id);
console.log('transfer erc20 using wallet execute', transaction_hash);
expect(code).toBe('TRANSACTION_RECEIVED');

await waitForTx(tx_id);
await waitForTx(transaction_hash);
});
test('read balance of wallet after transfer', async () => {
const { res } = await erc20.call('balance_of', {
Expand Down
5 changes: 3 additions & 2 deletions src/contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import assert from 'minimalistic-assert';

import { addTransaction, callContract } from './starknet';
import { Abi } from './types';
import { toBN } from './utils/number';
import { BigNumberish, toBN } from './utils/number';
import { getSelectorFromName } from './utils/starknet';

type Args = { [inputName: string]: string | string[] };
Expand Down Expand Up @@ -109,7 +109,7 @@ export class Contract {
}, {} as Args);
}

public invoke(method: string, args: Args = {}) {
public invoke(method: string, args: Args = {}, signature?: [BigNumberish, BigNumberish]) {
// ensure contract is connected
assert(this.connectedTo !== null, 'contract isnt connected to an address');

Expand All @@ -123,6 +123,7 @@ export class Contract {
return addTransaction({
type: 'INVOKE_FUNCTION',
contract_address: this.connectedTo,
signature,
calldata,
entry_point_selector: entrypointSelector,
});
Expand Down
Loading

0 comments on commit 1cf4616

Please sign in to comment.