Skip to content

Commit

Permalink
strategy factory
Browse files Browse the repository at this point in the history
  • Loading branch information
0xKurt committed May 16, 2024
1 parent 32618ac commit b36a2a9
Show file tree
Hide file tree
Showing 4 changed files with 550 additions and 1 deletion.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@allo-team/allo-v2-sdk",
"version": "1.0.72",
"version": "1.0.73",
"description": "sdk for allo v2",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
95 changes: 95 additions & 0 deletions src/strategies/StrategyFactory/StrategyFactory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import {
Chain,
PublicClient,
Transaction,
Transport,
encodeFunctionData,
extractChain,
getContract,
} from "viem";
import { supportedChains } from "../../chains.config";
import { create } from "../../Client/Client";
import { abi as DGLabi } from "./strategyFactory.DGL.config";
import { abi as DVMDTabi } from "./strategyFactory.DVMDT.config";
import { TransactionData } from "../../types";

export type StrategyFactoryType = "DGL" | "DVMDT";

export class StrategyFactory {
private client: PublicClient<Transport, Chain>;

private contract: any;

private factory: `0x${string}` | undefined;
private factoryType: StrategyFactoryType;

constructor({
chain,
factoryType,
address,
rpc,
}: {
chain: number;
factoryType: StrategyFactoryType;
address: `0x${string}`;
rpc?: string;
}) {
const usedChain = extractChain({
chains: supportedChains,
id: chain as any,
});

this.factoryType = factoryType;
this.client = create(usedChain, rpc);

this.setFactoryAddress(address);
}

private checkFactoryAddress(): void {
if (!this.factory) {
throw new Error("No factory address provided");
}
}

private getAbi(): any {
switch (this.factoryType) {
case "DGL":
return DGLabi;
case "DVMDT":
return DVMDTabi;
default:
throw new Error("Invalid factory type");
}
}

public setFactoryAddress(address: `0x${string}`): void {
if (address) {
this.contract = getContract({
address: address,
abi: this.getAbi(),
client: {
public: this.client,
},
});
this.factory = address;
}
}

public getCreateStrategyData(): TransactionData {
this.checkFactoryAddress();

const encodedData = encodeFunctionData({
abi: this.getAbi(),
functionName: "createStrategy",
args: [],
});

return {
to: this.factory!,
data: encodedData,
value: "0",
};
}

// tbc
}
182 changes: 182 additions & 0 deletions src/strategies/StrategyFactory/strategyFactory.DGL.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
export const abi = [
{
inputs: [
{
internalType: "address",
name: "_allo",
type: "address",
},
{
internalType: "string",
name: "_name",
type: "string",
},
{
internalType: "contract ISignatureTransfer",
name: "_permit2",
type: "address",
},
],
stateMutability: "nonpayable",
type: "constructor",
},
{
anonymous: false,
inputs: [
{
indexed: false,
internalType: "address",
name: "allo",
type: "address",
},
],
name: "AlloUpdated",
type: "event",
},
{
anonymous: false,
inputs: [
{
indexed: false,
internalType: "string",
name: "name",
type: "string",
},
],
name: "NameUpdated",
type: "event",
},
{
anonymous: false,
inputs: [
{
indexed: false,
internalType: "contract ISignatureTransfer",
name: "permit2",
type: "address",
},
],
name: "Permit2Updated",
type: "event",
},
{
inputs: [],
name: "allo",
outputs: [
{
internalType: "address",
name: "",
type: "address",
},
],
stateMutability: "view",
type: "function",
},
{
inputs: [],
name: "createStrategy",
outputs: [
{
internalType: "address",
name: "",
type: "address",
},
],
stateMutability: "nonpayable",
type: "function",
},
{
inputs: [
{
internalType: "address",
name: "_allo",
type: "address",
},
{
internalType: "string",
name: "_name",
type: "string",
},
{
internalType: "contract ISignatureTransfer",
name: "_permit2",
type: "address",
},
],
name: "createStrategyCustom",
outputs: [
{
internalType: "address",
name: "",
type: "address",
},
],
stateMutability: "nonpayable",
type: "function",
},
{
inputs: [],
name: "name",
outputs: [
{
internalType: "string",
name: "",
type: "string",
},
],
stateMutability: "view",
type: "function",
},
{
inputs: [],
name: "permit2",
outputs: [
{
internalType: "contract ISignatureTransfer",
name: "",
type: "address",
},
],
stateMutability: "view",
type: "function",
},
{
inputs: [
{
internalType: "address",
name: "_allo",
type: "address",
},
],
name: "updateAllo",
outputs: [],
stateMutability: "nonpayable",
type: "function",
},
{
inputs: [
{
internalType: "string",
name: "_name",
type: "string",
},
],
name: "updateName",
outputs: [],
stateMutability: "nonpayable",
type: "function",
},
{
inputs: [
{
internalType: "contract ISignatureTransfer",
name: "_permit2",
type: "address",
},
],
name: "updatePermit2",
outputs: [],
stateMutability: "nonpayable",
type: "function",
},
] as const;
Loading

0 comments on commit b36a2a9

Please sign in to comment.