Skip to main content

Deploy and test the swap contract

This test of the SwapMatch contract does the follows:

  • Creates two new smart accounts
  • Deploys the contract
  • Mints two tokens for these smart accounts
  • Places two swap requests (one from Smart account 1 and another from Smart account 2)
  • Evaluates whether the swap requests were matched and if excesses were returned correctly

Create two new smart accounts

const SALT = BigInt(Math.floor(Math.random() * 10000));

const client = new PublicClient({
transport: new HttpTransport({
endpoint: RPC_ENDPOINT,
}),
shardId: 1,
});

const smartAccountOne = await generateSmartAccount({
shardId: 2,
rpcEndpoint: RPC_ENDPOINT,
faucetEndpoint: FAUCET_ENDPOINT,
});

const smartAccountTwo = await generateSmartAccount({
shardId: 3,
rpcEndpoint: RPC_ENDPOINT,
faucetEndpoint: FAUCET_ENDPOINT,
});

const smartAccountOneAddress = smartAccountOne.address;
const smartAccountTwoAddress = smartAccountTwo.address;

Deploy the contract

const smartAccount = await generateSmartAccount({
shardId: 1,
rpcEndpoint: RPC_ENDPOINT,
faucetEndpoint: FAUCET_ENDPOINT,
});

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

const receipts = await waitTillCompleted(client, deploymentTransactionHash);

Mint two new tokens

{
const hashTransaction = await smartAccountOne.mintToken(100_000_000n);
await waitTillCompleted(client, hashTransaction);
}

{
const hashTransaction = await smartAccountTwo.mintToken(100_000_000n);
await waitTillCompleted(client, hashTransaction);
}

Place the first swap request

{
const gasPrice = await client.getGasPrice(smartAccountOne.shardId);
const hashTransaction = await smartAccountOne.sendTransaction({
to: swapMatchAddress,
tokens: [
{
id: smartAccountOneAddress,
amount: 30_000_000n,
},
],
abi: SWAP_MATCH_ABI,
functionName: "placeSwapRequest",
args: [20_000_000n, smartAccountTwoAddress],
feeCredit: gasPrice * 1_000_000n,
});

await waitTillCompleted(client, hashTransaction);
}

Place the second swap request

{
const gasPrice = await client.getGasPrice(smartAccountTwo.shardId);
const hashTransaction = await smartAccountTwo.sendTransaction({
to: swapMatchAddress,
tokens: [
{
id: smartAccountTwoAddress,
amount: 50_000_000n,
},
],
abi: SWAP_MATCH_ABI,
functionName: "placeSwapRequest",
args: [10_000_000n, smartAccountOneAddress],
feeCredit: gasPrice * 1_000_000n,
});

await waitTillCompleted(client, hashTransaction);
}

Check the token amounts

const tokensOne = await client.getTokens(smartAccountOneAddress, "latest");
const tokensTwo = await client.getTokens(smartAccountTwoAddress, "latest");
console.log("Smart account 1 tokens: ", tokensOne);
console.log("Smart account 2 tokens: ", tokensTwo);

Expected output:

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