import { privateKeyToAccount } from "viem/accounts";
import {
createWalletClient,
createPublicClient,
publicActions,
webSocket,
} from "viem";
import { sepolia } from "viem/chains";
import {
newtonWalletClientActions,
newtonPublicClientActions,
} from "@magicnewton/newton-protocol-sdk";
const signer = privateKeyToAccount(PRIVATE_KEY);
export const walletClient = createWalletClient({
chain: sepolia,
transport: webSocket(webSocketUrl),
account: signer,
})
.extend(publicActions)
.extend(newtonWalletClientActions());
const publicClient = createPublicClient({
chain: sepolia,
transport: webSocket(webSocketUrl),
}).extend(
newtonPublicClientActions({
policyContractAddress: POLICY_CLIENT_ADDRESS,
})
);
function stringToHexBytes(str: string) {
const encoder = new TextEncoder();
const bytes = encoder.encode(str);
return Array.from(bytes)
.map((byte) => byte.toString(16).padStart(2, "0"))
.join("");
}
const wasmArgs = stringToHexBytes(toAddress);
const intentCode = `{
"policyClient": "${POLICY_CLIENT_ADDRESS}",
"intent": {
"from": "${connectedAddress}",
"to": "${toAddress}",
"value": "0x1",
"data": "0x",
"chainId": 11155111,
"functionSignature": "0x"
},
"timeout": 60,
"wasmArgs": "${wasmArgs}"
}`;
const pendingTask = await walletClient.submitEvaluationRequest(
JSON.parse(intentCode)
);
const response: TaskResponseResult = await publicClient.waitForTaskResponded({
taskId: pendingTask.result.taskId,
timeoutMs: 20_000,
});
console.log("evaluation result: ", response.taskResponse.evaluationResult);
console.log("attestation: ", response.attestation);