Last active
September 20, 2022 07:24
-
-
Save cmdzro/f605da15c6a35da991d665f4e0ce5649 to your computer and use it in GitHub Desktop.
This script distributes ERC-20 tokens using a gnosis safe multisend transaction. Learn more about it in this article: https://mirror.xyz/0xa1AC2cC82249A44892802a99CA84c4ed1072B29C/lL8AYV_b4VzTbojuZEprrxD7-RTTap2IMIS8qIObfl8
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
import { ethers } from "hardhat" | |
import Safe, { EthersAdapter } from "@gnosis.pm/safe-core-sdk" | |
import { MetaTransactionData } from "@gnosis.pm/safe-core-sdk-types" | |
import SafeServiceClient from "@gnosis.pm/safe-service-client" | |
import { BigNumber } from "@ethersproject/bignumber" | |
const safeAddress = "0xAAA" // on rinkeby testnet | |
const tokenAddress = "0xBBB" // on rinkeby testnet | |
const transactionServiceUrl = "https://safe-transaction.rinkeby.gnosis.io/" // on rinkeby testnet | |
interface Receiver { | |
address: string | |
amount: BigNumber | |
} | |
async function main() { | |
const [owner] = await ethers.getSigners() | |
const TokenFactory = await ethers.getContractFactory("PreAtem") | |
const token = TokenFactory.attach(tokenAddress) | |
const receivers = [ | |
{ | |
address: "0x123", | |
amount: BigNumber.from("555555555555556000000") | |
}, | |
{ | |
address: "0x456", | |
amount: BigNumber.from("1388888888888890000000") | |
} | |
] | |
const ethAdapter = new EthersAdapter({ | |
ethers, | |
signer: owner | |
}) | |
const safeService = new SafeServiceClient(transactionServiceUrl) | |
const safeSdk = await Safe.create({ ethAdapter, safeAddress }) | |
const transactions: MetaTransactionData[] = await Promise.all( | |
receivers.map(async receiver => { | |
const unsignedTransaction = await token.populateTransaction.transfer(receiver.address, receiver.amount) | |
return { | |
to: tokenAddress, | |
value: "0", | |
data: unsignedTransaction.data! | |
} | |
}) | |
) | |
const safeTransaction = await safeSdk.createTransaction(transactions) | |
console.log("Created safe transaction") | |
console.log(safeTransaction) | |
const safeTxHash = await safeSdk.getTransactionHash(safeTransaction) | |
console.log("Retrieved safe transaction hash") | |
console.log(safeTxHash) | |
await safeService.proposeTransaction({ | |
safeAddress: safeAddress, | |
senderAddress: owner.address, | |
safeTransaction: safeTransaction, | |
safeTxHash: safeTxHash | |
}) | |
const signature = await safeSdk.signTransactionHash(safeTxHash) | |
await safeService.confirmTransaction(safeTxHash, signature.data) | |
console.log("Confirmed signature") | |
} | |
main() | |
.then(() => process.exit(0)) | |
.catch((error) => { | |
console.error(error) | |
process.exit(1) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment