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

Gas Relayer Contracts #26

Closed
wants to merge 3 commits into from
Closed
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
2 changes: 1 addition & 1 deletion src/chains/ethereum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import {
TxPayload,
TransactionWithSignature,
} from "../types";
import { MultichainContract } from "../mpcContract";
import { MultichainContract } from "../contracts/mpc";
import BN from "bn.js";
import { queryGasPrice } from "../utils/gasPrice";
import { buildTxPayload, addSignature } from "../utils/transaction";
Expand Down
2 changes: 1 addition & 1 deletion src/chains/near.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { keyStores, KeyPair, connect, Account } from "near-api-js";
import { Wallet } from "@near-wallet-selector/core";

export const TGAS = new BN(1000000000000);
export const NO_DEPOSIT = "0";
export const NO_DEPOSIT = new BN("0");

export interface NearConfig {
networkId: string;
Expand Down
125 changes: 125 additions & 0 deletions src/contracts/gas_station.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
import { Contract, Account } from "near-api-js";
import { NO_DEPOSIT, TGAS, nearAccountFromEnv } from "../chains/near";
import { ChangeMethodArgs } from "../types";
import BN from "bn.js";

interface GetPaymasterArgs {
chain_id: string;
}

interface PaymasterConfiguration {
nonce: number;
token_id: string;
foreign_address: string;
minimum_available_balance: bigint;
}

interface SetPaymasterNonceArgs {
chain_id: string;
token_id: string;
nonce: number;
}

interface CreateTxArgs {
transaction_rlp_hex: string;
use_paymaster: boolean;
token_id: string;
}

interface SignNextArgs {
id: string;
}

interface TransactionSequenceCreation {
id: bigint;
pending_signature_count: number;
}

interface GasStationInterface extends Contract {
// TODO - determine return types...
get_paymasters: (
args: ChangeMethodArgs<GetPaymasterArgs>
) => Promise<PaymasterConfiguration[]>;
set_paymaster_nonce: (
args: ChangeMethodArgs<SetPaymasterNonceArgs>
) => Promise<void>;
create_transaction: (
args: ChangeMethodArgs<CreateTxArgs>
) => Promise<TransactionSequenceCreation>;
/// This returns some deep nested Promise Type...
sign_next: (args: ChangeMethodArgs<SignNextArgs>) => Promise<void>;
}

/**
* High-level interface for the Near MPC-Recovery Contract
* located in: https://github.com/near/mpc-recovery
*/
export class GasStationContract {
contract: GasStationInterface;

constructor(account: Account, contractId: string) {
this.contract = new Contract(account, contractId, {
changeMethods: [
"get_paymasters",
"set_paymaster_nonce",
"create_transaction",
"sign_next",
],
viewMethods: [],
useLocalViewExecution: false,
}) as GasStationInterface;
}

static async fromEnv(): Promise<GasStationContract> {
const account = await nearAccountFromEnv();
return new GasStationContract(
account,
process.env.NEAR_MULTICHAIN_CONTRACT!
);
}

async get_paymasters(
args: GetPaymasterArgs,
gas?: BN
): Promise<PaymasterConfiguration[]> {
const paymasters = await this.contract.get_paymasters({
args,
gas: gas || TGAS.muln(100),
attachedDeposit: NO_DEPOSIT,
});
return paymasters;
}

async set_paymaster_nonce(
args: SetPaymasterNonceArgs,
gas?: BN
): Promise<void> {
this.contract.set_paymaster_nonce({
args,
gas: gas || TGAS.muln(100),
attachedDeposit: NO_DEPOSIT,
});
}

async create_transaction(
args: CreateTxArgs,
attachedDeposit: BN,
gas?: BN
): Promise<TransactionSequenceCreation> {
const sequence = await this.contract.create_transaction({
args,
gas: gas || TGAS.muln(100),
attachedDeposit,
});
return sequence;
}

async sign_next(args: SignNextArgs, gas?: BN): Promise<void> {
// TODO - determine return type.
await this.contract.sign_next({
args,
gas: gas || TGAS.muln(100),
attachedDeposit: NO_DEPOSIT,
});
}
}
16 changes: 8 additions & 8 deletions src/mpcContract.ts → src/contracts/mpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@ import {
deriveChildPublicKey,
najPublicKeyStrToUncompressedHexPoint,
uncompressedHexPointToEvmAddress,
} from "./utils/kdf";
import { NO_DEPOSIT, nearAccountFromEnv, TGAS } from "./chains/near";
} from "../utils/kdf";
import { NO_DEPOSIT, nearAccountFromEnv, TGAS } from "../chains/near";
import BN from "bn.js";
import {
ChangeMethodArgs,
MPCSignature,
NearContractFunctionPayload,
SignArgs,
} from "./types";
} from "../types";

interface MultichainContractInterface extends Contract {
interface MPCInterface extends Contract {
// Define the signature for the `public_key` view method
public_key: () => Promise<string>;

Expand All @@ -27,14 +27,14 @@ interface MultichainContractInterface extends Contract {
* located in: https://github.com/near/mpc-recovery
*/
export class MultichainContract {
contract: MultichainContractInterface;
contract: MPCInterface;

constructor(account: Account, contractId: string) {
this.contract = new Contract(account, contractId, {
changeMethods: ["sign"],
viewMethods: ["public_key"],
useLocalViewExecution: false,
}) as MultichainContractInterface;
}) as MPCInterface;
}

static async fromEnv(): Promise<MultichainContract> {
Expand Down Expand Up @@ -65,7 +65,7 @@ export class MultichainContract {
args: signArgs,
// Default of 200 TGAS
gas: gas || TGAS.muln(200),
attachedDeposit: new BN(NO_DEPOSIT),
attachedDeposit: NO_DEPOSIT,
});
return { big_r, big_s };
};
Expand All @@ -84,7 +84,7 @@ export class MultichainContract {
methodName: "sign",
args: signArgs,
gas: (gas || TGAS.muln(200)).toString(),
deposit: NO_DEPOSIT,
deposit: NO_DEPOSIT.toString(),
},
},
],
Expand Down
62 changes: 62 additions & 0 deletions src/contracts/nft_key.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { Contract, Account } from "near-api-js";
import { NO_DEPOSIT, TGAS, nearAccountFromEnv } from "../chains/near";
import { ChangeMethodArgs } from "../types";
import BN from "bn.js";

interface CktApproveArgs {
token_id: bigint;
account_id: string;
msg: string;
}

interface NftKeyInterface extends Contract {
// TODO - determine return types...
storage_deposit: (args: ChangeMethodArgs<object>) => Promise<void>;
mint: (args: ChangeMethodArgs<object>) => Promise<void>;
ckt_approve: (args: ChangeMethodArgs<CktApproveArgs>) => Promise<void>;
}

/**
* High-level interface for the Near MPC-Recovery Contract
* located in: https://github.com/near/mpc-recovery
*/
export class NftKeyContract {
contract: NftKeyInterface;

constructor(account: Account, contractId: string) {
this.contract = new Contract(account, contractId, {
changeMethods: ["storage_deposit", "mint", "ckt_approve"],
viewMethods: [],
useLocalViewExecution: false,
}) as NftKeyInterface;
}

static async fromEnv(): Promise<NftKeyContract> {
const account = await nearAccountFromEnv();
return new NftKeyContract(account, process.env.NEAR_MULTICHAIN_CONTRACT!);
}

async storage_deposit(attachedDeposit: bigint, gas?: BN): Promise<void> {
await this.contract.storage_deposit({
args: {},
gas: gas || TGAS.muln(100),
attachedDeposit: new BN(attachedDeposit.toString()),
});
}

async mint(gas?: BN): Promise<void> {
await this.contract.mint({
args: {},
gas: gas || TGAS.muln(100),
attachedDeposit: NO_DEPOSIT,
});
}

async ckt_approve(args: CktApproveArgs, gas?: BN): Promise<void> {
await this.contract.mint({
args,
gas: gas || TGAS.muln(100),
attachedDeposit: NO_DEPOSIT,
});
}
}
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export * from "./chains/ethereum";
export * from "./mpcContract";
export * from "./contracts/mpc";
export * from "./chains/near";
export * from "./types";
export * from "./network";
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { MultichainContract } from "./mpcContract";
import { MultichainContract } from "./contracts/mpc";
import { FunctionCallAction } from "@near-wallet-selector/core";
import BN from "bn.js";
import { Hex } from "viem";
Expand Down
2 changes: 1 addition & 1 deletion tests/e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ describe("End To End", () => {
});

afterAll(async () => {
clearTimeout();
clearTimeout(undefined);
});

it("signAndSendTransaction", async () => {
Expand Down