-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #133 from hackdays-io/issue/131
WIP: add splits command
- Loading branch information
Showing
7 changed files
with
274 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
export const SPLITS_ABI = [ | ||
{ | ||
anonymous: false, | ||
inputs: [ | ||
{ | ||
indexed: false, | ||
internalType: "address", | ||
name: "split", | ||
type: "address", | ||
}, | ||
], | ||
name: "SplitsCreated", | ||
type: "event", | ||
}, | ||
{ | ||
inputs: [], | ||
name: "FRACTION_TOKEN", | ||
outputs: [ | ||
{ | ||
internalType: "contract IFractionToken", | ||
name: "", | ||
type: "address", | ||
}, | ||
], | ||
stateMutability: "pure", | ||
type: "function", | ||
}, | ||
{ | ||
inputs: [], | ||
name: "HATS_TIME_FRAME_MODULE", | ||
outputs: [ | ||
{ | ||
internalType: "contract IHatsTimeFrameModule", | ||
name: "", | ||
type: "address", | ||
}, | ||
], | ||
stateMutability: "pure", | ||
type: "function", | ||
}, | ||
{ | ||
inputs: [], | ||
name: "SPLIT_FACTORY_V2", | ||
outputs: [ | ||
{ | ||
internalType: "contract ISplitFactoryV2", | ||
name: "", | ||
type: "address", | ||
}, | ||
], | ||
stateMutability: "pure", | ||
type: "function", | ||
}, | ||
{ | ||
inputs: [], | ||
name: "TRUSTED_FORWARDER", | ||
outputs: [ | ||
{ | ||
internalType: "address", | ||
name: "", | ||
type: "address", | ||
}, | ||
], | ||
stateMutability: "pure", | ||
type: "function", | ||
}, | ||
{ | ||
inputs: [ | ||
{ | ||
components: [ | ||
{ | ||
internalType: "uint256", | ||
name: "hatId", | ||
type: "uint256", | ||
}, | ||
{ | ||
internalType: "uint256", | ||
name: "multiplierBottom", | ||
type: "uint256", | ||
}, | ||
{ | ||
internalType: "uint256", | ||
name: "multiplierTop", | ||
type: "uint256", | ||
}, | ||
{ | ||
internalType: "address[]", | ||
name: "wearers", | ||
type: "address[]", | ||
}, | ||
], | ||
internalType: "struct ISplitsCreator.SplitsInfo[]", | ||
name: "_splitsInfo", | ||
type: "tuple[]", | ||
}, | ||
], | ||
name: "create", | ||
outputs: [ | ||
{ | ||
internalType: "address", | ||
name: "", | ||
type: "address", | ||
}, | ||
], | ||
stateMutability: "nonpayable", | ||
type: "function", | ||
}, | ||
] as const; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import { Command } from "commander"; | ||
import { create } from "../modules/splits"; | ||
import { getAccount } from "../services/wallet"; | ||
import { rootProgram } from ".."; | ||
import { Address } from "viem"; | ||
|
||
export const splitsCommands = new Command(); | ||
|
||
// ############################################################### | ||
// CLI init setup | ||
// ############################################################### | ||
|
||
splitsCommands | ||
.name("splits") | ||
.description("This is a CLI splits for toban project") | ||
.version("1.0.0"); | ||
|
||
// ############################################################### | ||
// command setUp | ||
// ############################################################### | ||
|
||
/** | ||
* スプリットを作成 | ||
*/ | ||
splitsCommands | ||
.command("create") | ||
.description("Create Splits") | ||
.requiredOption("-splits, --splitsAddress <splitsAddress>", "Splits Address") | ||
.requiredOption( | ||
"-hid, --hatId <hatId>", | ||
"Hat ID", | ||
(value, previous: string[]) => | ||
previous ? previous.concat([value]) : [value], | ||
[] | ||
) | ||
.requiredOption( | ||
"-mb, --multiplierBottom <multiplierBottom>", | ||
"Multiplier Bottom", | ||
(value, previous: string[]) => | ||
previous ? previous.concat([value]) : [value], | ||
[] | ||
) | ||
.requiredOption( | ||
"-mt, --multiplierTop <multiplierTop>", | ||
"Multiplier Top", | ||
(value, previous: string[]) => | ||
previous ? previous.concat([value]) : [value], | ||
[] | ||
) | ||
.requiredOption( | ||
"-w, --wearers <wearers>", | ||
"Wearers (comma-separated addresses)", | ||
(value: string, previous: Address[][]) => { | ||
const addresses = value | ||
.split(",") | ||
.map((addr: string) => addr.trim() as Address); | ||
return previous ? [...previous, addresses] : [addresses]; | ||
}, | ||
[] | ||
) | ||
.action( | ||
async ({ | ||
splitsAddress, | ||
hatId, | ||
multiplierBottom, | ||
multiplierTop, | ||
wearers, | ||
}) => { | ||
const splitsData = hatId.map((id: string, index: number) => ({ | ||
hatId: BigInt(id), | ||
multiplierBottom: BigInt(multiplierBottom[index]), | ||
multiplierTop: BigInt(multiplierTop[index]), | ||
wearers: wearers[index], | ||
})); | ||
|
||
const hash = await create(splitsAddress, splitsData); | ||
console.log("Transaction Hash:", hash); | ||
} | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import { Address, decodeEventLog } from "viem"; | ||
import { publicClient, walletClient } from ".."; | ||
import { SPLITS_ABI } from "../abi/splits"; | ||
import { startLoading } from "../services/loading"; | ||
|
||
type SplitsInfo = { | ||
hatId: bigint; | ||
multiplierBottom: bigint; | ||
multiplierTop: bigint; | ||
wearers: Address[]; | ||
}; | ||
|
||
/** | ||
* Splitsを作成 | ||
*/ | ||
export const create = async (splitsAddress: Address, args: SplitsInfo[]) => { | ||
const stop = startLoading(); | ||
const { request } = await publicClient.simulateContract({ | ||
address: splitsAddress, | ||
abi: SPLITS_ABI, | ||
account: walletClient.account, | ||
functionName: "create", | ||
args: [args], | ||
}); | ||
|
||
const hash = await walletClient.writeContract(request); | ||
|
||
const receipt = await publicClient.waitForTransactionReceipt({ | ||
hash, | ||
}); | ||
|
||
const log = receipt.logs.find((log) => { | ||
try { | ||
const decodedLog = decodeEventLog({ | ||
abi: SPLITS_ABI, | ||
data: log.data, | ||
topics: log.topics, | ||
}); | ||
return decodedLog.eventName === "SplitsCreated"; | ||
} catch (error) {} | ||
}); | ||
|
||
stop(); | ||
|
||
if (log) { | ||
const decodedLog = decodeEventLog({ | ||
abi: SPLITS_ABI, | ||
data: log.data, | ||
topics: log.topics, | ||
}); | ||
console.log(decodedLog); | ||
console.log( | ||
"Split Link:", | ||
`https://app.splits.org/accounts/${ | ||
decodedLog.args.split | ||
}/?chainId=${String(publicClient.chain?.id)}` | ||
); | ||
} | ||
|
||
return hash; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters