|
| 1 | +import { Address } from 'ox' |
| 2 | +import { |
| 3 | + Precondition, |
| 4 | + NativeBalancePrecondition, |
| 5 | + Erc20BalancePrecondition, |
| 6 | + Erc20ApprovalPrecondition, |
| 7 | + Erc721OwnershipPrecondition, |
| 8 | + Erc721ApprovalPrecondition, |
| 9 | + Erc1155BalancePrecondition, |
| 10 | + Erc1155ApprovalPrecondition, |
| 11 | +} from './types' |
| 12 | + |
| 13 | +export interface IntentPrecondition { |
| 14 | + type: string |
| 15 | + data: string |
| 16 | +} |
| 17 | + |
| 18 | +export function decodePreconditions(preconditions: IntentPrecondition[]): Precondition[] { |
| 19 | + const decodedPreconditions: Precondition[] = [] |
| 20 | + |
| 21 | + for (const p of preconditions) { |
| 22 | + const decoded = decodePrecondition(p) |
| 23 | + if (decoded) { |
| 24 | + decodedPreconditions.push(decoded) |
| 25 | + } |
| 26 | + } |
| 27 | + |
| 28 | + return decodedPreconditions |
| 29 | +} |
| 30 | + |
| 31 | +export function decodePrecondition(p: IntentPrecondition): Precondition | undefined { |
| 32 | + if (!p) { |
| 33 | + return undefined |
| 34 | + } |
| 35 | + |
| 36 | + let precondition: Precondition | undefined |
| 37 | + |
| 38 | + try { |
| 39 | + const data = JSON.parse(p.data) |
| 40 | + |
| 41 | + switch (p.type) { |
| 42 | + case 'native-balance': |
| 43 | + precondition = new NativeBalancePrecondition( |
| 44 | + Address.from(data.address), |
| 45 | + data.min ? BigInt(data.min) : undefined, |
| 46 | + data.max ? BigInt(data.max) : undefined, |
| 47 | + ) |
| 48 | + break |
| 49 | + |
| 50 | + case 'erc20-balance': |
| 51 | + precondition = new Erc20BalancePrecondition( |
| 52 | + Address.from(data.address), |
| 53 | + Address.from(data.token), |
| 54 | + data.min ? BigInt(data.min) : undefined, |
| 55 | + data.max ? BigInt(data.max) : undefined, |
| 56 | + ) |
| 57 | + break |
| 58 | + |
| 59 | + case 'erc20-approval': |
| 60 | + precondition = new Erc20ApprovalPrecondition( |
| 61 | + Address.from(data.address), |
| 62 | + Address.from(data.token), |
| 63 | + Address.from(data.operator), |
| 64 | + BigInt(data.min), |
| 65 | + ) |
| 66 | + break |
| 67 | + |
| 68 | + case 'erc721-ownership': |
| 69 | + precondition = new Erc721OwnershipPrecondition( |
| 70 | + Address.from(data.address), |
| 71 | + Address.from(data.token), |
| 72 | + BigInt(data.tokenId), |
| 73 | + data.owned, |
| 74 | + ) |
| 75 | + break |
| 76 | + |
| 77 | + case 'erc721-approval': |
| 78 | + precondition = new Erc721ApprovalPrecondition( |
| 79 | + Address.from(data.address), |
| 80 | + Address.from(data.token), |
| 81 | + BigInt(data.tokenId), |
| 82 | + Address.from(data.operator), |
| 83 | + ) |
| 84 | + break |
| 85 | + |
| 86 | + case 'erc1155-balance': |
| 87 | + precondition = new Erc1155BalancePrecondition( |
| 88 | + Address.from(data.address), |
| 89 | + Address.from(data.token), |
| 90 | + BigInt(data.tokenId), |
| 91 | + data.min ? BigInt(data.min) : undefined, |
| 92 | + data.max ? BigInt(data.max) : undefined, |
| 93 | + ) |
| 94 | + break |
| 95 | + |
| 96 | + case 'erc1155-approval': |
| 97 | + precondition = new Erc1155ApprovalPrecondition( |
| 98 | + Address.from(data.address), |
| 99 | + Address.from(data.token), |
| 100 | + BigInt(data.tokenId), |
| 101 | + Address.from(data.operator), |
| 102 | + BigInt(data.min), |
| 103 | + ) |
| 104 | + break |
| 105 | + |
| 106 | + default: |
| 107 | + return undefined |
| 108 | + } |
| 109 | + |
| 110 | + const error = precondition.isValid() |
| 111 | + if (error) { |
| 112 | + console.warn(`Invalid precondition: ${error.message}`) |
| 113 | + return undefined |
| 114 | + } |
| 115 | + |
| 116 | + return precondition |
| 117 | + } catch (e) { |
| 118 | + console.warn(`Failed to decode precondition: ${e}`) |
| 119 | + return undefined |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +export function encodePrecondition(p: Precondition): string { |
| 124 | + const data: any = {} |
| 125 | + |
| 126 | + switch (p.type()) { |
| 127 | + case 'native-balance': { |
| 128 | + const native = p as NativeBalancePrecondition |
| 129 | + data.address = native.address.toString() |
| 130 | + if (native.min !== undefined) data.min = native.min.toString() |
| 131 | + if (native.max !== undefined) data.max = native.max.toString() |
| 132 | + break |
| 133 | + } |
| 134 | + |
| 135 | + case 'erc20-balance': { |
| 136 | + const erc20 = p as Erc20BalancePrecondition |
| 137 | + data.address = erc20.address.toString() |
| 138 | + data.token = erc20.token.toString() |
| 139 | + if (erc20.min !== undefined) data.min = erc20.min.toString() |
| 140 | + if (erc20.max !== undefined) data.max = erc20.max.toString() |
| 141 | + break |
| 142 | + } |
| 143 | + |
| 144 | + case 'erc20-approval': { |
| 145 | + const erc20 = p as Erc20ApprovalPrecondition |
| 146 | + data.address = erc20.address.toString() |
| 147 | + data.token = erc20.token.toString() |
| 148 | + data.operator = erc20.operator.toString() |
| 149 | + data.min = erc20.min.toString() |
| 150 | + break |
| 151 | + } |
| 152 | + |
| 153 | + case 'erc721-ownership': { |
| 154 | + const erc721 = p as Erc721OwnershipPrecondition |
| 155 | + data.address = erc721.address.toString() |
| 156 | + data.token = erc721.token.toString() |
| 157 | + data.tokenId = erc721.tokenId.toString() |
| 158 | + if (erc721.owned !== undefined) data.owned = erc721.owned |
| 159 | + break |
| 160 | + } |
| 161 | + |
| 162 | + case 'erc721-approval': { |
| 163 | + const erc721 = p as Erc721ApprovalPrecondition |
| 164 | + data.address = erc721.address.toString() |
| 165 | + data.token = erc721.token.toString() |
| 166 | + data.tokenId = erc721.tokenId.toString() |
| 167 | + data.operator = erc721.operator.toString() |
| 168 | + break |
| 169 | + } |
| 170 | + |
| 171 | + case 'erc1155-balance': { |
| 172 | + const erc1155 = p as Erc1155BalancePrecondition |
| 173 | + data.address = erc1155.address.toString() |
| 174 | + data.token = erc1155.token.toString() |
| 175 | + data.tokenId = erc1155.tokenId.toString() |
| 176 | + if (erc1155.min !== undefined) data.min = erc1155.min.toString() |
| 177 | + if (erc1155.max !== undefined) data.max = erc1155.max.toString() |
| 178 | + break |
| 179 | + } |
| 180 | + |
| 181 | + case 'erc1155-approval': { |
| 182 | + const erc1155 = p as Erc1155ApprovalPrecondition |
| 183 | + data.address = erc1155.address.toString() |
| 184 | + data.token = erc1155.token.toString() |
| 185 | + data.tokenId = erc1155.tokenId.toString() |
| 186 | + data.operator = erc1155.operator.toString() |
| 187 | + data.min = erc1155.min.toString() |
| 188 | + break |
| 189 | + } |
| 190 | + } |
| 191 | + |
| 192 | + return JSON.stringify(data) |
| 193 | +} |
0 commit comments