Skip to content

Commit

Permalink
Merge pull request #52 from reservoirprotocol/ted/add-permit-param
Browse files Browse the repository at this point in the history
Add isValidatingSignature to step item + update demo with usePermit
  • Loading branch information
ted-palmer authored Mar 15, 2024
2 parents 4ba24c5 + a76f067 commit b3a03f4
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 22 deletions.
5 changes: 5 additions & 0 deletions .changeset/tidy-impalas-marry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@reservoir0x/relay-sdk': patch
---

Add isValidatingSignature to step item
28 changes: 28 additions & 0 deletions demo/pages/sdk/actions/bridge.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const BridgeActionPage: NextPage = () => {
const [recipient, setRecipient] = useState<string | undefined>()
const [amount, setAmount] = useState<string>("")
const [currency, setCurrency] =useState<BridgeActionParameters['currency']>('eth')
const [usePermit, setUsePermit] = useState(false)
const [toChainId, setToChainId] = useState<number>(zora.id)
const [fromChainId, setFromChainId] = useState<number>(base.id)
const [depositGasLimit, setDepositGasLimit] = useState("")
Expand Down Expand Up @@ -67,6 +68,30 @@ const BridgeActionPage: NextPage = () => {
</div>
</div>

<div>
<label>Use Permit: </label>
<div>
<input
type="radio"
value="usePermit-true"
name="usePermit"
checked={usePermit === true}
onChange={(e) => setUsePermit(true)}
/>
<label>True</label>
</div>
<div>
<input
type="radio"
value="usePermit-false"
name="usePermit"
checked={usePermit === false}
onChange={(e) => setUsePermit(false)}
/>
<label>False</label>
</div>
</div>

<div>
<label>Recipient: </label>
<input placeholder='Who is the receiver?' value={recipient} onChange={(e) => setRecipient(e.target.value)} />
Expand Down Expand Up @@ -106,6 +131,9 @@ const BridgeActionPage: NextPage = () => {
currency,
recipient: recipient ? recipient as Address : undefined,
depositGasLimit,
options: {
usePermit: usePermit
},
onProgress: (steps, fees, currentStep, currentStepItem, txHashes) => {
console.log(steps, fees, currentStep, currentStepItem, txHashes)
}
Expand Down
1 change: 1 addition & 0 deletions packages/sdk/src/types/Execute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export type Execute = {
orderId: string
orderIndex: string
}[]
isValidatingSignature?: boolean
}[]
}[]
}
Expand Down
3 changes: 3 additions & 0 deletions packages/sdk/src/types/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export interface paths {
name?: string;
address?: string;
decimals?: number;
supportsPermit?: boolean;
}[];
}[];
};
Expand Down Expand Up @@ -292,6 +293,7 @@ export interface paths {
enabled?: boolean;
user?: {
balance?: string;
maxBridgeAmount?: string;
};
solver?: {
address?: string;
Expand Down Expand Up @@ -595,6 +597,7 @@ export interface paths {
id?: string;
status?: string;
user?: string;
recipient?: string;
data?: {
fees?: {
gas?: string;
Expand Down
53 changes: 31 additions & 22 deletions packages/sdk/src/utils/executeSteps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,14 @@ export async function executeSteps(
setState: (
steps: Execute['steps'],
fees?: Execute['fees'],
breakdown?: Execute['breakdown'],
breakdown?: Execute['breakdown']
) => any,
newJson?: Execute,
stepOptions?: {
[stepId: string]: {
gasLimit?: string
}
},
}
) {
const client = getClient()

Expand Down Expand Up @@ -94,7 +94,7 @@ export async function executeSteps(
}

incompleteStepItemIndex = step.items.findIndex(
(item) => item.status == 'incomplete',
(item) => item.status == 'incomplete'
)
if (incompleteStepItemIndex !== -1) {
incompleteStepIndex = i
Expand Down Expand Up @@ -124,7 +124,7 @@ export async function executeSteps(
if (!stepItems) {
client.log(
['Execute Steps: skipping step, no items in step'],
LogLevel.Verbose,
LogLevel.Verbose
)
return
}
Expand All @@ -135,12 +135,12 @@ export async function executeSteps(
if (!stepItem.data) {
client.log(
['Execute Steps: step item data is missing, begin polling'],
LogLevel.Verbose,
LogLevel.Verbose
)
json = (await pollUntilHasData(request, (json) => {
client.log(
['Execute Steps: step item data is missing, polling', json],
LogLevel.Verbose,
LogLevel.Verbose
)
const data = json as Execute
// An item is ready if:
Expand Down Expand Up @@ -169,7 +169,7 @@ export async function executeSteps(
}
client.log(
[`Execute Steps: Begin processing step items for: ${step.action}`],
LogLevel.Verbose,
LogLevel.Verbose
)

const promises = stepItems
Expand All @@ -191,7 +191,7 @@ export async function executeSteps(
[
'Execute Steps: Begin transaction step for, sending transaction',
],
LogLevel.Verbose,
LogLevel.Verbose
)

// if chainId is present in the tx data field then you should relay the tx on that chain
Expand All @@ -212,14 +212,14 @@ export async function executeSteps(
'Execute Steps: Transaction step, got transactions',
txHashes,
],
LogLevel.Verbose,
LogLevel.Verbose
)
stepItem.txHashes = txHashes
if (json) {
setState(
[...json.steps],
{ ...json?.fees },
json?.breakdown,
json?.breakdown
)
}
},
Expand All @@ -229,13 +229,13 @@ export async function executeSteps(
setState(
[...json.steps],
{ ...json?.fees },
json?.breakdown,
json?.breakdown
)
}
},
request,
undefined,
crossChainIntentChainId,
crossChainIntentChainId
)
} catch (e) {
throw e
Expand All @@ -250,12 +250,12 @@ export async function executeSteps(
const postData = stepData['post']
client.log(
['Execute Steps: Begin signature step'],
LogLevel.Verbose,
LogLevel.Verbose
)
if (signData) {
signature = await wallet.handleSignMessageStep(
stepItem as SignatureStepItem,
step,
step
)

if (signature) {
Expand All @@ -269,7 +269,7 @@ export async function executeSteps(
if (postData) {
client.log(['Execute Steps: Posting order'], LogLevel.Verbose)
const postOrderUrl = new URL(
`${request.baseURL}${postData.endpoint}`,
`${request.baseURL}${postData.endpoint}`
)
const headers = {
'Content-Type': 'application/json',
Expand All @@ -294,6 +294,13 @@ export async function executeSteps(

// If check, poll check until validated
if (stepItem?.check) {
stepItem.isValidatingSignature = true
setState(
[...json?.steps],
{ ...json?.fees },
json?.breakdown
)

await pollUntilOk(
{
url: `${request.baseURL}${stepItem?.check.endpoint}`,
Expand All @@ -306,7 +313,7 @@ export async function executeSteps(
`Execute Steps: Polling execute status to check if indexed`,
res,
],
LogLevel.Verbose,
LogLevel.Verbose
)
if (
res?.data?.status === 'success' &&
Expand All @@ -321,20 +328,20 @@ export async function executeSteps(
chainId:
res.data.destinationChainId ?? chain?.id,
}
},
}
)
stepItem.txHashes = chainTxHashes
return true
} else if (res?.data?.status === 'failure') {
throw Error(
res?.data?.details || 'Transaction failed',
res?.data?.details || 'Transaction failed'
)
}
return false
},
maximumAttempts,
0,
pollingInterval,
pollingInterval
)
}

Expand All @@ -354,7 +361,7 @@ export async function executeSteps(
setState(
[...json?.steps],
{ ...json?.fees },
json?.breakdown,
json?.breakdown
)
} catch (err) {
throw err
Expand All @@ -369,6 +376,7 @@ export async function executeSteps(
}

stepItem.status = 'complete'
stepItem.isValidatingSignature = false
setState([...json?.steps], { ...json?.fees }, json?.breakdown)
resolve(stepItem)
} catch (e) {
Expand All @@ -381,6 +389,7 @@ export async function executeSteps(
json.steps[incompleteStepIndex].error = errorMessage
stepItem.error = errorMessage
stepItem.errorData = (e as any)?.response?.data || e
stepItem.isValidatingSignature = false
setState([...json?.steps], { ...json?.fees }, json?.breakdown)
}
reject(error)
Expand All @@ -399,12 +408,12 @@ export async function executeSteps(
} catch (blockError) {
client.log(
['Execute Steps: Failed to get block number', blockError],
LogLevel.Error,
LogLevel.Error
)
}
client.log(
['Execute Steps: An error occurred', err, 'Block Number:', blockNumber],
LogLevel.Error,
LogLevel.Error
)

if (json) {
Expand Down

0 comments on commit b3a03f4

Please sign in to comment.