-
-
Save cmdzro/0547725ca6faa2067e7edf3c70b697f1 to your computer and use it in GitHub Desktop.
Send erc20 token transfer transaction to gnosis safe
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 { run, ethers } from "hardhat" | |
import Safe, { SafeTransactionOptionalProps } from "@gnosis.pm/safe-core-sdk" | |
import { EthersAdapter } from "@gnosis.pm/safe-core-sdk" | |
import { MetaTransactionData } from '@gnosis.pm/safe-core-sdk-types' | |
const safeAddress = "0xfA64EE..." // on rinkeby testnet | |
const tokenAddress = "0x18B225..." // on rinkeby testnet | |
const TokenFactory = await ethers.getContractFactory("TokenContract") | |
const token = TokenFactory.attach(tokenAddress) | |
const [owner] = await ethers.getSigners() | |
const ethAdapter = new EthersAdapter({ | |
ethers, | |
signer: owner | |
}) | |
const safeSdk = await Safe.create({ ethAdapter, safeAddress }) | |
const safe = await safeSdk.connect({ ethAdapter, safeAddress }) | |
const unsignedTransaction = await token.populateTransaction.transfer("0x5d07C6...", 100) | |
console.log(unsignedTransaction) | |
const transactions: MetaTransactionData[] = [{ | |
to: tokenAddress, | |
value: "0", | |
data: unsignedTransaction.data! | |
}] | |
const safeTransaction = await safe.createTransaction(transactions) | |
console.log(safeTransaction) // returns with a different "to" than specified in line 27 | |
const owner1Signature = await safe.signTransaction(safeTransaction) | |
console.log(`owner signature: ${owner1Signature}`) // returns undefined |
Thx a lot for your help @germartinez , greatly appreciated 🤗 ... I'll read up on all this and get back if I have further questions.
You are all very supportive ❤️ – Thx again!
Quick update: I got it to work 🎉 thx @germartinez @rmeissner and @DmitryBespalov!
I'll try to write something up to have others also benefitting from my learnings.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi @cmdzro, to display the transaction in the web interface you should use the Safe Service Client package inside the Safe Core SDK Monorepo. This package allows to comunicate with our Safe Transaction Service to collect signatures offchain.
These should be the steps to follow:
If there are more than one owner, repeat the next two actions for each of them (or confirm the transaction in the web interface)
As a side note:
Line 20 is not needed. It would only be useful to connect the sdk to a different Safe account or to connect another signer to the same Safe account (o another signer to another Safe account).