/* Pseudo-code to write to Solana program (= contract) *//* 写入 Solana 程序(= 合约)的伪代码 */// Decode all 'bytes32' types in EVM to 'PubKey' type in SVM// 将 EVM 中所有 'bytes32' 类型解码为 SVM 中的 'PubKey' 类型const[programId,account,node,key,value]=E2SVM([programId,account,node,key,value],["bytes32","bytes32","bytes32","bytes32","bytes32"]);// Instantiate program interface on Solana// 在 Solana 上实例化程序接口constprogram=newprogram(programId,rpcProvider);// Connect to Solana wallet// 连接到 Solana 钱包constwallet=useWallet();// Call the Solana program using connected wallet with initial calldata// 使用连接的钱包和初始 calldata 调用 Solana 程序// [!] Only approved manager in the Solana program should call// [!] 只有 Solana 程序中批准的管理器才能调用if(wallet.publicKey===account){awaitprogram(wallet).setValue(node,key,value);}
// Example function in Solana program// Solana 程序中的示例函数pubfnsetValue(ctx:Context,node:PubKey,key:PubKey,value:PubKey)->ProgramResult{// Code to verify PROGRAM_ID and rent exemption status// 验证 PROGRAM_ID 和租金豁免状态的代码...// Code for de-serialising, updating and re-serialising the data// 用于反序列化、更新和重新序列化数据的代码...// Store serialised data in account// 将序列化数据存储在帐户中// [!] Stored data must be mapped by node & account// [!] 存储的数据必须按节点和帐户映射...}
/* Pseudo-code of an ERC-3668-compliant HTTP gateway tunneling Solana content to Ethereum *//* 符合 ERC-3668 的 HTTP 网关将 Solana 内容隧道传输到以太坊的伪代码 */// CCIP-Read call by contract to a known gateway URL; gatewayUrl = 'https://read.solana.namesys.xyz/<programId>/<node>/<key>/'// 合约通过 CCIP-Read 调用到已知的网关 URL;gatewayUrl = 'https://read.solana.namesys.xyz/<programId>/<node>/<key>/'const[programId,node,key]=parseQuery(path);// Parse query parameters from path; path = '/<programId>/<node>/<key>/'// 从路径解析查询参数;path = '/<programId>/<node>/<key>/'// Decode 'bytes32' types in EVM to 'PubKey' type in SVM// 将 EVM 中 'bytes32' 类型解码为 SVM 中的 'PubKey' 类型const[programId,node,key]=E2SVM([programId,node,key],["bytes32","bytes32","bytes32"]);// Instantiate program interface on Solana// 在 Solana 上实例化程序接口constprogram=newprogram(programId,rpcProvider);// Call the Solana program to read in cross-chain data// 调用 Solana 程序以读取跨链数据constvalue=awaitprogram.getValue(node,key);if(value!=="NOT_FOUND"){// Decode 'PubKey' type in SVM to 'bytes32' type in EVM// 将 SVM 中 'PubKey' 类型解码为 EVM 中的 'bytes32' 类型constvalue=S2EVM(value,"PubKey");}else{// Null value// 空值constvalue="0x0";}// Compile CCIP-Read-compatible payload// 编译与 CCIP-Read 兼容的有效负载constdata=abi.encode(["bytes"],[value]);// Create HTTP gateway emitting value in format 'data: ...'// 创建以 'data: ...' 格式发出值的 HTTP 网关emitERC3668(data);
在上面的示例中,Solana 程序中的通用 getValue() 函数的形式为
// Example getValue() function in Solana program// Solana 程序中的示例 getValue() 函数pubfngetValue<'a>(ctx:Context,node:Pubkey,key:Pubkey,account:&AccountInfo<'a>,// Lifetime-bound parameter// 生命周期绑定的参数)->Result<Pubkey,ProgramError>{// Validate that the account belongs to the correct program ID// 验证账户是否属于正确的程序 ID...// Retrieve the data from the account// 从帐户中检索数据letdata=&account.data.borrow();// De-serialise the data from the account// 反序列化帐户中的数据...// Look up the value by node and key// 按节点和键查找值matchdeserialised.get(&node,&key){Some(value)=>{msg!("VALUE: {:?}",value);Ok(value)},None=>{msg!("NOT_FOUND");Err(ProgramError::InvalidArgument)}}}