Onlife/common/provider.js
2025-04-19 15:38:48 +08:00

38 lines
1.0 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { ethers } from 'ethers';
// 自定义Provider使用uni.request替代原生fetch
class UniAppProvider extends ethers.providers.JsonRpcProvider {
async send(method, params) {
const payload = {
method: method,
params: params,
id: Math.floor(Math.random() * 1000000),
jsonrpc: "2.0"
};
return new Promise((resolve, reject) => {
uni.request({
url: this.connection.url,
data: JSON.stringify(payload),
method: 'POST',
header: {
'content-type': 'application/json'
},
success: (res) => {
if (res.statusCode !== 200) {
reject(new Error(`HTTP Error: ${res.statusCode}`));
return;
}
if (res.data.error) {
reject(new Error(res.data.error.message || JSON.stringify(res.data.error)));
return;
}
resolve(res.data.result);
},
fail: (err) => {
reject(new Error(err.errMsg || 'Request failed'));
}
});
});
}
}