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

feat: add DirectAllocationStrategy to sdk #65

Merged
merged 3 commits into from
Jul 12, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
4 changes: 3 additions & 1 deletion dist/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { SQFSuperFluidStrategy } from "./strategies/SuperFluidStrategy/SQFSuperF
import { DonationVotingMerkleDistributionStrategy } from "./strategies/DonationVotingMerkleDistributionStrategy/DonationVotingMerkleDistribution";
import { DirectGrantsStrategy } from "./strategies/DirectGrants/DirectGrantsStrategy";
import { DirectGrantsLiteStrategy } from "./strategies/DirectGrantsLiteStrategy/DirectGrantsLite";
import { DirectAllocationStrategy } from "./strategies/DirectAllocationStrategy/DirectAllocationStrategy";
import { StrategyFactory } from "./strategies/StrategyFactory/StrategyFactory";
export * from "./types";
export * from "./strategies/types";
Expand All @@ -26,4 +27,5 @@ export { abi as DirectGrantsStrategyAbi } from "./strategies/DirectGrants/direct
export { abi as DirectGrantsLiteStrategyAbi } from "./strategies/DirectGrantsLiteStrategy/directGrantsLite.config";
export { abi as StrategyFactoryDGLAbi } from "./strategies/StrategyFactory/strategyFactory.DGL.config";
export { abi as StrategyFactoryDVMDTAbi } from "./strategies/StrategyFactory/strategyFactory.DVMDT.config";
export { Allo, Registry, MicroGrantsStrategy, SQFSuperFluidStrategy, DonationVotingMerkleDistributionStrategy, DirectGrantsStrategy, DirectGrantsLiteStrategy, StrategyFactory, };
export { abi as DirectAllocationStrategyAbi } from "./strategies/DirectAllocationStrategy/directAllocation.config";
export { Allo, Registry, MicroGrantsStrategy, SQFSuperFluidStrategy, DonationVotingMerkleDistributionStrategy, DirectGrantsStrategy, DirectGrantsLiteStrategy, StrategyFactory, DirectAllocationStrategy, };
6 changes: 5 additions & 1 deletion dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.StrategyFactory = exports.DirectGrantsLiteStrategy = exports.DirectGrantsStrategy = exports.DonationVotingMerkleDistributionStrategy = exports.SQFSuperFluidStrategy = exports.MicroGrantsStrategy = exports.Registry = exports.Allo = exports.StrategyFactoryDVMDTAbi = exports.StrategyFactoryDGLAbi = exports.DirectGrantsLiteStrategyAbi = exports.DirectGrantsStrategyAbi = exports.SQFSuperFluidStrategyAbi = exports.MicroGrantsStrategyAbi = exports.DonationVotingMerkleDistributionVaultStrategyAbi = exports.DonationVotingMerkleDistributionDirectTransferStrategyAbi = exports.RegistryAbi = exports.AlloAbi = exports.DirectGrantsLiteStrategyTypes = exports.DirectGrantsStrategyTypes = exports.SQFSuperFluidStrategyTypes = exports.MicroGrantsStrategyTypes = exports.DonationVotingMerkleDistributionStrategyTypes = void 0;
exports.DirectAllocationStrategy = exports.StrategyFactory = exports.DirectGrantsLiteStrategy = exports.DirectGrantsStrategy = exports.DonationVotingMerkleDistributionStrategy = exports.SQFSuperFluidStrategy = exports.MicroGrantsStrategy = exports.Registry = exports.Allo = exports.DirectAllocationStrategyAbi = exports.StrategyFactoryDVMDTAbi = exports.StrategyFactoryDGLAbi = exports.DirectGrantsLiteStrategyAbi = exports.DirectGrantsStrategyAbi = exports.SQFSuperFluidStrategyAbi = exports.MicroGrantsStrategyAbi = exports.DonationVotingMerkleDistributionVaultStrategyAbi = exports.DonationVotingMerkleDistributionDirectTransferStrategyAbi = exports.RegistryAbi = exports.AlloAbi = exports.DirectGrantsLiteStrategyTypes = exports.DirectGrantsStrategyTypes = exports.SQFSuperFluidStrategyTypes = exports.MicroGrantsStrategyTypes = exports.DonationVotingMerkleDistributionStrategyTypes = void 0;
const Allo_1 = require("./Allo/Allo");
Object.defineProperty(exports, "Allo", { enumerable: true, get: function () { return Allo_1.Allo; } });
const Registry_1 = require("./Registry/Registry");
Expand All @@ -29,6 +29,8 @@ const DirectGrantsStrategy_1 = require("./strategies/DirectGrants/DirectGrantsSt
Object.defineProperty(exports, "DirectGrantsStrategy", { enumerable: true, get: function () { return DirectGrantsStrategy_1.DirectGrantsStrategy; } });
const DirectGrantsLite_1 = require("./strategies/DirectGrantsLiteStrategy/DirectGrantsLite");
Object.defineProperty(exports, "DirectGrantsLiteStrategy", { enumerable: true, get: function () { return DirectGrantsLite_1.DirectGrantsLiteStrategy; } });
const DirectAllocationStrategy_1 = require("./strategies/DirectAllocationStrategy/DirectAllocationStrategy");
Object.defineProperty(exports, "DirectAllocationStrategy", { enumerable: true, get: function () { return DirectAllocationStrategy_1.DirectAllocationStrategy; } });
const StrategyFactory_1 = require("./strategies/StrategyFactory/StrategyFactory");
Object.defineProperty(exports, "StrategyFactory", { enumerable: true, get: function () { return StrategyFactory_1.StrategyFactory; } });
__exportStar(require("./types"), exports);
Expand Down Expand Up @@ -60,3 +62,5 @@ var strategyFactory_DGL_config_1 = require("./strategies/StrategyFactory/strateg
Object.defineProperty(exports, "StrategyFactoryDGLAbi", { enumerable: true, get: function () { return strategyFactory_DGL_config_1.abi; } });
var strategyFactory_DVMDT_config_1 = require("./strategies/StrategyFactory/strategyFactory.DVMDT.config");
Object.defineProperty(exports, "StrategyFactoryDVMDTAbi", { enumerable: true, get: function () { return strategyFactory_DVMDT_config_1.abi; } });
var directAllocation_config_1 = require("./strategies/DirectAllocationStrategy/directAllocation.config");
Object.defineProperty(exports, "DirectAllocationStrategyAbi", { enumerable: true, get: function () { return directAllocation_config_1.abi; } });
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { Allo } from "../../Allo/Allo";
import { ConstructorArgs, DeployParams, TransactionData } from "../../types";
import { Allocation } from "./types";
export declare class DirectAllocationStrategy {
private client;
private contract;
private strategy;
private poolId;
private allo;
constructor({ chain, rpc, address, poolId }: ConstructorArgs);
getDeployParams(): DeployParams;
setPoolId(poolId: bigint): Promise<void>;
setContract(address: `0x${string}`): void;
private checkPoolId;
private checkStrategy;
getNative(): Promise<string>;
getAllo(): Promise<Allo>;
getPoolId(): Promise<number>;
getStrategyId(): Promise<string>;
getAllocateData(allocation: Allocation): TransactionData;
getBatchAllocateData(allocations: Allocation[]): TransactionData;
}
136 changes: 136 additions & 0 deletions dist/strategies/DirectAllocationStrategy/DirectAllocationStrategy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.DirectAllocationStrategy = void 0;
const viem_1 = require("viem");
const Allo_1 = require("../../Allo/Allo");
const chains_config_1 = require("../../chains.config");
const Client_1 = require("../../Client/Client");
const types_1 = require("../../types");
const allo_config_1 = require("../../Allo/allo.config");
const directAllocation_config_1 = require("./directAllocation.config");
class DirectAllocationStrategy {
constructor({ chain, rpc, address, poolId }) {
const usedChain = (0, viem_1.extractChain)({
chains: chains_config_1.supportedChains,
id: chain,
});
this.client = (0, Client_1.create)(usedChain, rpc);
this.allo = new Allo_1.Allo({ chain, rpc });
if (address) {
this.contract = (0, viem_1.getContract)({
address: address,
abi: directAllocation_config_1.abi,
client: {
public: this.client,
}
});
this.strategy = address;
}
this.poolId = poolId || BigInt(-1);
}
getDeployParams() {
const constructorArgs = (0, viem_1.encodeAbiParameters)((0, viem_1.parseAbiParameters)("address,string"), [this.allo.address(), "DirectAllocationStrategyv1.0"]);
const constructorArgsNo0x = constructorArgs.slice(2);
return {
abi: directAllocation_config_1.abi,
bytecode: (directAllocation_config_1.bytecode +
constructorArgsNo0x),
};
}
setPoolId(poolId) {
return __awaiter(this, void 0, void 0, function* () {
this.poolId = poolId;
const strategyAddress = yield this.allo.getStrategy(poolId);
this.setContract(strategyAddress);
});
}
setContract(address) {
this.contract = (0, viem_1.getContract)({
address: address,
abi: directAllocation_config_1.abi,
client: {
public: this.client,
}
});
this.strategy = address;
}
checkPoolId() {
if (this.poolId === BigInt(-1))
throw new Error("DirectAllocationStrategy: No poolId provided. Please call `setPoolId` first.");
}
checkStrategy() {
if (!this.strategy)
throw new Error("DirectAllocationStrategy: No strategy address provided. Please call `setContract` first.");
}
getNative() {
return __awaiter(this, void 0, void 0, function* () {
this.checkStrategy();
const native = yield this.contract.read.NATIVE();
return native;
});
}
getAllo() {
return __awaiter(this, void 0, void 0, function* () {
return this.contract.read.getAllo();
});
}
getPoolId() {
return __awaiter(this, void 0, void 0, function* () {
this.checkStrategy();
const poolId = yield this.contract.read.getPoolId();
return poolId;
});
}
getStrategyId() {
return __awaiter(this, void 0, void 0, function* () {
this.checkStrategy();
const id = yield this.contract.read.getStrategyId();
return id;
});
}
// Write methods
getAllocateData(allocation) {
this.checkPoolId();
const encoded = (0, viem_1.encodeAbiParameters)((0, viem_1.parseAbiParameters)("address,uint256,address,uint256"), [allocation.profileOwner, allocation.amount, allocation.token, allocation.nonce]);
const encodedData = (0, viem_1.encodeFunctionData)({
abi: allo_config_1.abi,
functionName: "allocate",
args: [this.poolId, encoded],
});
return {
to: this.allo.address(),
data: encodedData,
value: allocation.token.toLowerCase() === types_1.NATIVE ? allocation.amount.toString() : "0",
};
}
// Note: batchAllocate will not be supported for NATIVE tokens until Allo v2.1
getBatchAllocateData(allocations) {
this.checkPoolId();
const encodedParams = [];
allocations.forEach((allocation) => {
const encoded = (0, viem_1.encodeAbiParameters)((0, viem_1.parseAbiParameters)("address,uint256,address,uint256"), [allocation.profileOwner, allocation.amount, allocation.token, allocation.nonce]);
encodedParams.push(encoded);
});
const poolIds = Array(encodedParams.length).fill(this.poolId);
const encodedData = (0, viem_1.encodeFunctionData)({
abi: allo_config_1.abi,
functionName: "batchAllocate",
args: [poolIds, encodedParams],
});
return {
to: this.allo.address(),
data: encodedData,
value: "0",
};
}
}
exports.DirectAllocationStrategy = DirectAllocationStrategy;
Loading
Loading