Skip to content

Commit

Permalink
feat: deploy contract flow with proxy deploy
Browse files Browse the repository at this point in the history
  • Loading branch information
danielbate committed Sep 20, 2024
1 parent 3d0c0f6 commit 17f3c40
Show file tree
Hide file tree
Showing 7 changed files with 874 additions and 14 deletions.
80 changes: 72 additions & 8 deletions packages/fuels/src/cli/commands/deploy/deployContract.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import type { WalletUnlocked } from '@fuel-ts/account';
import { ContractFactory } from '@fuel-ts/contract';
import type { DeployContractOptions } from '@fuel-ts/contract';
import { Contract } from '@fuel-ts/program';
import { existsSync, readFileSync } from 'fs';

import type { ForcToml } from '../../config/forcUtils';
import { debug } from '../../utils/logger';

import { Src14OwnedProxy, Src14OwnedProxyFactory } from './proxy';

export async function deployContract(
wallet: WalletUnlocked,
binaryPath: string,
Expand All @@ -24,15 +27,76 @@ export async function deployContract(
deployConfig.storageSlots = storageSlots;
}

console.log('TOML', tomlContents);
// console.log('TOML', tomlContents?.proxy?.enabled);
// console.log('TOML', tomlContents?.proxy?.address);

const abi = JSON.parse(readFileSync(abiPath, 'utf-8'));
const contractFactory = new ContractFactory(bytecode, abi, wallet);
const proxyAbi = Src14OwnedProxy.abi;
const proxyFactory = new Src14OwnedProxyFactory(wallet);

const isProxy = tomlContents?.proxy?.enabled;
const proxyAddress = tomlContents?.proxy?.address;

const deployedContractIds: string[] = [];

if (isProxy) {
if (proxyAddress) {
// If the proxy address is already set, we need to deploy the proxied contract and update the address
// at the proxy address with the new proxied contract ID

// Deploy the proxied contract
const proxiedContractFactory = new ContractFactory(bytecode, abi, wallet);
const { waitForResult: waitForProxied } = await proxiedContractFactory.deploy(deployConfig);
const { contract: proxiedContract } = await waitForProxied();

// Update the contract at the proxy address with the new proxied contract ID
const proxyContract = new Contract(proxyAddress, proxyAbi, wallet);
const { waitForResult: waitForProxyUpdate } = await proxyContract.functions
.set_proxy_target({ bits: proxiedContract.id.toB256() })
.call();
await waitForProxyUpdate();

// Return the proxied contract ID
deployedContractIds.push(proxiedContract.id.toB256());
} else {
// If the proxy address is not set, we need to deploy the proxy and the proxied contract and
// set the proxy address in the proxied contracts TOML file

const { waitForResult } = await contractFactory.deploy(deployConfig);
const { contract } = await waitForResult();
// Deploy the proxied contract
const proxiedContractFactory = new ContractFactory(bytecode, abi, wallet);
const { waitForResult: waitForProxied } = await proxiedContractFactory.deploy(deployConfig);
const { contract: proxiedContract } = await waitForProxied();

// Deploy the SRC14 Compliant Proxy Contract
const { waitForResult: waitForProxy } = await proxyFactory.deploy({
...deployConfig,
configurableConstants: {
INITIAL_TARGET: { bits: proxiedContract.id.toB256() },
INITIAL_OWNER: { Initialized: { Address: { bits: wallet.address.toB256() } } },
},
});

// Initialize the proxy contract
const { contract: proxyContract } = await waitForProxy();
const { waitForResult: waitForProxyInit } = await proxyContract.functions
.initialize_proxy()
.call();
await waitForProxyInit();

// Write the address of the proxy contract to proxied TOML
// TODO: Toml.write({ address: proxyContract.id.toB256() })

// Return both contract IDs
deployedContractIds.push(proxyContract.id.toB256());
deployedContractIds.push(proxiedContract.id.toB256());
}
} else {
// If the contract does not have a proxy, we can deploy it as a normal contract

const contractFactory = new ContractFactory(bytecode, abi, wallet);

const { waitForResult } = await contractFactory.deploy(deployConfig);
const { contract } = await waitForResult();

deployedContractIds.push(contract.id.toB256());
}

return contract.id.toB256();
return deployedContractIds;
}
12 changes: 7 additions & 5 deletions packages/fuels/src/cli/commands/deploy/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export async function deploy(config: FuelsConfig) {
contractPath,
});

const contractId = await deployContract(
const deployedContractIds = await deployContract(
wallet,
binaryPath,
abiPath,
Expand All @@ -47,11 +47,13 @@ export async function deploy(config: FuelsConfig) {
tomlContents
);

debug(`Contract deployed: ${projectName} - ${contractId}`);
deployedContractIds.forEach((contractId) => {
debug(`Contract deployed: ${projectName} - ${contractId}`);

contracts.push({
name: contractName,
contractId,
contracts.push({
name: contractName,
contractId,
});
});
}

Expand Down
Loading

0 comments on commit 17f3c40

Please sign in to comment.