import { Address, beginCell, Cell, Contract, contractAddress, ContractProvider, Sender, SendMode } from '@ton/core';
export type VanityContractConfig = {
owner: Address;
salt: Buffer;
};
export function vanityContractConfigToCell(
config: VanityContractConfig
): Cell {
return beginCell()
.storeUint(0, 5)
.storeAddress(config.owner)
.storeBuffer(config.salt, 32)
.endCell();
}
// from https://github.com/ton-community/vanity-contract/blob/6baeb39500de0fee79bd241047699ca65ee71f55/src/contract/vanity-address.cell
const vanityCode = Cell.fromBoc(
Buffer.from(
'b5ee9c72010102010032000114ff00f4a413f4bcf2c80b010046d3ed44d075d721fa408307d721d102d0d30331fa403058c705f288d4d4d101fb04ed54',
'hex',
),
)[0];
export class VanityContract implements Contract {
constructor(
readonly address: Address,
readonly init?: { code: Cell; data: Cell },
) {}
static createFromAddress(address: Address) {
return new VanityContract(address);
}
static createFromConfig(
config: VanityContractConfig,
workchain = 0
) {
const data = vanityContractConfigToCell(config);
const init = {
code: vanityCode,
data,
};
return new VanityContract(
contractAddress(workchain, init),
init,
);
}
async sendDeploy(
provider: ContractProvider,
via: Sender,
value: bigint,
newCode: Cell,
newData: Cell,
) {
const body = beginCell()
.storeRef(newCode)
.storeRef(newData)
.endCell();
await provider.internal(via, {
value,
sendMode: SendMode.PAY_GAS_SEPARATELY,
body,
});
}
}