Skip to content

Commit

Permalink
Merge pull request #3 from matiasbenary/main
Browse files Browse the repository at this point in the history
update readme and fix details
  • Loading branch information
gagdiez authored Feb 28, 2024
2 parents 1b1754a + 9b34790 commit 648ff74
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 63 deletions.
1 change: 0 additions & 1 deletion contract-advanced-rs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ This contract presents 3 examples on how to do complex cross-contract calls. Par
│ │ └── main.ava.ts
│ ├── ava.config.cjs
│ └── package.json
├── package.json
├── src # contract's code
│ ├── batch_actions.rs
│ ├── lib.rs
Expand Down
2 changes: 1 addition & 1 deletion contract-advanced-ts/src/contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,4 @@ export class CrossContractCall {
similar_contracts_callback({ number_promises }: { number_promises: number;}): string[] {
return internal_similar_contracts_callback(number_promises);
}
}
}
2 changes: 1 addition & 1 deletion contract-simple-rs/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[package]
name = "contract"
name = "cross_contract"
version = "1.0.0"
authors = ["Near Inc <hello@near.org>"]
edition = "2021"
Expand Down
2 changes: 1 addition & 1 deletion contract-simple-rs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ near create-account <accountId> --useFaucet
# Deploy the contract
./build.sh

near deploy <accountId> contract.wasm init '{"hello_account":"hello.near-example.testnet"}'
near deploy <accountId> ./target/wasm32-unknown-unknown/release/cross_contract.wasm init '{"hello_account":"hello.near-example.testnet"}'
```
### CLI: Interacting with the Contract

Expand Down
2 changes: 1 addition & 1 deletion contract-simple-rs/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ cargo test
./build.sh
cd sandbox-ts
npm i
npm run test -- -- "../target/wasm32-unknown-unknown/release/contract.wasm"
npm run test -- -- "../target/wasm32-unknown-unknown/release/cross_contract.wasm"
3 changes: 1 addition & 2 deletions contract-simple-ts/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ The smart contract implements the simplest form of cross-contract calls: it call
│ │ └── main.ava.ts
│ ├── ava.config.cjs
│ └── package.json
├── package.json
├── src # contract's code
│ └── contract.ts
├── package.json
Expand Down Expand Up @@ -93,7 +92,7 @@ near create-account <accountId> --useFaucet

# Deploy the contract
yarn build
near deploy <accountId> contract.wasm init '{"hello_account":"hello.near-example.testnet"}'
near deploy <accountId> ./build/cross_contract.wasm init '{"hello_account":"hello.near-example.testnet"}'
```
### CLI: Interacting with the Contract

Expand Down
87 changes: 31 additions & 56 deletions contract-simple-ts/src/contract.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@
import {
NearBindgen,
initialize,
call,
near,
bytes,
NearPromise,
} from "near-sdk-js";
import { NearBindgen, initialize, call, near, bytes, NearPromise } from "near-sdk-js";
import { AccountId } from "near-sdk-js/lib/types";

const FIVE_TGAS = BigInt("50000000000000");
Expand All @@ -18,82 +11,64 @@ class CrossContractCall {

@initialize({})
init({ hello_account }: { hello_account: AccountId }) {
this.hello_account = hello_account;
this.hello_account = hello_account
}

@call({})
query_greeting(): NearPromise {
const promise = NearPromise.new(this.hello_account)
.functionCall("get_greeting", NO_ARGS, NO_DEPOSIT, FIVE_TGAS)
.then(
NearPromise.new(near.currentAccountId()).functionCall(
"query_greeting_callback",
NO_ARGS,
NO_DEPOSIT,
FIVE_TGAS
)
);

.functionCall("get_greeting", NO_ARGS, NO_DEPOSIT, FIVE_TGAS)
.then(
NearPromise.new(near.currentAccountId())
.functionCall("query_greeting_callback", NO_ARGS, NO_DEPOSIT, FIVE_TGAS)
)

return promise.asReturn();
}

@call({ privateFunction: true })
@call({privateFunction: true})
query_greeting_callback(): String {
let { result, success } = promiseResult();
let {result, success} = promiseResult()

if (success) {
return result.substring(1, result.length - 1);
return result.substring(1, result.length-1);
} else {
near.log("Promise failed...");
return "";
near.log("Promise failed...")
return ""
}
}

@call({})
change_greeting({ new_greeting }: { new_greeting: string }): NearPromise {
const promise = NearPromise.new(this.hello_account)
.functionCall(
"set_greeting",
JSON.stringify({ greeting: new_greeting }),
NO_DEPOSIT,
FIVE_TGAS
)
.then(
NearPromise.new(near.currentAccountId()).functionCall(
"change_greeting_callback",
NO_ARGS,
NO_DEPOSIT,
FIVE_TGAS
)
);
.functionCall("set_greeting", JSON.stringify({ greeting: new_greeting }), NO_DEPOSIT, FIVE_TGAS)
.then(
NearPromise.new(near.currentAccountId())
.functionCall("change_greeting_callback", NO_ARGS, NO_DEPOSIT, FIVE_TGAS)
)

return promise.asReturn();
}

@call({ privateFunction: true })
@call({privateFunction: true})
change_greeting_callback(): boolean {
let { success } = promiseResult();
let { success } = promiseResult()

if (success) {
near.log(`Success!`);
return true;
near.log(`Success!`)
return true
} else {
near.log("Promise failed...");
return false;
near.log("Promise failed...")
return false
}
}
}

function promiseResult(): { result: string; success: boolean } {
function promiseResult(): {result: string, success: boolean}{
let result, success;

try {
result = near.promiseResult(0);
success = true;
} catch {
result = undefined;
success = false;
}

return { result, success };
}

try{ result = near.promiseResult(0); success = true }
catch{ result = undefined; success = false }

return {result, success}
}

0 comments on commit 648ff74

Please sign in to comment.