Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Development to main #609

Merged
merged 4 commits into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions packages/safe-apps-provider/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# @safe-global/safe-apps-provider

## 0.18.4

### Patch Changes

- 21ffde5: Add EIP-5792 support

## 0.18.3

### Patch Changes
Expand Down
73 changes: 72 additions & 1 deletion packages/safe-apps-provider/dist/provider.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/safe-apps-provider/dist/provider.js.map

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions packages/safe-apps-provider/dist/utils.d.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export declare function getLowerCase(value: string): string;
export declare function numberToHex(value: number): `0x${string}`;
6 changes: 5 additions & 1 deletion packages/safe-apps-provider/dist/utils.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/safe-apps-provider/dist/utils.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/safe-apps-provider/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@safe-global/safe-apps-provider",
"version": "0.18.3",
"version": "0.18.4",
"description": "A provider wrapper of Safe Apps SDK",
"main": "dist/index.js",
"typings": "dist/index.d.ts",
Expand Down
96 changes: 93 additions & 3 deletions packages/safe-apps-provider/src/provider.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import SafeAppsSDK, { SafeInfo, Web3TransactionObject } from '@safe-global/safe-apps-sdk';
import SafeAppsSDK, { SafeInfo, TransactionStatus, Web3TransactionObject } from '@safe-global/safe-apps-sdk';
import { EventEmitter } from 'events';
import { EIP1193Provider } from './types';
import { getLowerCase } from './utils';
import { getLowerCase, numberToHex } from './utils';

// The API is based on Ethereum JavaScript API Provider Standard. Link: https://eips.ethereum.org/EIPS/eip-1193
export class SafeAppProvider extends EventEmitter implements EIP1193Provider {
Expand Down Expand Up @@ -38,7 +38,7 @@

case 'net_version':
case 'eth_chainId':
return `0x${this.chainId.toString(16)}`;
return numberToHex(this.chainId);

case 'personal_sign': {
const [message, address] = params;
Expand Down Expand Up @@ -195,6 +195,96 @@
case 'safe_setSettings':
return this.sdk.eth.setSafeSettings([params[0]]);

case 'wallet_sendCalls': {
if (params[0].from !== this.safe.safeAddress) {
throw Error('Invalid from address');
}

const txs = params[0].calls.map(
(
call: { to?: `0x${string}`; data?: `0x${string}`; value?: `0x${string}`; chainId?: `0x${string}` },
i: number,
) => {
if (call.chainId !== numberToHex(this.chainId)) {
throw new Error(`Invalid call #${i}: Safe is not on chain ${call.chainId}`);
}
if (!call.to) {
throw new Error(`Invalid call #${i}: missing "to" field`);
}
return {
to: call.to,
data: call.data ?? '0x',
value: call.value ?? numberToHex(0),
};
},
);

const { safeTxHash } = await this.sdk.txs.send({ txs });
return safeTxHash;
}

case 'wallet_getCallsStatus': {
const CallStatus: {
[key in TransactionStatus]: 'PENDING' | 'CONFIRMED';
} = {
[TransactionStatus.AWAITING_CONFIRMATIONS]: 'PENDING',
[TransactionStatus.AWAITING_EXECUTION]: 'PENDING',
[TransactionStatus.CANCELLED]: 'CONFIRMED',
[TransactionStatus.FAILED]: 'CONFIRMED',
[TransactionStatus.SUCCESS]: 'CONFIRMED',
};

const tx = await this.sdk.txs.getBySafeTxHash(params[0]).catch(() => null);
if (!tx?.txHash) {
throw new Error('Transaction not found');
}

const receipt = await this.sdk.eth.getTransactionReceipt([tx.txHash]).catch(() => null);
if (!receipt) {
throw new Error('Transaction receipt not found');
}

const calls =
tx.txData?.dataDecoded?.method !== 'multiSend'
? 1
: // Number of batched transactions
tx.txData.dataDecoded.parameters?.[0].valueDecoded?.length ?? 1;

// Typed as number; is hex
const blockNumber = Number(receipt.blockNumber);
const gasUsed = Number(receipt.gasUsed);

const receipts = Array(calls).fill({
success: numberToHex(tx.txStatus === TransactionStatus.SUCCESS ? 1 : 0),
blockHash: receipt.blockHash,
blockNumber: numberToHex(blockNumber),
blockTimestamp: numberToHex(tx.executedAt ?? 0),
gasUsed: numberToHex(gasUsed),
transactionHash: tx.txHash,
logs: receipt.logs,
});

return {
status: CallStatus[tx.txStatus],
receipts,
};
}

case 'wallet_showCallsStatus': {
// Cannot open transaction details page via SDK
throw new Error(`"${request.method}" not supported`);
}

case 'wallet_getCapabilities': {
return {
[numberToHex(this.chainId)]: {
atomicBatch: {
supported: true,
},
},
};
}

default:
throw Error(`"${request.method}" not implemented`);
}
Expand All @@ -202,7 +292,7 @@

// this method is needed for ethers v4
// https://github.com/ethers-io/ethers.js/blob/427e16826eb15d52d25c4f01027f8db22b74b76c/src.ts/providers/web3-provider.ts#L41-L55
send(request: any, callback: (error: any, response?: any) => void): void {

Check warning on line 295 in packages/safe-apps-provider/src/provider.ts

View workflow job for this annotation

GitHub Actions / eslint

Unexpected any. Specify a different type

Check warning on line 295 in packages/safe-apps-provider/src/provider.ts

View workflow job for this annotation

GitHub Actions / eslint

Unexpected any. Specify a different type

Check warning on line 295 in packages/safe-apps-provider/src/provider.ts

View workflow job for this annotation

GitHub Actions / eslint

Unexpected any. Specify a different type
if (!request) callback('Undefined request');
this.request(request)
.then((result) => callback(null, { jsonrpc: '2.0', id: request.id, result }))
Expand Down
4 changes: 4 additions & 0 deletions packages/safe-apps-provider/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,7 @@ export function getLowerCase(value: string): string {
}
return value;
}

export function numberToHex(value: number): `0x${string}` {
return `0x${value.toString(16)}`;
}
2 changes: 1 addition & 1 deletion packages/safe-apps-sdk/dist/cjs/version.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/safe-apps-sdk/dist/esm/version.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/safe-apps-sdk/src/version.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export const getSDKVersion = () => '9.0.0';
export const getSDKVersion = () => '9.1.0';
6 changes: 3 additions & 3 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7882,9 +7882,9 @@ internal-slot@^1.0.3, internal-slot@^1.0.5:
side-channel "^1.0.4"

ip@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/ip/-/ip-2.0.0.tgz#4cf4ab182fee2314c75ede1276f8c80b479936da"
integrity sha512-WKa+XuLG1A1R0UWhl2+1XQSi+fZWMsYKffMZTTYsiZaUD8k2yDAj5atimTUD2TZkyCkNEeYE5NhFZmupOGtjYQ==
version "2.0.1"
resolved "https://registry.yarnpkg.com/ip/-/ip-2.0.1.tgz#e8f3595d33a3ea66490204234b77636965307105"
integrity sha512-lJUL9imLTNi1ZfXT+DU6rBBdbiKGBuay9B6xGSPVjUeQwaH1RIGqef8RZkUtHioLmSNpPR5M4HVKJGm1j8FWVQ==

[email protected]:
version "1.9.1"
Expand Down
Loading