Skip to content

Commit

Permalink
refactoring(gsdk): structure gsdk API (#3035)
Browse files Browse the repository at this point in the history
  • Loading branch information
mertwole authored Aug 19, 2023
1 parent 2638a27 commit 8b54aca
Show file tree
Hide file tree
Showing 13 changed files with 238 additions and 101 deletions.
2 changes: 1 addition & 1 deletion gcli/src/cmd/claim.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ impl Claim {
pub async fn exec(&self, signer: Signer) -> Result<()> {
let message_id = self.message_id.to_hash()?.into();

signer.claim_value(message_id).await?;
signer.calls.claim_value(message_id).await?;

Ok(())
}
Expand Down
2 changes: 2 additions & 0 deletions gcli/src/cmd/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ impl Create {

let gas = if self.gas_limit == 0 {
signer
.rpc
.calculate_create_gas(None, code_id, payload.clone(), self.value, false, None)
.await?
.min_limit
Expand All @@ -62,6 +63,7 @@ impl Create {

// create program
signer
.calls
.create_program(code_id, self.salt.to_vec()?, payload, gas_limit, self.value)
.await?;

Expand Down
1 change: 1 addition & 0 deletions gcli/src/cmd/reply.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ impl Reply {
let reply_to_id = self.reply_to_id.to_hash()?;

signer
.calls
.send_reply(
reply_to_id.into(),
self.payload.to_vec()?,
Expand Down
1 change: 1 addition & 0 deletions gcli/src/cmd/send.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ pub struct Send {
impl Send {
pub async fn exec(&self, signer: Signer) -> Result<()> {
signer
.calls
.send_message(
self.destination.to_hash()?.into(),
self.payload.to_vec()?,
Expand Down
1 change: 1 addition & 0 deletions gcli/src/cmd/transfer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ impl Transfer {
println!("Value: {}", self.value);

signer
.calls
.transfer(AccountId32::from_ss58check(&self.destination)?, self.value)
.await?;

Expand Down
4 changes: 3 additions & 1 deletion gcli/src/cmd/upload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,14 @@ impl Upload {
pub async fn exec(&self, signer: Signer) -> Result<()> {
let code = fs::read(&self.code)?;
if self.code_only {
signer.upload_code(code).await?;
signer.calls.upload_code(code).await?;
return Ok(());
}

let payload = self.payload.to_vec()?;
let gas = if self.gas_limit == 0 {
signer
.rpc
.calculate_upload_gas(None, code.clone(), payload.clone(), self.value, false, None)
.await?
.min_limit
Expand All @@ -66,6 +67,7 @@ impl Upload {
// Estimate gas and upload program.
let gas_limit = signer.api().cmp_gas_limit(gas)?;
signer
.calls
.upload_program(code, self.salt.to_vec()?, payload, gas_limit, self.value)
.await?;

Expand Down
18 changes: 14 additions & 4 deletions gclient/src/api/calls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ impl GearApi {
pub async fn transfer(&self, destination: ProgramId, value: u128) -> Result<H256> {
let destination: [u8; 32] = destination.into();

let tx = self.0.transfer(destination, value).await?;
let tx = self.0.calls.transfer(destination, value).await?;

for event in tx.wait_for_success().await?.iter() {
if let Event::Balances(BalancesEvent::Transfer { .. }) =
Expand Down Expand Up @@ -165,6 +165,7 @@ impl GearApi {

let tx = self
.0
.calls
.create_program(code_id, salt, payload, gas_limit, value)
.await?;

Expand Down Expand Up @@ -357,16 +358,19 @@ impl GearApi {

dest_node_api
.0
.storage
.set_code_storage(src_code_id, &src_code)
.await?;

dest_node_api
.0
.storage
.set_code_len_storage(src_code_id, src_code_len)
.await?;

dest_node_api
.0
.storage
.set_gas_nodes(&src_program_reserved_gas_nodes)
.await?;

Expand Down Expand Up @@ -411,19 +415,22 @@ impl GearApi {

dest_node_api
.0
.storage
.set_total_issuance(
dest_gas_total_issuance.saturating_add(src_program_reserved_gas_total),
)
.await?;

dest_node_api
.0
.storage
.set_gpages(dest_program_id, &src_program_pages)
.await?;

src_program.expiration_block = dest_node_api.last_block_number().await?;
dest_node_api
.0
.storage
.set_gprog(dest_program_id, src_program)
.await?;

Expand Down Expand Up @@ -513,7 +520,7 @@ impl GearApi {
)
.await?;

self.0.set_gpages(program_id, &pages).await?;
self.0.storage.set_gpages(program_id, &pages).await?;

Ok(())
}
Expand All @@ -537,7 +544,7 @@ impl GearApi {
.await?
.map(|(message, _interval)| message.value());

let tx = self.0.claim_value(message_id).await?;
let tx = self.0.calls.claim_value(message_id).await?;

for event in tx.wait_for_success().await?.iter() {
if let Event::Gear(GearEvent::UserMessageRead { .. }) =
Expand Down Expand Up @@ -639,6 +646,7 @@ impl GearApi {

let tx = self
.0
.calls
.send_message(destination, payload, gas_limit, value, prepaid)
.await?;

Expand Down Expand Up @@ -754,6 +762,7 @@ impl GearApi {

let tx = self
.0
.calls
.send_reply(reply_to_id, payload, gas_limit, value, prepaid)
.await?;

Expand Down Expand Up @@ -884,7 +893,7 @@ impl GearApi {
/// - [`upload_program`](Self::upload_program) function uploads a new
/// program and initialize it.
pub async fn upload_code(&self, code: impl AsRef<[u8]>) -> Result<(CodeId, H256)> {
let tx = self.0.upload_code(code.as_ref().to_vec()).await?;
let tx = self.0.calls.upload_code(code.as_ref().to_vec()).await?;

for event in tx.wait_for_success().await?.iter() {
if let Event::Gear(GearEvent::CodeChanged {
Expand Down Expand Up @@ -999,6 +1008,7 @@ impl GearApi {

let tx = self
.0
.calls
.upload_program(code, salt, payload, gas_limit, value)
.await?;

Expand Down
4 changes: 4 additions & 0 deletions gclient/src/api/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ impl GearApi {
at: Option<H256>,
) -> Result<GasInfo> {
self.0
.rpc
.calculate_create_gas(origin, code_id, payload, value, allow_other_panics, at)
.await
.map_err(Into::into)
Expand Down Expand Up @@ -109,6 +110,7 @@ impl GearApi {
at: Option<H256>,
) -> Result<GasInfo> {
self.0
.rpc
.calculate_upload_gas(origin, code, payload, value, allow_other_panics, at)
.await
.map_err(Into::into)
Expand Down Expand Up @@ -158,6 +160,7 @@ impl GearApi {
at: Option<H256>,
) -> Result<GasInfo> {
self.0
.rpc
.calculate_handle_gas(origin, destination, payload, value, allow_other_panics, at)
.await
.map_err(Into::into)
Expand Down Expand Up @@ -203,6 +206,7 @@ impl GearApi {
at: Option<H256>,
) -> Result<GasInfo> {
self.0
.rpc
.calculate_reply_gas(origin, message_id, payload, value, allow_other_panics, at)
.await
.map_err(Into::into)
Expand Down
Loading

0 comments on commit 8b54aca

Please sign in to comment.