Skip to content

Commit

Permalink
refactor!: replaced deploy method as a static method (#18)
Browse files Browse the repository at this point in the history
* refactor!: replaced deploy method as a static method

BREAKING:
Contract(abc.testnet).deploy(code) to Contract::deploy(abc.testnet, code)

* clippy
  • Loading branch information
akorchyn authored Dec 3, 2024
1 parent 87833ee commit 15d4959
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 64 deletions.
21 changes: 12 additions & 9 deletions examples/deploy_and_call_method.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,21 @@ async fn main() {
let account = network.dev_create_account().await.unwrap();
let network = NetworkConfig::from(network);

let contract = Contract(account.id().clone());
let signer = Signer::new(Signer::from_workspace(&account)).unwrap();

// Let's deploy the contract. The contract is simple counter with `get_num`, `increase`, `decrease` arguments
contract
.deploy(include_bytes!("../resources/counter.wasm").to_vec())
// You can add init call as well using `with_init_call`
.without_init_call()
.with_signer(signer.clone())
.send_to(&network)
.await
.unwrap();
Contract::deploy(
account.id().clone(),
include_bytes!("../resources/counter.wasm").to_vec(),
)
// You can add init call as well using `with_init_call`
.without_init_call()
.with_signer(signer.clone())
.send_to(&network)
.await
.unwrap();

let contract = Contract(account.id().clone());

// Let's fetch current value on a contract
let current_value: Data<i8> = contract
Expand Down
30 changes: 16 additions & 14 deletions examples/ft.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,22 @@ async fn main() {
let token_signer = Signer::new(Signer::from_workspace(&token)).unwrap();

// Deploying token contract
Contract(token.id().clone())
.deploy(include_bytes!("../resources/fungible_token.wasm").to_vec())
.with_init_call(
"new_default_meta",
json!({
"owner_id": token.id().to_string(),
"total_supply": "1000000000000000000000000000"
}),
)
.unwrap()
.with_signer(token_signer.clone())
.send_to(&network)
.await
.unwrap();
Contract::deploy(
token.id().clone(),
include_bytes!("../resources/fungible_token.wasm").to_vec(),
)
.with_init_call(
"new_default_meta",
json!({
"owner_id": token.id().to_string(),
"total_supply": "1000000000000000000000000000"
}),
)
.unwrap()
.with_signer(token_signer.clone())
.send_to(&network)
.await
.unwrap();

// Verifying that user has 1000 tokens
let tokens = Tokens::of(token.id().clone())
Expand Down
31 changes: 17 additions & 14 deletions examples/nft.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,26 @@ async fn main() {
let account = network.dev_create_account().await.unwrap();
let network = NetworkConfig::from(network);

let contract = Contract(nft.id().clone());
let nft_signer = Signer::new(Signer::from_workspace(&nft)).unwrap();

// Deploying token contract
contract
.deploy(include_bytes!("../resources/nft.wasm").to_vec())
.with_init_call(
"new_default_meta",
json!({
"owner_id": nft.id().to_string(),
}),
)
.unwrap()
.with_signer(nft_signer.clone())
.send_to(&network)
.await
.unwrap();
Contract::deploy(
nft.id().clone(),
include_bytes!("../resources/nft.wasm").to_vec(),
)
.with_init_call(
"new_default_meta",
json!({
"owner_id": nft.id().to_string(),
}),
)
.unwrap()
.with_signer(nft_signer.clone())
.send_to(&network)
.await
.unwrap();

let contract = Contract(nft.id().clone());

// Mint NFT via contract call
contract
Expand Down
4 changes: 2 additions & 2 deletions src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ impl Contract {
})
}

pub fn deploy(&self, code: Vec<u8>) -> DeployContractBuilder {
DeployContractBuilder::new(self.0.clone(), code)
pub const fn deploy(contract: AccountId, code: Vec<u8>) -> DeployContractBuilder {
DeployContractBuilder::new(contract, code)
}

pub fn abi(
Expand Down
54 changes: 29 additions & 25 deletions tests/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,18 @@ async fn contract_without_init_call() {
let account = network.dev_create_account().await.unwrap();
let network = NetworkConfig::from(network);

let contract = Contract(account.id().clone());
Contract::deploy(
account.id().clone(),
include_bytes!("../resources/counter.wasm").to_vec(),
)
.without_init_call()
.with_signer(Signer::new(Signer::from_workspace(&account)).unwrap())
.send_to(&network)
.await
.unwrap()
.assert_success();

contract
.deploy(include_bytes!("../resources/counter.wasm").to_vec())
.without_init_call()
.with_signer(Signer::new(Signer::from_workspace(&account)).unwrap())
.send_to(&network)
.await
.unwrap()
.assert_success();
let contract = Contract(account.id().clone());

assert!(!contract
.wasm()
Expand Down Expand Up @@ -76,23 +78,25 @@ async fn contract_with_init_call() {
let account = network.dev_create_account().await.unwrap();
let network = NetworkConfig::from(network);

let contract = Contract(account.id().clone());
Contract::deploy(
account.id().clone(),
include_bytes!("../resources/fungible_token.wasm").to_vec(),
)
.with_init_call(
"new_default_meta",
json!({
"owner_id": account.id().to_string(),
"total_supply": "1000000000000000000000000000"
}),
)
.unwrap()
.with_signer(Signer::new(Signer::from_workspace(&account)).unwrap())
.send_to(&network)
.await
.unwrap()
.assert_success();

contract
.deploy(include_bytes!("../resources/fungible_token.wasm").to_vec())
.with_init_call(
"new_default_meta",
json!({
"owner_id": account.id().to_string(),
"total_supply": "1000000000000000000000000000"
}),
)
.unwrap()
.with_signer(Signer::new(Signer::from_workspace(&account)).unwrap())
.send_to(&network)
.await
.unwrap()
.assert_success();
let contract = Contract(account.id().clone());

assert!(!contract
.wasm()
Expand Down

0 comments on commit 15d4959

Please sign in to comment.