Skip to content

Commit

Permalink
fix: lib code added to the tests
Browse files Browse the repository at this point in the history
  • Loading branch information
siandreev committed Sep 28, 2023
1 parent 924e3c6 commit cd4f684
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 7 deletions.
13 changes: 12 additions & 1 deletion tests/wallet-v5-extensions.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
} from './actions';
import { TransactionDescriptionGeneric } from 'ton-core/src/types/TransactionDescription';
import { TransactionComputeVm } from 'ton-core/src/types/TransactionComputePhase';
import { buildBlockchainLibraries, LibraryDeployer } from '../wrappers/library-deployer';

const WALLET_ID = new WalletId({ networkGlobalId: -239, workChain: 0, subwalletNumber: 0 });

Expand Down Expand Up @@ -47,6 +48,8 @@ describe('Wallet V5 extensions auth', () => {

beforeEach(async () => {
blockchain = await Blockchain.create();
blockchain.libs = buildBlockchainLibraries([code]);

keypair = keyPairFromSeed(await getSecureRandomBytes(32));

walletV5 = blockchain.openContract(
Expand All @@ -57,7 +60,7 @@ describe('Wallet V5 extensions auth', () => {
publicKey: keypair.publicKey,
extensions: Dictionary.empty()
},
code
LibraryDeployer.exportLibCode(code)
)
);

Expand Down Expand Up @@ -103,6 +106,14 @@ describe('Wallet V5 extensions auth', () => {
});

const fee = receipt.transactions[2].totalFees.coins;
console.debug(
'SINGLE INTERNAL TRANSFER FROM EXTENSION GAS USED:',
(
(receipt.transactions[1].description as TransactionDescriptionGeneric)
.computePhase as TransactionComputeVm
).gasUsed
);

const receiverBalanceAfter = (await blockchain.getContract(testReceiver)).balance;
expect(receiverBalanceAfter).toEqual(receiverBalanceBefore + forwardValue - fee);
});
Expand Down
14 changes: 12 additions & 2 deletions tests/wallet-v5-external.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
import { WalletV4 } from '../wrappers/wallet-v4';
import { TransactionDescriptionGeneric } from 'ton-core/src/types/TransactionDescription';
import { TransactionComputeVm } from 'ton-core/src/types/TransactionComputePhase';
import { buildBlockchainLibraries, LibraryDeployer } from '../wrappers/library-deployer';

const WALLET_ID = new WalletId({ networkGlobalId: -239, workChain: -1, subwalletNumber: 0 });

Expand Down Expand Up @@ -51,7 +52,7 @@ describe('Wallet V5 sign auth external', () => {
publicKey: params?.publicKey ?? _keypair.publicKey,
extensions: params?.extensions ?? Dictionary.empty()
},
code
LibraryDeployer.exportLibCode(code)
)
);

Expand Down Expand Up @@ -80,6 +81,8 @@ describe('Wallet V5 sign auth external', () => {

beforeEach(async () => {
blockchain = await Blockchain.create();
blockchain.libs = buildBlockchainLibraries([code]);

keypair = keyPairFromSeed(await getSecureRandomBytes(32));

walletV5 = blockchain.openContract(
Expand All @@ -90,7 +93,7 @@ describe('Wallet V5 sign auth external', () => {
publicKey: keypair.publicKey,
extensions: Dictionary.empty()
},
code
LibraryDeployer.exportLibCode(code)
)
);

Expand Down Expand Up @@ -150,6 +153,13 @@ describe('Wallet V5 sign auth external', () => {
});

const fee = receipt.transactions[1].totalFees.coins;
console.debug(
'SINGLE EXTERNAL TRANSFER GAS USED:',
(
(receipt.transactions[0].description as TransactionDescriptionGeneric)
.computePhase as TransactionComputeVm
).gasUsed
);

const receiverBalanceAfter = (await blockchain.getContract(testReceiver)).balance;
expect(receiverBalanceAfter).toEqual(receiverBalanceBefore + forwardValue - fee);
Expand Down
4 changes: 3 additions & 1 deletion tests/wallet-v5-get.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import '@ton-community/test-utils';
import { compile } from '@ton-community/blueprint';
import { getSecureRandomBytes, KeyPair, keyPairFromSeed } from 'ton-crypto';
import { bufferToBigInt, packAddress } from './utils';
import { buildBlockchainLibraries, LibraryDeployer } from '../wrappers/library-deployer';

const WALLET_ID = new WalletId({ networkGlobalId: -239, workChain: 0, subwalletNumber: 0 });

Expand All @@ -22,6 +23,7 @@ describe('Wallet V5 get methods', () => {

async function deploy(params?: Partial<Parameters<typeof WalletV5.createFromConfig>[0]>) {
blockchain = await Blockchain.create();
blockchain.libs = buildBlockchainLibraries([code]);
if (!params?.publicKey) {
keypair = keyPairFromSeed(await getSecureRandomBytes(32));
}
Expand All @@ -34,7 +36,7 @@ describe('Wallet V5 get methods', () => {
publicKey: params?.publicKey ?? keypair.publicKey,
extensions: params?.extensions ?? Dictionary.empty()
},
code
LibraryDeployer.exportLibCode(code)
)
);

Expand Down
14 changes: 12 additions & 2 deletions tests/wallet-v5-internal.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
import { WalletV4 } from '../wrappers/wallet-v4';
import { TransactionDescriptionGeneric } from 'ton-core/src/types/TransactionDescription';
import { TransactionComputeVm } from 'ton-core/src/types/TransactionComputePhase';
import { buildBlockchainLibraries, LibraryDeployer } from '../wrappers/library-deployer';

const WALLET_ID = new WalletId({ networkGlobalId: -239, workChain: 0, subwalletNumber: 0 });

Expand Down Expand Up @@ -45,7 +46,7 @@ describe('Wallet V5 sign auth internal', () => {
publicKey: params?.publicKey ?? _keypair.publicKey,
extensions: params?.extensions ?? Dictionary.empty()
},
code
LibraryDeployer.exportLibCode(code)
)
);

Expand Down Expand Up @@ -74,6 +75,8 @@ describe('Wallet V5 sign auth internal', () => {

beforeEach(async () => {
blockchain = await Blockchain.create();
blockchain.libs = buildBlockchainLibraries([code]);

keypair = keyPairFromSeed(await getSecureRandomBytes(32));

walletV5 = blockchain.openContract(
Expand All @@ -84,7 +87,7 @@ describe('Wallet V5 sign auth internal', () => {
publicKey: keypair.publicKey,
extensions: Dictionary.empty()
},
code
LibraryDeployer.exportLibCode(code)
)
);

Expand Down Expand Up @@ -147,6 +150,13 @@ describe('Wallet V5 sign auth internal', () => {
});

const fee = receipt.transactions[2].totalFees.coins;
console.debug(
'SINGLE INTERNAL TRANSFER GAS USED:',
(
(receipt.transactions[1].description as TransactionDescriptionGeneric)
.computePhase as TransactionComputeVm
).gasUsed
);

const receiverBalanceAfter = (await blockchain.getContract(testReceiver)).balance;
expect(receiverBalanceAfter).toEqual(receiverBalanceBefore + forwardValue - fee);
Expand Down
23 changes: 22 additions & 1 deletion wrappers/library-deployer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,35 @@ import {
Contract,
contractAddress,
ContractProvider,
Dictionary,
DictionaryValue,
Sender,
SendMode
SendMode,
SimpleLibrary
} from 'ton-core';
import { SimpleLibraryValue } from 'ton-core/dist/types/SimpleLibrary';

export type LibraryDeployerConfig = {
libraryCode: Cell;
};

export function buildBlockchainLibraries(libs: Cell[]): Cell {
const libraries = Dictionary.empty(Dictionary.Keys.BigUint(256), Dictionary.Values.Cell());
libs.forEach(lib => libraries.set(BigInt('0x' + lib.hash().toString('hex')), lib));

return beginCell().storeDictDirect(libraries).endCell();
}

export function buildLibraryStateInit(library: SimpleLibrary): Cell {
const libraries = Dictionary.empty(
Dictionary.Keys.BigUint(256),
SimpleLibraryValue as unknown as DictionaryValue<SimpleLibrary>
);
libraries.set(BigInt('0x' + library.root.hash().toString('hex')), library);

return beginCell().storeDictDirect(libraries).endCell();
}

export class LibraryDeployer implements Contract {
static exportLibCode(code: Cell) {
const bits = new BitBuilder();
Expand Down

0 comments on commit cd4f684

Please sign in to comment.