Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add OP Sepolia for testing #147

Draft
wants to merge 6 commits into
base: devcon-rc1
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 24 additions & 23 deletions packages/client/guestCheckout_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { describe, it } from "jsr:@std/testing/bdd";
import {
type Account,
type Address,
bytesToBigInt,
createPublicClient,
createWalletClient,
hexToBytes,
Expand All @@ -29,6 +30,8 @@ import {
import {
BlockchainClient,
type ConcreteWalletClient,
getPaymentId,
payTokenPreApproved,
} from "@massmarket/blockchain";
import { discoverRelay, RelayClient } from "./mod.ts";
import type schema from "../schema/mod.ts";
Expand Down Expand Up @@ -295,31 +298,29 @@ describe({
for await (const { event } of stream) {
if (event.updateOrder?.setPaymentDetails) {
const order = event.updateOrder.setPaymentDetails;
const args = [
31337,
order.ttl,
pad(zeroAddress, { size: 32 }), //orderHash
toHex(currency), //currency address
toHex(order.total!.raw!),
toHex(payee), //payee address
false, // is paymentendpoint?
expect(order.total).toBeDefined();
expect(order.ttl).toBeDefined();
expect(order.shopSignature).toBeDefined();
const args = {
chainId: 31337,
ttl: order.ttl,
orderHash: pad(zeroAddress, { size: 32 }),
currencyAddress: toHex(currency),
total: bytesToBigInt(order.total!.raw!),
payeeAddress: toHex(payee),
isPaymentEndpoint: false,
shopId,
toHex(order.shopSignature!.raw!),
];
const paymentId = (await publicClient.readContract({
address: abi.addresses.Payments as Address,
abi: abi.PaymentsByAddress,
functionName: "getPaymentId",
args: [args],
})) as bigint;
expect(toHex(order.paymentId!.raw!)).toEqual(toHex(paymentId));

shopSignature: toHex(order.shopSignature!.raw!),
};
const paymentId = await getPaymentId({
wallet: publicClient,
...args,
});
expect(toHex(paymentId)).toEqual(toHex(order.paymentId!.raw!));
// call the pay function
const hash = await guestWallet.writeContract({
address: abi.addresses.Payments as Address,
abi: abi.PaymentsByAddress,
functionName: "pay",
args: [args],
const hash = await payTokenPreApproved({
wallet: guestWallet,
...args,
});
const receipt = await publicClient.waitForTransactionReceipt({
hash,
Expand Down
59 changes: 28 additions & 31 deletions packages/client/mod_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
import { WebSocket } from "isows";
import {
type Address,
bytesToBigInt,
createPublicClient,
createWalletClient,
hexToBigInt,
hexToBytes,
http,
pad,
Expand All @@ -29,7 +29,11 @@ import {
zeroAddress,
} from "@massmarket/utils";
import * as abi from "@massmarket/contracts";
import { BlockchainClient } from "@massmarket/blockchain";
import {
BlockchainClient,
getPaymentAddressAndID,
payNative,
} from "@massmarket/blockchain";

import { discoverRelay, RelayClient } from "./mod.ts";

Expand Down Expand Up @@ -377,37 +381,30 @@ describe({
for await (const { event } of stream) {
if (event.updateOrder?.setPaymentDetails) {
const paymentDetails = event.updateOrder.setPaymentDetails;
const zeros32Bytes = pad(zeroAddress, { size: 32 });
const total = toHex(paymentDetails.total!.raw!);
const args = [
31337, // chainid
paymentDetails.ttl,
zeros32Bytes,
zeroAddress,
total,
anvilAddress,
false, // is paymentendpoint?
const zeros32Bytes = pad(zeroAddress);
const total = bytesToBigInt(paymentDetails.total!.raw!);
const args = {
chainId: 31337,
ttl: paymentDetails.ttl,
orderHash: zeros32Bytes,
currencyAddress: zeroAddress,
total: total,
payeeAddress: anvilAddress as `0x${string}`,
isPaymentEndpoint: false,
shopId,
toHex(paymentDetails.shopSignature!.raw!),
];
const paymentId = (await publicClient.readContract({
address: abi.addresses.Payments as Address,
abi: abi.PaymentsByAddress,
functionName: "getPaymentId",
args: [args],
})) as bigint;

// TODO: toHex is not padding BigInt coerrect, so this cause random test
// failures
expect(toHex(paymentDetails.paymentId!.raw!)).toEqual(
toHex(paymentId),
shopSignature: toHex(paymentDetails.shopSignature!.raw!),
};
const calculatedPaymentInfo = await getPaymentAddressAndID({
wallet: publicClient,
refundAddress: args.payeeAddress,
...args,
});
expect(toHex(calculatedPaymentInfo.id)).toEqual(
toHex(paymentDetails.paymentId!.raw!),
);
const hash = await wallet.writeContract({
address: abi.addresses.Payments as Address,
abi: abi.PaymentsByAddress,
functionName: "pay",
args: [args],
value: hexToBigInt(total),
const hash = await payNative({
wallet,
...args,
});
const receipt = await publicClient.waitForTransactionReceipt({
hash,
Expand Down
4 changes: 4 additions & 0 deletions packages/contracts/tokenAddresses.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,9 @@
"11155111": {
"chainName": "Sepolia",
"USDC": "0x822585D682B973e4b1B47C0311f162b29586DD02"
},
"11155420": {
"chainName": "Optimism Sepolia",
"USDC": "0x5fd84259d66Cd46123540766Be93DFE6D43130D7"
}
}
5 changes: 2 additions & 3 deletions packages/frontend/.env.production
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
NEXT_PUBLIC_RELAY_ENDPOINT=wss://relay-beta.mass.market/v3
NEXT_PUBLIC_RELAY_ENDPOINT=wss://relay-mainnet.mass.market/v3
NEXT_PUBLIC_RELAY_TOKEN_ID=0x0
NEXT_PUBLIC_CHAIN_NAME=sepolia
NEXT_PUBLIC_CHAIN_NAME=mainnet
NEXT_PUBLIC_SENTRY_DSN=https://[email protected]/2
NEXT_PUBLIC_ETH_RPC_URL=https://1rpc.io/sepolia
NEXT_PUBLIC_MATOMO_URL=https://cdn.matomo.cloud/massmarket.matomo.cloud/container_T3nvm0KZ.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
assert,
formatUnitsFromString,
logger,
padUint256,
zeroAddress,
} from "@massmarket/utils";
import * as abi from "@massmarket/contracts";
Expand Down Expand Up @@ -156,7 +157,7 @@ export default function ChoosePayment({
if (!calculatedPaymentInfo.address) {
throw new Error("No payment address found");
}
if (toHex(calculatedPaymentInfo.id) !== paymentId) {
if (toHex(padUint256(calculatedPaymentInfo.id)) !== paymentId) {
debug(`received payment Id: ${paymentId}`);
debug(`calculated payment Id: ${toHex(calculatedPaymentInfo.id)}`);
throw new Error("Payment ID mismatch");
Expand Down
1 change: 1 addition & 0 deletions packages/frontend/src/app/create-store/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,7 @@ const StoreCreation = () => {
];
// Get all unique chain IDs for selected accepted currencies and add payee for each chain.
const payees = uniqueByChainId.map((chainId) => {
assert(payeeAddress, "payeeAddress is required");
return {
address: payeeAddress,
callAsContract: false,
Expand Down
3 changes: 1 addition & 2 deletions packages/frontend/src/app/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,7 @@ export const isValidHex = (hex: string) => {
};

const defaultRPC =
(process && process.env && process.env.NEXT_PUBLIC_ETH_RPC_URL) ||
"http://localhost:8545";
(process && process.env && process.env.NEXT_PUBLIC_ETH_RPC_URL) || undefined;

export function createPublicClientForChain(chain: Chain) {
return createPublicClient({
Expand Down
11 changes: 9 additions & 2 deletions packages/frontend/src/wagmi.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
import { getDefaultConfig } from "@rainbow-me/rainbowkit";
import { hardhat, mainnet, optimism, sepolia } from "wagmi/chains";
import {
hardhat,
mainnet,
optimism,
optimismSepolia,
sepolia,
} from "wagmi/chains";
import { http } from "wagmi";

export const config = getDefaultConfig({
appName: "Mass Labs",
projectId: "6c432edcd930e0fa2c87a8d940ae5b91",
chains: [mainnet, optimism, sepolia, hardhat],
chains: [mainnet, optimism, sepolia, hardhat, optimismSepolia],
ssr: false,
transports: {
[mainnet.id]: http(),
[sepolia.id]: http(),
[hardhat.id]: http(),
[optimism.id]: http(),
[optimismSepolia.id]: http(),
},
});
12 changes: 10 additions & 2 deletions packages/stateManager/mod.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import { EventEmitter } from "events";
import { Address } from "@ethereumjs/util";
import { bytesToHex, fromBytes, hexToBytes, type PublicClient } from "viem";
import {
bytesToBigInt,
bytesToHex,
fromBytes,
hexToBytes,
type PublicClient,
toHex,
} from "viem";

import {
type ChoosePayment,
Expand Down Expand Up @@ -37,6 +44,7 @@ import {
assertField,
logger,
objectId,
padUint256,
priceToUint256,
} from "@massmarket/utils";

Expand Down Expand Up @@ -853,7 +861,7 @@ class OrderManager extends PublicObjectManager<Order | OrdersByStatus> {
);
assert(pd.ttl, "updateOrder.setPaymentDetails.ttl");
const paymentDetails = {
paymentId: bytesToHex(pd.paymentId.raw, { size: 32 }),
paymentId: toHex(padUint256(bytesToBigInt(pd.paymentId.raw))),
total: fromBytes(pd.total.raw, "bigint").toString(),
shopSignature: bytesToHex(pd.shopSignature.raw),
ttl: pd.ttl,
Expand Down
11 changes: 11 additions & 0 deletions packages/utils/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
formatUnits,
hexToBytes,
numberToBytes,
pad,
parseUnits,
toBytes,
} from "viem";
Expand Down Expand Up @@ -107,6 +108,16 @@ export function randomBytes(n: number) {
return b;
}

// Pad a value to 32 bytes, useful for uint256
export function pad32Bytes(value: Uint8Array) {
// return padBytes(value, 32, false);
return pad(value, { dir: "right", size: 32 });
}

export function padUint256(value: bigint) {
return numberToBytes(value, { size: 32 });
}

export function random32BytesHex() {
return bytesToHex(randomBytes(32));
}
Expand Down
12 changes: 12 additions & 0 deletions packages/utils/mod_test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,19 @@
import { assertEquals } from "jsr:@std/assert";
import { toHex } from "viem";
import * as Utils from "./mod.ts";

Deno.test("objectId should be the 8 bytes", () => {
const id = Utils.objectId();
assertEquals(id.length, 8);
});

Deno.test("padBytes should pad the message to the given length", () => {
const msg = BigInt(123);
assertEquals(toHex(msg), "0x7b");
const padded = Utils.padUint256(msg);
assertEquals(padded.length, 32);
assertEquals(
toHex(padded),
"0x000000000000000000000000000000000000000000000000000000000000007b",
);
});
Loading