-
-
Save cmdzro/0547725ca6faa2067e7edf3c70b697f1 to your computer and use it in GitHub Desktop.
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 |
(I updated the code accordingly and also included parts of the addresses to better understand what goes in and out)
The sdk is encoding your array of txs (which contains 1 tx) into a multisend. The to
is the address of the multisend contract (see https://github.com/gnosis/safe-deployments/blob/main/src/assets/v1.1.1/multi_send.json#L7)
To avoid the usage of multisend, you should not use an array, but just the single tx, as the parameter for createTransaction
Ah that's actually ok then as I want to have multiple transaction to be batched (this is just a first quick test). Thx for the explanation @rmeissner 👍
So then why does it not show up in the gnosis frontend yet? And why is it not getting signed? Any idea? Thx!
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:
- Create the transaction (safe-core-sdk) https://github.com/gnosis/safe-core-sdk/tree/main/packages/safe-core-sdk#createtransaction
- Propose the transaction to the service (safe-service-client) https://github.com/gnosis/safe-core-sdk/tree/main/packages/safe-service-client#proposetransaction
If there are more than one owner, repeat the next two actions for each of them (or confirm the transaction in the web interface) - Sign the transaction (safe-core-sdk) https://github.com/gnosis/safe-core-sdk/tree/main/packages/safe-core-sdk#signtransactionhash
- Send the signature to the service (safe-service-client) https://github.com/gnosis/safe-core-sdk/tree/main/packages/safe-service-client#confirmtransaction.
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).
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.
Thx @DmitryBespalov , I changed it but still get the same output: