Skip to main content

Deploy and test the swap contract

This test of the SwapMatch contract does the follows:

  • Creates two new wallets
  • Deploys the contract
  • Mints two currencies for these wallets
  • Places two swap requests (one from Wallet 1 and another from Wallet 2)
  • Evaluates whether the swap requests were matched and if excesses were returned correctly

Create two new wallets

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 signer = new LocalECDSAKeySigner({
privateKey: generateRandomPrivateKey(),
});

const pubkey = signer.getPublicKey();

const walletOne = new WalletV1({
pubkey: pubkey,
client,
signer,
shardId: 2,
salt: SALT,
});

const walletTwo = new WalletV1({
pubkey: pubkey,
client,
signer,
shardId: 3,
salt: SALT,
});

const walletOneAddress = walletOne.address;
const walletTwoAddress = walletTwo.address;

const fundingWalletOne = await faucet.withdrawToWithRetry(
walletOneAddress,
convertEthToWei(10),
);

const fundingWalletTwo = await faucet.withdrawToWithRetry(
walletTwoAddress,
convertEthToWei(10),
);

await walletOne.selfDeploy(true);
await walletTwo.selfDeploy(true);

Deploy the contract

const wallet = new WalletV1({
pubkey: pubkey,
client,
signer,
shardId: 1,
salt: SALT,
});

const fundingNewWallet = await faucet.withdrawToWithRetry(
wallet.address,
convertEthToWei(10),
);

await wallet.selfDeploy(true);

const { address: swapMatchAddress, hash: deploymentMessageHash } =
await wallet.deployContract({
bytecode: SWAP_MATCH_BYTECODE,
value: 0n,
feeCredit: 100_000_000n,
salt: SALT,
shardId: 4,
});

const receipts = await waitTillCompleted(client, deploymentMessageHash);

Mint two new currencies

{
const hashMessage = await walletOne.mintCurrency(100_000_000n);
await waitTillCompleted(client, hashMessage);
}

{
const hashMessage = await walletTwo.mintCurrency(100_000_000n);
await waitTillCompleted(client, hashMessage);
}

Place the first swap request

{
const gasPrice = await client.getGasPrice(2);
const hashMessage = await walletOne.sendMessage({
to: swapMatchAddress,
tokens: [
{
id: walletOneAddress,
amount: 30_000_000n,
},
],
abi: SWAP_MATCH_ABI,
functionName: "placeSwapRequest",
args: [20_000_000n, walletTwoAddress],
feeCredit: gasPrice * 1_000_000_000n,
});

await waitTillCompleted(client, hashMessage);
}

Place the second swap request

{
const gasPrice = await client.getGasPrice(3);
const hashMessage = await walletTwo.sendMessage({
to: swapMatchAddress,
tokens: [
{
id: walletTwoAddress,
amount: 50_000_000n,
},
],
abi: SWAP_MATCH_ABI,
functionName: "placeSwapRequest",
args: [10_000_000n, walletOneAddress],
feeCredit: gasPrice * 1_000_000_000n,
});

await waitTillCompleted(client, hashMessage);
}

Check the token amounts

const tokensOne = await client.getCurrencies(walletOneAddress, "latest");
const tokensTwo = await client.getCurrencies(walletTwoAddress, "latest");
console.log("Wallet 1 tokens: ", tokensOne);
console.log("Wallet 2 tokens: ", tokensTwo);

Expected output:

Wallet 1 tokens:  {
TOKEN_ONE_ID: 90000000n,
TOKEN_TWO_ID: 20000000n
}
Wallet 2 tokens: {
TOKEN_ONE_ID: 10000000n,
TOKEN_TWO_ID: 80000000n
}