Deploy and test the 'English Auction'
This test of the EnglishAuction
contract does the following:
- Deploys the auction contract and the NFT contract
- Creates a wallet and starts the auction
- Creates another wallet and makes a bid
- Ends the auction and tracks whether the NFT has been transferred to the winner
Import statements
Import the following components to follow the test:
import {
convertEthToWei,
Faucet,
generateRandomPrivateKey,
HttpTransport,
LocalECDSAKeySigner,
PublicClient,
waitTillCompleted,
WalletV1,
} from "@nilfoundation/niljs";
import { encodeFunctionData, type Abi } from "viem";
Deploy all contracts
To create a new wallet and deploy both contracts:
const SALT = BigInt(Math.floor(Math.random() * 10000));
const client = new PublicClient({
transport: new HttpTransport({
endpoint: RPC_ENDPOINT,
}),
shardId: 1,
});
const faucet = new Faucet(client);
const pkey = generateRandomPrivateKey();
const signer = new LocalECDSAKeySigner({
privateKey: pkey,
});
const pubkey = signer.getPublicKey();
const wallet = new WalletV1({
pubkey: pubkey,
client: client,
signer: signer,
shardId: 1,
salt: SALT,
});
const walletAddress = wallet.address;
await faucet.withdrawToWithRetry(walletAddress, convertEthToWei(10));
await wallet.selfDeploy(true);
const { address: addressNFT, hash: hashNFT } = await wallet.deployContract({
salt: SALT,
shardId: 1,
bytecode: NFT_BYTECODE,
abi: NFT_ABI,
args: [],
feeCredit: 3_000_000n,
});
const receiptsNFT = await waitTillCompleted(client, hashNFT);
const { address: addressAuction, hash: hashAuction } = await wallet.deployContract({
salt: SALT,
shardId: 3,
bytecode: AUCTION_BYTECODE,
value: 50_000n,
abi: AUCTION_ABI,
args: [addressNFT],
feeCredit: 5_000_000n,
});
const receiptsAuction = await waitTillCompleted(client, hashAuction);
Participating in the auction
To start the auction using the 'owner' wallet:
const startAuctionHash = await wallet.sendMessage({
to: addressAuction,
feeCredit: 1_000_000n,
data: encodeFunctionData({
abi: AUCTION_ABI,
functionName: "start",
}),
});
const receiptsStart = await waitTillCompleted(client, startAuctionHash);
To place a bid using another wallet:
const signerTwo = new LocalECDSAKeySigner({
privateKey: pkey,
});
const pubkeyTwo = signer.getPublicKey();
const walletTwo = new WalletV1({
pubkey: pubkeyTwo,
client: client,
signer: signerTwo,
shardId: 2,
salt: SALT,
});
const walletTwoAddress = walletTwo.address;
await faucet.withdrawToWithRetry(walletTwoAddress, convertEthToWei(10));
await walletTwo.selfDeploy(true);
const bidHash = await walletTwo.sendMessage({
to: addressAuction,
feeCredit: 1_000_000n,
data: encodeFunctionData({
abi: AUCTION_ABI,
functionName: "bid",
args: [],
}),
value: 300_000n,
});
const receiptsBid = await waitTillCompleted(client, bidHash);
To end the auction and check for its results:
const endHash = await wallet.sendMessage({
to: addressAuction,
feeCredit: 1_000_000n,
data: encodeFunctionData({
abi: AUCTION_ABI,
functionName: "end",
}),
});
const receiptsEnd = await waitTillCompleted(client, endHash);
const result = await client.getCurrencies(walletTwoAddress, "latest");
console.log(result);