|
1 | | -import { BaseUtils, isValidEd25519PublicKey } from '@bitgo/sdk-core'; |
2 | 1 | import TonWeb from 'tonweb'; |
3 | | - |
| 2 | +import { BN } from 'bn.js'; |
| 3 | +import { BaseUtils, isValidEd25519PublicKey } from '@bitgo/sdk-core'; |
| 4 | +import { VESTING_CONTRACT_CODE_B64 } from './constants'; |
| 5 | +import { VestingContractParams } from './iface'; |
4 | 6 | export class Utils implements BaseUtils { |
5 | 7 | /** @inheritdoc */ |
6 | 8 | isValidAddress(address: string): boolean { |
@@ -85,6 +87,30 @@ export class Utils implements BaseUtils { |
85 | 87 | const address = slice.loadAddress(); |
86 | 88 | return address.toString(); |
87 | 89 | } |
| 90 | + |
| 91 | + async getVestingContractAddress(params: VestingContractParams): Promise<string> { |
| 92 | + const cell = new TonWeb.boc.Cell(); |
| 93 | + cell.bits.writeUint(0, 32); //seq no |
| 94 | + cell.bits.writeUint(params.subWalletId, 32); |
| 95 | + cell.bits.writeBytes(TonWeb.utils.hexToBytes(params.publicKeyHex)); |
| 96 | + cell.bits.writeUint(0, 1); // empty whitelist |
| 97 | + const vestingParamsCell = new TonWeb.boc.Cell(); |
| 98 | + vestingParamsCell.bits.writeUint(params.vestingStartTime, 64); |
| 99 | + vestingParamsCell.bits.writeUint(params.vestingTotalDuration, 32); |
| 100 | + vestingParamsCell.bits.writeUint(params.unlockPeriod, 32); |
| 101 | + vestingParamsCell.bits.writeUint(params.cliffDuration, 32); |
| 102 | + vestingParamsCell.bits.writeCoins(new BN(params.vestingTotalAmount.toString())); |
| 103 | + const senderAddress = new TonWeb.Address(params.vestingSenderAddress); |
| 104 | + vestingParamsCell.bits.writeAddress(senderAddress); |
| 105 | + const ownerAddress = new TonWeb.Address(params.ownerAddress); |
| 106 | + vestingParamsCell.bits.writeAddress(ownerAddress); |
| 107 | + cell.refs.push(vestingParamsCell); |
| 108 | + const contractCodeCell = TonWeb.boc.Cell.oneFromBoc(TonWeb.utils.base64ToBytes(VESTING_CONTRACT_CODE_B64)); |
| 109 | + const stateInit = TonWeb.Contract.createStateInit(contractCodeCell, cell); |
| 110 | + const stateInitHash = await stateInit.hash(); |
| 111 | + const contractAddress = new TonWeb.Address('0:' + TonWeb.utils.bytesToHex(stateInitHash)); |
| 112 | + return contractAddress.toString(true, true, true); |
| 113 | + } |
88 | 114 | } |
89 | 115 |
|
90 | 116 | const DUMMY_PRIVATE_KEY = '43e8594854cb53947c4a1a2fab926af11e123f6251dcd5bd0dfb100604186430'; // This dummy private key is used only for fee estimation |
|
0 commit comments