Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 4 additions & 14 deletions clijs/src/commands/smart-account/new.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ export default class SmartAccountNew extends BaseCommand {
throw new Error("Faucet client is not initialized");
}

logger.debug("Deploying new smart-account");

const pubkey = signer.getPublicKey();
const smartAccount = new SmartAccountV1({
pubkey: pubkey,
Expand All @@ -91,20 +93,8 @@ export default class SmartAccountNew extends BaseCommand {
});
const smartAccountAddress = smartAccount.address;

logger.debug(`Withdrawing ${flags.amount} to ${smartAccountAddress}`);

const faucets = await this.faucetClient.getAllFaucets();
await this.faucetClient.topUpAndWaitUntilCompletion(
{
amount: flags.amount,
smartAccountAddress: smartAccountAddress,
faucetAddress: faucets.NIL,
},
this.rpcClient,
);

const tx = await smartAccount.selfDeploy(true);
this.info(`Successfully deployed smart account with tx hash: ${tx.hash}`);
const address = await smartAccount.selfDeploy(this.faucetClient);
this.info(`Successfully deployed smart account at: ${address}`);

if (this.quiet) {
this.log(smartAccountAddress);
Expand Down
15 changes: 1 addition & 14 deletions clijs/test/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import {
LocalECDSAKeySigner,
SmartAccountV1,
generateRandomPrivateKey,
waitTillCompleted,
} from "@nilfoundation/niljs";
import { PublicClient } from "@nilfoundation/niljs";
import type { Errors } from "@oclif/core";
Expand Down Expand Up @@ -126,19 +125,7 @@ export const CliTest = test.extend<CliTestFixture>({
signer: signer,
});

const faucets = await faucetClient.getAllFaucets();
await faucetClient.topUpAndWaitUntilCompletion(
{
faucetAddress: faucets.NIL,
smartAccountAddress: smartAccount.address,
amount: 1_000_000_000_000_000_000n,
},
rpcClient,
);

const deployTx = await smartAccount.selfDeploy(true);

await waitTillCompleted(rpcClient, deployTx.hash);
await smartAccount.selfDeploy(faucetClient);
await use(smartAccount);
},
});
33 changes: 0 additions & 33 deletions docs/nil/nilcli/deploying-smart-contract.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -25,36 +25,3 @@ To deploy the manufactuer with the given public key and the address of the retai
```

This will make sure that `Manufacturer.sol` and `Retailer.sol` are on different shards.

## External deployment

First, calulate the address of the retailer contract and send funds to this address:

```bash file=../../tests/working-with-smart-contracts-cli.test.mjs start=startExternalRetailerAddressCommand end=endExternalRetailerAddressCommand
```

```bash file=../../tests/working-with-smart-contracts-cli.test.mjs start=startSendTokensToRetailerForExternalDeployment end=endSendTokensToRetailerForExternalDeployment
```

Then, deploy the retailer contract while providing the same `SALT`:

```bash file=../../tests/working-with-smart-contracts-cli.test.mjs start=startRetailerExternalDeploymentCommand end=endRetailerExternalDeploymentCommand
```

Retrieve the public key assigned to the smart account:

```bash file=../../tests/commands.mjs start=startInfo end=endInfo
```

Calculate the address of the manufacturer contract and send funds to it:

```bash file=../../tests/working-with-smart-contracts-cli.test.mjs start=startExternalManufacturerAddressCommand end=endExternalManufacturerAddressCommand
```

```bash file=../../tests/working-with-smart-contracts-cli.test.mjs start=startSendTokensToManufacturerForExternalDeploymentCommand end=endSendTokensToManufacturerForExternalDeploymentCommand
```

Deploy the manufacturer contract:

```bash file=../../tests/working-with-smart-contracts-cli.test.mjs start=startManufacturerExternalDeploymentCommand end=endManufacturerExternalDeploymentCommand
```
50 changes: 0 additions & 50 deletions docs/tests/working-with-smart-contracts-cli.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -93,54 +93,4 @@ describe.sequential("CLI deployment tests", async () => {
expect(stdout).toMatch(/new-product/);
},
);

test.sequential("external deployment of Retailer and Manufacturer is successful", async () => {
const SALT = BigInt(Math.floor(Math.random() * 10000));
//startExternalRetailerAddressCommand
const RETAILER_ADDRESS_COMMAND = `${NIL_GLOBAL} contract address ./tests/Retailer/Retailer.bin --shard-id 1 --salt ${SALT} ${CONFIG_FLAG}`;
//endExternalRetailerAddressCommand

let { stdout, stderr } = await exec(RETAILER_ADDRESS_COMMAND);
expect(stdout).toMatch(ADDRESS_PATTERN);
let addressMatches = stdout.match(ADDRESS_PATTERN);
RETAILER_ADDRESS = addressMatches[0];

const AMOUNT = 10000000;

//startSendTokensToRetailerForExternalDeploymentCommand
const RETAILER_SEND_TOKENS_COMMAND_EXTERNAL = `${NIL_GLOBAL} smart-account send-tokens ${RETAILER_ADDRESS} ${AMOUNT} ${CONFIG_FLAG}`;
//endSendTokensToRetailerForExternalDeploymentCommand

await exec(RETAILER_SEND_TOKENS_COMMAND_EXTERNAL);

//startRetailerExternalDeploymentCommand
const RETAILER_EXTERNAL_DEPLOYMENT_COMMAND = `${NIL_GLOBAL} contract deploy ./tests/Retailer/Retailer.bin --shard-id 1 --salt ${SALT} ${CONFIG_FLAG}`;
//endRetailerExternalDeploymentCommand

({ stdout, stderr } = await exec(RETAILER_EXTERNAL_DEPLOYMENT_COMMAND));
expect(stdout).toBeDefined;
expect(stdout).toMatch(CONTRACT_ADDRESS_PATTERN);

//startExternalManufacturerAddressCommand
const MANUFACTURER_ADDRESS_COMMAND = `${NIL_GLOBAL} contract address ./tests/Manufacturer/Manufacturer.bin ${PUBKEY} ${RETAILER_ADDRESS} --shard-id 2 --salt ${SALT} ${CONFIG_FLAG} --abi ./tests/Manufacturer/Manufacturer.abi`;
//endExternalManufacturerAddressCommand

({ stdout, stderr } = await exec(MANUFACTURER_ADDRESS_COMMAND));
addressMatches = stdout.match(ADDRESS_PATTERN);
MANUFACTURER_ADDRESS = addressMatches[0];

//startSendTokensToManufacturerForExternalDeploymentCommand
const MANUFACTURER_SEND_TOKENS_COMMAND_EXTERNAL = `${NIL_GLOBAL} smart-account send-tokens ${MANUFACTURER_ADDRESS} ${AMOUNT} ${CONFIG_FLAG}`;
//endSendTokensToManufacturerForExternalDeploymentCommand

await exec(MANUFACTURER_SEND_TOKENS_COMMAND_EXTERNAL);

//startManufacturerExternalDeploymentCommand
const MANUFACTURER_EXTERNAL_DEPLOYMENT_COMMAND = `${NIL_GLOBAL} contract deploy ./tests/Manufacturer/Manufacturer.bin ${PUBKEY} ${RETAILER_ADDRESS} --salt ${SALT} --shard-id 2 --abi ./tests/Manufacturer/Manufacturer.abi ${CONFIG_FLAG}`;
//endManufacturerExternalDeploymentCommand

({ stdout, stderr } = await exec(MANUFACTURER_EXTERNAL_DEPLOYMENT_COMMAND));
expect(stdout).toBeDefined;
expect(stdout).toMatch(CONTRACT_ADDRESS_PATTERN);
});
});
2 changes: 1 addition & 1 deletion explorer_frontend/src/features/account-connector/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ createSmartAccountFx.use(async ({ privateKey, rpcUrl }) => {

const code = await client.getCode(smartAccount.address);
if (code.length === 0) {
await smartAccount.selfDeploy(true);
await smartAccount.selfDeploy(faucetClient);
}

setInitializingSmartAccountState("Adding some tokens...");
Expand Down
21 changes: 4 additions & 17 deletions hardhat-plugin/src/core/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import {
LocalECDSAKeySigner,
type PublicClient,
SmartAccountV1,
convertEthToWei,
generateRandomPrivateKey,
topUp,
} from "@nilfoundation/niljs";
Expand All @@ -25,23 +24,10 @@ export async function deployWallet(
signer,
});

if (faucetClient) {
const faucets = await faucetClient.getAllFaucets();
await faucetClient.topUpAndWaitUntilCompletion(
{
amount: convertEthToWei(1),
smartAccountAddress: address,
faucetAddress: faucets.NIL,
},
client,
);
console.log("Faucet depositing to smart account", smartAccount.address);
}

const deployed = await smartAccount.checkDeploymentStatus();
if (!deployed) {
if (faucetClient && !deployed) {
console.log("Deploying smartAccount", smartAccount.address);
await smartAccount.selfDeploy(true);
await smartAccount.selfDeploy(faucetClient);
}
return smartAccount;
}
Expand All @@ -51,6 +37,7 @@ export async function createSmartAccount(
config: CreateSmartAccountConfig,
): Promise<SmartAccountV1> {
const client = hre.nil.getPublicClient();
const faucetClient = hre.nil.getFaucetClient();
const pk = generateRandomPrivateKey();
const signer = new LocalECDSAKeySigner({
privateKey: pk,
Expand All @@ -67,7 +54,7 @@ export async function createSmartAccount(
await topUpSmartAccount(smartAccount.address, (hre.network.config as HttpNetworkConfig).url);
}

await smartAccount.selfDeploy(true);
await smartAccount.selfDeploy(faucetClient);
console.log("SmartAccount PK:", pk);
return smartAccount;
}
Expand Down
7 changes: 7 additions & 0 deletions hardhat-plugin/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { extendEnvironment } from "hardhat/config";
import "./tasks/wallet";
import {
FaucetClient,
HttpTransport,
LocalECDSAKeySigner,
PublicClient,
Expand All @@ -22,6 +23,9 @@ extendEnvironment((hre) => {
const publicClient = new PublicClient({
transport: nilProvider,
});
const faucetClient = new FaucetClient({
transport: nilProvider,
});

const pk = <`0x${string}`>`0x${process.env.PRIVATE_KEY}`;
const signer = new LocalECDSAKeySigner({
Expand All @@ -30,6 +34,9 @@ extendEnvironment((hre) => {

hre.nil = {
provider: publicClient,
getFaucetClient: () => {
return faucetClient;
},
getPublicClient: () => {
return publicClient;
},
Expand Down
2 changes: 2 additions & 0 deletions hardhat-plugin/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type {
CommonReadContractMethods,
CommonWriteContractMethods,
FaucetClient,
IAddress,
ISigner,
PublicClient,
Expand Down Expand Up @@ -54,6 +55,7 @@ export declare function createSmartAccount(
export type NilHelper = {
provider: PublicClient;
getPublicClient: () => PublicClient;
getFaucetClient: () => FaucetClient;
getSmartAccount: () => Promise<SmartAccountInterface>;
getContractAt: typeof getContractAt;
deployContract: typeof deployContract;
Expand Down
1 change: 0 additions & 1 deletion nil/cmd/nil/internal/contract/contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ func GetCommand(cfg *common.Config) *cobra.Command {
GetTokensCommand(cfg),
GetCodeCommand(cfg),
GetCallReadonlyCommand(cfg),
GetDeployCommand(cfg),
GetSendExternalTransactionCommand(cfg),
GetEstimateFeeCommand(cfg),
GetTopUpCommand(cfg),
Expand Down
108 changes: 0 additions & 108 deletions nil/cmd/nil/internal/contract/deploy.go

This file was deleted.

4 changes: 2 additions & 2 deletions nil/cmd/nil/internal/smartaccount/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ func runNew(cmd *cobra.Command, _ []string, cfg *common.Config, params *smartAcc
}
srv := cliservice.NewService(cmd.Context(), common.GetRpcClient(), cfg.PrivateKey, faucet)
check.PanicIfNotf(cfg.PrivateKey != nil, "A private key is not set in the config file")
smartAccountAddress, err := srv.CreateSmartAccount(params.shardId, &params.salt, amount,
types.NewFeePackFromFeeCredit(params.Fee.FeeCredit), &cfg.PrivateKey.PublicKey)

smartAccountAddress, err := srv.DeploySmartAccount(params.shardId, cfg.PrivateKey, params.salt, amount)
if err != nil {
return err
}
Expand Down
Loading