Skip to content

Instantly share code, notes, and snippets.

@parv3213
Created October 13, 2021 11:30
Show Gist options
  • Save parv3213/5f1a3dc8bd3d37592a3e665ec0a45de2 to your computer and use it in GitHub Desktop.
Save parv3213/5f1a3dc8bd3d37592a3e665ec0a45de2 to your computer and use it in GitHub Desktop.
Interact with Gnosis Safe using Gnosis Transaction Service
import { ethers } from 'hardhat';
import Safe, { EthersAdapter } from '@gnosis.pm/safe-core-sdk';
import { SafeTransactionDataPartial } from '@gnosis.pm/safe-core-sdk-types';
import SafeServiceClient, {
SafeInfoResponse,
} from '@gnosis.pm/safe-service-client';
const SUPPORTED_NETWORKS: number[] = [4]; // Array of supported networks
const GNOSIS_SAFE_ADDRESS: string =
'0xF16cAC48d46135247CC2f1e5054d0B2b0f0000AF'; // Address of Gnosis Safe
const GNOSIS_SAFE_TX_VALUE: string = '100000000'; // Value for transaction
const GNOSIS_SAFE_TX_DATA: string = '0x'; // data for transaction
const SAFE_SERVICE_CLIENT_URL: string =
'https://safe-transaction.rinkeby.gnosis.io'; // service url for service-client
async function main() {
const safeService = new SafeServiceClient(SAFE_SERVICE_CLIENT_URL);
const safeInfo: SafeInfoResponse = await safeService.getSafeInfo(
GNOSIS_SAFE_ADDRESS,
);
console.log(safeInfo);
const [owner1] = await ethers.getSigners();
if (!owner1.provider) {
process.exit(1);
}
const { chainId } = await owner1.provider.getNetwork();
if (SUPPORTED_NETWORKS.indexOf(chainId) === -1)
throw new Error('Un-supported network');
const ethAdapterOwner1 = new EthersAdapter({
ethers,
signer: owner1,
});
const safeSdk: Safe = await Safe.create({
ethAdapter: ethAdapterOwner1,
safeAddress: GNOSIS_SAFE_ADDRESS,
});
const transactions: SafeTransactionDataPartial[] = [
{
to: GNOSIS_SAFE_ADDRESS,
value: GNOSIS_SAFE_TX_VALUE,
data: GNOSIS_SAFE_TX_DATA,
},
];
const safeTransaction = await safeSdk.createTransaction(...transactions);
console.log(safeTransaction);
const safeTransactionHash = await safeSdk.getTransactionHash(safeTransaction);
const owner1Signature = await safeSdk.signTransactionHash(
safeTransactionHash,
);
const result = await safeService.proposeTransaction(
GNOSIS_SAFE_ADDRESS,
safeTransaction.data,
safeTransactionHash,
owner1Signature,
);
console.log(result);
}
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