Skip to content

Commit

Permalink
remove comments, unify naming
Browse files Browse the repository at this point in the history
  • Loading branch information
BlaineHeffron committed May 29, 2024
1 parent 33d9ef9 commit d560018
Showing 1 changed file with 23 additions and 28 deletions.
51 changes: 23 additions & 28 deletions test/e2e/src/test-swap.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ const amountAToSwap = 2n;
const amountBToSwap = 1n;

describe("Swap Contract Tests", function () {
let context = {};

before(async function () {
const alice = await generateFundedKeypair();
Expand Down Expand Up @@ -49,7 +48,7 @@ describe("Swap Contract Tests", function () {
await tokenB.mint({ amount: amountBToSwap, to: bob.publicKey() })
).signAndSend();

context = {
this.context = {
root,
alice,
bob,
Expand All @@ -63,11 +62,11 @@ describe("Swap Contract Tests", function () {
});

it("calling `signAndSend()` too soon throws descriptive error", async function() {
const tx = await context.swapContractAsRoot.swap({
a: context.alice.publicKey(),
b: context.bob.publicKey(),
token_a: context.tokenAId,
token_b: context.tokenBId,
const tx = await this.context.swapContractAsRoot.swap({
a: this.context.alice.publicKey(),
b: this.context.bob.publicKey(),
token_a: this.context.tokenAId,
token_b: this.context.tokenBId,
amount_a: amountAToSwap,
min_a_for_b: amountAToSwap,
amount_b: amountBToSwap,
Expand All @@ -83,18 +82,14 @@ describe("Swap Contract Tests", function () {
expect(error.message).to.match(/needsNonInvokerSigningBy/);
}
});
/*const error = await expect(await tx.signAndSend()).to.be.rejectedWith(contract.AssembledTransaction.Errors.NeedsMoreSignatures);
expect(error).to.be.an.instanceof(contract.AssembledTransaction.Errors.NeedsMoreSignatures,
`error is not of type 'NeedsMoreSignaturesError'; instead it is of type '${error?.constructor.name}'`);
expect(error).to.have.property('message').that.matches(/needsNonInvokerSigningBy/);*/
});

it("alice swaps bob 10 A for 1 B", async function() {
const tx = await context.swapContractAsRoot.swap({
a: context.alice.publicKey(),
b: context.bob.publicKey(),
token_a: context.tokenAId,
token_b: context.tokenBId,
const tx = await this.context.swapContractAsRoot.swap({
a: this.context.alice.publicKey(),
b: this.context.bob.publicKey(),
token_a: this.context.tokenAId,
token_b: this.context.tokenBId,
amount_a: amountAToSwap,
min_a_for_b: amountAToSwap,
amount_b: amountBToSwap,
Expand All @@ -103,32 +98,32 @@ describe("Swap Contract Tests", function () {

const needsNonInvokerSigningBy = await tx.needsNonInvokerSigningBy();
expect(needsNonInvokerSigningBy).to.have.lengthOf(2);
expect(needsNonInvokerSigningBy.indexOf(context.alice.publicKey())).to.equal(0, "needsNonInvokerSigningBy does not have alice's public key!");
expect(needsNonInvokerSigningBy.indexOf(context.bob.publicKey())).to.equal(1, "needsNonInvokerSigningBy does not have bob's public key!");
expect(needsNonInvokerSigningBy.indexOf(this.context.alice.publicKey())).to.equal(0, "needsNonInvokerSigningBy does not have alice's public key!");
expect(needsNonInvokerSigningBy.indexOf(this.context.bob.publicKey())).to.equal(1, "needsNonInvokerSigningBy does not have bob's public key!");

// root serializes & sends to alice
const jsonFromRoot = tx.toJSON();
const { client: clientAlice } = await clientFor("swap", {
keypair: context.alice,
contractId: context.swapId,
keypair: this.context.alice,
contractId: this.context.swapId,
});
const txAlice = clientAlice.txFromJSON(jsonFromRoot);
await txAlice.signAuthEntries();

// alice serializes & sends to bob
const jsonFromAlice = txAlice.toJSON();
const { client: clientBob } = await clientFor("swap", {
keypair: context.bob,
contractId: context.swapId,
keypair: this.context.bob,
contractId: this.context.swapId,
});
const txBob = clientBob.txFromJSON(jsonFromAlice);
await txBob.signAuthEntries();

// bob serializes & sends back to root
const jsonFromBob = txBob.toJSON();
const { client: clientRoot } = await clientFor("swap", {
keypair: context.root,
contractId: context.swapId,
keypair: this.context.root,
contractId: this.context.swapId,
});
const txRoot = clientRoot.txFromJSON(jsonFromBob);

Expand All @@ -140,10 +135,10 @@ describe("Swap Contract Tests", function () {
expect(result.getTransactionResponse).to.have.property('status').that.is.not.equal('FAILED');
expect(result.getTransactionResponse).to.have.property('status', rpc.Api.GetTransactionStatus.SUCCESS);

const aliceTokenABalance = await context.tokenA.balance({ id: context.alice.publicKey() });
const aliceTokenBBalance = await context.tokenB.balance({ id: context.alice.publicKey() });
const bobTokenABalance = await context.tokenA.balance({ id: context.bob.publicKey() });
const bobTokenBBalance = await context.tokenB.balance({ id: context.bob.publicKey() });
const aliceTokenABalance = await this.context.tokenA.balance({ id: this.context.alice.publicKey() });
const aliceTokenBBalance = await this.context.tokenB.balance({ id: this.context.alice.publicKey() });
const bobTokenABalance = await this.context.tokenA.balance({ id: this.context.bob.publicKey() });
const bobTokenBBalance = await this.context.tokenB.balance({ id: this.context.bob.publicKey() });

expect(aliceTokenABalance.result).to.equal(0n);
expect(aliceTokenBBalance.result).to.equal(amountBToSwap);
Expand Down

0 comments on commit d560018

Please sign in to comment.