Skip to content
Merged
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
34 changes: 34 additions & 0 deletions dts.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import replace from '@rollup/plugin-replace';
import { config as loadEnv } from 'dotenv';
import type { DtsConfig } from 'dts-cli/dist/types';

loadEnv();

const code = process.env.BASE_BUILDER_CODE || '';
if (code && (code.length > 255 || !/^[\x21-\x2b\x2d-\x7e]+$/.test(code))) {
throw new Error(
'BASE_BUILDER_CODE must be 1-255 printable ASCII bytes without commas'
);
}
const suffix = code
? `0x${Buffer.from(code, 'ascii').toString('hex')}${code.length
.toString(16)
.padStart(2, '0')}0080218021802180218021802180218021`
: '';

const config: DtsConfig = {
rollup(config) {
return {
...config,
plugins: [
replace({
'process.env.BASE_BUILDER_CODE_SUFFIX': JSON.stringify(suffix),
preventAssignment: true,
}),
...(Array.isArray(config.plugins) ? config.plugins : []),
],
};
},
};

export default config;
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@velora-dex/sdk",
"version": "11.0.0",
"version": "11.0.1",
"main": "dist/index.js",
"module": "dist/sdk.esm.js",
"typings": "dist/index.d.ts",
Expand Down Expand Up @@ -59,6 +59,7 @@
"@ethersproject/bignumber": "^5.8.0",
"@ethersproject/contracts": "^5.8.0",
"@ethersproject/providers": "^5.8.0",
"@rollup/plugin-replace": "^5.0.7",
"@size-limit/preset-small-lib": "^12.1.0",
"@tsconfig/recommended": "^1.0.8",
"@types/jest": "^30.0.0",
Expand Down
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions src/helpers/buildConfig.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const BASE_BUILDER_CODE_SUFFIX = process.env.BASE_BUILDER_CODE_SUFFIX as
| `0x${string}`
| '';
67 changes: 67 additions & 0 deletions src/helpers/providers/dataSuffix.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import type { Signer as EthersV5Signer } from '@ethersproject/abstract-signer';
import type { ContractRunner, Signer as EthersV6Signer } from 'ethers';
import { bytesToHex } from 'viem';

export type WithDataSuffixInput<T> = { signer: T; suffix?: string };

export function withEthersV5DataSuffix({
signer,
suffix,
}: WithDataSuffixInput<EthersV5Signer>): EthersV5Signer {
if (!suffix) return signer;
return {
_isSigner: true,
provider: signer.provider,
getAddress: signer.getAddress.bind(signer),
signMessage: signer.signMessage.bind(signer),
signTransaction: signer.signTransaction.bind(signer),
getBalance: signer.getBalance.bind(signer),
getTransactionCount: signer.getTransactionCount.bind(signer),
estimateGas: signer.estimateGas.bind(signer),
call: signer.call.bind(signer),
getChainId: signer.getChainId.bind(signer),
getGasPrice: signer.getGasPrice.bind(signer),
getFeeData: signer.getFeeData.bind(signer),
resolveName: signer.resolveName.bind(signer),
checkTransaction: signer.checkTransaction.bind(signer),
populateTransaction: signer.populateTransaction.bind(signer),
_checkProvider: signer._checkProvider.bind(signer),
connect: (provider) =>
withEthersV5DataSuffix({ signer: signer.connect(provider), suffix }),
// Contract retains its receipt decoding; the signer receives final calldata.
sendTransaction: async (tx) =>
signer.sendTransaction({
...tx,
data: appendSuffix(await tx.data, suffix),
}),
Comment thread
Velenir marked this conversation as resolved.
Resolved
};
}

export function withEthersV6DataSuffix({
signer,
suffix,
}: WithDataSuffixInput<EthersV6Signer>): ContractRunner {
if (!suffix) return signer;
return {
provider: signer.provider,
resolveName: signer.resolveName.bind(signer),
sendTransaction: (tx) =>
signer.sendTransaction({
...tx,
data: appendSuffix(tx.data, suffix),
}),
};
}

function appendSuffix(
data: string | ArrayLike<number> | null | undefined,
suffix: string
): string {
const hex =
typeof data === 'string'
? data
: data
? bytesToHex(Uint8Array.from(data))
: '0x';
return `${hex}${suffix.slice(2)}`;
}
7 changes: 6 additions & 1 deletion src/helpers/providers/ethers.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { withEthersV5DataSuffix } from './dataSuffix';
import type {
Address,
ContractCallerFunctions,
Expand Down Expand Up @@ -82,7 +83,11 @@ export const constructEthersV5ContractCaller = (
? providerOrSigner.getSigner(account)
: providerOrSigner;

const contract = new Contract(address, abi, signer);
const contract = new Contract(
address,
abi,
withEthersV5DataSuffix({ signer, suffix: params.dataSuffix })
);

assertEthersContractHasMethodsV5(contract, contractMethod);
// drop keys not in PayableOverrides
Expand Down
7 changes: 6 additions & 1 deletion src/helpers/providers/ethersV6.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { withEthersV6DataSuffix } from './dataSuffix';
import type {
Address,
ContractCallerFunctions,
Expand Down Expand Up @@ -64,7 +65,11 @@ export const constructContractCaller = (
? await providerOrSigner.getSigner(account)
: providerOrSigner;

const contract = new Contract(address, abi, signer);
const contract = new Contract(
address,
abi,
withEthersV6DataSuffix({ signer, suffix: params.dataSuffix })
);

// drop keys not in PayableOverrides
const { gas, from, ...restOverrides } = overrides;
Expand Down
1 change: 1 addition & 0 deletions src/helpers/providers/viem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ export const constructContractCaller = (
abi,
functionName: contractMethod,
args: argsToViemArgs(args),
dataSuffix: params.dataSuffix,
value: viemTxParams.value,
gas: viemTxParams.gas,
// ...viemTxParams,
Expand Down
10 changes: 10 additions & 0 deletions src/helpers/providers/web3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,16 @@ export const constructContractCaller = (

const preparedCall = contract.methods[contractMethod](...args);

if (params.dataSuffix) {
const data = `${preparedCall.encodeABI()}${params.dataSuffix.slice(2)}`;
contract.setTransactionMiddleware({
processTransaction: async (tx) => ({
...tx,
data,
...(tx.input !== undefined ? { input: data } : {}),
}),
});
}
const promiEvent = preparedCall.send(normalizedOverrides);

// can't just return promiEvent, because async function returns a Promise<PromiEvent>
Expand Down
7 changes: 7 additions & 0 deletions src/methods/otcOrders/fillOrderDirectly.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { BASE_BUILDER_CODE_SUFFIX } from '../../helpers/buildConfig';
import type { ExtractAbiMethodNames } from '../../helpers/misc';
import type {
ConstructProviderFetchInput,
Expand Down Expand Up @@ -210,6 +211,10 @@ export function constructFillOTCOrder<T>(
// getAugustusRFQ is cached internally for the same instance of SDK
// so should persist across same apiUrl & network
const { getAugustusRFQ } = constructGetSpender(options);
const dataSuffix =
options.chainId === 8453 && BASE_BUILDER_CODE_SUFFIX
? BASE_BUILDER_CODE_SUFFIX
: undefined;

const fillOTCOrder: FillOTCOrcer<T> = async (
{ order, signature, takerPermit },
Expand All @@ -227,6 +232,7 @@ export function constructFillOTCOrder<T>(
contractMethod: 'fillOrder',
args: [sanitizedOrder, signature],
overrides,
dataSuffix,
});

return res;
Expand Down Expand Up @@ -268,6 +274,7 @@ export function constructFillOTCOrder<T>(
'0x', // permitMakerAsset, unused because hard to account for changing nonce for long running Orders
],
overrides,
dataSuffix,
});

return res;
Expand Down
3 changes: 3 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import type {
import type { SignableTypedData } from './methods/common/orders/buildOrderData';
import { TransactionParams } from './methods/swap/transaction';
import type { MarkRequired, Prettify } from 'ts-essentials';
import type { Hex } from 'viem';

export type {
Address,
Expand Down Expand Up @@ -105,6 +106,8 @@ export interface ContractCallStaticInput<T extends string>
interface ContractCallTransactionInput<T extends string>
extends ContractCallInput<T> {
overrides: TxSendOverrides;
/** Hex-encoded suffix to append before sending; custom callers must honor it. */
dataSuffix?: Hex;
}

// may have to type result T differently if we ever use staticCalls in SDK
Expand Down
Loading
Loading