Skip to content

Commit

Permalink
demo: show how to modify tx with new resource fee
Browse files Browse the repository at this point in the history
it works!
  • Loading branch information
chadoh committed Jun 5, 2024
1 parent 1b6d7aa commit 6197eb8
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 16 deletions.
2 changes: 1 addition & 1 deletion .cargo/config.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# paths = ["/path/to/override"] # path dependency overrides

[alias] # command aliases
install_soroban = "install --version 21.0.0-preview.1 --root ./target soroban-cli --debug"
install_soroban = "install --version 21.0.0-rc.1 --root ./target soroban-cli --debug"
# b = "build --target wasm32-unknown-unknown --release"
# c = "check"
# t = "test"
Expand Down
60 changes: 45 additions & 15 deletions test/e2e/src/test-swap.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
const test = require("ava");
const { contract, rpc } = require("../../..");
const {
contract,
rpc,
SorobanDataBuilder,
xdr,
TransactionBuilder,
} = require("../../..");
const { clientFor, generateFundedKeypair } = require("./util");

const amountAToSwap = 2n;
Expand All @@ -19,7 +25,7 @@ test.before(async (t) => {
});
const { client: swapContractAsRoot, contractId: swapId } = await clientFor(
"swap",
{ keypair: root },
{ keypair: root }
);
await (
await tokenA.initialize({
Expand Down Expand Up @@ -73,7 +79,7 @@ test("calling `signAndSend()` too soon throws descriptive error", async (t) => {
const error = await t.throwsAsync(tx.signAndSend());
t.true(
error instanceof contract.AssembledTransaction.Errors.NeedsMoreSignatures,
`error is not of type 'NeedsMoreSignaturesError'; instead it is of type '${error?.constructor.name}'`,
`error is not of type 'NeedsMoreSignaturesError'; instead it is of type '${error?.constructor.name}'`
);
if (error) t.regex(error.message, /needsNonInvokerSigningBy/);
});
Expand All @@ -95,12 +101,12 @@ test("alice swaps bob 10 A for 1 B", async (t) => {
t.is(
needsNonInvokerSigningBy.indexOf(t.context.alice.publicKey()),
0,
"needsNonInvokerSigningBy does not have alice's public key!",
"needsNonInvokerSigningBy does not have alice's public key!"
);
t.is(
needsNonInvokerSigningBy.indexOf(t.context.bob.publicKey()),
1,
"needsNonInvokerSigningBy does not have bob's public key!",
"needsNonInvokerSigningBy does not have bob's public key!"
);

// root serializes & sends to alice
Expand Down Expand Up @@ -129,20 +135,44 @@ test("alice swaps bob 10 A for 1 B", async (t) => {
});
const txRoot = clientRoot.txFromJSON(jsonFromBob);

const bumpedResourceFee = xdr.Int64.fromString(
(
Number(txRoot.simulationData.transactionData.resourceFee()) + 100000
).toString()
);

t.log("setting new tx data", txRoot.simulationData.transactionData.toXDR(), {
bumpedResourceFee,
});

const newSorobanData = new SorobanDataBuilder(
txRoot.simulationData.transactionData.toXDR()
)
.setResourceFee(bumpedResourceFee.toBigInt())
.build();

t.log({ newSorobanData });
t.log({ tx: txRoot.built });

const result = await txRoot.signAndSend();

txRoot.built = TransactionBuilder.cloneFrom(txRoot.built, {
fee: txRoot.built.fee,
sorobanData: newSorobanData,
}).build();

t.truthy(
result.sendTransactionResponse,
`tx failed: ${JSON.stringify(result, null, 2)}`,
`tx failed: ${JSON.stringify(result, null, 2)}`
);
t.is(
result.sendTransactionResponse.status,
"PENDING",
`tx failed: ${JSON.stringify(result, null, 2)}`,
`tx failed: ${JSON.stringify(result, null, 2)}`
);
t.truthy(
result.getTransactionResponseAll?.length,
`tx failed: ${JSON.stringify(result.getTransactionResponseAll, null, 2)}`,
`tx failed: ${JSON.stringify(result.getTransactionResponseAll, null, 2)}`
);
t.not(
result.getTransactionResponse.status,
Expand All @@ -153,31 +183,31 @@ test("alice swaps bob 10 A for 1 B", async (t) => {
.value()
.map((op) => op.value()?.value().switch()),
null,
2,
)}`,
2
)}`
);
t.is(
result.getTransactionResponse.status,
rpc.Api.GetTransactionStatus.SUCCESS,
`tx failed: ${JSON.stringify(result.getTransactionResponse, null, 2)}`,
`tx failed: ${JSON.stringify(result.getTransactionResponse, null, 2)}`
);

t.is(
(await t.context.tokenA.balance({ id: t.context.alice.publicKey() }))
.result,
0n,
0n
);
t.is(
(await t.context.tokenB.balance({ id: t.context.alice.publicKey() }))
.result,
amountBToSwap,
amountBToSwap
);
t.is(
(await t.context.tokenA.balance({ id: t.context.bob.publicKey() })).result,
amountAToSwap,
amountAToSwap
);
t.is(
(await t.context.tokenB.balance({ id: t.context.bob.publicKey() })).result,
0n,
0n
);
});

0 comments on commit 6197eb8

Please sign in to comment.