编译器
0.8.20+commit.a1b79de6
文件 1 的 4:AIPoweredWallet.sol
pragma solidity ^0.8.12;
import {Strings} from "@openzeppelin/contracts/utils/Strings.sol";
interface AIKernel {
event NewInference(
uint256 indexed inferenceId,
address indexed model,
address indexed creator,
uint256 value,
uint256 originInferenceId
);
function infer(
bytes calldata _data,
bool _flag
) external payable returns (uint256 referenceId);
function infer(
bytes calldata _data
) external payable returns (uint256 referenceId);
}
interface PromptScheduler {
enum InferenceStatus {
Nil,
Solving,
Commit,
Reveal,
Processed,
Killed,
Transferred
}
enum AssignmentRole {
Nil,
Validating,
Mining
}
enum Vote {
Nil,
Disapproval,
Approval
}
struct Assignment {
uint256 inferenceId;
bytes32 commitment;
bytes32 digest;
uint40 revealNonce;
address worker;
AssignmentRole role;
Vote vote;
bytes output;
}
struct Inference {
uint256[] assignments;
bytes input;
uint256 value;
uint256 feeL2;
uint256 feeTreasury;
address modelAddress;
uint40 submitTimeout;
uint40 commitTimeout;
uint40 revealTimeout;
InferenceStatus status;
address creator;
address processedMiner;
address referrer;
}
function getInferenceInfo(
uint256 _inferenceId
) external view returns (Inference memory);
function getAssignmentInfo(
uint256 _assignmentId
) external view returns (Assignment memory);
}
contract AIPoweredWallet {
struct TxInfo {
address sender;
address receiver;
uint256 amount;
}
address public kernel;
address public promptScheduler;
mapping(address user => string) public context;
mapping(uint256 inferId => TxInfo) public txInfo;
event SuspiciousTransaction(uint256 indexed inferenceId, bytes prompt);
event Sent(uint256 indexed inferenceId, TxInfo txInfo);
constructor(address _kernelAddress, address _promptSchedulerAddress) {
require(
_kernelAddress != address(0) &&
_promptSchedulerAddress != address(0),
"AIPoweredWallet: Invalid address"
);
kernel = _kernelAddress;
promptScheduler = _promptSchedulerAddress;
}
function buildRequest(
string memory _prompt
) public pure returns (string memory) {
string memory request = string.concat('{ "messages"');
request = string.concat(request, " :[");
request = string.concat(
request,
'{"role":"system","content":"You are a helpful assistant"},'
);
request = string.concat(request, '{"role":"user","content":"');
request = string.concat(request, _prompt);
request = string.concat(request, ' "}');
request = string.concat(request, "],");
request = string.concat(request, '"max_tokens":1024,');
request = string.concat(
request,
'"model":"PrimeIntellect/INTELLECT-1-Instruct"'
);
request = string.concat(request, "}");
return request;
}
function suspiciousTransaction(
address _receiver,
uint256 _amount
) external {
string memory prompt = string.concat(
"Assess the following Ethereum transaction history for any suspicious patterns. Respond with 'yes' if there is ANY indication of unusual or potentially malicious activity, however slight. Respond with 'no' ONLY if the transaction history is completely clean and normal. Your answer must be one word only. ",
Strings.toHexString(msg.sender),
" transfer ",
Strings.toString(_amount),
" wei to ",
Strings.toHexString(_receiver),
". ",
context[msg.sender]
);
string memory request = buildRequest(prompt);
uint256 inferenceId = AIKernel(kernel).infer(bytes(request));
txInfo[inferenceId] = TxInfo(msg.sender, _receiver, _amount);
emit SuspiciousTransaction(inferenceId, bytes(request));
}
function send(uint256 _inferenceId) external payable {
TxInfo memory info = txInfo[_inferenceId];
require(info.sender == msg.sender, "AIPoweredWallet: Unauthorized");
address receivedWallet = info.receiver;
require(
receivedWallet != address(0),
"AIPoweredWallet: Invalid wallet address"
);
require(
info.amount == msg.value,
"AIPoweredWallet: Invalid transaction amount"
);
bytes memory result = fetchInferenceResult(_inferenceId);
require(
keccak256(result) == keccak256(abi.encodePacked("no")),
"AIPoweredWallet: Suspicious transaction"
);
(bool success, ) = payable(receivedWallet).call{value: msg.value}("");
require(success, "AIPoweredWallet: Transfer failed");
context[msg.sender] = string.concat(
context[msg.sender],
Strings.toHexString(msg.sender),
" transfer ",
Strings.toString(msg.value),
" wei to ",
Strings.toHexString(receivedWallet),
". "
);
}
function fetchInferenceResult(
uint256 _inferenceId
) public view returns (bytes memory) {
PromptScheduler.Inference memory inferInfo = PromptScheduler(
promptScheduler
).getInferenceInfo(_inferenceId);
if (inferInfo.assignments.length == 0) revert("Wait for inference");
return
PromptScheduler(promptScheduler)
.getAssignmentInfo(inferInfo.assignments[0])
.output;
}
}
文件 2 的 4:Math.sol
pragma solidity ^0.8.0;
library Math {
enum Rounding {
Down,
Up,
Zero
}
function max(uint256 a, uint256 b) internal pure returns (uint256) {
return a > b ? a : b;
}
function min(uint256 a, uint256 b) internal pure returns (uint256) {
return a < b ? a : b;
}
function average(uint256 a, uint256 b) internal pure returns (uint256) {
return (a & b) + (a ^ b) / 2;
}
function ceilDiv(uint256 a, uint256 b) internal pure returns (uint256) {
return a == 0 ? 0 : (a - 1) / b + 1;
}
function mulDiv(uint256 x, uint256 y, uint256 denominator) internal pure returns (uint256 result) {
unchecked {
uint256 prod0;
uint256 prod1;
assembly {
let mm := mulmod(x, y, not(0))
prod0 := mul(x, y)
prod1 := sub(sub(mm, prod0), lt(mm, prod0))
}
if (prod1 == 0) {
return prod0 / denominator;
}
require(denominator > prod1, "Math: mulDiv overflow");
uint256 remainder;
assembly {
remainder := mulmod(x, y, denominator)
prod1 := sub(prod1, gt(remainder, prod0))
prod0 := sub(prod0, remainder)
}
uint256 twos = denominator & (~denominator + 1);
assembly {
denominator := div(denominator, twos)
prod0 := div(prod0, twos)
twos := add(div(sub(0, twos), twos), 1)
}
prod0 |= prod1 * twos;
uint256 inverse = (3 * denominator) ^ 2;
inverse *= 2 - denominator * inverse;
inverse *= 2 - denominator * inverse;
inverse *= 2 - denominator * inverse;
inverse *= 2 - denominator * inverse;
inverse *= 2 - denominator * inverse;
inverse *= 2 - denominator * inverse;
result = prod0 * inverse;
return result;
}
}
function mulDiv(uint256 x, uint256 y, uint256 denominator, Rounding rounding) internal pure returns (uint256) {
uint256 result = mulDiv(x, y, denominator);
if (rounding == Rounding.Up && mulmod(x, y, denominator) > 0) {
result += 1;
}
return result;
}
function sqrt(uint256 a) internal pure returns (uint256) {
if (a == 0) {
return 0;
}
uint256 result = 1 << (log2(a) >> 1);
unchecked {
result = (result + a / result) >> 1;
result = (result + a / result) >> 1;
result = (result + a / result) >> 1;
result = (result + a / result) >> 1;
result = (result + a / result) >> 1;
result = (result + a / result) >> 1;
result = (result + a / result) >> 1;
return min(result, a / result);
}
}
function sqrt(uint256 a, Rounding rounding) internal pure returns (uint256) {
unchecked {
uint256 result = sqrt(a);
return result + (rounding == Rounding.Up && result * result < a ? 1 : 0);
}
}
function log2(uint256 value) internal pure returns (uint256) {
uint256 result = 0;
unchecked {
if (value >> 128 > 0) {
value >>= 128;
result += 128;
}
if (value >> 64 > 0) {
value >>= 64;
result += 64;
}
if (value >> 32 > 0) {
value >>= 32;
result += 32;
}
if (value >> 16 > 0) {
value >>= 16;
result += 16;
}
if (value >> 8 > 0) {
value >>= 8;
result += 8;
}
if (value >> 4 > 0) {
value >>= 4;
result += 4;
}
if (value >> 2 > 0) {
value >>= 2;
result += 2;
}
if (value >> 1 > 0) {
result += 1;
}
}
return result;
}
function log2(uint256 value, Rounding rounding) internal pure returns (uint256) {
unchecked {
uint256 result = log2(value);
return result + (rounding == Rounding.Up && 1 << result < value ? 1 : 0);
}
}
function log10(uint256 value) internal pure returns (uint256) {
uint256 result = 0;
unchecked {
if (value >= 10 ** 64) {
value /= 10 ** 64;
result += 64;
}
if (value >= 10 ** 32) {
value /= 10 ** 32;
result += 32;
}
if (value >= 10 ** 16) {
value /= 10 ** 16;
result += 16;
}
if (value >= 10 ** 8) {
value /= 10 ** 8;
result += 8;
}
if (value >= 10 ** 4) {
value /= 10 ** 4;
result += 4;
}
if (value >= 10 ** 2) {
value /= 10 ** 2;
result += 2;
}
if (value >= 10 ** 1) {
result += 1;
}
}
return result;
}
function log10(uint256 value, Rounding rounding) internal pure returns (uint256) {
unchecked {
uint256 result = log10(value);
return result + (rounding == Rounding.Up && 10 ** result < value ? 1 : 0);
}
}
function log256(uint256 value) internal pure returns (uint256) {
uint256 result = 0;
unchecked {
if (value >> 128 > 0) {
value >>= 128;
result += 16;
}
if (value >> 64 > 0) {
value >>= 64;
result += 8;
}
if (value >> 32 > 0) {
value >>= 32;
result += 4;
}
if (value >> 16 > 0) {
value >>= 16;
result += 2;
}
if (value >> 8 > 0) {
result += 1;
}
}
return result;
}
function log256(uint256 value, Rounding rounding) internal pure returns (uint256) {
unchecked {
uint256 result = log256(value);
return result + (rounding == Rounding.Up && 1 << (result << 3) < value ? 1 : 0);
}
}
}
文件 3 的 4:SignedMath.sol
pragma solidity ^0.8.0;
library SignedMath {
function max(int256 a, int256 b) internal pure returns (int256) {
return a > b ? a : b;
}
function min(int256 a, int256 b) internal pure returns (int256) {
return a < b ? a : b;
}
function average(int256 a, int256 b) internal pure returns (int256) {
int256 x = (a & b) + ((a ^ b) >> 1);
return x + (int256(uint256(x) >> 255) & (a ^ b));
}
function abs(int256 n) internal pure returns (uint256) {
unchecked {
return uint256(n >= 0 ? n : -n);
}
}
}
文件 4 的 4:Strings.sol
pragma solidity ^0.8.0;
import "./math/Math.sol";
import "./math/SignedMath.sol";
library Strings {
bytes16 private constant _SYMBOLS = "0123456789abcdef";
uint8 private constant _ADDRESS_LENGTH = 20;
function toString(uint256 value) internal pure returns (string memory) {
unchecked {
uint256 length = Math.log10(value) + 1;
string memory buffer = new string(length);
uint256 ptr;
assembly {
ptr := add(buffer, add(32, length))
}
while (true) {
ptr--;
assembly {
mstore8(ptr, byte(mod(value, 10), _SYMBOLS))
}
value /= 10;
if (value == 0) break;
}
return buffer;
}
}
function toString(int256 value) internal pure returns (string memory) {
return string(abi.encodePacked(value < 0 ? "-" : "", toString(SignedMath.abs(value))));
}
function toHexString(uint256 value) internal pure returns (string memory) {
unchecked {
return toHexString(value, Math.log256(value) + 1);
}
}
function toHexString(uint256 value, uint256 length) internal pure returns (string memory) {
bytes memory buffer = new bytes(2 * length + 2);
buffer[0] = "0";
buffer[1] = "x";
for (uint256 i = 2 * length + 1; i > 1; --i) {
buffer[i] = _SYMBOLS[value & 0xf];
value >>= 4;
}
require(value == 0, "Strings: hex length insufficient");
return string(buffer);
}
function toHexString(address addr) internal pure returns (string memory) {
return toHexString(uint256(uint160(addr)), _ADDRESS_LENGTH);
}
function equal(string memory a, string memory b) internal pure returns (bool) {
return keccak256(bytes(a)) == keccak256(bytes(b));
}
}
{
"compilationTarget": {
"contracts/AIPoweredWallet.sol": "AIPoweredWallet"
},
"evmVersion": "paris",
"libraries": {},
"metadata": {
"bytecodeHash": "ipfs",
"useLiteralContent": true
},
"optimizer": {
"enabled": true,
"runs": 2000000
},
"remappings": [],
"viaIR": true
}
[{"inputs":[{"internalType":"address","name":"_kernelAddress","type":"address"},{"internalType":"address","name":"_promptSchedulerAddress","type":"address"}],"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"uint256","name":"inferenceId","type":"uint256"},{"components":[{"internalType":"address","name":"sender","type":"address"},{"internalType":"address","name":"receiver","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"}],"indexed":false,"internalType":"struct AIPoweredWallet.TxInfo","name":"txInfo","type":"tuple"}],"name":"Sent","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"uint256","name":"inferenceId","type":"uint256"},{"indexed":false,"internalType":"bytes","name":"prompt","type":"bytes"}],"name":"SuspiciousTransaction","type":"event"},{"inputs":[{"internalType":"string","name":"_prompt","type":"string"}],"name":"buildRequest","outputs":[{"internalType":"string","name":"","type":"string"}],"stateMutability":"pure","type":"function"},{"inputs":[{"internalType":"address","name":"user","type":"address"}],"name":"context","outputs":[{"internalType":"string","name":"","type":"string"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"_inferenceId","type":"uint256"}],"name":"fetchInferenceResult","outputs":[{"internalType":"bytes","name":"","type":"bytes"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"kernel","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"promptScheduler","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"_inferenceId","type":"uint256"}],"name":"send","outputs":[],"stateMutability":"payable","type":"function"},{"inputs":[{"internalType":"address","name":"_receiver","type":"address"},{"internalType":"uint256","name":"_amount","type":"uint256"}],"name":"suspiciousTransaction","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"inferId","type":"uint256"}],"name":"txInfo","outputs":[{"internalType":"address","name":"sender","type":"address"},{"internalType":"address","name":"receiver","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"}],"stateMutability":"view","type":"function"}]