Skip to content

Commit

Permalink
Feat/allow encoded message (#115)
Browse files Browse the repository at this point in the history
* allow pre-encoded message

* test with simple transfer

* add changeset

* assert message
  • Loading branch information
gsteenkamp89 authored Dec 30, 2024
1 parent bb73f49 commit 6c46231
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/tidy-kings-kick.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@across-protocol/app-sdk": patch
---

Allow passing of pre encoded message field in getQuote
1 change: 0 additions & 1 deletion apps/example/lib/stake.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { CrossChainAction } from "@across-protocol/app-sdk";
import { Address, encodeFunctionData } from "viem";
import { optimism } from "viem/chains";

Expand Down
6 changes: 5 additions & 1 deletion packages/sdk/src/actions/getQuote.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Address, Hex } from "viem";
import { Address, Hex, isHex } from "viem";
import { Amount, CrossChainAction } from "../types/index.js";
import {
getMultiCallHandlerAddress,
Expand Down Expand Up @@ -129,6 +129,10 @@ export async function getQuote(params: GetQuoteParams): Promise<Quote> {
let message: Hex = "0x";
let recipient = _recipient;

if (isHex(crossChainMessage)) {
message = crossChainMessage;
}

if (crossChainMessage && typeof crossChainMessage === "object") {
if (crossChainMessage.actions.length === 0) {
throw new Error("No 'crossChainMessage.actions' provided");
Expand Down
108 changes: 108 additions & 0 deletions packages/sdk/test/unit/actions/getQuote.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
import { assert, assertType, describe, test } from "vitest";
import { testClient } from "../../common/sdk.js";
import {
buildMulticallHandlerMessage,
getMultiCallHandlerAddress,
type Quote,
type Route,
} from "../../../src/index.js";
import { encodeFunctionData, erc20Abi, parseUnits } from "viem";
import { arbitrum, optimism } from "viem/chains";

// arbitrum USDC
const inputToken = {
address: "0xaf88d065e77c8cC2239327C5EDb3A432268e5831",
symbol: "USDC",
name: "USD Coin",
decimals: 6,
} as const;

// optimism USDC
const outputToken = {
address: "0x0b2C639c533813f4Aa9D7837CAf62653d097Ff85",
symbol: "USDC",
name: "USD Coin",
decimals: 6,
} as const;

const testEoa = "0x9A8f92a830A5cB89a3816e3D267CB7791c16b04D";
const inputAmount = 100;
const inputAmountBN = parseUnits(inputAmount.toString(), inputToken.decimals);
const outputAmountBN = inputAmountBN / 2n; // 50% of input ensures this doesn't fail, since we cannot recompute after getting the initial quote

// ARBITRUM => OPTIMISM
const testRoute: Route = {
originChainId: arbitrum.id,
destinationChainId: optimism.id,
inputToken: inputToken.address,
outputToken: outputToken.address,
inputTokenSymbol: inputToken.symbol,
outputTokenSymbol: outputToken.symbol,
isNative: false,
};

describe("getQuote with Raw Pre-Encoded Message", () => {
test("Gets a quote using a raw pre-encoded message from a test EOA", async () => {
// pre compute a simple transfer message
const calldata = buildMulticallHandlerMessage({
fallbackRecipient: testEoa,
actions: [
{
target: outputToken.address,
value: 0n,
callData: encodeFunctionData({
abi: erc20Abi,
functionName: "transfer",
args: [testEoa, outputAmountBN],
}),
},
],
});

// Invoke getQuote with the raw message
const _quote = await testClient.getQuote({
route: testRoute,
recipient: getMultiCallHandlerAddress(testRoute.destinationChainId),
inputAmount: inputAmountBN,
crossChainMessage: calldata,
// Removed 'sender' as it's not a parameter for getQuote
});

// Assert that a quote was returned
assert(_quote, "No quote returned for the provided route and message");
assertType<Quote>(_quote);

assert.equal(
_quote.deposit.message,
calldata,
"Message should be equal to pre-encoded calldata",
);

// Optional: Additional assertions to verify quote details
assert.equal(
_quote.deposit.inputAmount.toString(),
inputAmountBN.toString(),
"Input amounts should match",
);
assert.equal(
_quote.deposit.originChainId,
testRoute.originChainId,
"Origin chain IDs should match",
);
assert.equal(
_quote.deposit.destinationChainId,
testRoute.destinationChainId,
"Destination chain IDs should match",
);
assert.equal(
_quote.deposit.inputToken,
testRoute.inputToken,
"Input tokens should match",
);
assert.equal(
_quote.deposit.outputToken,
testRoute.outputToken,
"Output tokens should match",
);
});
});

0 comments on commit 6c46231

Please sign in to comment.