Created
August 25, 2020 21:12
-
-
Save cawfree/6286e64e883b0c03a9e79e364a4055a8 to your computer and use it in GitHub Desktop.
GnosisSafe-3
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 { useCallback } from "react"; | |
import EIP712Domain from "eth-typed-data"; | |
import BigNumber from "bignumber.js"; | |
import { SimpleSigner } from "did-jwt"; | |
import { ethers } from "ethers"; | |
const { utils } = ethers; | |
import { useGnosisApi, useGnosisEstimateTransaction } from "../../session"; | |
export default function useGnosisWithdraw() { | |
const gnosis = useGnosisApi(); | |
const { gnosisEstimateTransaction } = useGnosisEstimateTransaction(); | |
const gnosisWithdraw = useCallback( | |
async ({ safe, privateKey }) => { | |
const safeDomain = new EIP712Domain({ | |
verifyingContract: safe, | |
}); | |
const SafeTx = safeDomain.createType('SafeTx', [ | |
{ type: "address", name: "to" }, | |
{ type: "uint256", name: "value" }, | |
{ type: "bytes", name: "data" }, | |
{ type: "uint8", name: "operation" }, | |
{ type: "uint256", name: "safeTxGas" }, | |
{ type: "uint256", name: "baseGas" }, | |
{ type: "uint256", name: "gasPrice" }, | |
{ type: "address", name: "gasToken" }, | |
{ type: "address", name: "refundReceiver" }, | |
{ type: "uint256", name: "nonce" }, | |
]); | |
const to = utils.getAddress("0x0ebd146ffd9e20bf74e374e5f3a5a567a798177e"); | |
const baseTxn = { | |
to, | |
value: "1000", | |
data: [], | |
nonce: "0", | |
operation: "0", | |
}; | |
console.log(JSON.stringify({ baseTxn })); | |
const { safeTxGas, baseGas, dataGas, gasPrice, gasToken, refundReceiver } = await gnosisEstimateTransaction( | |
safe, | |
{ | |
...baseTxn, | |
data: "0x", | |
}, | |
); | |
const txn = { | |
...baseTxn, | |
safeTxGas, | |
baseGas: dataGas, | |
gasPrice, | |
gasToken, | |
refundReceiver, | |
}; | |
const safeTx = new SafeTx(txn); | |
const signer = new SimpleSigner(privateKey); | |
const { r, s, recoveryParam: v } = await safeTx.sign(signer); | |
console.log({ r, s, v }); | |
const signature = Object.freeze({ | |
r: new BigNumber(`0x${r}`, 16).toString(10), | |
s: new BigNumber(`0x${s}`, 16).toString(10), | |
v, | |
}); | |
console.log({ signature }); | |
const toSend = { | |
...txn, | |
data: "0x", | |
dataGas: dataGas, | |
signatures: [signature], | |
}; | |
console.log(JSON.stringify({ toSend })); | |
const { data: x } = await gnosis({ | |
url: `/v1/safes/${safe}/transactions/`, | |
method: "post", | |
data: toSend, | |
}); | |
console.log("Done?"); | |
}, | |
[gnosis, gnosisEstimateTransaction] | |
); | |
return { gnosisWithdraw }; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment