Skip to content

Commit

Permalink
feat: lib deployment scripts added
Browse files Browse the repository at this point in the history
  • Loading branch information
siandreev committed Sep 27, 2023
1 parent 1748d80 commit 13d1e72
Show file tree
Hide file tree
Showing 11 changed files with 185 additions and 5 deletions.
4 changes: 4 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
WALLET_MNEMONIC=

# v4
WALLET_VERSION=
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ node_modules
temp
build
.idea
.env
19 changes: 19 additions & 0 deletions contracts/library-deployer.fc
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#pragma version =0.4.4;
#include "imports/stdlib.fc";

() set_lib_code(cell code, int mode) impure asm "SETLIBCODE";

() deploy_lib() impure {
set_lib_code(get_data(), 2);
cell empty = begin_cell().end_cell();
set_code(empty);
set_data(empty);
}

() recv_internal(slice in_msg) impure {
deploy_lib();
}

() recv_external(slice in_msg) impure {
deploy_lib();
}
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
"start": "blueprint run",
"build": "blueprint build",
"build:v5": "blueprint build wallet_v5",
"deploy-library": "npx blueprint run deployLibrary",
"deploy-wallet": "blueprint run deployWalletV5",
"print-wallet-code": "ts-node ./scripts/printWalletCode.ts",
"test": "jest"
},
"devDependencies": {
Expand Down
54 changes: 54 additions & 0 deletions scripts/deployLibrary.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { toNano } from 'ton-core';
import { compile, NetworkProvider } from '@ton-community/blueprint';
import 'dotenv/config';
import { LibraryDeployer } from '../wrappers/library-deployer';

export async function run(provider: NetworkProvider) {
/* const library: SimpleLibrary = {
public: true,
root: await compile('wallet_v5')
};
let secretKey = process.env.LIBRARY_KEEPER_SECRET_KEY;
if (!secretKey) {
const keypair = keyPairFromSeed(await getSecureRandomBytes(32));
console.log('GENERATED PRIVATE_KEY', keypair.secretKey.toString('hex'));
secretKey = keypair.secretKey.toString('hex');
fs.appendFileSync('.env', `LIBRARY_KEEPER_SECRET_KEY=${secretKey}`);
}
const keypair = keyPairFromSecretKey(Buffer.from(secretKey, 'hex'));
const libraryKeeper = provider.open(
LibraryKeeper.createFromConfig(
{ publicKey: keypair.publicKey, seqno: 0 },
await compile('library-keeper')
)
);
const isActive = await libraryKeeper.getIsActive();
if (!isActive) {
await libraryKeeper.sendDeploy(provider.sender(), toNano('0.1'));
await provider.waitForDeploy(libraryKeeper.address);
}
await libraryKeeper.sendAddLibrary({
libraryCode: library.root,
secretKey: keypair.secretKey
});
console.log('LIBRARY KEEPER ADDRESS', libraryKeeper.address);*/

const libraryDeployer = provider.open(
LibraryDeployer.createFromConfig(
{ libraryCode: await compile('wallet_v5') },
await compile('library-deployer')
)
);

await libraryDeployer.sendDeploy(provider.sender(), toNano('0.1'));
await provider.waitForDeploy(libraryDeployer.address);

console.log('LIBRARY ADDRESS', libraryDeployer.address);
}
14 changes: 11 additions & 3 deletions scripts/deployWalletV5.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,17 @@ import { Dictionary, toNano } from 'ton-core';
import { WalletId, WalletV5 } from '../wrappers/wallet-v5';
import { compile, NetworkProvider } from '@ton-community/blueprint';
import { getSecureRandomBytes, keyPairFromSeed } from 'ton-crypto';
import 'dotenv/config';
import { LibraryKeeper } from '../wrappers/library-keeper';
import {randomTestKey} from "ton/dist/utils/randomTestKey";

/*
DOESN'T WORK WITH TONKEEPER AND TONCONNECT. CHOOSE DEPLOY WITH MNEMONIC
*/
export async function run(provider: NetworkProvider) {
const keypair = keyPairFromSeed(await getSecureRandomBytes(32));
const keypair = randomTestKey('v5-treasure'); // keyPairFromSeed(
//await getSecureRandomBytes(32)
//);
console.log('KEYPAIR PUBKEY', keypair.publicKey.toString('hex'));
console.log('KEYPAIR PRIVATE_KEY', keypair.secretKey.toString('hex'));

Expand All @@ -16,11 +24,11 @@ export async function run(provider: NetworkProvider) {
publicKey: keypair.publicKey,
extensions: Dictionary.empty()
},
await compile('wallet_v5')
LibraryKeeper.exportLibCode(await compile('wallet_v5'))
)
);

await walletV5.sendDeploy(provider.sender(), toNano('0.05'));
await walletV5.sendDeploy(provider.sender(), toNano('0.1'));

await provider.waitForDeploy(walletV5.address);

Expand Down
28 changes: 28 additions & 0 deletions scripts/deployWalletV5WithoutLibrary.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { Dictionary, toNano } from 'ton-core';
import { WalletId, WalletV5 } from '../wrappers/wallet-v5';
import { compile, NetworkProvider } from '@ton-community/blueprint';
import { getSecureRandomBytes, keyPairFromSeed } from 'ton-crypto';

export async function run(provider: NetworkProvider) {
const keypair = keyPairFromSeed(await getSecureRandomBytes(32));
console.log('KEYPAIR PUBKEY', keypair.publicKey.toString('hex'));
console.log('KEYPAIR PRIVATE_KEY', keypair.secretKey.toString('hex'));

const walletV5 = provider.open(
WalletV5.createFromConfig(
{
seqno: 0,
walletId: new WalletId({ networkGlobalId: -3 }).serialized, // testnet
publicKey: keypair.publicKey,
extensions: Dictionary.empty()
},
await compile('wallet_v5')
)
);

await walletV5.sendDeploy(provider.sender(), toNano('0.05'));

await provider.waitForDeploy(walletV5.address);

console.log('ADDRESS', walletV5.address);
}
11 changes: 11 additions & 0 deletions scripts/printWalletCode.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { compile } from '@ton-community/blueprint';
import { LibraryKeeper } from '../wrappers/library-keeper';

export async function run() {
const code = LibraryKeeper.exportLibCode(await compile('wallet_v5'));

console.log('WALLET CODE HEX', code.toBoc().toString('hex'), '\n');
console.log('WALLET CODE BASE64', code.toBoc().toString('base64'));
}

run();
6 changes: 6 additions & 0 deletions wrappers/library-deployer.compile.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { CompilerConfig } from '@ton-community/blueprint';

export const compile: CompilerConfig = {
lang: 'func',
targets: ['contracts/library-deployer.fc']
};
41 changes: 41 additions & 0 deletions wrappers/library-deployer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import {
Address,
beginCell,
BitBuilder,
Cell,
Contract,
contractAddress,
ContractProvider,
Sender,
SendMode
} from 'ton-core';

export type LibraryDeployerConfig = {
libraryCode: Cell;
};

export class LibraryDeployer implements Contract {
static exportLibCode(code: Cell) {
const bits = new BitBuilder();
bits.writeUint(2, 8);
bits.writeUint(BigInt('0x' + code.hash().toString('hex')), 256);

return new Cell({ exotic: true, bits: bits.build() });
}

constructor(readonly address: Address, readonly init?: { code: Cell; data: Cell }) {}

static createFromConfig(config: LibraryDeployerConfig, code: Cell, workchain = -1) {
const data = config.libraryCode;
const init = { code, data };
return new LibraryDeployer(contractAddress(workchain, init), init);
}

async sendDeploy(provider: ContractProvider, via: Sender, value: bigint) {
await provider.internal(via, {
value,
sendMode: SendMode.PAY_GAS_SEPARATELY,
body: beginCell().endCell()
});
}
}
9 changes: 7 additions & 2 deletions wrappers/wallet-v5.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,13 @@ export class WalletV5 implements Contract {
}

async getSeqno(provider: ContractProvider) {
const result = await provider.get('seqno', []);
return result.stack.readNumber();
const state = await provider.getState();
if (state.state.type === 'active') {
let res = await provider.get('seqno', []);
return res.stack.readNumber();
} else {
return 0;
}
}

async getWalletId(provider: ContractProvider) {
Expand Down

0 comments on commit 13d1e72

Please sign in to comment.