Skip to content

Commit ad0cde0

Browse files
committed
Added FeeStrategies
1 parent e5cf80c commit ad0cde0

File tree

3 files changed

+70
-19
lines changed

3 files changed

+70
-19
lines changed

packages/sequencer/src/protocol/baselayer/MinaBaseLayer.ts

Lines changed: 41 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { DependencyFactory } from "@proto-kit/common";
22
import { Mina } from "o1js";
3+
import { match } from "ts-pattern";
34

45
import { MinaIncomingMessageAdapter } from "../../settlement/messages/MinaIncomingMessageAdapter";
56
import { SequencerModule } from "../../sequencer/builder/SequencerModule";
@@ -9,11 +10,21 @@ import { WithdrawalQueue } from "../../settlement/messages/WithdrawalQueue";
910
import { BaseLayer } from "./BaseLayer";
1011

1112
export interface MinaBaseLayerConfig {
12-
network: {
13-
local: boolean;
14-
graphql?: string;
15-
archive?: string;
16-
};
13+
network:
14+
| {
15+
type: "local";
16+
}
17+
| {
18+
type: "lightnet";
19+
graphql: string;
20+
archive: string;
21+
accountManager?: string;
22+
}
23+
| {
24+
type: "remote";
25+
graphql: string;
26+
archive: string;
27+
};
1728
}
1829

1930
export class MinaBaseLayer
@@ -22,6 +33,8 @@ export class MinaBaseLayer
2233
{
2334
public network?: Parameters<typeof Mina.setActiveInstance>[0];
2435

36+
public originalNetwork?: Parameters<typeof Mina.setActiveInstance>[0];
37+
2538
public dependencies() {
2639
return {
2740
IncomingMessageAdapter: {
@@ -39,27 +52,36 @@ export class MinaBaseLayer
3952
}
4053

4154
public isLocalBlockChain(): boolean {
42-
return this.config.network.local;
55+
return this.config.network.type === "local";
4356
}
4457

4558
public async start(): Promise<void> {
4659
const { network } = this.config;
4760

48-
if (
49-
!network.local &&
50-
(network.graphql === undefined || network.archive === undefined)
51-
) {
52-
throw new Error(
53-
"The API endpoints have to be defined, if the network is remote"
54-
);
55-
}
61+
this.originalNetwork = Mina.activeInstance;
5662

57-
const Network = this.config.network.local
58-
? await Mina.LocalBlockchain({ proofsEnabled: false })
59-
: Mina.Network({
60-
mina: network.graphql!,
61-
archive: network.archive!,
63+
const Network = await match(network)
64+
.with(
65+
{ type: "local" },
66+
async () => await Mina.LocalBlockchain({ proofsEnabled: false })
67+
)
68+
.with({ type: "lightnet" }, async (lightnet) => {
69+
const net = Mina.Network({
70+
mina: lightnet.graphql,
71+
archive: lightnet.archive,
72+
lightnetAccountManager: lightnet.accountManager,
6273
});
74+
net.proofsEnabled = false;
75+
return net;
76+
})
77+
.with({ type: "remote" }, async (remote) =>
78+
Mina.Network({
79+
mina: remote.graphql,
80+
archive: remote.archive,
81+
})
82+
)
83+
.exhaustive();
84+
6385
Mina.setActiveInstance(Network);
6486
this.network = Network;
6587
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import {
2+
sequencerModule,
3+
SequencerModule,
4+
} from "../../../sequencer/builder/SequencerModule";
5+
import { noop } from "@proto-kit/common";
6+
import { FeeStrategy } from "./FeeStrategy";
7+
8+
export type ConstantFeeStrategyConfig = {
9+
fee?: number;
10+
};
11+
12+
const DEFAULT_FEE = 0.1 * 1e9;
13+
14+
@sequencerModule()
15+
export class ConstantFeeStrategy
16+
extends SequencerModule<ConstantFeeStrategyConfig>
17+
implements FeeStrategy
18+
{
19+
getFee(): number {
20+
return this.config.fee ?? DEFAULT_FEE;
21+
}
22+
23+
public async start() {
24+
noop();
25+
}
26+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export interface FeeStrategy {
2+
getFee(): number;
3+
}

0 commit comments

Comments
 (0)