|
| 1 | +// Copyright (c) Zefchain Labs, Inc. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +#![cfg_attr(target_arch = "wasm32", no_main)] |
| 5 | + |
| 6 | +use contract_call::{ContractTransferAbi, Operation, Parameters}; |
| 7 | +use linera_sdk::{ |
| 8 | + linera_base_types::{Account, ApplicationId, WithContractAbi}, |
| 9 | + Contract, ContractRuntime, |
| 10 | +}; |
| 11 | + |
| 12 | +pub struct ContractTransferContract { |
| 13 | + runtime: ContractRuntime<Self>, |
| 14 | +} |
| 15 | + |
| 16 | +linera_sdk::contract!(ContractTransferContract); |
| 17 | + |
| 18 | +impl WithContractAbi for ContractTransferContract { |
| 19 | + type Abi = ContractTransferAbi; |
| 20 | +} |
| 21 | + |
| 22 | +impl Contract for ContractTransferContract { |
| 23 | + type Message = (); |
| 24 | + type InstantiationArgument = (); |
| 25 | + type Parameters = Parameters; |
| 26 | + type EventValue = (); |
| 27 | + |
| 28 | + async fn load(runtime: ContractRuntime<Self>) -> Self { |
| 29 | + ContractTransferContract { runtime } |
| 30 | + } |
| 31 | + |
| 32 | + async fn instantiate(&mut self, _value: ()) { |
| 33 | + // Validate that the application parameters were configured correctly. |
| 34 | + self.runtime.application_parameters(); |
| 35 | + } |
| 36 | + |
| 37 | + async fn execute_operation(&mut self, operation: Operation) { |
| 38 | + match operation { |
| 39 | + Operation::DirectTransfer { |
| 40 | + source, |
| 41 | + destination, |
| 42 | + amount, |
| 43 | + } => { |
| 44 | + // Direct transfer using runtime.transfer |
| 45 | + self.runtime.transfer(source, destination, amount); |
| 46 | + } |
| 47 | + Operation::IndirectTransfer { |
| 48 | + source, |
| 49 | + destination, |
| 50 | + amount, |
| 51 | + } => { |
| 52 | + // Indirect transfer: create application, transfer to it, then transfer from it |
| 53 | + let module_id = self.runtime.application_parameters().module_id; |
| 54 | + // let typed_module_id: ModuleId<ContractTransferAbi, Parameters, ()> = module_id.with_abi(); |
| 55 | + |
| 56 | + // Create a new application instance |
| 57 | + let parameters = self.runtime.application_parameters(); |
| 58 | + let application_id = self |
| 59 | + .runtime |
| 60 | + .create_application::<ContractTransferAbi, Parameters, ()>( |
| 61 | + module_id, |
| 62 | + ¶meters, |
| 63 | + &(), |
| 64 | + vec![], |
| 65 | + ); |
| 66 | + |
| 67 | + // Transfer from source to the created application |
| 68 | + let chain_id = self.runtime.chain_id(); |
| 69 | + let app_account = Account { |
| 70 | + chain_id, |
| 71 | + owner: application_id.into(), |
| 72 | + }; |
| 73 | + self.runtime.transfer(source, app_account, amount); |
| 74 | + |
| 75 | + // Authenticated calls should be fully visible. |
| 76 | + let operation = Operation::TestSomeAuthenticatedSignerCaller; |
| 77 | + self.runtime |
| 78 | + .call_application(true, application_id, &operation); |
| 79 | + |
| 80 | + // Non-authenticated calls should be fully non-visible |
| 81 | + let operation = Operation::TestNoneAuthenticatedSignerCaller; |
| 82 | + self.runtime |
| 83 | + .call_application(false, application_id, &operation); |
| 84 | + |
| 85 | + // Non-authenticated calls should be fully non-visible |
| 86 | + let operation = Operation::TestNoneAuthenticatedSignerCaller; |
| 87 | + self.runtime |
| 88 | + .call_application(false, application_id, &operation); |
| 89 | + |
| 90 | + // Authenticated calls should be fully visible. |
| 91 | + let operation = Operation::TestSomeAuthenticatedSignerCaller; |
| 92 | + self.runtime |
| 93 | + .call_application(true, application_id, &operation); |
| 94 | + |
| 95 | + // Call the created application to transfer from itself to destination. |
| 96 | + // Authenticated or not, the system is able to access the caller |
| 97 | + // for making the transfer. |
| 98 | + let operation = Operation::DirectTransfer { |
| 99 | + source: ApplicationId::into(application_id), |
| 100 | + destination, |
| 101 | + amount, |
| 102 | + }; |
| 103 | + self.runtime |
| 104 | + .call_application(false, application_id, &operation); |
| 105 | + } |
| 106 | + Operation::TestNoneAuthenticatedSignerCaller => { |
| 107 | + assert!(self.runtime.authenticated_signer().is_none()); |
| 108 | + assert!(self.runtime.authenticated_caller_id().is_none()); |
| 109 | + } |
| 110 | + Operation::TestSomeAuthenticatedSignerCaller => { |
| 111 | + assert!(self.runtime.authenticated_signer().is_some()); |
| 112 | + assert!(self.runtime.authenticated_caller_id().is_some()); |
| 113 | + } |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + async fn execute_message(&mut self, _message: ()) { |
| 118 | + panic!("Contract transfer application doesn't support any cross-chain messages"); |
| 119 | + } |
| 120 | + |
| 121 | + async fn store(self) {} |
| 122 | +} |
0 commit comments