Skip to content

Commit

Permalink
Merge pull request #106 from gnosisguild/6963-override-improvements
Browse files Browse the repository at this point in the history
Fix curve transaction flow
  • Loading branch information
jfschwarz authored May 2, 2024
2 parents 48de842 + a9bf1c9 commit 286d92a
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 12 deletions.
14 changes: 10 additions & 4 deletions extension/src/injection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ const announceEip6963Provider = (provider: InjectedProvider) => {
const info = {
uuid: '2a0c727b-4359-49a0-920c-b411d32b1d1e', // random uuid
name: 'Zodiac Pilot',
// icon: 'TODO',
icon: '//pilot.gnosisguild.org/zodiac48.png',
rdns: 'org.gnosisguild.pilot',
}

Expand All @@ -66,13 +66,19 @@ announceEip6963Provider(injectedProvider)
// override EIP-6963 provider announcement for MetaMask
window.addEventListener('eip6963:announceProvider', (event) => {
const ev = event as CustomEvent
if (ev.detail.info.rdns === 'io.metamask' && !ev.detail.metamaskOverride) {
if (
ev.detail.info.rdns === 'io.metamask' &&
ev.detail.info.name !== 'Zodiac Pilot'
) {
window.dispatchEvent(
new CustomEvent('eip6963:announceProvider', {
detail: Object.freeze({
info: ev.detail.info,
info: {
...ev.detail.info,
name: 'Zodiac Pilot',
icon: '//pilot.gnosisguild.org/zodiac48.png',
},
provider: injectedProvider,
metamaskOverride: true,
}),
})
)
Expand Down
37 changes: 29 additions & 8 deletions extension/src/providers/ProvideTenderly.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -179,12 +179,14 @@ export class TenderlyProvider extends EventEmitter {
private blockNumber: number | undefined

private tenderlyForkApi: string
private throttledIncreaseBlock: () => void

constructor(chainId: ChainId) {
super()
this.provider = getEip1193ReadOnlyProvider(chainId, ZERO_ADDRESS)
this.chainId = chainId
this.tenderlyForkApi = 'https://fork-api.pilot.gnosisguild.org'
this.throttledIncreaseBlock = throttle(this.increaseBlock, 1000)
}

async request(request: JsonRpcRequest): Promise<any> {
Expand All @@ -194,7 +196,10 @@ export class TenderlyProvider extends EventEmitter {
}

if (request.method === 'eth_blockNumber' && this.blockNumber) {
// Save some polling requests so we don't hit Tenderly's rate limits too quickly
// Some apps (such as Curve) only let the user continue in a transaction flow after a certain number of blocks have been mined.
// To simulate progress of time/blocks, we increase the block number when polled, throttled in intervals of 1 second.
// We cache the block number to save some polling requests so we don't hit Tenderly's rate limits too quickly.
this.throttledIncreaseBlock()
return this.blockNumber
}

Expand Down Expand Up @@ -228,17 +233,11 @@ export class TenderlyProvider extends EventEmitter {
}

if (request.method === 'eth_sendTransaction') {
// when sending a transaction, we need retrieve that transaction's ID on Tenderly
// when sending a transaction, we need to retrieve that transaction's ID on Tenderly
const { global_head: headTransactionId, block_number } =
await this.fetchForkInfo()
this.blockNumber = block_number
this.transactionIds.set(result, headTransactionId) // result is the transaction hash

// advance two extra blocks to account for apps waiting on the transaction's inclusion in the next block
window.setTimeout(async () => {
await provider.send('evm_increaseBlocks', ['0x2'])
if (this.blockNumber) this.blockNumber += 2
}, 1)
}

return result
Expand Down Expand Up @@ -337,4 +336,26 @@ export class TenderlyProvider extends EventEmitter {
dashboardLink: `https://dashboard.tenderly.co/public/gnosisguild/zodiac-pilot/fork-simulation/${transactionId}`,
}
}

private increaseBlock = async () => {
if (!this.forkProviderPromise || !this.blockNumber) return
const provider = await this.forkProviderPromise
await provider.send('evm_increaseBlocks', ['0x1'])
this.blockNumber++
}
}

function throttle(func: (...args: any[]) => void, timeout: number) {
let ready = true
return (...args: any[]) => {
if (!ready) {
return
}

ready = false
func(...args)
setTimeout(() => {
ready = true
}, timeout)
}
}

0 comments on commit 286d92a

Please sign in to comment.