From cd4f68490863eeeec16c6706b1657e3698dc408a Mon Sep 17 00:00:00 2001 From: siandreev Date: Thu, 28 Sep 2023 12:19:33 +0200 Subject: [PATCH] fix: lib code added to the tests --- tests/wallet-v5-extensions.spec.ts | 13 ++++++++++++- tests/wallet-v5-external.spec.ts | 14 ++++++++++++-- tests/wallet-v5-get.spec.ts | 4 +++- tests/wallet-v5-internal.spec.ts | 14 ++++++++++++-- wrappers/library-deployer.ts | 23 ++++++++++++++++++++++- 5 files changed, 61 insertions(+), 7 deletions(-) diff --git a/tests/wallet-v5-extensions.spec.ts b/tests/wallet-v5-extensions.spec.ts index ad8288d..f6df833 100644 --- a/tests/wallet-v5-extensions.spec.ts +++ b/tests/wallet-v5-extensions.spec.ts @@ -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 }); @@ -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( @@ -57,7 +60,7 @@ describe('Wallet V5 extensions auth', () => { publicKey: keypair.publicKey, extensions: Dictionary.empty() }, - code + LibraryDeployer.exportLibCode(code) ) ); @@ -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); }); diff --git a/tests/wallet-v5-external.spec.ts b/tests/wallet-v5-external.spec.ts index 748014b..d8001c4 100644 --- a/tests/wallet-v5-external.spec.ts +++ b/tests/wallet-v5-external.spec.ts @@ -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 }); @@ -51,7 +52,7 @@ describe('Wallet V5 sign auth external', () => { publicKey: params?.publicKey ?? _keypair.publicKey, extensions: params?.extensions ?? Dictionary.empty() }, - code + LibraryDeployer.exportLibCode(code) ) ); @@ -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( @@ -90,7 +93,7 @@ describe('Wallet V5 sign auth external', () => { publicKey: keypair.publicKey, extensions: Dictionary.empty() }, - code + LibraryDeployer.exportLibCode(code) ) ); @@ -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); diff --git a/tests/wallet-v5-get.spec.ts b/tests/wallet-v5-get.spec.ts index c50e89f..c3fac3a 100644 --- a/tests/wallet-v5-get.spec.ts +++ b/tests/wallet-v5-get.spec.ts @@ -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 }); @@ -22,6 +23,7 @@ describe('Wallet V5 get methods', () => { async function deploy(params?: Partial[0]>) { blockchain = await Blockchain.create(); + blockchain.libs = buildBlockchainLibraries([code]); if (!params?.publicKey) { keypair = keyPairFromSeed(await getSecureRandomBytes(32)); } @@ -34,7 +36,7 @@ describe('Wallet V5 get methods', () => { publicKey: params?.publicKey ?? keypair.publicKey, extensions: params?.extensions ?? Dictionary.empty() }, - code + LibraryDeployer.exportLibCode(code) ) ); diff --git a/tests/wallet-v5-internal.spec.ts b/tests/wallet-v5-internal.spec.ts index 1beb665..7eea026 100644 --- a/tests/wallet-v5-internal.spec.ts +++ b/tests/wallet-v5-internal.spec.ts @@ -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 }); @@ -45,7 +46,7 @@ describe('Wallet V5 sign auth internal', () => { publicKey: params?.publicKey ?? _keypair.publicKey, extensions: params?.extensions ?? Dictionary.empty() }, - code + LibraryDeployer.exportLibCode(code) ) ); @@ -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( @@ -84,7 +87,7 @@ describe('Wallet V5 sign auth internal', () => { publicKey: keypair.publicKey, extensions: Dictionary.empty() }, - code + LibraryDeployer.exportLibCode(code) ) ); @@ -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); diff --git a/wrappers/library-deployer.ts b/wrappers/library-deployer.ts index 1441b0e..efec67e 100644 --- a/wrappers/library-deployer.ts +++ b/wrappers/library-deployer.ts @@ -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 + ); + 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();