@polymarket/relayer-client 1.0.18 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +30 -0
- package/dist/auth/handler.js +24 -25
- package/dist/builder/create.d.ts +3 -2
- package/dist/builder/create.js +44 -47
- package/dist/builder/derive.d.ts +2 -0
- package/dist/builder/derive.js +21 -0
- package/dist/builder/index.d.ts +1 -1
- package/dist/builder/index.js +1 -1
- package/dist/builder/proxy.d.ts +3 -2
- package/dist/builder/proxy.js +64 -83
- package/dist/builder/safe.d.ts +4 -3
- package/dist/builder/safe.js +52 -77
- package/dist/client.d.ts +10 -8
- package/dist/client.js +119 -133
- package/dist/config/index.d.ts +13 -0
- package/dist/config/index.js +35 -0
- package/dist/constants/index.d.ts +0 -4
- package/dist/constants/index.js +1 -5
- package/dist/encode/index.d.ts +0 -1
- package/dist/encode/index.js +0 -1
- package/dist/encode/proxy.d.ts +1 -1
- package/dist/encode/proxy.js +12 -7
- package/dist/encode/safe.d.ts +1 -1
- package/dist/encode/safe.js +11 -9
- package/dist/http-helpers/index.js +33 -35
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/manual/index.d.ts +4 -4
- package/dist/manual/index.js +36 -17
- package/dist/response/index.d.ts +21 -2
- package/dist/response/index.js +41 -3
- package/dist/types.d.ts +8 -3
- package/dist/utils/index.d.ts +6 -0
- package/dist/utils/index.js +28 -0
- package/package.json +21 -28
- package/dist/builder/utils.d.ts +0 -2
- package/dist/builder/utils.js +0 -13
- package/dist/encode/utils.d.ts +0 -6
- package/dist/encode/utils.js +0 -46
- package/dist/response/relayer.d.ts +0 -12
- package/dist/response/relayer.js +0 -28
- package/dist/response/viem.d.ts +0 -8
- package/dist/response/viem.js +0 -19
- package/dist/signeradapter/adapter.d.ts +0 -20
- package/dist/signeradapter/adapter.js +0 -188
- package/dist/signeradapter/index.d.ts +0 -2
- package/dist/signeradapter/index.js +0 -5
- package/dist/signeradapter/interfaces.d.ts +0 -8
- package/dist/signeradapter/interfaces.js +0 -2
package/README.md
CHANGED
|
@@ -1 +1,31 @@
|
|
|
1
1
|
## relayer-client
|
|
2
|
+
|
|
3
|
+
### Install
|
|
4
|
+
|
|
5
|
+
```bash
|
|
6
|
+
npm install @polymarket/relayer-client
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
### Usage
|
|
10
|
+
|
|
11
|
+
```ts
|
|
12
|
+
import { ethers } from "ethers";
|
|
13
|
+
import { createWalletClient, Hex, http } from "viem";
|
|
14
|
+
import { privateKeyToAccount } from "viem/accounts";
|
|
15
|
+
import { RelayClient } from "@polymarket/relayer-client";
|
|
16
|
+
|
|
17
|
+
const relayerUrl = `${process.env.POLYMARKET_RELAYER_URL}`;
|
|
18
|
+
const chainId = parseInt(`${process.env.CHAIN_ID}`);
|
|
19
|
+
|
|
20
|
+
// Ethers
|
|
21
|
+
const provider = new ethers.providers.JsonRpcProvider(`${process.env.RPC_URL}`);
|
|
22
|
+
const pk = new ethers.Wallet(`${process.env.PK}`);
|
|
23
|
+
const wallet = pk.connect(provider);
|
|
24
|
+
|
|
25
|
+
// Viem
|
|
26
|
+
const pk = privateKeyToAccount(`${process.env.PK}` as Hex);
|
|
27
|
+
const wallet = createWalletClient({account: pk, chain: polygon, transport: http(`${process.env.RPC_URL}`)});
|
|
28
|
+
|
|
29
|
+
// Initialize Relay Client
|
|
30
|
+
const client = new RelayClient(relayerUrl, chainId, wallet);
|
|
31
|
+
```
|
package/dist/auth/handler.js
CHANGED
|
@@ -1,10 +1,13 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.AuthHandler = void 0;
|
|
4
|
-
const tslib_1 = require("tslib");
|
|
5
4
|
const http_helpers_1 = require("../http-helpers");
|
|
6
5
|
const POLYMARKET_COOKIE_NAME = "polymarket";
|
|
7
6
|
class AuthHandler {
|
|
7
|
+
url;
|
|
8
|
+
httpClient;
|
|
9
|
+
token;
|
|
10
|
+
cookie;
|
|
8
11
|
constructor(httpClient, args) {
|
|
9
12
|
if (args.authUrl == undefined || args.authToken == undefined) {
|
|
10
13
|
throw new Error("invalid authorization arguments");
|
|
@@ -17,33 +20,29 @@ class AuthHandler {
|
|
|
17
20
|
* Logs in to the auth url
|
|
18
21
|
* Extracts polymarket session cookies from the auth response header and cache it locally.
|
|
19
22
|
*/
|
|
20
|
-
login() {
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
aggregatedCookie += `${cookie}; `;
|
|
30
|
-
}
|
|
23
|
+
async login() {
|
|
24
|
+
if (this.cookie == undefined) {
|
|
25
|
+
// Logging in will set the session cookies on the httpClient
|
|
26
|
+
const resp = await this.httpClient.send(`${this.url}/login/internal`, http_helpers_1.GET, { Authorization: `Bearer ${this.token}` });
|
|
27
|
+
const cookies = resp.headers['set-cookie'];
|
|
28
|
+
let aggregatedCookie = "";
|
|
29
|
+
for (const cookie of cookies) {
|
|
30
|
+
if (cookie.includes(POLYMARKET_COOKIE_NAME)) {
|
|
31
|
+
aggregatedCookie += `${cookie}; `;
|
|
31
32
|
}
|
|
32
|
-
if (aggregatedCookie != "") {
|
|
33
|
-
this.cookie = aggregatedCookie;
|
|
34
|
-
}
|
|
35
|
-
console.log(`Successfully logged in!`);
|
|
36
33
|
}
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
getPolymarketCookies() {
|
|
40
|
-
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
41
|
-
if (this.cookie == undefined) {
|
|
42
|
-
console.log("Authorization not set, logging in...");
|
|
43
|
-
yield this.login();
|
|
34
|
+
if (aggregatedCookie != "") {
|
|
35
|
+
this.cookie = aggregatedCookie;
|
|
44
36
|
}
|
|
45
|
-
|
|
46
|
-
}
|
|
37
|
+
console.log(`Successfully logged in!`);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
async getPolymarketCookies() {
|
|
41
|
+
if (this.cookie == undefined) {
|
|
42
|
+
console.log("Authorization not set, logging in...");
|
|
43
|
+
await this.login();
|
|
44
|
+
}
|
|
45
|
+
return this.cookie;
|
|
47
46
|
}
|
|
48
47
|
}
|
|
49
48
|
exports.AuthHandler = AuthHandler;
|
package/dist/builder/create.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
1
|
import { SafeCreateTransactionArgs, TransactionRequest } from "../types";
|
|
2
|
-
import {
|
|
3
|
-
|
|
2
|
+
import { IAbstractSigner } from "@polymarket/abstract-signer";
|
|
3
|
+
import { SafeContractConfig } from "../config";
|
|
4
|
+
export declare function buildSafeCreateTransactionRequest(signer: IAbstractSigner, safeContractConfig: SafeContractConfig, args: SafeCreateTransactionArgs): Promise<TransactionRequest>;
|
package/dist/builder/create.js
CHANGED
|
@@ -1,54 +1,51 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.buildSafeCreateTransactionRequest = buildSafeCreateTransactionRequest;
|
|
4
|
-
const tslib_1 = require("tslib");
|
|
5
|
-
const ethers_1 = require("ethers");
|
|
6
4
|
const types_1 = require("../types");
|
|
7
5
|
const constants_1 = require("../constants");
|
|
8
|
-
const
|
|
9
|
-
function createSafeCreateSignature(signer, chainId, paymentToken, payment, paymentReceiver) {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
6
|
+
const derive_1 = require("./derive");
|
|
7
|
+
async function createSafeCreateSignature(signer, safeFactory, chainId, paymentToken, payment, paymentReceiver) {
|
|
8
|
+
const domain = {
|
|
9
|
+
name: constants_1.SAFE_FACTORY_NAME,
|
|
10
|
+
chainId: BigInt(chainId),
|
|
11
|
+
verifyingContract: safeFactory,
|
|
12
|
+
};
|
|
13
|
+
const types = {
|
|
14
|
+
CreateProxy: [
|
|
15
|
+
{ name: "paymentToken", type: "address" },
|
|
16
|
+
{ name: "payment", type: "uint256" },
|
|
17
|
+
{ name: "paymentReceiver", type: "address" },
|
|
18
|
+
],
|
|
19
|
+
};
|
|
20
|
+
const values = {
|
|
21
|
+
paymentToken,
|
|
22
|
+
payment: BigInt(payment),
|
|
23
|
+
paymentReceiver,
|
|
24
|
+
};
|
|
25
|
+
const sig = await signer.signTypedData(domain, types, values, "CreateProxy");
|
|
26
|
+
console.log(`Sig: ${sig}`);
|
|
27
|
+
return sig;
|
|
30
28
|
}
|
|
31
|
-
function buildSafeCreateTransactionRequest(signer, args) {
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
});
|
|
29
|
+
async function buildSafeCreateTransactionRequest(signer, safeContractConfig, args) {
|
|
30
|
+
const safeFactory = safeContractConfig.SafeFactory;
|
|
31
|
+
const sig = await createSafeCreateSignature(signer, safeFactory, args.chainId, args.paymentToken, args.payment, args.paymentReceiver);
|
|
32
|
+
const sigParams = {
|
|
33
|
+
paymentToken: args.paymentToken,
|
|
34
|
+
payment: args.payment,
|
|
35
|
+
paymentReceiver: args.paymentReceiver,
|
|
36
|
+
};
|
|
37
|
+
const safeAddress = (0, derive_1.deriveSafe)(args.from, safeFactory);
|
|
38
|
+
const request = {
|
|
39
|
+
from: args.from,
|
|
40
|
+
to: safeFactory,
|
|
41
|
+
// Note: obviously the safe here does not exist yet but useful to have this data in the db
|
|
42
|
+
proxyWallet: safeAddress,
|
|
43
|
+
data: "0x",
|
|
44
|
+
signature: sig,
|
|
45
|
+
signatureParams: sigParams,
|
|
46
|
+
type: types_1.TransactionType.SAFE_CREATE,
|
|
47
|
+
};
|
|
48
|
+
console.log(`Created a SAFE-CREATE Transaction:`);
|
|
49
|
+
console.log(request);
|
|
50
|
+
return request;
|
|
54
51
|
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.deriveSafe = exports.deriveProxyWallet = void 0;
|
|
4
|
+
const viem_1 = require("viem");
|
|
5
|
+
const constants_1 = require("../constants");
|
|
6
|
+
const deriveProxyWallet = (address, proxyFactory) => {
|
|
7
|
+
return (0, viem_1.getCreate2Address)({
|
|
8
|
+
bytecodeHash: constants_1.PROXY_INIT_CODE_HASH,
|
|
9
|
+
from: proxyFactory,
|
|
10
|
+
salt: (0, viem_1.keccak256)((0, viem_1.encodePacked)(["address"], [address]))
|
|
11
|
+
});
|
|
12
|
+
};
|
|
13
|
+
exports.deriveProxyWallet = deriveProxyWallet;
|
|
14
|
+
const deriveSafe = (address, safeFactory) => {
|
|
15
|
+
return (0, viem_1.getCreate2Address)({
|
|
16
|
+
bytecodeHash: constants_1.SAFE_INIT_CODE_HASH,
|
|
17
|
+
from: safeFactory,
|
|
18
|
+
salt: (0, viem_1.keccak256)((0, viem_1.encodeAbiParameters)([{ name: 'address', type: 'address' }], [address]))
|
|
19
|
+
});
|
|
20
|
+
};
|
|
21
|
+
exports.deriveSafe = deriveSafe;
|
package/dist/builder/index.d.ts
CHANGED
package/dist/builder/index.js
CHANGED
|
@@ -4,4 +4,4 @@ const tslib_1 = require("tslib");
|
|
|
4
4
|
tslib_1.__exportStar(require("./proxy"), exports);
|
|
5
5
|
tslib_1.__exportStar(require("./safe"), exports);
|
|
6
6
|
tslib_1.__exportStar(require("./create"), exports);
|
|
7
|
-
tslib_1.__exportStar(require("./
|
|
7
|
+
tslib_1.__exportStar(require("./derive"), exports);
|
package/dist/builder/proxy.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { IAbstractSigner } from "@polymarket/abstract-signer";
|
|
1
2
|
import { ProxyTransactionArgs, TransactionRequest } from "../types";
|
|
2
|
-
import {
|
|
3
|
-
export declare function buildProxyTransactionRequest(signer:
|
|
3
|
+
import { ProxyContractConfig } from "../config";
|
|
4
|
+
export declare function buildProxyTransactionRequest(signer: IAbstractSigner, args: ProxyTransactionArgs, proxyContractConfig: ProxyContractConfig, metadata?: string): Promise<TransactionRequest>;
|
package/dist/builder/proxy.js
CHANGED
|
@@ -1,92 +1,73 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.buildProxyTransactionRequest = buildProxyTransactionRequest;
|
|
4
|
-
const
|
|
5
|
-
const web3_utils_1 = tslib_1.__importDefault(require("web3-utils"));
|
|
6
|
-
const constants_1 = require("../constants");
|
|
4
|
+
const viem_1 = require("viem");
|
|
7
5
|
const types_1 = require("../types");
|
|
8
|
-
const
|
|
9
|
-
const ethers_1 = require("ethers");
|
|
10
|
-
const utils_2 = require("ethers/lib/utils");
|
|
11
|
-
const relayPrefix = "rlx:";
|
|
12
|
-
const zeroPad = "0000000000000000000000000000000000000000000000000000000000000000";
|
|
13
|
-
function padTo64(_hex) {
|
|
14
|
-
let hex = _hex;
|
|
15
|
-
if (hex.length < 64) {
|
|
16
|
-
hex = (zeroPad + hex).slice(-64);
|
|
17
|
-
}
|
|
18
|
-
return hex;
|
|
19
|
-
}
|
|
20
|
-
function removeHexPrefix(hex) {
|
|
21
|
-
return hex.replace(/^0x/, "");
|
|
22
|
-
}
|
|
23
|
-
function toUint256NoPrefix(hexString) {
|
|
24
|
-
return padTo64(removeHexPrefix(hexString));
|
|
25
|
-
}
|
|
26
|
-
function bytesToHexNoPrefix(bytes) {
|
|
27
|
-
let hex = removeHexPrefix(web3_utils_1.default.toHex(bytes));
|
|
28
|
-
if (hex.length % 2 !== 0) {
|
|
29
|
-
hex = "0" + hex;
|
|
30
|
-
}
|
|
31
|
-
return hex;
|
|
32
|
-
}
|
|
6
|
+
const derive_1 = require("./derive");
|
|
33
7
|
function createStructHash(from, to, data, txFee, gasPrice, gasLimit, nonce, relayHubAddress, relayAddress) {
|
|
34
|
-
const
|
|
35
|
-
const
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
8
|
+
const relayHubPrefix = (0, viem_1.toHex)("rlx:");
|
|
9
|
+
const encodedFrom = from;
|
|
10
|
+
const encodedTo = to;
|
|
11
|
+
const encodedData = data;
|
|
12
|
+
const encodedTxFee = (0, viem_1.toHex)(BigInt(txFee), { size: 32 });
|
|
13
|
+
const encodedGasPrice = (0, viem_1.toHex)(BigInt(gasPrice), { size: 32 });
|
|
14
|
+
const encodedGasLimit = (0, viem_1.toHex)(BigInt(gasLimit), { size: 32 });
|
|
15
|
+
const encodedNonce = (0, viem_1.toHex)(BigInt(nonce), { size: 32 });
|
|
16
|
+
const encodedRelayHubAddress = relayHubAddress;
|
|
17
|
+
const encodedRelayAddress = relayAddress;
|
|
18
|
+
const dataToHash = (0, viem_1.concat)([
|
|
19
|
+
relayHubPrefix,
|
|
20
|
+
encodedFrom,
|
|
21
|
+
encodedTo,
|
|
22
|
+
encodedData,
|
|
23
|
+
encodedTxFee,
|
|
24
|
+
encodedGasPrice,
|
|
25
|
+
encodedGasLimit,
|
|
26
|
+
encodedNonce,
|
|
27
|
+
encodedRelayHubAddress,
|
|
28
|
+
encodedRelayAddress,
|
|
29
|
+
]);
|
|
30
|
+
return (0, viem_1.keccak256)(dataToHash);
|
|
46
31
|
}
|
|
47
|
-
function createProxySignature(signer, structHash) {
|
|
48
|
-
return
|
|
49
|
-
return signer.signMessage(structHash);
|
|
50
|
-
});
|
|
32
|
+
async function createProxySignature(signer, structHash) {
|
|
33
|
+
return signer.signMessage(structHash);
|
|
51
34
|
}
|
|
52
|
-
function buildProxyTransactionRequest(signer, args, metadata) {
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
data: args.data,
|
|
63
|
-
});
|
|
64
|
-
const gasLimitStr = gasLimit.toString();
|
|
65
|
-
const sigParams = {
|
|
66
|
-
gasPrice: args.gasPrice,
|
|
67
|
-
gasLimit: gasLimitStr,
|
|
68
|
-
relayerFee: relayerFee,
|
|
69
|
-
relayHub: constants_1.RELAY_HUB_ADDRESS,
|
|
70
|
-
relay: args.relay,
|
|
71
|
-
};
|
|
72
|
-
const txHash = createStructHash(args.from, to, args.data, relayerFee, args.gasPrice, gasLimitStr, args.nonce, relayHub, args.relay);
|
|
73
|
-
const sig = yield createProxySignature(signer, txHash);
|
|
74
|
-
if (metadata == undefined) {
|
|
75
|
-
metadata = "";
|
|
76
|
-
}
|
|
77
|
-
const req = {
|
|
78
|
-
from: args.from,
|
|
79
|
-
to: to,
|
|
80
|
-
proxyWallet: proxy,
|
|
81
|
-
data: args.data,
|
|
82
|
-
nonce: args.nonce,
|
|
83
|
-
signature: sig,
|
|
84
|
-
signatureParams: sigParams,
|
|
85
|
-
type: types_1.TransactionType.PROXY,
|
|
86
|
-
metadata: metadata,
|
|
87
|
-
};
|
|
88
|
-
console.log(`Created Proxy Transaction Request:`);
|
|
89
|
-
console.log(req);
|
|
90
|
-
return req;
|
|
35
|
+
async function buildProxyTransactionRequest(signer, args, proxyContractConfig, metadata) {
|
|
36
|
+
const proxyWalletFactory = proxyContractConfig.ProxyFactory;
|
|
37
|
+
const to = proxyWalletFactory;
|
|
38
|
+
const proxy = (0, derive_1.deriveProxyWallet)(args.from, proxyWalletFactory);
|
|
39
|
+
const relayerFee = "0";
|
|
40
|
+
const relayHub = proxyContractConfig.RelayHub;
|
|
41
|
+
const gasLimit = args.gasLimit ? args.gasLimit : await signer.estimateGas({
|
|
42
|
+
from: args.from,
|
|
43
|
+
to: to,
|
|
44
|
+
data: args.data,
|
|
91
45
|
});
|
|
46
|
+
const gasLimitStr = gasLimit.toString();
|
|
47
|
+
const sigParams = {
|
|
48
|
+
gasPrice: args.gasPrice,
|
|
49
|
+
gasLimit: gasLimitStr,
|
|
50
|
+
relayerFee: relayerFee,
|
|
51
|
+
relayHub: relayHub,
|
|
52
|
+
relay: args.relay,
|
|
53
|
+
};
|
|
54
|
+
const txHash = createStructHash(args.from, to, args.data, relayerFee, args.gasPrice, gasLimitStr, args.nonce, relayHub, args.relay);
|
|
55
|
+
const sig = await createProxySignature(signer, txHash);
|
|
56
|
+
if (metadata == undefined) {
|
|
57
|
+
metadata = "";
|
|
58
|
+
}
|
|
59
|
+
const req = {
|
|
60
|
+
from: args.from,
|
|
61
|
+
to: to,
|
|
62
|
+
proxyWallet: proxy,
|
|
63
|
+
data: args.data,
|
|
64
|
+
nonce: args.nonce,
|
|
65
|
+
signature: sig,
|
|
66
|
+
signatureParams: sigParams,
|
|
67
|
+
type: types_1.TransactionType.PROXY,
|
|
68
|
+
metadata: metadata,
|
|
69
|
+
};
|
|
70
|
+
console.log(`Created Proxy Transaction Request:`);
|
|
71
|
+
console.log(req);
|
|
72
|
+
return req;
|
|
92
73
|
}
|
package/dist/builder/safe.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
+
import { IAbstractSigner } from "@polymarket/abstract-signer";
|
|
1
2
|
import { SafeTransaction, SafeTransactionArgs, TransactionRequest } from "../types";
|
|
2
|
-
import {
|
|
3
|
-
export declare function aggregateTransaction(txns: SafeTransaction[]): SafeTransaction;
|
|
4
|
-
export declare function buildSafeTransactionRequest(signer:
|
|
3
|
+
import { SafeContractConfig } from "../config";
|
|
4
|
+
export declare function aggregateTransaction(txns: SafeTransaction[], safeMultisend: string): SafeTransaction;
|
|
5
|
+
export declare function buildSafeTransactionRequest(signer: IAbstractSigner, args: SafeTransactionArgs, safeContractConfig: SafeContractConfig, metadata?: string): Promise<TransactionRequest>;
|
package/dist/builder/safe.js
CHANGED
|
@@ -2,38 +2,13 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.aggregateTransaction = aggregateTransaction;
|
|
4
4
|
exports.buildSafeTransactionRequest = buildSafeTransactionRequest;
|
|
5
|
-
const
|
|
5
|
+
const viem_1 = require("viem");
|
|
6
6
|
const types_1 = require("../types");
|
|
7
|
-
const
|
|
8
|
-
const ethers_1 = require("ethers");
|
|
9
|
-
const utils_2 = require("ethers/lib/utils");
|
|
7
|
+
const derive_1 = require("./derive");
|
|
10
8
|
const safe_1 = require("../encode/safe");
|
|
11
|
-
const
|
|
12
|
-
function createSafeSignature(signer, structHash) {
|
|
13
|
-
return
|
|
14
|
-
return signer.signMessage(structHash);
|
|
15
|
-
});
|
|
16
|
-
}
|
|
17
|
-
function splitSignature(sig) {
|
|
18
|
-
let sigV = parseInt(sig.slice(-2), 16);
|
|
19
|
-
switch (sigV) {
|
|
20
|
-
case 0:
|
|
21
|
-
case 1:
|
|
22
|
-
sigV += 31;
|
|
23
|
-
break;
|
|
24
|
-
case 27:
|
|
25
|
-
case 28:
|
|
26
|
-
sigV += 4;
|
|
27
|
-
break;
|
|
28
|
-
default:
|
|
29
|
-
throw new Error("Invalid signature");
|
|
30
|
-
}
|
|
31
|
-
sig = sig.slice(0, -2) + sigV.toString(16);
|
|
32
|
-
return {
|
|
33
|
-
r: ethers_1.BigNumber.from("0x" + sig.slice(2, 66)).toString(),
|
|
34
|
-
s: ethers_1.BigNumber.from("0x" + sig.slice(66, 130)).toString(),
|
|
35
|
-
v: ethers_1.BigNumber.from("0x" + sig.slice(130, 132)).toString(),
|
|
36
|
-
};
|
|
9
|
+
const utils_1 = require("../utils");
|
|
10
|
+
async function createSafeSignature(signer, structHash) {
|
|
11
|
+
return signer.signMessage(structHash);
|
|
37
12
|
}
|
|
38
13
|
function createStructHash(chainId, safe, to, value, data, operation, safeTxGas, baseGas, gasPrice, gasToken, refundReceiver, nonce) {
|
|
39
14
|
const domain = {
|
|
@@ -69,61 +44,61 @@ function createStructHash(chainId, safe, to, value, data, operation, safeTxGas,
|
|
|
69
44
|
refundReceiver: refundReceiver,
|
|
70
45
|
nonce: nonce,
|
|
71
46
|
};
|
|
72
|
-
|
|
73
|
-
|
|
47
|
+
// // viem hashTypedData
|
|
48
|
+
// const structHash = _TypedDataEncoder.hash(domain, types, values);
|
|
49
|
+
const structHash = (0, viem_1.hashTypedData)({ primaryType: "SafeTx", domain: domain, types: types, message: values });
|
|
74
50
|
return structHash;
|
|
75
51
|
}
|
|
76
|
-
function aggregateTransaction(txns) {
|
|
52
|
+
function aggregateTransaction(txns, safeMultisend) {
|
|
77
53
|
let transaction;
|
|
78
54
|
if (txns.length == 1) {
|
|
79
55
|
transaction = txns[0];
|
|
80
56
|
}
|
|
81
57
|
else {
|
|
82
|
-
transaction = (0, safe_1.createSafeMultisendTransaction)(txns);
|
|
58
|
+
transaction = (0, safe_1.createSafeMultisendTransaction)(txns, safeMultisend);
|
|
83
59
|
}
|
|
84
60
|
return transaction;
|
|
85
61
|
}
|
|
86
|
-
function buildSafeTransactionRequest(signer, args, metadata) {
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
});
|
|
62
|
+
async function buildSafeTransactionRequest(signer, args, safeContractConfig, metadata) {
|
|
63
|
+
const safeFactory = safeContractConfig.SafeFactory;
|
|
64
|
+
const safeMultisend = safeContractConfig.SafeMultisend;
|
|
65
|
+
const transaction = aggregateTransaction(args.transactions, safeMultisend);
|
|
66
|
+
const safeTxnGas = "0";
|
|
67
|
+
const baseGas = "0";
|
|
68
|
+
const gasPrice = "0";
|
|
69
|
+
const gasToken = viem_1.zeroAddress;
|
|
70
|
+
const refundReceiver = viem_1.zeroAddress;
|
|
71
|
+
const safeAddress = (0, derive_1.deriveSafe)(args.from, safeFactory);
|
|
72
|
+
// Generate the struct hash
|
|
73
|
+
// NOTE: this includes a client side optimization over the current relayers
|
|
74
|
+
// Generates struct hash locally instead of going across the network using the RPC call
|
|
75
|
+
const structHash = createStructHash(args.chainId, safeAddress, transaction.to, transaction.value, transaction.data, transaction.operation, safeTxnGas, baseGas, gasPrice, gasToken, refundReceiver, args.nonce);
|
|
76
|
+
const sig = await createSafeSignature(signer, structHash);
|
|
77
|
+
// Split the sig then pack it into Gnosis accepted rsv format
|
|
78
|
+
const packedSig = (0, utils_1.splitAndPackSig)(sig);
|
|
79
|
+
const sigParams = {
|
|
80
|
+
gasPrice,
|
|
81
|
+
operation: `${transaction.operation}`,
|
|
82
|
+
safeTxnGas,
|
|
83
|
+
baseGas,
|
|
84
|
+
gasToken,
|
|
85
|
+
refundReceiver,
|
|
86
|
+
};
|
|
87
|
+
if (metadata == undefined) {
|
|
88
|
+
metadata = "";
|
|
89
|
+
}
|
|
90
|
+
const req = {
|
|
91
|
+
from: args.from,
|
|
92
|
+
to: transaction.to,
|
|
93
|
+
proxyWallet: safeAddress,
|
|
94
|
+
data: transaction.data,
|
|
95
|
+
nonce: args.nonce,
|
|
96
|
+
signature: packedSig,
|
|
97
|
+
signatureParams: sigParams,
|
|
98
|
+
type: types_1.TransactionType.SAFE,
|
|
99
|
+
metadata: metadata,
|
|
100
|
+
};
|
|
101
|
+
console.log(`Created Safe Transaction Request: `);
|
|
102
|
+
console.log(req);
|
|
103
|
+
return req;
|
|
129
104
|
}
|
package/dist/client.d.ts
CHANGED
|
@@ -1,17 +1,19 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Wallet } from "@ethersproject/wallet";
|
|
2
|
+
import { JsonRpcSigner } from "@ethersproject/providers";
|
|
3
|
+
import { WalletClient } from "viem";
|
|
4
|
+
import { IAbstractSigner } from "@polymarket/abstract-signer";
|
|
2
5
|
import { HttpClient } from "./http-helpers";
|
|
3
|
-
import { AddressPayload, AuthArgs, ManualOverrides, NoncePayload, ProxyTransaction, RelayerTransaction, RelayerTransactionResponse, RelayPayload, SafeTransaction } from "./types";
|
|
6
|
+
import { AddressPayload, AuthArgs, IManualTransactionResponse, ManualOverrides, NoncePayload, ProxyTransaction, RelayerTransaction, RelayerTransactionResponse, RelayPayload, SafeTransaction } from "./types";
|
|
4
7
|
import { AuthHandler } from "./auth";
|
|
5
|
-
import {
|
|
6
|
-
import { SignerAdapter } from "./signeradapter";
|
|
8
|
+
import { ContractConfig } from "./config";
|
|
7
9
|
export declare class RelayClient {
|
|
8
10
|
readonly relayerUrl: string;
|
|
9
11
|
readonly chainId: number;
|
|
12
|
+
readonly contractConfig: ContractConfig;
|
|
10
13
|
readonly httpClient: HttpClient;
|
|
11
|
-
readonly signer?:
|
|
12
|
-
readonly provider?: Provider;
|
|
14
|
+
readonly signer?: IAbstractSigner;
|
|
13
15
|
readonly authHandler?: AuthHandler;
|
|
14
|
-
constructor(relayerUrl: string, chainId: number, signer?:
|
|
16
|
+
constructor(relayerUrl: string, chainId: number, signer?: Wallet | JsonRpcSigner | WalletClient, authArgs?: AuthArgs);
|
|
15
17
|
getRelayAddress(): Promise<AddressPayload>;
|
|
16
18
|
getNonce(signerAddress: string, signerType: string): Promise<NoncePayload>;
|
|
17
19
|
getRelayPayload(signerAddress: string, signerType: string): Promise<RelayPayload>;
|
|
@@ -19,7 +21,7 @@ export declare class RelayClient {
|
|
|
19
21
|
getTransactions(): Promise<RelayerTransaction[]>;
|
|
20
22
|
executeProxyTransactions(txns: ProxyTransaction[], metadata?: string): Promise<RelayerTransactionResponse>;
|
|
21
23
|
executeSafeTransactions(txns: SafeTransaction[], metadata?: string): Promise<RelayerTransactionResponse>;
|
|
22
|
-
executeManualTransactions(txns: SafeTransaction[], overrides?: ManualOverrides): Promise<
|
|
24
|
+
executeManualTransactions(txns: SafeTransaction[], overrides?: ManualOverrides): Promise<IManualTransactionResponse>;
|
|
23
25
|
deploySafe(): Promise<RelayerTransactionResponse>;
|
|
24
26
|
/**
|
|
25
27
|
* Periodically polls the transaction id until it reaches a desired state
|